const verify = require('../../utils/deleteMark');
Component({
    externalClasses: [],
    properties: {
        form: Array,
        showAll: {
            type: Boolean,
            value: true
        }, //不显示必填项
        onConfirm: Function
    },
    options: {
        multipleSlots: true //允许使用多个slot
    },
    methods: {
        /* 改变值 */
        inputChange(e) {
            let item = e.target.dataset.item,
                index = this.data.form.findIndex(v => v.valueName === item.valueName),
                value = e.detail;
            //开始校验  //校验规则 不填:不校验  "base":默认校验 "phone":手机号  "mail":邮箱  "正则表达式"：以自定义内容为校验标准
            if (item.checking) {
                let reg = item.checking;
                switch (item.checking) {
                    case 'base':
                        value = verify.queryStr(value);
                        break;
                    case 'phone':
                        reg = /^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/;
                        this.setData({
                            [`form[${index}].errMsg`]: !reg.test(value) ? '请输入正确11位手机号码' : ''
                        });
                        break;
                    case 'mail':
                        reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
                        this.setData({
                            [`form[${index}].errMsg`]: !reg.test(value) ? '请输入正确的邮箱格式' : ''
                        });
                        break;
                    default:
                        reg = new RegExp(reg);
                        this.setData({
                            [`form[${index}].errMsg`]: !reg.test(value) ? '输入文本不符合条件!' : ''
                        });
                        break;
                };
            };
            this.setData({
                [`form[${index}].value`]: value,
                [`form[${index}].error`]: false,
            });
            if (!item.required && value == '') this.setData({
                [`form[${index}].errMsg`]: "",
            });
            this.confirm();
        },
        /* 日期,时间 选择器 */
        bindDateChange(e) {
            let item = e.target.dataset.item,
                index = this.data.form.findIndex(v => v.valueName === item.valueName),
                value = e.detail.value;
            this.setData({
                [`form[${index}].value`]: value,
                [`form[${index}].error`]: false,
            });
            this.confirm();
        },
        /* 省市县 */
        bindRegionChange(e) {
            let item = e.currentTarget.dataset.item,
                index = this.data.form.findIndex(v => v.valueName === item.valueName),
                value = e.detail.value;
            this.setData({
                [`form[${index}].value`]: value,
                [`form[${index}].error`]: false,
            });
            this.confirm();
        },
        /* 是否完成必填项 */
        confirm() {
            this.triggerEvent("onConfirm", this.data.form.some(v => v.required && v.value == ''))
        },
        /* 性别 */
        sexChange(e){
            let item = e.currentTarget.dataset.item,
            index = this.data.form.findIndex(v => v.valueName === item.valueName);
        this.setData({
            [`form[${index}].value`]:  e.detail,
            [`form[${index}].error`]: false,
        });
        this.confirm();
        },
        /* 提交 */
        submit() {
            let obj = {},
                isPass = false;
            this.data.form.forEach((v, i) => {
                obj[v.valueName] = v.value;
                if (v.errMsg != '') {
                    this.setData({
                        [`form[${i}].error`]: true
                    });
                    isPass = true;
                }
            });
            if (isPass) {
                wx.showToast({
                    title: '请检查表单内容',
                    icon: "none"
                })
            } else {
                return obj;
            }
        },
    }
})