package com.jfinal.club.ws;

import com.jfinal.aop.Inject;
import com.jfinal.club.common.model.Docws;
import com.jfinal.club.common.model.Wsman;
import com.jfinal.club.util.Cons;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.validate.Validator;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * @ProjectName: jfinal-club
 * @Package: com.jfinal.club.ws
 * @ClassName: DocWs2Validator
 * @Author: Jzdsh
 * @Description: 文书页面2验证
 * @Date: 2020/4/1 11:10
 * @Version: 1.0
 */
public class DocWs3Validator extends Validator {


    @Override
    protected void validate(Controller c) {
        setShortCircuit(true);
        setRet(Ret.fail( ));
        HttpServletRequest request = c.getRequest( );

        List< String > id = Arrays.asList(request.getParameterValues("wsman.id"));
        List< String > mtype = Arrays.asList(request.getParameterValues("wsman.mtype"));
        List< String > name = Arrays.asList(request.getParameterValues("wsman.name"));
        List< String > birthday = Arrays.asList(request.getParameterValues("wsman.birthday"));
        List< String > hujiAddress = Arrays.asList(request.getParameterValues("wsman.hujiAddress"));
        List< String > nowAddress = Arrays.asList(request.getParameterValues("wsman.nowAddress"));
        List< String > phone = Arrays.asList(request.getParameterValues("wsman.phone"));
        List< String > idType = Arrays.asList(request.getParameterValues("wsman.idType"));
        List< String > idNumber = Arrays.asList(request.getParameterValues("wsman.idNumber"));

        List< String > company = Arrays.asList(request.getParameterValues("wsman.company"));
        List< String > legalPerson = Arrays.asList(request.getParameterValues("wsman.legalPerson"));
        List< String > companyAddress = Arrays.asList(request.getParameterValues("wsman.companyAddress"));
        List< String > creditCode = Arrays.asList(request.getParameterValues("wsman.creditCode"));
        List< String > legalPhone = Arrays.asList(request.getParameterValues("wsman.legalPhone"));

        if (id != null && id.size( ) > 0) {
            for (int i = 0; i < id.size( ); i++) {

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

                    if (!StringUtils.isBlank(hujiAddress.get(i)) && hujiAddress.get(i).length( ) > 250) {
                        addError("msg", "第" + (i + 1) + "被告人,户籍地址长度不大于250个字符");
                    }


                    if (!StringUtils.isBlank(nowAddress.get(i)) && nowAddress.get(i).length( ) > 250) {
                        addError("msg", "第" + (i + 1) + "被告人,现住址长度不大于250个字符");
                    }

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

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

                    if (!StringUtils.isBlank(legalPerson.get(i)) && legalPerson.get(i).length( ) > 20) {
                        addError("msg", "第" + (i + 1) + "位被告人,法人姓名不能大于20个字符");
                    }


                    if (!StringUtils.isBlank(companyAddress.get(i)) && companyAddress.get(i).length( ) > 250) {
                        addError("msg", "第" + (i + 1) + "位被告人,公司地址长度不大于250个字符");
                    }

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

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

            }
        }

    }

    @Override
    protected void handleError(Controller c) {
        c.keepModel(Wsman.class);
        c.renderJson(getRet( ));
    }

/*    @Override
    protected void validate(Controller c) {
        setShortCircuit(true);
        setRet(Ret.fail( ));
        HttpServletRequest request = c.getRequest( );

        List< String > id = Arrays.asList(request.getParameterValues("wsman.id"));
        List< String > mtype = Arrays.asList(request.getParameterValues("wsman.mtype"));
        List< String > name = Arrays.asList(request.getParameterValues("wsman.name"));
        List< String > birthday = Arrays.asList(request.getParameterValues("wsman.birthday"));
        List< String > hujiAddress = Arrays.asList(request.getParameterValues("wsman.hujiAddress"));
        List< String > nowAddress = Arrays.asList(request.getParameterValues("wsman.nowAddress"));
        List< String > phone = Arrays.asList(request.getParameterValues("wsman.phone"));
        List< String > idType = Arrays.asList(request.getParameterValues("wsman.idType"));
        List< String > idNumber = Arrays.asList(request.getParameterValues("wsman.idNumber"));

        List< String > company = Arrays.asList(request.getParameterValues("wsman.company"));
        List< String > legalPerson = Arrays.asList(request.getParameterValues("wsman.legalPerson"));
        List< String > companyAddress = Arrays.asList(request.getParameterValues("wsman.companyAddress"));
        List< String > creditCode = Arrays.asList(request.getParameterValues("wsman.creditCode"));
        List< String > legalPhone = Arrays.asList(request.getParameterValues("wsman.legalPhone"));

        if (id != null && id.size( ) > 0) {
            for (int i = 0; i < id.size( ); i++) {

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

                    if (StringUtils.isBlank(birthday.get(i))) {
                        addError("msg", "第" + (i + 1) + "被告人,请选择出生日期");
                    }

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


                    if (StringUtils.isBlank(nowAddress.get(i))) {
                        addError("msg", "第" + (i + 1) + "被告人,请填写现在住址");
                    } else {
                        if (nowAddress.get(i).length( ) > 250) {
                            addError("msg", "第" + (i + 1) + "被告人,现住址长度不大于250个字符");
                        }
                    }

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

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

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


                    if (StringUtils.isBlank(companyAddress.get(i))) {
                        addError("msg", "第" + (i + 1) + "位被告人,请填写公司地址");
                    } else {
                        if (companyAddress.get(i).length( ) > 250) {
                            addError("msg", "第" + (i + 1) + "位被告人,公司地址长度不大于250个字符");
                        }
                    }


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

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

            }
        }

    }*/
}
