<?php

namespace app\enterprise\validate;

use think\Validate;

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

    protected $rule = [
        "type" => "require",
        "name" => "require",
        "cardType" => "require",
        "idCard" => "require|checkCardNumber",
        "declareType" => "require|checkDeclareType",
        "phone" => "require|mobile",
        "marryStatus" => "require"
    ];
    protected $message = [
        "type.require" => "人才类别不能为空",
        "name.require" => "姓名不能为空",
        "cardType.require" => "证件类型不能为空",
        "idCard.require" => "证件号码不能为空",
        "declareType.require" => "申报类型不能为空",
        "phone.require" => "手机号码不能为空",
        "phone.mobile" => "手机号格式不正确",
        "marryStatus.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 checkDeclareType($value, $rule, $data = []) {
        if ($value == 1) {

            if (\StrUtil::isEmpOrNull($data["houseAddress"])) {
                return "房屋坐落地址不能为空";
            }
            if (!$data["houseArea"]) {
                return "房屋建筑面积不能为空";
            }
            if (\StrUtil::isEmpOrNull($data["recordTime"])) {
                return "商品房购房合同备案时间不能为空";
            }
            if (!$data["houseMoney"]) {
                return "房屋成交金额不能为空";
            }
            if (!$data["isEnjoyOther"]) {
                return "是否享受我市其他政策不能为空";
            }
            if (\StrUtil::isEmpOrNull($data["realEstateNo"]) && \StrUtil::isEmpOrNull($data["recordNo"])) {
                return "不动产权证编号和备案合同编号至少填写一个";
            }
            if (\StrUtil::isNotEmpAndNull($data["realEstateNo"]) && !preg_match("/^闽(\\(|（)[0-9]{4}(\\)|）)晋江市不动产权第(\\(|（)[0-9]+(\\)|）)号$/", $data["realEstateNo"])) {
                return "不动产权证编号格式不正确";
            }
            if (\StrUtil::isNotEmpAndNull($data["recordNo"]) && !preg_match("/^\\d{12}$/", $data["recordNo"])) {
                return "备案合同编号格式不正确";
            }
        }
        return true;
    }

}
