<?php

namespace app\enterprise\validate;

use think\Validate;

/**
 * Description of LivingAllowance
 * @author sgq
 */
class LivingAllowance extends Validate {

    protected $rule = [
        "type" => "require",
        "idCard" => "require|checkCardNumber",
        "cardType" => "require",
        "name" => "require",
        "sex" => "require",
        "nation" => "require",
        "nationality" => "require",
        "provinceCode" => "require",
        "cityCode" => "require",
        "birthday" => "require",
        "address" => "require",
        "politics" => "require",
        "highEducation" => "require",
        "major" => "require",
        "post" => "require|regex:/^[\x{4e00}-\x{9fa5}]+$/u",
        "phone" => "require|mobile",
        "email" => "require|email",
        "bank" => "require|regex:/^[\x{4e00}-\x{9fa5}]*银行$/u",
        "bankNetwork" => "require|regex:/^[\x{4e00}-\x{9fa5}]*银行[\x{4e00}-\x{9fa5}]*省?[\x{4e00}-\x{9fa5}]+市[\x{4e00}-\x{9fa5}]*$/u",
        "bankAccount" => "require|regex:/^\d+$/",
        "entryTime" => "require",
        "startTime" => "require",
        "endTime" => "require",
        "mainHonours" => "require",
        "educationAndResume" => "require",
        "bankNumber" => "require|regex:/^\d+$/",
        "introductionMethod" => "require|checkInJJTime",
        "industryFieldNew" => "checkIndustryFieldNew",
        "industryField" => "checkIndustryField",
        //"socialSecurity" => "require"
    ];
    protected $message = [
        "type.require" => "人才类别不能为空",
        "idCard.require" => "证件号码不能为空",
        "cardType.require" => "证件类型不能为空",
        "name.require" => "姓名不能为空",
        "sex.require" => "性别不能为空",
        "nation.require" => "民族不能为空",
        "nationality.require" => "国籍/地区不能为空",
        "provinceCode.require" => "籍贯-省不能为空",
        "cityCode.require" => "籍贯-市不能为空",
        "birthday.require" => "出生日期不能为空",
        "address.require" => "现居地址不能为空",
        "politics.require" => "政治面貌不能为空",
        "highEducation.require" => "最高学历不能为空",
        "major.require" => "专业不能为空",
        "post.require" => "职务不能为空",
        "post.regex" => "职务必须为汉字",
        "phone.require" => "手机号码不能为空",
        "phone.mobile" => "手机号格式不正确",
        "email.require" => "电子邮箱不能为空",
        "email.email" => "电子邮箱格式不正确",
        "bank.require" => "开户银行不能为空",
        "bank.regex" => "开户银行格式不正确",
        "bankNetwork.require" => "开户银行网点不能为空",
        "bankNetwor.regex" => "开户银行网点格式不正确",
        "bankAccount.require" => "银行账号不能为空",
        "bacnkAccount.regex" => "银行账户格式不合法",
        "entryTime.require" => "入职时间不能为空",
        "startTime.require" => "工作合同开始时间不能为空",
        "endTime.require" => "工作合同结束时间不能为空",
        "mainHonours.require" => "主要业绩及取得的荣誉不能为空",
        "educationAndResume.require" => "教育背景及工作简历不能为空",
        "bankNumber.require" => "银行行号不能为空",
        "bankNumber.regex" => "银行行号格式不合法",
        "introductionMethod.require" => "引进方式不能为空",
        "socialSecurity.require" => "社保缴交情况不能为空"
    ];

    protected function checkCardNumber($value, $rule, $data = []) {
        if (!$data["cardType"]) {
            return "填写证件号码前请先选择证件类型";
        }
        if ($data["cardType"] == 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["cardType"] == 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 checkInJJTime($value, $rule, $data = []) {
        if ($value == 1 && !$data["firstInJJTime"]) {
            return "首次来晋时间不能为空";
        }
        if ($value == 2 && !$data["backJJBusinessTime"]) {
            return "返晋创业时间不能为空";
        }
        return true;
    }

    protected function checkIndustryFieldNew($value, $rule, $data = []) {
        if ($data["type"] == 1 && !$value) {
            return "产业领域不能为空";
        }
        return true;
    }

    protected function checkIndustryField($value, $rule, $data = []) {
        if ($data["type"] == 1 && !$value) {
            return "行业领域不能为空";
        }
        return true;
    }

    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;
    }

}
