const getTime = require("../../utils/getTime"),
    deleteMark = require("../../utils/Check");

Component({
    /**
     * 组件的属性列表
     */
    properties: {
        fromList: {
            type: Array,
            value: []
        },
        CompletedOrNot: {
            type: Function
        }, //完成与否回调
    },
    options: {
        multipleSlots: true
    },

    /**
     * 组件的初始数据
     */
    data: {
        endTime: "", //结束时间
        selectTime: "", //选择时间
        sexActions: [{
            name: '男'
        }, {
            name: '女'
        }],
        seleteSexShow: false
    },

    lifetimes: {
        attached: function () {
            this.setData({
                endTime: getTime.formatTime(new Date(), '-').split(' ')[0]
            })
        }
    },

    /**
     * 组件的方法列表
     */
    methods: {
        /* 性别选择器 */
        seleteSex(e) {
            this.setData({
                seleteSexShow: true
            })
        },
        handleSex(e) {
            let index = this.data.fromList.findIndex(v => v.type == 'sex');
            if (index) this.setData({
                ['fromList[' + index + '].value']: e.detail.name,
                seleteSexShow: false
            })
            console.log(this.data.fromList[index])
        },
        cancel() {
            this.setData({
                seleteSexShow: false
            })
        },
        /* 输入 */
        inputChange(e) {
            let {
                index,
                item
            } = e.currentTarget.dataset;
            item.value = deleteMark.queryStr(e.detail);
            item.error = item.required && item.value == '' ? true : false;
            let fromList = this.data.fromList;
            fromList[index] = item;
            if (item.callback) item.callback(item);
            this.setData({
                fromList
            })
            this.statistics();
        },
        /* 日期选择器 */
        bindDateChange(e) {
            const {
                index
            } = e.currentTarget.dataset;
            let fromList = this.data.fromList;
            fromList[index].value = e.detail.value;
            fromList[index].error = false;
            this.setData({
                fromList
            })
            this.statistics();
        },
        /* 清空输入框 */
        inputClear(e) {
            console.log("清空", e.target.dataset.item.label)
            this.statistics();
        },
        /* 统计是否完成全部必填项 */
        statistics() {
            let list = this.data.fromList,
                sumCount = 0,
                count = 0;
            for (let i = 0; i < list.length; i++) {
                if (list[i].required) {
                    sumCount++;
                    if (list[i].value != "") count++;
                }
            }
            // console.log("必填总数", sumCount, '已填', count)
            this.triggerEvent("CompletedOrNot", sumCount == count)
        },
        /* 提交 */
        getData() {
            let list = this.data.fromList,
                returnData = {},
                isReturn = true;
            for (let i = 0; i < list.length; i++) {
                if (list[i].required && list[i].value == "") {
                    list[i].error = true;
                    isReturn = false;
                } else {
                    returnData[list[i].valueName] = list[i].value;
                }
            }
            this.setData({
                fromList: list
            })
            return {
                returnData,
                isReturn
            }
        }
    }
})