<?php

namespace App\Http\Controllers\Cp\Manage;

use App\Http\Controllers\Cp\BaseController;
use App\Modules\CpUser\Services\CpUserService;
use Illuminate\Http\Request;

class ManageController extends BaseController
{

    /**
     * @apiVersion 1.0.0
     * @apiDescription 修改密码
     * @api {POST} auth/modifyPassword 修改密码
     * @apiGroup Manage
     * @apiName modifyPassword
     * @apiParam {String}  account 账号.
     * @apiParam {String}  password 密码.
     * @apiParam {String}  new_password 新密码.
     * @apiParam {String}  new_password_repeat 重复新密码.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": {}
     *     }
     */
    function modifyPassword(Request $request)
    {
        $msg = '';
        do{
        	$username = $request->has('username') ? trim($request->input('username')) : '';
        	$password = $request->has('password') ? trim($request->input('password')) : '';
        	$new_password = $request->has('new_password') ? trim($request->input('new_password')) : '';
        	$new_password_repeat = $request->has('new_password_repeat') ? trim($request->input('new_password_repeat')) : '';
        	 
        	if(!$username || !$password || !$new_password || !$new_password_repeat) {
        		$msg = '账号或者密码不能为空';
        		break;
        	}
        	if($new_password != $new_password_repeat) {
        		$msg = '新密码2次不一致';
        		break;
        	}

        	$cp_user = CpUserService::getCpUser($username);
        	
        	if($cp_user->password != md5($password."^-^zhuishuyun^_^")) {
        		$msg = '老密码错误';
        		break;
        	}
        	
        	if($cp_user->username)
        	{
        		$password = md5($new_password."^-^zhuishuyun^_^");
        		if(CpUserService::modifyPassword($cp_user->username, $password))
        		{
        			// 成功后跳后台
        			return redirect('login');
        		}
        	}
        	
        }while(false);
        
        return redirect('/api/auth/modifyPasswordView?msg='.$msg);
    }
    
    function modifyPasswordView(Request $request)
    {
    	\Log::info('modifyPasswordView');
    	$msg = $request->has('msg')?$request->get('msg'):'';
    	return view('cp.web.auth.reset',compact('msg'));
    }

    function cplist(Request $request){
        if(session('cp_manage_auth') !=1){
            return redirect('book/index');
        }
        $param = $request->except('_url');
        $data = CpUserService::getAllCpUser($param);
        return view('cp.web.manage.cplist',compact('data','param'));
    }

    function cplistJson(){
        if(session('cp_manage_auth') !=1){
            return response()->json(['code'=>0,'msg'=>'参数不能为空']);
        }
        $data = CpUserService::getAllCpUser([],1);
        $result = '<div class="form-group"> <label>cp</label><select class="form-control" name="cpid" placeholder="cp" id="cp-inpt"><option value=""></option>';
        $format = '<option value="%s">%s</option>';
        foreach ($data as $v){
            $result .= sprintf($format,$v->id,$v->username);
        }
        $result .= '</div>';
        return response()->json(['code'=>1,'data'=>$result]);
    }

    function createCp(Request $request){
        if(session('cp_manage_auth') !=1){
            return response()->json(['code'=>0,'msg'=>'参数不能为空']);
        }
        $username = $request->post('username');
        if(!$username){
           return response()->json(['code'=>0,'msg'=>'参数不能为空']);
        }
        $result = CpUserService::createCp($username);
        if($result){
            return response()->json(['code'=>1,'msg'=>'成功']);
        }else{
            return response()->json(['code'=>0,'msg'=>'已经添加过']);
        }
    }

}