<?php

namespace app\enterprise\controller;

use app\enterprise\common\EnterpriseController;
use app\common\model\TalentBankChange as TbcModel;
use app\common\api\TalentLogApi;
use app\common\state\ProjectState;
use app\common\model\TalentLog;
use think\facade\Db;
use app\common\api\EnterpriseApi;
use app\common\api\DictApi;

/**
 * Description of TalentBankChange
 *
 * @author sgq
 */
class TalentBankChange extends EnterpriseController {

    public function index() {
        return view("", ["type" => $this->user["type"]]);
    }

    public function list() {
        $type = $this->user["type"];
        $params = \StrUtil::batchGetRequestDecodeParam($this->request);
        $order = $params["order"] ?: "desc";
        $offset = $params["offset"] ?: 0;
        $limit = $params["limit"] ?: 10;
        $where = [];
        $where[] = ["enterpriseId", "=", $this->user["uid"]];
        $where[] = ["delete", "=", 0];
        if ($params["talentName"]) {
            $where[] = ["talentName", "like", "%" . $params["talentName"] . "%"];
        }
        if ($params["idCard"]) {
            $where[] = ["idCard", "like", "%" . $params["idCard"] . "%"];
        }
        if ($params["oldBankName"]) {
            $where[] = ["oldBankName", "like", "%" . $params["oldBankName"] . "%"];
        }
        if ($params["newBankName"]) {
            $where[] = ["newBankName", "like", "%" . $params["newBankName"] . "%"];
        }
        if ($params["checkState"]) {
            $where[] = ["checkState", "=", $params["checkState"]];
        }
        $count = TbcModel::where($where)->count();
        $list = TbcModel::where($where)->limit($offset, $limit)->order("createTime " . $order)->select()->toArray();
        $talentArrangeMap = DictApi::selectByParentCode("talent_arrange");
        foreach ($list as &$item) {
            $item["talentArrangeName"] = $talentArrangeMap[$item["talentArrange"]];
        }unset($item);

        return json(["total" => $count, "rows" => $list]);
    }

    public function apply() {
        $request = $this->request;
        $id = isset($request["id"]) ? $request["id"] : 0;
        $info = TbcModel::where("id", $id)->find();
        if ($info) {
            $info["talentArrangeName"] = DictApi::selectByParentCode("talent_arrange")[$info["talentArrange"]];
        }
        if ($this->request->isPost()) {
            $response = new \stdClass();
            $response->code = 500;
            $data = $request->param();
            $check = $this->dataCheck($data);
            if ($check->code == 500) {
                return $check;
            }
            $talentInfo = \app\common\api\VerifyApi::getTalentInfoById($request["talentId"]);
            if ($data["id"]) {
                $data["updateUser"] = $this->user["uid"];
                $data["updateTime"] = date("Y-m-d H:i:s");
                TbcModel::update($data);
                $response->code = 200;
                $response->msg = "修改成功";
                return $response;
            } else {
                $data["id"] = getStringId();
                $data["checkState"] = -1;
                $data["createUser"] = $this->user["uid"];
                $data["createTime"] = date("Y-m-d H:i:s");
                TbcModel::create($data);

                $user = $this->user;
                $log["id"] = getStringId();
                $log["active"] = 1;
                $log["state"] = 1;
                $log["step"] = 0;
                $log["stateChange"] = "";
                $log["type"] = ProjectState::BANKCHANGE;
                $log["mainId"] = $data["id"];
                $log["description"] = "添加银行账号变更申请";
                $log["createUser"] = $user ? sprintf("%s(%s)", $user["account"], $user["companyName"] ?: $user["rolename"]) : "系统";
                $log["createTime"] = date("Y-m-d H:i:s");
                TalentLog::create($log);
                $response->code = 200;
                $response->msg = "添加成功";
                $response->obj = $data;
                return $response;
            }
        }
        return view("", ["row" => $info]);
    }

