<?php

namespace app\admin\controller;

use app\admin\common\AdminController;
use app\common\api\EnterpriseVerifyMgrApi;

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

    /**
     * @auth {{/enterprisetypeProperties}}
     * @return type
     */
    public function index() {
        return view();
    }

    public function list() {
        return json(EnterpriseVerifyMgrApi::getList($this->request->param()));
    }

    /**
     * @auth {{/enterprisetypeProperties/add}}
     * @return type
     */
    public function add() {
        if ($this->request->isPost()) {
            $params = $this->request->param();
            if (!$params["companyId"]) {
                return json(["msg" => "请选择审核单位"]);
            }
            if (!$params["enterpriseTag"] && !$params["uniCode"] && !$params["organizationTag"] && !$params["institutionTag"]) {
                return json(["msg" => "请选择单位标签或填写例外企业统一信用代码，可同时设置"]);
            }
            if (EnterpriseVerifyMgrApi::chkExist($params["enterpriseTag"]) && $params["enterpriseTag"])
                return json(["msg" => "该标签已经存在审核配置，不需要重复配置"]);
            if (EnterpriseVerifyMgrApi::chkUnicodeExist($params["uniCode"]) && $params["uniCode"])
                return json(["msg" => "输入的例外企业统一信用代码中包含已经被其它单位设置过的企业，不能重复设置"]);
            if (EnterpriseVerifyMgrApi::edit($params))
                return json(["code" => 200, "msg" => "添加单位标签审核配置成功"]);
            return json(["msg" => "添加单位标签审核配置失败"]);
        }
        return view();
    }

    /**
     * @auth {{/enterprisetypeProperties/update}}
     * @return type
     */
    public function edit() {
        $params = $this->request->param();
        if ($this->request->isPost()) {
            if (!$params["id"])
                return json(["msg" => "没有正文，无法编辑"]);
            if (!$params["companyId"]) {
                return json(["msg" => "请选择审核单位"]);
            }
            if (!$params["enterpriseTag"] && !$params["uniCode"] && !$params["organizationTag"] && !$params["institutionTag"]) {
                return json(["msg" => "请选择单位标签或填写例外企业统一信用代码，可同时设置"]);
            }
            if (EnterpriseVerifyMgrApi::chkExist($params["enterpriseTag"], $params["id"]) && $params["enterpriseTag"])
                return json(["msg" => "该标签已经存在审核配置，不需要重复配置"]);
            if (EnterpriseVerifyMgrApi::chkUnicodeExist($params["uniCode"], $params["id"]) && $params["uniCode"])
                return json(["msg" => "输入的例外企业统一信用代码中包含已经被其它单位设置过的企业，不能重复设置"]);
            if (EnterpriseVerifyMgrApi::edit($params))
                return json(["code" => 200, "msg" => "编辑单位标签审核配置成功"]);
            return json(["msg" => "编辑单位标签审核配置失败"]);
        }
        return view("", ["info" => EnterpriseVerifyMgrApi::getOne($params["id"])]);
    }

    /**
     * @auth {{/enterprisetypeProperties/delete}}
     * @return type
     */
    public function delete() {
        if ($this->request->isPost()) {
            $id = $this->request->param("id");
            if (EnterpriseVerifyMgrApi::delete($id))
                return json(["code" => 200, "msg" => "删除单位标签审核配置成功"]);
            return json(["msg" => "删除单位标签审核配置失败"]);
        }
    }

}
