<?php

namespace app\enterprise\validate;

use think\Validate;
use app\common\api\TalentState;

/**
 * Description of TalentInfo
 * 先不用。分两步等下通不过
 * @author sgq
 */
class TalentInfo extends Validate {

    protected $rule = [
        //"name" => "regex:/^[\x{4e00}-\x{9fa5}A-Za-z ]+$/u",
        "nation" => "checkInSelect:nation,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57",
        "card_type" => "between:1,3",
        "card_number" => "checkCardNumber|checkUnique:new_talent_info",
        "sex" => "between:1,2",
        "politics" => "checkInSelect:politics,01,02,03,04,05,06,07,08,09,10,11,12,13,14",
        "birthday" => "dateFormat:Y-m-d",
        "nationality" => "checkInSelect:nationality,cn,cn_hk,cn_mc,cn_tw,us,en,jp,co,nl,sg,other",
        "talent_type" => "between:1,3",
        "tax_insurance_month" => "checkRangeDate:tax_insurance_month",
        "labor_contract_rangetime" => "checkRangeDate:labor_contract_rangetime",
        "fst_work_time" => "dateFormat:Y-m-d",
        "pre_import_type" => "between:1,2",
        "return" => "between:0,2",
        "salary_pay_way" => "between:1,2",
        "salary_pay_month" => "checkRangeDate:salary_pay_month",
        "experience" => "max:1000",
        "education" => "max:1000",
        "phone" => "mobile",
        "email" => "email",
        "highest_degree" => "checkInSelect:highest_degree,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21",
        "study_abroad" => "between:1,2",
        "import_way" => "between:1,3",
        "cur_entry_time" => "dateFormat:Y-m-d",
        "source" => "between:1,5",
        "fujian_highcert_pubtime" => "dateFormat:Y-m-d",
        "fujian_highcert_exptime" => "dateFormat:Y-m-d",
        "quanzhou_highcert_pubtime" => "dateFormat:Y-m-d",
        "quanzhou_highcert_exptime" => "dateFormat:Y-m-d",
        "talent_arrange" => "between:1,7",
        "bank" => "regex:/^[\x{4e00}-\x{9fa5}]+银行$/u",
        "bank_number" => "regex:/^\d+$/",
        "bank_branch_name" => "regex:/^[\x{4e00}-\x{9fa5}]+银行[\x{4e00}-\x{9fa5}]+省?[\x{4e00}-\x{9fa5}]+市[\x{4e00}-\x{9fa5}]+$/u",
        "bank_account" => "regex:/^\d+$/",
        "break_faith" => "between:1,2",
        "identifyGetTime" => "dateFormat:Y-m-d"
    ];
    protected $message = [
        //"name.regex" => "姓名允许中文、英文、或者空格，不能含有其它字符",
        "card_type.between" => "不存在的证件类型，请在列表中选择",
        "card_number.checkUnique" => "身份证已经存在",
        "sex.between" => "性别只能在预设列表中选择",
        "politics.between" => "政治面貌只能在预设列表中选择",
        "birthday.dateFormat" => "生日必需是有效日期格式[yyyy-MM-dd]",
        "talent_type.between" => "不存在的人才类型，请在列表中选择",
        "fst_work_time.dateFormat" => "来晋工作时间必需是有效的日期格式[yyyy-MM-dd]",
        "pre_import_type.between" => "预引进类型只能在预设列表中选择",
        "return.between" => "是否流出晋江满3年后又返回晋江的选项只能在预设列表中选择",
        "salary_pay_way.between" => "工资发放渠道只能在预设列表中选择",
        "experience.max" => "工作经历不能超过200个字符，请简明扼要的描述您的工作经历",
        "education.max" => "教育背景不能超过200个字符，请简明扼要的描述您的教育背景",
        "phone.mobile" => "请填写正确的手机号",
        "email.email" => "电子邮箱格式错误",
        "highest_degree.between" => "最高学历只能在预设列表中选择",
        "study_abroad.between" => "留学经历只能选择是与否",
        "import_way.between" => "不存在的引进方式，请在列表中选择",
        "cur_entry_time.dateFormat" => "本单位入职时间必需是有效的日期格式[yyyy-MM-dd]",
        "source.between" => "不存在的申报来源，请在列表中选择",
        "fujian_highcert_pubtime.dateFormat" => "福建省高层次人才证书发证日期必需是有效的日期格式[yyyy-MM-dd]",
        "fujian_highcert_exptime.dateFormat" => "福建省高层次人才证书有效期必需是有效的日期格式[yyyy-MM-dd]",
        "quanzhou_highcert_pubtime.dateFormat" => "泉州高层次人才证书发证日期必需是有效的日期格式[yyyy-MM-dd]",
        "quanzhou_highcert_exptime.dateFormat" => "泉州高层次人才证书有效期必需是有效的日期格式[yyyy-MM-dd]",
        "talent_arrange" => "不存在的人才层次，请在列表中选择",
        "bank.regex" => "开户银行格式应为[XX银行]",
        "bank_number.regex" => "银行行号只能是数字",
        "bank_branch_name.regex" => "开户银行网点格式应为[XX银行XX省XX市XX行/处]",
        "bank_account.regex" => "银行账号只能是数字",
        "break_faith.between" => "是否曾被相关主管部门列为失信个人只能在预设列表中选择",
        "identifyGetTime.dateFormat" => "认定条件证书取得时间必需是有效的日期格式[yyyy-MM-dd]",
    ];

