const _Http = getApp().globalData.http,
    getTime = require("../../utils/getTime");

Page({
    data: {
        isReset: true,
        showAll: false,
        form: [{
            label: "任务标题",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "",
            valueName: "title",
            checking: "base",
            required: true
        }, {
            label: "任务要求",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "",
            valueName: "remarks",
            checking: "base",
            required: false
        }, {
            label: "执行人",
            error: false,
            errMsg: "",
            type: "route",
            url: "/packageA/select/contacts/select",
            value: "",
            params: {
                "id": "20221018122201",
                "content": {
                    "ownertable": "sys_task",
                    "ownerid": 0,
                    "where": {
                        "condition": "",
                        "withoutselect": 1
                    }
                }
            },
            query: "&radio=true&idname=userid",
            interrupt: true,
            placeholder: "选择任务执行人",
            valueName: "principal",
            checking: "base",
            required: true
        }, {
            label: "协助人",
            error: false,
            errMsg: "",
            type: "route",
            url: "/packageA/select/contacts/select",
            value: "",
            params: {
                "id": "20221018122201",
                "content": {
                    "ownertable": "sys_task",
                    "ownerid": 0,
                    "where": {
                        "condition": "",
                        "withoutselect": 1
                    }
                }
            },
            query: "&idname=userid",
            interrupt: true,
            placeholder: "选择任务协助人",
            valueName: "member",
            checking: "base",
            required: false
        }, {
            label: "开始日期",
            error: false,
            errMsg: "",
            type: "date",
            start: getTime.formatTime(new Date(), '-').split(' ')[0],
            value: "",
            placeholder: "开始日期",
            interrupt: true,
            valueName: "starttime",
            checking: "base",
            required: true
        }],
        "content": {
            "sys_taskid": 0
        },
        disabled: true
    },
    onLoad(options) {
        if (options.data) {
            let data = JSON.parse(options.data);
            console.log(data)
            if (options.group) {
                let group = JSON.parse(options.group)
                let leader = group.findIndex(v => v.userid == data.leader[0].userid);
                if (leader != -1) {
                    data.principal = [group[leader].name, [group[leader].userid]]
                    group.splice(leader, 1)
                    data.member = [group.map(s => s.name), group.map(s => s.userid - 0)]
                }
            }
            data.starttime = data.starttime.split(" ")[0]
            data.endtime = data.endtime ? data.endtime.split(" ")[0] : ""
            let form = this.data.form.concat([{
                label: "结束日期",
                error: false,
                errMsg: "",
                type: "date",
                start: data.value,
                value: "",
                interrupt: true,
                placeholder: "结束日期",
                valueName: "endtime",
                checking: "base",
                required: false
            }, {
                label: "提醒时间",
                error: false,
                errMsg: "",
                type: "namber",
                value: "",
                placeholder: "结束时间前几天提醒",
                valueName: "remindday",
                checking: "base",
                required: false
            }]);
            this.setData({
                "content.sys_taskid": data.sys_taskid,
                "content.ownertable": data.ownertable || "sys_task",
                "content.ownerid": data.ownerid || 0,
                isReset: false,
                disabled: false,
                form: form.map(v => {
                    if (data[v.valueName]) v.value = data[v.valueName];
                    return v
                })
            })
        }
        if (options.ownertable) this.setData({
            "content.ownertable": options.ownertable || "sys_task",
            "content.ownerid": options.ownerid || 0
        })
    },
    /* 打断处理form */
    interrupt(e) {
        let {
            data,
            form,
            temporary
        } = e.detail;
        console.log(data, form, temporary)
        let obj = null,
            index = null;
        switch (temporary.item.label) {
            case '开始日期':
                obj = {
                    label: "结束日期",
                    error: false,
                    errMsg: "",
                    type: "date",
                    start: data.value,
                    value: "",
                    interrupt: true,
                    placeholder: "结束日期",
                    valueName: "endtime",
                    checking: "base",
                    required: false
                };
                let index = form.findIndex(v => v.label == '结束日期');
                index == -1 ? form.push(obj) : form[index] = obj;
                this.setData({
                    isReset: false
                })
                break;
            case '结束日期':
                index = form.findIndex(v => v.label == '提醒时间');
                if (index == -1) form.push({
                    label: "提醒时间",
                    error: false,
                    errMsg: "",
                    type: "namber",
                    value: "",
                    placeholder: "结束时间前几天提醒",
                    valueName: "remindday",
                    checking: "base",
                    required: false
                });
                break;
            case '执行人':
                index = form.findIndex(v => v.label == '协助人');
                form[index].params.excludeid = data.value[1][0];
                if (index != -1 && form[index].value[1]) {
                    let i = form[index].value[1].findIndex(v => v == data.value[1][0]);
                    if (i != -1) {
                        form[index].value[0].splice(i, 1);
                        form[index].value[1].splice(i, 1);
                        if (form[index].value[0].length == 0) form[index].value = '';
                    }
                }
                temporary.item.value = data.value;
                form[temporary.index] = temporary.item;
                wx.navigateBack()
                this.selectComponent("#Form").confirm();
                break;
            case '协助人':
                index = form.findIndex(v => v.label == '执行人');
                if (index != -1 && form[index].value[1]) {
                    if (data.value[1].findIndex(v => v == form[index].value[1][0]) != -1) form[index].value = ''
                }
                temporary.item.value = data.value;
                form[temporary.index] = temporary.item;
                wx.navigateBack()
                break;
        };
        this.setData({
            form
        })
    },
    submit() {
        let content = {
            ...this.data.content,
            ...this.selectComponent("#Form").submit()
        };
        content.leaderuserid = content.principal[1][0];
        let userids = content.member.length ? content.member[1] : "";
        delete(content.principal)
        delete(content.member)
        _Http.basic({
            "id": 20221211112101,
            content
        }).then(res => {
            console.log("创建任务成功", res)
            wx.showToast({
                title: res.msg == '成功' ? "保存成功" : res.msg,
                icon: "none"
            });
            _Http.basic({
                "id": 20220930103601,
                "content": {
                    "ownertable": "sys_task",
                    "ownerid": res.data.sys_taskid,
                    "userids": userids,
                    "justuserids": 1
                }
            }, false).then(e => {
                console.log("设置团队", e)
            })
            if (res.msg == '成功') setTimeout(() => {
                getCurrentPages().forEach(v => {
                    if (v.__route__ == 'packageA/work/index') {
                        let index = v.data.list.findIndex(v => v.sys_taskid == res.data.sys_taskid);
                        if (index == -1) {
                            v.data.list.unshift(res.data)
                            v.setData({
                                list: v.data.list,
                                "content.total": v.data.content.total + 1
                            })
                        } else {
                            v.setData({
                                [`list[${index}]`]: res.data
                            })
                        }
                    }
                });
                let pages = getCurrentPages(),
                    page = pages[pages.length - 2];
                if (page.__route__ == 'packageA/work/index') {
                    wx.redirectTo({
                        url: '/packageA/work/detail?id=' + res.data.sys_taskid,
                        fail(err) {
                            console.log(err)
                        }
                    })
                } else if (page.__route__ == 'packageA/work/detail') {
                    wx.navigateBack();
                    page.getDetail();
                } else {
                    let p = page.selectComponent("#Work");
                    if (p) p.getList(0, true);
                    wx.navigateBack();
                }
            }, 500);
        })
    },
    reset() {
        let form = this.data.form;
        form[4].value = "";
        form.splice(5, 2);
        this.setData({
            isReset: true,
            disabled: true,
            form
        });
        wx.showToast({
            title: '重置成功',
            icon: "none"
        })
    },
    // 是否显示全部
    onChange({
        detail
    }) {
        this.setData({
            showAll: detail
        })
    },
    /* 表单必填项是否完成 */
    onConfirm({
        detail
    }) {
        this.setData({
            disabled: detail
        })
    }
})