package com.jfinal.club._admin.ws;

import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.validate.Validator;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @ClassName DocWsValidator
 * @Description TODO
 * @Author caolong
 * @Date 2020/3/24 18:21
 */
public class DetailUpdateValidator extends Validator {


    @Override
    protected void validate(Controller c) {

        setShortCircuit(true);
        setRet(Ret.fail( ));
        c.getFile( );
        String ygbg = (c.get("wsman.ygbg"));
        String mtype = (c.get("wsman.mtype"));
        String name = (c.get("wsman.name"));
        String birthday = (c.get("wsman.birthday"));
        String hujiAddress = (c.get("wsman.hujiAddress"));
        String nowAddress = (c.get("wsman.nowAddress"));
        String phone = (c.get("wsman.phone"));
        String idType = (c.get("wsman.idType"));
        String idNumber = (c.get("wsman.idNumber"));

        String company = (c.get("wsman.company"));
        String legalPerson = (c.get("wsman.legalPerson"));
        String companyAddress = (c.get("wsman.companyAddress"));
        String creditCode = (c.get("wsman.creditCode"));
        String legalPhone = (c.get("wsman.legalPhone"));


        if (mtype.equals("个人") || mtype.equals("0") || mtype == null) {
            if (mtype.equals("0")) {
                addError("msg", "请选择原告类型");
            }
            if (StringUtils.isBlank(name)) {
                addError("msg", "请填写姓名");
            } else {
                if (name.length( ) > 10) {
                    addError("msg", "姓名长度不大于10个字符");
                }
            }

            if (!StringUtils.isBlank(hujiAddress) && hujiAddress.length( ) > 250) {
                addError("msg", "户籍地址长度不大于250个字符");
            }

            if (ygbg.equals("2")) {
                if (!StringUtils.isBlank(nowAddress) && nowAddress.length( ) > 250) {
                    addError("msg", "现住址长度不大于250个字符");
                }
            }

            String telreg = "^[0-9]{11}$";
            if (!StringUtils.isBlank(phone) && !phone.matches(telreg)) {
                addError("msg", "请输入11位手机号码!");
            }

            if (idType != null) {
                if (idType.equals("身份证")) {
                    String idreg = "^[0-9]{15}|[0-9]{17}[A-Za-z0-9_]{1}$";
                    if (!StringUtils.isBlank(idNumber) && !idNumber.matches(idreg)) {
                        addError("msg", "身份证输入有误,请修改后提交!");
                    }
                }
                if (idType.equals("护照")) {
                    String preg = "^[0-9]{9}$";
                    if (!StringUtils.isBlank(idNumber) && !idNumber.matches(preg)) {
                        addError("msg", "护照输入有误,请修改后提交!");
                    }
                }
            }
        } else {
            if (StringUtils.isBlank(company)) {
                addError("msg", "请输入公司名称");
            } else {
                if (company.length( ) > 30) {
                    addError("msg", "公司名称不能大于30个字符");
                }
            }

            if (!StringUtils.isBlank(legalPerson) && legalPerson.length( ) > 20) {
                addError("msg", "法人姓名不能大于20个字符");
            }


            if (!StringUtils.isBlank(companyAddress) && companyAddress.length( ) > 250) {
                addError("msg", "公司地址长度不大于250个字符");
            }

            String ccreg = "^[A-Za-z0-9_]{18}$";
            if (!StringUtils.isBlank(creditCode) && !creditCode.matches(ccreg)) {
                addError("msg", "信用代码输入有误,请修改后提交");
            }

            String telreg = "^[0-9]{11}$";
            if (!StringUtils.isBlank(legalPhone) && !legalPhone.matches(telreg)) {
                addError("msg", "请输入11位手机号码!");
            }
        }
    }

