<?php

namespace app\admin\controller;

use app\admin\common\AdminController;
use app\common\api\TalentState;
use app\enterprise\model\Talent;

/**
 * Description of Login
 *
 * @author sgq
 */
class Index extends AdminController {

    public function index() {
        $vars["user"] = [
            "name" => $this->user["name"],
            "rolename" => $this->user["rolename"],
            "avatar" => $this->user["avatar"],
            "sex" => $this->user["sex"]
        ];
        $menus = \app\common\api\MenuApi::getMenuListByRoleid($this->user["roleid"]);
        $vars["menus"] = $menus;

        $codes = \app\common\api\MenuApi::getCodesByRoleId($this->user["roleid"]);
        $backLogCount = 0;
        $userType = session("user")["type"];
        $companyId = session("user")["companyId"];
        if (in_array("talentInfo_firstCheck", $codes)) {
            $where[] = ["e.type", "=", $userType];
            $where[] = ["ti.checkState", "=", TalentState::SCND_SUBMIT];
            $where[] = ["tl.state", "in", [TalentState::SCND_SUBMIT, TalentState::DEPT_VERIFY_REJECT, TalentState::REVERIFY_REJECT]];
            $count = Talent::alias("ti")
                            ->leftJoin("un_enterprise e", "e.id=ti.enterprise_id")
                            ->leftJoin("(select mainId,last_state,new_state,state,createTime from new_talent_checklog where md5(concat(createTime,mainId,`type`)) in (select md5(concat(max(createTime),mainId,`type`)) from `new_talent_checklog` where `type`=1 and `step` is null and active=1 and typeFileId is null group by mainId,`type`)) tl", "`tl`.`mainId`=ti.id")
                            ->where($where)->count();
            if ($count > 0) {
                $backlog = ["title" => "【人才认定申报】待初审", "url" => "$('a.J_menuItem[href=\"/admin/talent/fst_verify\"]').click()", "count" => $count];
                $vars["backlog"][] = $backlog;
                $backLogCount += $count;
            }
        }
        if (in_array("talentInfo_depCheck", $codes)) {
            $where = [];
            if (\app\common\api\VerifyApi::chkUserInSuperDeptUsers()) {
                $where[] = ["e.type", "=", $userType];
                $where[] = ["ti.checkState", "=", TalentState::FST_VERIFY_PASS];
                $where[] = ["ti.pass_dept_check", "=", 0];
                $count = Talent::alias("ti")
                                ->leftJoin("un_enterprise e", "e.id=ti.enterprise_id")
                                ->where($where)->count();
            } else {
                //$where[] = ["tl.active", "=", 0];
                //$where[] = ["tl.state", "=", TalentState::FST_VERIFY_PASS];                
                $whereRaw = sprintf("((tl.active=0 and tl.state=%d) or (tl.active is null)) and ti.pass_dept_check=0 and tl.state=%d", TalentState::FST_VERIFY_PASS, TalentState::FST_VERIFY_PASS);
                $count = Talent::alias("ti")
                                ->leftJoin("new_talent_condition tc", "tc.id=ti.talent_condition")
                                ->leftJoin("un_enterprise e", "e.id=ti.enterprise_id")
                                ->leftJoin("(select mainId,active,last_state,new_state,state,createTime from new_talent_checklog where md5(concat(createTime,companyId,mainId,`type`)) in (select md5(concat(max(createTime),companyId,mainId,`type`)) from `new_talent_checklog` where `type`=1 and `step`=3 and companyId='{$companyId}' and typeFileId is null group by mainId,`type`)) tl", "`tl`.`mainId`=ti.id")
                                ->where($where)
                                ->where($whereRaw)
                                ->whereRaw("find_in_set(:companyId,companyIds)", ["companyId" => $companyId])->count();
            }
            if ($count > 0) {
                $backlog = ["title" => "【人才认定申报】待部门并审", "url" => "$('a.J_menuItem[href=\"/admin/talent/dept_verify\"]').click()", "count" => $count];
                $vars["backlog"][] = $backlog;
                $backLogCount += $count;
            }
        }
        if (in_array("talentInfo_thirdCheck", $codes)) {
            $whereRaw = sprintf("((ti.checkState=%d and ti.pass_dept_check=0) or (ti.checkState=%d and ti.pass_dept_check=1) or (ti.checkState=%d and (tc.companyIds is null or tc.companyIds = ''))) and e.type=%d"
                    , TalentState::DEPT_VERIFY_PASS, TalentState::FST_VERIFY_PASS, TalentState::FST_VERIFY_PASS, $userType);

            $count = Talent::alias("ti")
                            ->leftJoin("un_enterprise e", "e.id=ti.enterprise_id")
                            ->leftJoin("new_talent_condition tc", "tc.id=ti.talent_condition")
                            ->whereRaw($whereRaw)->count();
            if ($count > 0) {
                $backlog = ["title" => "【人才认定申报】待复审", "url" => "$('a.J_menuItem[href=\"/admin/talent/re_verify\"]').click()", "count" => $count];
                $vars["backlog"][] = $backlog;
                $backLogCount += $count;
            }
        }



        $vars["backLogCount"] = $backLogCount;
        return view("", $vars);
    }

}
