<?php

namespace app\common\validate;

use think\Validate;
use app\admin\model\Enterprise as EnterpriseModel;

class Enterprise extends Validate {

    protected $rule = [
        'username' => 'require|max:50|checkUserName',
        'password' => 'require|min:8|regex:/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,}$/',
        're_password' => 'require|confirm:password',
        'name' => 'require|max:100|checkName', //|regex:/^[\x{4e00}-\x{9fa5}\(\)（）\da-zA-Z&]{2,50}$/u
        'idCard' => 'require|checkID',
        'type' => 'require',
        'agencyType' => 'require',
        'industryFieldNew' => 'require',
        'enterpriseTag' => 'require',
        'enterpriseType' => 'require',
        'agentName' => 'require|regex:/^[\x{4e00}-\x{9fa5}]+$/u|max:30',
        'agentPhone' => 'require|mobile',
        'verificationCode' => 'require',
        'legal' => 'require', //|regex:/^[\x{4e00}-\x{9fa5}]+$/u
        'street' => 'require',
        'address' => 'require',
        'ephone' => 'require',
        'agentEmail' => 'require|email',
        'bankCard' => 'require',
        'bank' => 'require|regex:/^[\x{4e00}-\x{9fa5}\(\)（）\da-zA-Z&]{2,50}$/u',
        'bankNetwork' => 'require|regex:/^[\x{4e00}-\x{9fa5}\(\)（）\da-zA-Z&]{2,50}$/u'
    ];
    protected $message = [
        'username.require' => '请填写账号!',
        'username.max' => '账号最多50个字符!',
        'username.unique' => '该账号已被注册',
        'password.require' => '请填写密码!',
        'password.min' => '密码最少长度8位',
        'password.regex' => '密码应包含字母与数字两种字符',
        're_password.require' => '请输入重复密码',
        're_password.confirm' => '两次密码输入不一致',
        'name.require' => '请输入单位名称',
        'name.max' => '单位名称最多100个字符!',
        'name.unique' => '该单位名称已存在',
        'idCard.require' => '请填写统一社会信用代码!',
        'idCard.regex' => '统一社会信用代码不正确！',
        'idCard.unique' => '该统一社会信用代码已存在',
        'type.require' => '申报类型必须选择',
        'type.require' => '机构类型必须选择',
        'industryFieldNew.require' => '产业领域必须选择',
        'enterpriseTag.require' => '单位标签必须选择',
        'enterpriseType.require' => '单位类型必须选择',
        'agentName.require' => '请填写人才联络员',
        'agentName.max' => '人才联络员最多30个字符',
        'agentName.regex' => '人才联络员姓名只能是中文',
        'agentPhone.require' => '请填写人才联络员手机号',
        "agentPhone.mobile" => "请填写正确的人才联络员手机号",
        'verificationCode.require' => '请输入手机验证码',
        'legal.require' => '请输入单位法人',
        //'legal.regex' => '法人姓名只能是中文',
        'street.require' => '请选择所属镇（街道）',
        'address.require' => '请输入单位地址',
        'ephone.require' => '请输入单位电话',
        'agentEmail|require' => '请输入邮箱',
        'agentEmail.email' => '邮箱格式错误',
        'bankCard.require' => '请输入企业银行账号',
        'bank.require' => '请输入企业开户银行',
        'bankNetwork.require' => '请输入企业开户银行网点',
        'bank.regex' => '开户银行不正确，请检查',
        'bankNetwork.regex' => '开户银行网点不正确，请检查'
    ];
    protected $scene = [
        'add' => ['username', 'password', 're_password', 'name', 'idCard', 'type', 'agencyType', 'industryFieldNew', 'enterpriseTag', 'enterpriseType', 'agentName', 'agentPhone', 'verificationCode', 'legal', 'street', 'address', 'ephone', 'agentEmail', 'bankCard', 'bank', 'bankNetwork'],
        'add2' => ['username', 'password', 're_password', 'name', 'idCard', 'type', 'enterpriseTag', 'enterpriseType', 'agentName', 'agentPhone', 'verificationCode', 'legal', 'street', 'address', 'ephone', 'agentEmail', 'bankCard', 'bank', 'bankNetwork'],
        'change' => ['name', 'idCard', 'industryFieldNew', 'enterpriseTag', 'enterpriseType', 'agentName', 'agentPhone', 'legal', 'street', 'address', 'ephone', 'agentEmail', 'bankCard', 'bank', 'bankNetwork'],
        'change2' => ['name', 'idCard', 'enterpriseTag', 'enterpriseType', 'agentName', 'agentPhone', 'legal', 'street', 'address', 'ephone', 'agentEmail', 'bankCard', 'bank', 'bankNetwork'],
        'changePwd' => ['password'],
        'check_common' => ['name', 'idCard', 'industryFieldNew', 'enterpriseTag', 'enterpriseType', 'agentName', 'legal', 'street', 'address', 'ephone', 'agentEmail', 'bankCard', 'bank', 'bankNetwork'],
        'jc_add' => ['username', 'password', 're_password', 'name', 'idCard', 'type', 'agentName', 'agentPhone', 'verificationCode', 'legal', 'street', 'address', 'ephone', 'agentEmail'],
        'sy_add' => ['username', 'password', 're_password', 'name', 'verificationCode', 'street', 'address', 'ephone', 'agentEmail'],
        'sy_change' => ['name', 'agentName', 'agentPhone', 'street', 'address', 'ephone', 'agentEmail'],
        'jc_change' => ['name', 'idCard', 'agentName', 'agentPhone', 'legal', 'street', 'address', 'ephone', 'agentEmail']
    ];

    protected function checkUserName($value, $rule, $data = []) {
        $info = EnterpriseModel::where('username', $value)->where('delete', 0)->where('id', '<>', $data['id'])->find();
        if ($info) {
            return "该账号已被注册";
        } else {
            return true;
        }
    }

    protected function checkName($value, $rule, $data = []) {
        $info = EnterpriseModel::where('name', $value)->where('delete', 0)->where('id', '<>', $data['id'])->find();
        if ($info) {
            return "机构名称已存在";
        } else {
            return true;
        }
    }

    protected function checkID($value, $rule, $data = []) {
        if (!preg_match("/^([0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}|[1-9]\d{14})$/", $value)) {
            return "统一社会信用代码不正确";
        }
        $info = EnterpriseModel::where('idCard', $value)->where('delete', 0)->where('id', '<>', $data['id'])->find();
        if ($info) {
            return "该统一社会信用代码已存在";
        } else {
            return true;
        }
    }

}
