export default {
    inject: ['applicable'],
    data () {
        // 输入框属性
        const minmax = {}
        const { length, lengthMin, lengthMax } = this.listItem.verify
        if (length && lengthMin) minmax.min = lengthMin - 0
        if (length && lengthMax) minmax.max = lengthMax - 0
        return {
            minmax,
            checked: [...this.listItem.option.checked]
        }
    },
    mounted () {
        // 问卷添加提示
        if (this.applicable === 'Question') {
            const { length, lengthMin, lengthMax } = this.listItem.verify
            if (length && (lengthMin || lengthMax)) {
                let text = lengthMin && !lengthMax ? `至少选择 ${lengthMin} 项`
                    : !lengthMin && lengthMax ? `最多选择 ${lengthMax} 项`
                        : `可选 ${lengthMin} ~ ${lengthMax} 个项`
                this.$el.querySelector('.item-title').innerHTML += `<span class="verify">${text}</span>`
            }
        }
    },
    methods: {
        // 校验初始化
        verifyInit () {
            const rules = []
            const { required, length, lengthMin, lengthMax } = this.listItem.verify
            if (required) rules.push(this.required(this.listItem.title))
            if (length && lengthMin) rules.push(this.checkboxMin(lengthMin))
            if (length && lengthMax) rules.push(this.checkboxMax(lengthMax))
            if (rules.length) this.validator = new this.Schema({ value: rules })
        },
        // 多选框最小数量校验
        checkboxMin (min) {
            return {
                trigger: 'blur',
                validator: (rule, value, callback) => {
                    value.length && value.length < min
                        ? callback(new Error(`至少选择 ${min} 个选项`))
                        : callback()
                }
            }
        },
        // 多选框最大数量校验
        checkboxMax (max) {
            return {
                trigger: 'blur',
                validator: (rule, value, callback) => {
                    value.length && value.length > max
                        ? callback(new Error(`最多选择 ${max} 个选项`))
                        : callback()
                }
            }
        }
    }
}
