<?php

namespace app\common\validate;

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

class Hospital 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',
        'legal' => 'require', //|regex:/^[\x{4e00}-\x{9fa5}]+$/u
        'ephone' => 'require',
        'medicalCommunityId' => 'require',
        'isGeneral' => 'require',
        'province' => 'require',
        'city' => 'require',
        'address' => 'require',
        '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',
        'agentName' => 'require|regex:/^[\x{4e00}-\x{9fa5}]+$/u|max:30',
        'agentPhone' => 'require|mobile',
        'verificationCode' => 'require',
        'agentEmail' => 'require|email'
    ];
    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个字符!',
        'idCard.require' => '请填写登记号!',
        'legal.require' => '请输入医院法人',
        'ephone.require' => '请输入医院电话',
        'medicalCommunityId.require' => '请选择医共体',
        'isGeneral.require' => '请选择是否总院',
        'province.require' => '请选择医院所在省份',
        'city.require' => '请选择医院所在市',
        'address.require' => '请输入医院地址',
        'bankCard.require' => '请输入银行账号',
        'bank.require' => '请输入开户银行',
        'bankNetwork.require' => '请输入开户银行网点',
        'bank.regex' => '开户银行不正确，请检查',
        'bankNetwork.regex' => '开户银行网点不正确，请检查',
        'agentName.require' => '请填写人才联络员',
        'agentName.max' => '人才联络员最多30个字符',
        'agentName.regex' => '人才联络员姓名只能是中文',
        'agentPhone.require' => '请填写人才联络员手机号',
        "agentPhone.mobile" => "请填写正确的人才联络员手机号",
        'verificationCode.require' => '请输入手机验证码',
        'agentEmail|require' => '请输入邮箱',
        'agentEmail.email' => '邮箱格式错误'
    ];
    protected $scene = [
        'add' => ['username', 'password', 're_password', 'name', 'idCard', 'legal', 'ephone', 'medicalCommunityId', 'isGeneral', 'province', 'city', 'address', 'bankCard', 'bank', 'bankNetwork', 'agentName', 'agentPhone', 'verificationCode', 'agentEmail'],
        'change' => ['name', 'idCard', 'legal', 'ephone', 'medicalCommunityId', 'isGeneral', 'province', 'city', 'address', 'bankCard', 'bank', 'bankNetwork', 'agentName', 'agentPhone', 'agentEmail'],
        'changePwd' => ['password']
    ];

    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 = []) {
        $info = EnterpriseModel::where('idCard', $value)->where('delete', 0)->where('id', '<>', $data['id'])->find();
        if ($info) {
            return "该统一社会信用代码已存在";
        } else {
            return true;
        }
    }

}
