<?php

namespace app\admin\controller;

use app\admin\common\AdminController;
use app\common\api\ChuanglanSmsApi;
use app\common\model\TalentLog;
use app\common\state\ProjectState;
use app\common\api\DictApi;
use think\facade\Db;
use app\common\api\EducationApi;
use app\common\state\MainState;

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

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

    public function list() {
        $res = EducationApi::getList($this->request);
        return json($res);
    }

    public function detail() {
        $id = $this->request->param("id");
        $info = EducationApi::getInfoById($id);

        return view("", ["row" => $info]);
    }

    /**
     * 校验是否在审核范围内
     * @param type $id
     * @param type $process
     * @return type
     */
    public function validateIsCheck($id) {
        $responseObj = new \stdClass();
        $responseObj->code = 500;
        $oldInfo = EducationApi::getInfoById($id);
        if ($oldInfo["checkState"] != 1 && $oldInfo["checkState"] != 9) {
            $responseObj->msg = "不在审核范围内";
            return json($responseObj);
        }
        $where = [];
        $where[] = ["mainId", "=", $id];
        $where[] = ["active", "=", 2];
        $where[] = ["step", "=", 11];
        $log = TalentLog::where($where)->order("createTime desc")->find();

        if ($log) {
            $oldInfo["checkState"] = $log["state"];
            $oldInfo["checkMsg"] = $log["description"];
        } else {
            $oldInfo["checkState"] = null;
            $oldInfo["checkMsg"] = "";
        }
        $responseObj->code = 200;
        $responseObj->obj = $oldInfo;
        return json($responseObj);
    }

    /**
     * 审核
     * @return type
     */
    public function check() {
        $responseObj = new \stdClass();
        $responseObj->code = 500;
        $obj = $this->request->param();
        if (!$obj) {
            $responseObj->msg = "系统错误，请联系管理员";
            return json($responseObj);
        }
        if (!$obj["checkState"]) {
            $responseObj->msg = "请选择审核状态";
            return json($responseObj);
        }
        Db::startTrans();
        try {
            //加入日志
            $newLog["id"] = getStringId();
            $newLog["type"] = ProjectState::EDUCATION;
            $newLog["mainId"] = $obj["id"];
            $newLog["active"] = 2;
            $newLog["state"] = $obj["checkState"];
            $newLog["step"] = 11;
            $newLog["stateChange"] = "保存未提交";
            $newLog["description"] = $obj["checkMsg"];
            $newLog["createTime"] = date("Y-m-d H:i:s");
            $newLog["createUser"] = sprintf("%s(%s)", $this->user["account"], $this->user["companyName"]);
            Db::table("new_talent_checklog")->insert($newLog);
            $responseObj->code = 200;
            $responseObj->msg = "审核成功";
            Db::commit();
            return json($responseObj);
        } catch (\Exception $e) {
            Db::rollback();
            $responseObj->msg = $e->getMessage();
            return json($responseObj);
        }
    }

    public function submitCheck() {
        $id = $this->request->param("id");
        $checkState = $this->request->param("id");
        $responseObj = new \stdClass();
        $responseObj->code = 500;
        if (!$id) {
            $responseObj->msg = "系统错误,请联系管理员!";
            return json($responseObj);
        }
        $oldInfo = EducationApi::getInfoById($id);

        if ($oldInfo["checkState"] != 1 && $oldInfo["checkState"] != 9) {
            $responseObj->msg = "不在审核范围内";
            return json($responseObj);
        }
        $where = [];
        $where[] = ["mainId", "=", $id];
        $where[] = ["active", "=", 2];
        $where[] = ["step", "=", 11];
        $oldLog = TalentLog::where($where)->order("createTime desc")->find();
        if (!$oldLog) {
            $responseObj->msg = "请先审核后再提交";
            return json($responseObj);
        }
        $updData = [];
        $updData["id"] = $id;
        $updData["checkState"] = $oldLog["state"];
        $updData["checkMsg"] = $oldLog["description"];

        if ($updData["checkState"] == 3) {
            $updData["passTime"] = date("Y-m-d H:i:s");
            if ($oldInfo["project"] == 2) {
                if ($oldInfo["talentArrange"] == 1) {
                    $updData["score"] = 15;
                } else if ($oldInfo["talentArrange"] == 2) {
                    $updData["score"] = 10;
                } else if ($oldInfo["talentArrange"] == 3) {
                    $updData["score"] = 5;
                }
            }
        }

        $newLog["id"] = getStringId();
        $newLog["type"] = ProjectState::EDUCATION;
        $newLog["mainId"] = $id;
        $newLog["active"] = 1;
        $newLog["state"] = $oldLog["state"];
        $newLog["step"] = 11;
        $newLog["stateChange"] = MainState::getStateName($oldInfo["checkState"]) . "->" . MainState::getStateName($updData["checkState"]);
        $newLog["description"] = $oldLog["description"];
        $newLog["createTime"] = date("Y-m-d H:i:s");
        $newLog["createUser"] = sprintf("%s(%s)", $this->user["account"], $this->user["companyName"]);
        Db::startTrans();
        try {
            $where = [];
            $where[] = ["mainId", "=", $id];
            $where[] = ["type", "=", ProjectState::EDUCATION];
            $where[] = ["active", "=", 2];
            Db::table("new_talent_checklog")->where($where)->delete();
            Db::table("new_talent_checklog")->insert($newLog);
            Db::table("un_education_school")->save($updData);
            Db::commit();

            $responseObj->code = 200;
            $responseObj->msg = "提交审核成功";
            return json($responseObj);
        } catch (\think\db\exception\DbException $e) {
            Db::rollback();
            $responseObj->msg = $e->getMessage();
            return json($responseObj);
        }
    }

    /**
     * 通用导出
     * @return type
     */
    public function export() {
        $response = new \stdClass();
        $response->code = 500;

        $request = $this->request;
        $user = $this->user;
        if (!in_array($user["type"], [1, 2, 5, 6])) {
            $response->msg = "当前账号类型没有操作权限";
            return \StrUtil::back($response, "TalentInfo.callBack");
        }
        $names = \StrUtil::getRequestDecodeParam($request, "names");
        $values = \StrUtil::getRequestDecodeParam($request, "values");

        $title = array_filter(explode(",", $names)); //exce标题
        $keys = array_filter(explode(",", $values)); //标题对应的字段

        $where = [];
        //$where[] = ["type", "=", $user["type"]];

        if ($_where = EducationApi::getWhereByParams($request->param())) {
            $where = array_merge($where, $_where);
        }

        $list = \app\common\model\EducationSchool::where($where)->select()->toArray();

        if (in_array("talentArrangeName", $keys)) {
            $levelMap = DictApi::selectByParentCode("talent_arrange");
        }
        if (in_array("companyStreetName", $keys) || in_array("houseStreetName", $keys)) {
            $streetMap = DictApi::selectByParentCode("street");
        }
        if (in_array("cRelationName", $keys)) {
            $relationMap = DictApi::selectByParentCode("education_relation");
        }
        if (in_array("nowGradeName", $keys)) {
            $gradeMap = DictApi::selectByParentCode("education_grade");
        }
        if (in_array("applySchoolName", $keys)) {
            $schoolMap = DictApi::selectByParentCode("education_school_pool");
        }
        if (in_array("projectName", $keys)) {
            $educationProjectMap = DictApi::selectByParentCode("education_project");
        }

        $rows = [];

        foreach ($list as $item) {
            $row = [];
            foreach ($keys as $k) {
                $val = $item[$k];
                switch ($k) {
                    case "talentArrangeName":
                        $val = $levelMap[$item["talentArrange"]];
                        break;
                    case "companyStreetName":
                        $val = $streetMap[$item["companyStreet"]];
                        break;
                    case "houseStreetName":
                        $val = $streetMap[$item["houseStreet"]];
                        break;
                    case "cRelationName":
                        $val = $relationMap[$item["cRelation"]];
                        break;
                    case "nowGradeName":
                        $val = $gradeMap[$item["nowGrade"]];
                        break;
                    case "applySchoolName":
                        $val = $schoolMap[$item["applySchool"]];
                        break;
                    case "projectName":
                        $val = $educationProjectMap[$item["project"]];
                        break;
                    case "pSexName":
                        $val = $item["pSex"] == 1 ? "男" : "女";
                        break;
                    case "cSexName":
                        $val = $item["cSex"] == 1 ? "男" : "女";
                        break;
                    case "checkStateName":
                        if ($item["checkState"] == -2) {
                            $val = "保存未提交";
                        }if ($item["checkState"] == -1) {
                            $val = "审核不通过";
                        }if ($item["checkState"] == 1) {
                            $val = "待审核";
                        }if ($item["checkState"] == 2) {
                            $val = "审核驳回";
                        }if ($item["checkState"] == 3) {
                            $val = "审核通过";
                        }if ($item["checkState"] == 9) {
                            $val = "重新提交";
                        }
                        break;
                }
                $row[] = $val;
            }
            $rows[] = $row;
        }
        if (!$rows) {
            $response->msg = "没有可导出的内容";
            return \StrUtil::back($response, "TalentInfo.callBack");
        }
        $fileName = "子女择校导出";
        export($title, $rows, $fileName);
    }

}