    public function view() {
        $id = $this->request["id"];
        $info = TbcModel::where("id", $id)->find();
        $info["talentArrangeName"] = DictApi::selectByParentCode("talent_arrange")[$info["talentArrange"]];
        return view("", ["row" => $info]);
    }

    public function submitToCheck() {
        $response = new \stdClass();
        $response->code = 500;
        $id = $this->request["id"];
        $info = TbcModel::where("id", $id)->find();
        if (!$info) {
            $response->msg = "提交审核失败，请先填写基础信息";
            return $response;
        }
        if ($info["checkState"] != -1 && $info["checkState"] != 2) {
            $response->msg = "不能重复提交审核";
            return $response;
        }
        $data["id"] = $id;
        $data["checkState"] = $info["checkState"] == 2 ? 9 : 1;
        TbcModel::update($data);
        $user = $this->user;
        $log["id"] = getStringId();
        $log["active"] = 1;
        $log["state"] = 1;
        $log["step"] = 0;
        $log["stateChange"] = "<span class='label'>待提交</span>-><span class='label label-success'>待初审</span>";
        $log["type"] = ProjectState::BANKCHANGE;
        $log["mainId"] = $id;
        $log["description"] = "确认提交审核";
        $log["createUser"] = $user ? sprintf("%s(%s)", $user["account"], $user["companyName"] ?: $user["rolename"]) : "系统";
        $log["createTime"] = date("Y-m-d H:i:s");
        TalentLog::create($log);
        $response->code = 200;
        $response->msg = "提交审核成功";
        $response->obj = 1;
        return $response;
    }

    public function delete() {
        $response = new \stdClass();
        $response->code = 500;
        $info = TbcModel::where("id", $this->request["id"])->find();
        if ($info["checkState"] != -1) {
            $response->msg = "已提交审核，无法删除";
            return $response;
        }
        $data["id"] = $info["id"];
        $data["delete"] = 1;
        $data["deleteTime"] = date("Y-m-d H:i:s");
        TbcModel::update($data);
        $response->code = 200;
        $response->msg = "删除成功";
        return $response;
    }

    private function dataCheck($data) {
        $response = new \stdClass();
        $response->code = 500;
        if (\StrUtil::isEmpOrNull($data["talentId"])) {
            $response->msg = "申报对象不能为空";
            return $response;
        }
        if (\StrUtil::isEmpOrNull($data["newBankName"])) {
            $response->msg = "新开户银行不能为空";
            return $response;
        }
        if (\StrUtil::isEmpOrNull($data["newBankNerPoint"])) {
            $response->msg = "新开户银行网点不能为空";
            return $response;
        }
        if (\StrUtil::isEmpOrNull($data["newBankAccount"])) {
            $response->msg = "新开户银行账号不能为空";
            return $response;
        }
        if (!preg_match("/^[\x{4e00}-\x{9fa5}]*银行$/u", $data["newBankName"])) {
            $response->msg = "新开户银行格式不正确";
            return $response;
        }
        if (!preg_match("/^[\x{4e00}-\x{9fa5}]*银行[\x{4e00}-\x{9fa5}]*省?[\x{4e00}-\x{9fa5}]+市[\x{4e00}-\x{9fa5}]*$/u", $data["newBankNerPoint"])) {
            $response->msg = "新开户银行网点格式不正确";
            return $response;
        }
        if (!preg_match("/^\d+$/", $data["newBankAccount"])) {
            $response->msg = "新银行账户格式不合法";
            return $response;
        }
        if ($data["type"] == 1) {
            if (\StrUtil::isEmpOrNull($data["newBankNumber"])) {
                $response->msg = "新银行行号不能为空";
                return $response;
            }
            if (!preg_match("/^\d+$/", $data["newBankNumber"])) {
                $response->msg = "银行行号格式不合法";
                return $response;
            }
        }
        $response->code = 200;
        return $response;
    }

}
