<?php

namespace app\enterprise\controller;

use app\admin\model\Enterprise;
use app\enterprise\common\EnterpriseController;
use app\common\api\DictApi;

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

    public function index() {
        $vars["user"] = [
            "name" => $this->user["name"],
            "rolename" => $this->user["rolename"],
            "avatar" => $this->user["avatar"]
        ];
        $ep = \app\common\api\EnterpriseApi::getOne($this->user["uid"]);
        switch ($ep["special"]) {
            case 0:
                if ($ep["type"] == 1) {
                    $checkEnterpriseFullFields = ["agencyType", "enterpriseTag", "enterpriseType", "bankCard", "bankNetwork", "bank", "imgurl", "bankImg", "beian"];
                    if ($ep["agencyType"] == 1) {
                        $checkEnterpriseFullFields[] = "industryFieldNew";
                        $checkEnterpriseFullFields[] = "industryFieldOld";
                        $checkEnterpriseFullFields[] = "domainImg";
                    }
                    if (in_array($ep["enterpriseType"], ['guishang', 'gaoxinjishu', 'zhuanjingtexin'])) {
                        $checkEnterpriseFullFields[] = "typeImg";
                    }
                } else {
                    $checkEnterpriseFullFields = ["bankCard", "bankNetwork", "bank", "imgurl", "bankImg", "beian"];
                }
                break;
            case 1:
                $checkEnterpriseFullFields = ["institutionTag"];
                break;
            case 3:
                $checkEnterpriseFullFields = ["organizationTag"];
                break;
        }
        $errorCounts = 0;
        while ($chk = array_shift($checkEnterpriseFullFields)) {
            if ($ep[$chk] == null)
                $errorCounts++;
        }
        $vars["isEnterpriseFull"] = $errorCounts > 0 ? 0 : 1;
        $menus = \app\common\api\MenuApi::getEnterpriseMenuByType($this->user["type"]);
        $vars["menus"] = $menus;
        return view("", $vars);
    }

    public function centerPage() {
        $ep = Enterprise::where('id', $this->user['uid'])->find();
        $ep['agencyTypeName'] = DictApi::findByParentCodeAndCode('agency_type', $ep['agencyType'])->name ?? "";
        $ep['industryFieldNewName'] = DictApi::findDictByCode($ep['industryFieldNew'])->name ?? '';
        $ep['industryFieldOldName'] = DictApi::findDictByCode($ep['industryFieldOld'])->name ?? '';
        $ep['enterpriseTagName'] = DictApi::findDictByCode($ep['enterpriseTag'])->name ?? '';
        $ep['organizationTagName'] = DictApi::findDictByCode($ep['organizationTag'])->name ?? '';
        $ep['institutionTagName'] = DictApi::findDictByCode($ep['institutionTag'])->name ?? '';
        $ep['streetName'] = DictApi::findDictByCode($ep['street'])->name ?? '';
        $ep['enterpriseTypeName'] = DictApi::findDictByCode($ep['enterpriseType'])->name ?? '';
        if ($ep["imgurl"]) {
            $pathinfo = pathinfo($ep["imgurl"]);
            if (in_array($pathinfo["extension"], ["jpg", "jpeg", "png", "gif"])) {
                $ep["imgurl_is_img"] = 1;
            }
        }
        if ($ep["bankImg"]) {
            $pathinfo = pathinfo($ep["bankImg"]);
            if (in_array($pathinfo["extension"], ["jpg", "jpeg", "png", "gif"])) {
                $ep["bankImg_is_img"] = 1;
            }
        }
        if ($ep["domainImg"]) {
            $pathinfo = pathinfo($ep["domainImg"]);
            if (in_array($pathinfo["extension"], ["jpg", "jpeg", "png", "gif"])) {
                $ep["domainImg_is_img"] = 1;
            }
        }
        if ($ep["typeImg"]) {
            $pathinfo = pathinfo($ep["typeImg"]);
            if (in_array($pathinfo["extension"], ["jpg", "jpeg", "png", "gif"])) {
                $ep["typeImg_is_img"] = 1;
            }
        }
        if ($ep["beian"]) {
            $pathinfo = pathinfo($ep["beian"]);
            if (in_array($pathinfo["extension"], ["jpg", "jpeg", "png", "gif"])) {
                $ep["beian_is_img"] = 1;
            }
        }
        switch ($ep['type']) {
            case 1:
                $ep['typeName'] = '晋江市现代产业体系人才';
                break;
            case 2:
                $ep['typeName'] = '集成电路优秀人才';
                break;
            case 3:
                $ep['typeName'] = '海峡计划团队';
                break;
        }
        switch ($ep['checkState']) {
            case 1:
                $ep['checkStateName'] = '待审核';
                break;
            case 2:
                $ep['checkStateName'] = '审核驳回';
                break;
            case 3:
                $ep['checkStateName'] = '审核通过';
                break;
            case 4:
                $ep['checkStateName'] = '重新提交';
                break;
        }
        switch ($ep['active']) {
            case 1:
                $ep['activeName'] = '账号有效';
                break;
            case 2:
                $ep['activeName'] = '冻结/拉黑';
                break;
        }


        return view("", ['ep' => $ep]);
    }

}
