<?php

namespace app\admin\controller;

use app\common\model\EnterpriseCloseAccount as EcaModel;
use app\admin\common\AdminController;
use think\facade\Db;
use app\common\state\MainState;

/**
 * Description of EnterpriseCloseAccount
 *
 * @author sgq
 */
class EnterpriseCloseAccount extends AdminController {

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

    public function list() {
        $params = $this->request->param();
        $offset = $params["offset"] ?: 0;
        $order = $params["order"] ?: "desc";
        $limit = $params["limit"] ?: 10;
        $where = $this->setFilter($params);
        $where[] = ["type", "=", $this->user["type"]];
        $count = EcaModel::where($where)->count();
        $list = EcaModel::where($where)->limit($offset, $limit)->order("createTime {$order}")->select()->toArray();

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

    public function check() {
        $id = $this->request["id"];
        $info = EcaModel::where("id", $id)->find();
        $files = json_decode($info["files"], true);
        $fileTmp = [];
        foreach ($files as $file) {
            $ext = pathinfo($file)["extension"];
            $fileTmp[] = ["ext" => $ext, "path" => $file];
        }
        $info["files"] = $fileTmp;
        return view("check", ["type" => $this->user["type"], "row" => $info]);
    }

    public function submitToCheck() {
        $responseObj = new \stdClass();
        $responseObj->code = 500;
        $id = $this->request["id"];
        $checkState = $this->request["checkState"];
        $checkMsg = $this->request["checkMsg"];
        $info = EcaModel::where("id", $id)->find();
        if (!$info) {
            $responseObj->msg = "系统错误，请联系管理员";
            return $responseObj;
        }
        if (!$checkState) {
            $responseObj->msg = "请选择审核状态";
            return $responseObj;
        }
        Db::startTrans();
        try {
            //添加日志
            $user = $this->user;
            $log["id"] = getStringId();
            $log["category"] = "enterprise_change";
            $log["active"] = 1;
            $log["state"] = $checkState;
            $log["step"] = 101;
            $log["stateChange"] = MainState::getStateName($info["checkState"]) . "->" . MainState::getStateName($checkState);
            $log["type"] = 10;
            $log["mainId"] = $id;
            $log["description"] = $checkMsg;
            $log["createUser"] = $user ? sprintf("%s(%s)", $user["account"], $user["companyName"] ?: $user["rolename"]) : "系统";
            $log["createTime"] = date("Y-m-d H:i:s");
            $updEca["id"] = $id;
            $updEca["checkState"] = $checkState;
            $updEca["checkMsg"] = $checkMsg;
            if ($checkState == 3) {
                $upd["id"] = $info["enterpriseId"];
                $upd["delete"] = 1;
                Db::table("un_enterprise")->save($upd);

                $updEca["passTime"] = date("Y-m-d H:i:s");
            }
            Db::table("new_talent_checklog")->insert($log);
            Db::table("un_enterpriseclose_record")->save($updEca);
            $responseObj->code = 200;
            $responseObj->msg = "审核成功";

            Db::commit();

            queue("app\job\Messenger", ["type" => 109, "userId" => $user["uid"]]);
            return $responseObj;
        } catch (\think\db\exception\DbException $e) {
            $responseObj->msg = $e->getMessage();
            Db::rollback();
            return $responseObj;
        }
    }

    public function export() {
        $request = $this->request;
        $data["name"] = \StrUtil::getRequestDecodeParam($request, "name");
        $data["idCard"] = \StrUtil::getRequestDecodeParam($request, "idCard");
        $data["checkState"] = \StrUtil::getRequestDecodeParam($request, "checkState");
        $where = $this->setFilter($data);
        $where[] = ["type", "=", $this->user["type"]];

        $list = EcaModel::where($where)->select()->toArray();

        $title = ["单位名称", "统一社会信用代码", "登录账号", "注销原因", "审核状态", "审核意见", "审核通过时间"];
        $keys = ["name", "idCard", "username", "reason", "checkStateName", "checkMsg", "passTime"];
        $rows = [];
        foreach ($list as &$item) {
            switch ($item["checkState"]) {
                case -1:
                    $item["checkStateName"] = "待提交";
                    break;
                case 1:
                    $item["checkStateName"] = "待审核";
                    break;
                case 2:
                    $item["checkStateName"] = "审核驳回";
                    break;
                case 3:
                    $item["checkStateName"] = "已通过";
                    break;
                case 9:
                    $item["checkStateName"] = "重新提交";
                    break;
            }
            $row = [];
            for ($i = 0; $i < count($keys); $i++) {
                $row[] = $item[$keys[$i]];
            }
            $rows[] = $row;
        }unset($item);

        if (!$rows) {
            return json(["msg" => "没有可导出的内容"]);
        }
        $fileName = "账户注销申请列表";
        export($title, $rows, $fileName);
    }

    private function setFilter($params) {
        $where = [];
        if (\StrUtil::isNotEmpAndNull($params["idCard"])) {
            $where[] = ["idCard", "=", $params["IdCard"]];
        }
        if (\StrUtil::isNotEmpAndNull($params["name"])) {
            $where[] = ["name", "=", $params["name"]];
        }
        if ($params["checkState"] != null) {
            $where[] = ["checkState", "=", $params["checkState"]];
        }
        return $where;
    }

}