    @Override
    protected void handleError(Controller c) {
        Ret ret = getRet( );
        c.renderJson(ret);
    }


/*    @Override
    protected void validate(Controller c) {

        setShortCircuit(true);
        setRet(Ret.fail( ));
        c.getFile();
        String ygbg = (c.get("wsman.ygbg"));
        String mtype = (c.get("wsman.mtype"));
        String name = (c.get("wsman.name"));
        String birthday = (c.get("wsman.birthday"));
        String hujiAddress = (c.get("wsman.hujiAddress"));
        String nowAddress = (c.get("wsman.nowAddress"));
        String phone = (c.get("wsman.phone"));
        String idType = (c.get("wsman.idType"));
        String idNumber = (c.get("wsman.idNumber"));

        String company = (c.get("wsman.company"));
        String legalPerson = (c.get("wsman.legalPerson"));
        String companyAddress = (c.get("wsman.companyAddress"));
        String creditCode = (c.get("wsman.creditCode"));
        String legalPhone = (c.get("wsman.legalPhone"));


        if (mtype.equals("个人") || mtype.equals("0") || mtype == null) {
            if (mtype.equals("0")) {
                addError("msg", "请选择原告类型");
            }
            if (StringUtils.isBlank(name)) {
                addError("msg", "请填写姓名");
            } else {
                if (name.length( ) > 10) {
                    addError("msg", "姓名长度不大于10个字符");
                }
            }

            if (StringUtils.isBlank(birthday)) {
                addError("msg", "请选择出生日期");
            }

            if (StringUtils.isBlank(hujiAddress)) {
                addError("msg", "请填写户籍地址");
            } else {
                if (hujiAddress.length( ) > 250) {
                    addError("msg", "户籍地址长度不大于250个字符");
                }
            }

            if(ygbg.equals("2")) {
                if (StringUtils.isBlank(nowAddress)) {
                    addError("msg", "请填写现在住址");
                } else {
                    if (nowAddress.length( ) > 250) {
                        addError("msg", "现住址长度不大于250个字符");
                    }
                }
            }

            if (StringUtils.isBlank(phone)) {
                addError("msg", "手机号码不能为空");
            } else {
                String telreg = "^[0-9]{11}$";
                if (!phone.matches(telreg)) {
                    addError("msg", "请输入11位手机号码!");
                }
            }

            if (idType != null) {
                if (idType.equals("身份证")) {
                    if (StringUtils.isBlank(idNumber)) {
                        addError("msg", "身份证号不能为空");
                    }
                    String idreg = "^[0-9]{15}|[0-9]{17}[A-Za-z0-9_]{1}$";
                    if (!idNumber.matches(idreg)) {
                        addError("msg", "身份证输入有误,请修改后提交!");
                    }
                }
                if (idType.equals("护照")) {
                    if (StringUtils.isBlank(idNumber)) {
                        addError("msg", "护照号不能为空");
                    }
                    String preg = "^[0-9]{9}$";
                    if (!idNumber.matches(preg)) {
                        addError("msg", "护照输入有误,请修改后提交!");
                    }
                }
            }
        } else {
            if (StringUtils.isBlank(company)) {
                addError("msg", "请输入公司名称");
            } else {
                if (company.length( ) > 30) {
                    addError("msg", "公司名称不能大于30个字符");
                }
            }

            if (StringUtils.isBlank(legalPerson)) {
                addError("msg", "请输入法人名称");
            } else {
                if (legalPerson.length( ) > 20) {
                    addError("msg", "法人姓名不能大于20个字符");
                }
            }


            if (StringUtils.isBlank(companyAddress)) {
                addError("msg", "请填写公司地址");
            } else {
                if (companyAddress.length( ) > 250) {
                    addError("msg", "公司地址长度不大于250个字符");
                }
            }


            if (StringUtils.isBlank(creditCode)) {
                addError("msg", "请填写信用代码");
            } else {
                String ccreg = "^[A-Za-z0-9_]{18}$";
                if (!creditCode.matches(ccreg)) {
                    addError("msg", "信用代码输入有误,请修改后提交");
                }
            }

            if (StringUtils.isBlank(legalPhone)) {
                addError("msg", "手机号码不能为空");
            } else {
                String telreg = "^[0-9]{11}$";
                if (!legalPhone.matches(telreg)) {
                    addError("msg", "请输入11位手机号码!");
                }
            }
        }
    }*/
}
