const _Http = getApp().globalData.http;
Page({
    data: {
        tabsActive: 0, //tabs 选中项
        sys_taskid: "",
        detail: '',
        tabsList: [{
            label: "详细信息",
            icon: "icon-tabxiangxixinxi1"
        }, {
            label: "跟进动态",
            icon: "icon-tabgenjinjilu"
        }, {
            label: "操作",
            icon: "icon-tabcaozuojilu1"
        }, {
            label: "附件",
            icon: "icon-tabfujian1"
        }],
        tabbarList: [],
        confirmShow: false,
        finishnotes: "", //完成说明
    },
    /* 完成情况说明输入 */
    areaInput(e) {
        this.setData({
            finishnotes: e.detail.value
        })
    },
    onConfirm(e) {
        console.log(this.data.finishnotes)
        _Http.basic({
            "id": 20221211112501,
            "content": {
                "sys_taskid": this.data.sys_taskid,
                "finishnotes": this.data.finishnotes
            }
        }).then(res => {
            console.log("完成情况说明", res)
            wx.showToast({
                title: res.msg == '成功' ? "提交成功" : res.msg,
                icon: "none"
            })
            if (res.msg == '成功') setTimeout(this.getDetail, 500)
        })
    },
    /* 取消提交说明 */
    onCancel(e) {
        this.setData({
            confirmShow: false
        })
    },
    onLoad(options) {
        this.setData({
            sys_taskid: options.id
        });
        this.getDetail();
    },
    getDetail() {
        _Http.basic({
            "id": 20221211111901,
            "content": {
                "nocache": true,
                "sys_taskid": this.data.sys_taskid
            },
        }).then(res => {
            console.log("任务详情", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            });

            let isLeader = res.data.leader.some(v => v.userid == wx.getStorageSync('userMsg').userid),
                isCreateby = res.data.createuserid == wx.getStorageSync('userMsg').userid,
                tabbarList = [{
                    icon: "icon-genjin",
                    label: "跟进"
                }, {
                    icon: "icon-dibu-chengjiao",
                    label: "完成"
                }];
            if (isCreateby) tabbarList.splice(1, 0, {
                icon: "icon-guanlian-bianji",
                label: "编辑"
            })

            this.setData({
                tabbarList,
                detail: res.data,
                briefs: [{
                        label: "任务要求",
                        value: res.data.remarks
                    }, {
                        label: "开始时间",
                        value: res.data.starttime
                    }, {
                        label: "截止时间",
                        value: res.data.endtime
                    },
                    {
                        label: '关联页数数据',
                        value: res.data.allocationstatus
                    },
                    {
                        label: '状态',
                        value: res.data.status
                    }
                ],
                list1: [{
                        label: "任务要求",
                        value: res.data.remarks
                    },
                    {
                        label: "开始时间",
                        value: res.data.starttime
                    },
                    {
                        label: "截止时间",
                        value: res.data.endtime
                    },
                    {
                        label: '关联页数数据',
                        value: res.data.allocationstatus
                    },
                    {
                        label: '状态',
                        value: res.data.status
                    }
                ],
                list2: [{
                    label: "创建人",
                    value: res.data.createby
                }, {
                    label: "创建时间",
                    value: res.data.createdate
                }, {
                    label: "最近编辑人",
                    value: res.data.changeby
                }, {
                    label: "最近编辑时间",
                    value: res.data.changedate
                }, {
                    label: "转手次数",
                    value: res.data.leader[0].leadernum
                }]
            });

            /* 更新列表中状态 */
            let page = getCurrentPages().find(v => v.__route__ == 'packageA/saleClue/index');
            if (page) {
                let index = page.data.list.findIndex(v => v.sys_taskid == res.data.sys_taskid);
                if (index != -1) page.setData({
                    [`list[${index}].status`]: res.data.status,
                    [`list[${index}].title`]: res.data.title,
                    [`list[${index}].remarks`]: res.data.remarks

                })
            } else {
                let pages = getCurrentPages(),
                    p = pages[pages.length - 2].selectComponent("#Work");
                if (p) {
                    let index = p.data.list.findIndex(v => v.sys_taskid == res.data.sys_taskid);
                    if (index != -1) p.setData({
                        [`list[${index}].status`]: res.data.status,
                        [`list[${index}].title`]: res.data.title,
                        [`list[${index}].remarks`]: res.data.remarks
                    })
                }
            }
            this.getTags();
            this.getGroup();
            this.partialRenewal();
        })
    },
    //tabs 切换
    tabsChange({
        detail
    }) {
        this.setData({
            tabsActive: detail
        });
        this.partialRenewal();
    },
    //更新标签
    getTags() {
        this.selectComponent("#Tags").getTags();
    },
    //更新团队成员
    getGroup() {
        this.selectComponent("#Group").getList();
    },
    onClose() {
        this.setData({
            show: false
        });
    },
    //局部数据更新 tabs
    partialRenewal(init = false) {
        let id = this.data.detail.sys_taskid;
        let model = '';
        let name = this.data.tabsList[this.data.tabsActive].label;
        switch (name) {
            case "操作":
                model = "#Record"
                break;
            case "跟进动态":
                model = "#Trace"
                break;
            case "附件":
                model = "#Files"
                break;
        };
        if (model) {
            // 确定好模块ID total = null 是第一次加载 加载数据
            // init是新增或修改需要清数据
            let Component = this.selectComponent(model);
            const {
                total,
                pageNumber,
                pageTotal
            } = Component.data.content;
            if (total == null || init) {
                Component.getList(id, init);
            } else if (pageNumber <= pageTotal) {
                Component.getList(id, false);
            } else {
                //用来判断 在搜索页面修改，与tabs选项不一致 但是切换到该选项 重置数据
            }
        }
    },
    onReachBottom() {
        this.partialRenewal();
    },
    //详情按钮回调
    tabbarOnClick({
        detail
    }) {
        switch (detail.label) {
            case "编辑":
                let group = this.selectComponent("#Group").query();
                wx.navigateTo({
                    url: `/packageA/work/add?data=${JSON.stringify(this.data.detail)}&group=${JSON.stringify(group)}`,
                })


                break;
            case "完成":
                this.setData({
                    confirmShow: true
                })
                break;
            case "跟进":
                wx.navigateTo({
                    url: `/packageA/setclient/modules/trace/add/index?ownertable=sys_task&ownerid=${this.data.sys_taskid}`,
                })
                break;
        }
    },
    onShareAppMessage() {}
})