<?php

namespace app\index\controller;

use app\BaseController;
use app\common\api\UserApi;
use think\facade\Db;

/**
 * Description of Login
 *
 * @author sgq
 */
class Auth extends BaseController {

    /**
     * 登录
     * @return type
     */
    public function login() {

        if ($user = session("user")) {
            if($user['usertype'] == 2){
                return redirect("/enterprise");
            }
        }
        $msg = "";
        if ($this->request->isPost()) {
            $username = $this->request["username"];
            $pwd = $this->request["password"];
            $usertype = $this->request["usertype"];
            $user = new UserApi($username, $pwd, $usertype);
            if (!$userinfo = $user->getUserInfo()) {
                $msg = "用户不存在";
            } else if (!$user->checkPwd()) {
                $login_fail = session('login_fail');
                if($login_fail){
                    $login_fail++;
                    if($login_fail >= 5){
                        session('captcha',1);
                    }
                }else{
                    $login_fail = 1;
                }
                session('login_fail',$login_fail);
                $msg = "用户名或者密码错误";
            } else if($res_msg = $user->checkState()){
                if($user->info['checkState'] == 2){
                    return redirect("/common/auth/enterprise_edit");
                }
                $msg = $res_msg;
            }
            $url = "/admin";
            switch ($usertype) {
                case 1:
                    //验证错误
                    break;
                case 2:
                    //验证错误
                    $url = "/enterprise";
                    break;
                case 3:
                    //验证错误
                    $url = "/person";
                    break;
            }

            if (!$msg) {
                $user->setSession();
                return redirect($url);
            }
        }
        return view("", ["msg" => $msg]);
    }

    /**
     * 退出
     * @return type
     */
    public function logout() {
        session("user", null);
        return redirect("/index/auth/login");
    }


    /**
     * 验证密码
     */
    public function valid_password() {
        if ($user = session("user")) {
            $username = $user["account"];
            $usertype = $user["usertype"];
            $pwd = $this->request["password"];
            $user = new UserApi($username, $pwd, $usertype);
            if (!$user->checkPwd()) {
                return json()->data(["status" => 1, "msg" => "密码错误"]);
            } else {
                return json(["code" => 200]);
            }
        } else {
            return json()->data(["status" => 2]);
        }
    }

    public function policy(){


        return view("", []);
    }

    public function policy_list(){
        $level = $this->request->post('level');
        if($level){
            $where[] = ['level','=',$level];
        }else{
            $where[] = ['level','>',0];
        }
        $list = Db::table('new_policy')->where($where)->select()->toArray();
        $result = [];
        foreach ($list as $k => $v){
            $check = [];
            $condition = [];
            if(!empty($v['checks'])){
                $check = explode(',',$v['checks']);
            }
            if(!empty($v['condition'])){
                $condition = explode(',',$v['condition']);
            }
            $item = [
                'id' => $v['id'],
                'tag' => $v['tag'],
                'policy' => $v['policy_name'],
                'checks' => $check,
                'condition' => $condition
            ];
            array_push($result,$item);
        }
        return json($result);
    }

    public function policy_update(){
        $res = $this->request->post();
        foreach ($res as $k => $v){

            $update = [];

            if(count($v['condition']) > 0){
                $update['condition'] = json_encode($v['condition']);
            }
            if(count($update) > 0){
                Db::table('new_policy')->where('id',$v['id'])->save($update);
            }
        }

    }

}