    protected function checkUnique($value, $rule, $data = [], $field) {
        $db = $this->db->name($rule);
        $pk = $db->getPk();
        $map = [];
        $map[] = [$field, "=", $data[$field]];
        $map[] = ["delete", "=", 0];
        $map[] = ["checkState", "not in", [TalentState::BASE_VERIFY_FAIL, TalentState::BASE_REVERIFY_FAIL, TalentState::FST_VERIFY_FAIL, TalentState::REVERIFY_FAIL, TalentState::ZX_FAIL, TalentState::ANNOUNCED_REVERIFY_FAIL, TalentState::PUBLISH_FAIL]];
        if ($data[$pk]) {
            $map[] = [$pk, "<>", $data[$pk]];
        }
        if ($db->where($map)->field($pk)->find()) {
            return false;
        }
        return true;
    }

    protected function checkInSelect($value, $rule, $data = []) {
        $title = "";
        $select = explode(",", $rule);
        $type = array_shift($select);
        switch ($type) {
            case "nation":
                $title = "民族";
                break;
            case "politics":
                $title = "政治面貌";
                break;
            case "nationality":
                $title = "国籍/地区";
                break;
            case "highest_degree":
                $title = "最高学历";
                break;
        }
        return in_array($value, $select) ?: "{$title}只能在预设列表中选择";
    }

    protected function checkCardNumber($value, $rule, $data = []) {
        if (!$data["card_type"]) {
            return "填写证件号码前请先选择证件类型";
        }
        if ($data["card_type"] == 1) {
            $num = strlen($value);
            switch ($num) {
                case 15:
                case 18:
                    return \app\common\api\IdCardApi::isValid($value) ?: "身份证号码不合法";
                    break;
                default:
                    //return \app\common\api\IdCardApi::isValidExceptMainland($value) ?: "身份证号码不合法";
                    break;
            }
            return "身份证号码不合法";
        } else if ($data["card_type"] == 2) {
            if (preg_match("/^[a-zA-Z0-9]{6,10}$/", $value) || preg_match("/^([0-9]{8}|[0-9]{10})$/", $value)) {
                return true;
            }
            return "通行证号码不合法";
        } else {
            if (preg_match("/^([a-zA-z]|[0-9]){5,17}$/", $value)) {
                return true;
            }
            return "护照号码不合法";
        }
    }

    protected function checkRangeDate($value, $rule, $data = []) {
        $title = "";
        $format = "[yyyy-MM-dd - yyyy-MM-dd]";
        $str = "日期";
        switch ($rule) {
            case "tax_insurance_month":
                $format = "[yyyy-MM - yyyy-MM]";
                $str = "月份";
                if ($data["talent_type"] == 1) {
                    $title = "缴交社会保险或个人所得税月份";
                }
                if ($data["talent_type"] == 2) {
                    $title = "首次在我市缴交社会保险或个人所得税月份";
                }
                break;
            case "labor_contract_rangetime":
                $title = "劳动合同起止时间";
                break;
            case "salary_pay_month":
                $format = "[yyyy-MM - yyyy-MM]";
                $str = "月份";
                $title = "工资发放月份";
                break;
        }
        $arr = explode(" - ", $value);
        $chk1 = strtotime($arr[0]);
        $chk2 = strtotime($arr[1]);
        if (!$chk1 || !$chk2)
            return "{$title}日期范围格式应为{$format}";
        if ($chk1 > $chk2)
            return "{$title}起始{$str}不能大于结束{$str}";
        return true;
    }

}
