const api = require("../../api/api");
import Dialog from 'tdesign-miniprogram/dialog/index';

// pages/workOrderDetail/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        workLeaderDialog: false,
        showTeams: false,
        teams: []
    },
    toServiceBillDetail() {
        console.log("查看来源申请单", this.data.billData)
        wx.navigateTo({
            url: '/Eservice/saler/serviceBillDetail/index?id=' + this.data.billData.sa_serviceorderid,
        })
    },
    async mainData() {
        const res = await api._post({
            "id": "20230208140103",
            "content": {
                nocache: true,
                "sa_workorderid": this.data.id
            }
        })
        console.log("详情", res)
        if (this.data.billData) {
            let userid = wx.getStorageSync('userMsg').userid;
            let item = this.data.billData;
            if (!item.team.some(v => v.userid == userid) && res.data.team.some(v => v.userid == userid)) wx.redirectTo({
                url: '/Eservice/workOrderDetail/index' + '?id=' + item.sa_workorderid,
            })
        }
        this.setData({
            billData: res.data
        })
        this.workerData()
    },
    changeBillStatus(e) {
        const {
            id,
            name
        } = e.target.dataset;
        if (!name) return;
        const dialogConfig = {
            context: this,
            title: '提示',
            content: `是否确定${name}当前工单?`,
            confirmBtn: name,
            cancelBtn: '取消',
        }
        Dialog.confirm(dialogConfig)
            .then(async () => {
                const res = await api._post({
                    "id": id,
                    "content": {
                        "sa_workorderid": this.data.id
                    }
                })
                wx.showToast({
                    title: res.msg == '成功' ? `工单${name}成功` : res.msg,
                    icon: "none",
                    mask: true
                });
                if (name == '作废') {
                    setTimeout(() => {
                        wx.navigateBack()
                    }, 500);
                } else {
                    this.mainData()
                }
            })
            .catch(() => console.log('点击了取消'))
            .finally(() => Dialog.close())
    },
    async addConfirmBill() {
        const res = await api._post({
            "id": "20230211105703",
            "version": 1,
            "content": {
                "sa_workorder_confirmationid": 0,
                "sa_workorderid": this.data.id,
                "attitudescore": 0,
                "responsescore": 0,
            }
        })
        if (this.data.billData.type === '安装培训') {
            wx.navigateTo({
                url: '/Eservice/trainConfirmBill/index?id=' + res.data.sa_workorder_confirmationid,
            })
        } else if (this.data.billData.type === '安装调试') {
            wx.navigateTo({
                url: '/Eservice/installConfirmBill/index?id=' + res.data.sa_workorder_confirmationid,
            })
        } else {}

    },
    toConfirmBill(data) {
        if (this.data.billData.type === '安装培训') {
            wx.navigateTo({
                url: '/Eservice/trainConfirmBill/index?class=stopClick&id=' + data.currentTarget.dataset.item.sa_workorder_confirmationid,
            })
        }
    },
    async confirmBillList() {
        const res = await api._post({
            "id": "20230211105903",
            "content": {
                "where": {
                    nocahce: true,
                    "condition": "",
                    "sa_workorderid": this.data.id
                }
            }
        })
        this.setData({
            confirmBills: res.data
        })
    },
    toWorkConfirmInfo() {
        getApp().globalData.handelSelect = this
        wx.navigateTo({
            url: '/Eservice/workBillConfirmInfo/index?class=stopClick'
        })
    },
    async workerData() {
        const res = await api._post({
            "id": "20230213143003",
            "version": 1,
            "content": {
                "where": {
                    "condition": ""
                }
            }
        })
        this.setData({
            workers: res.data
        })
    },
    /**
     * 开始添加成员或者取消添加
     */
    showTeamDialog() {
        this.setData({
            showTeams: !this.data.showTeams,
            teams: this.data.billData.team.map(v => {
                return v.isleader == 1 ? 0 : v.userid
            })
        })
    },
    /**
     * 选择添加成员
     */
    selectTeams(e) {
        const {
            userid
        } = e.currentTarget.dataset.item;
        let teams = this.data.teams;
        let i = teams.findIndex(v => v == userid);
        if (i != -1) {
            teams = teams.filter(v => v != userid)
        } else {
            teams.push(userid)
        }
        this.setData({
            teams
        })
    },
    addUser() {
        api._post({
            "id": 20220930103603,
            "content": {
                ownertable: 'sa_workorder',
                ownerid: this.data.billData.sa_workorderid,
                "userids": this.data.teams,
                "justuserids": 1
            }
        }).then(res => {
            console.log("添加成员", res)
            wx.showToast({
                title: res.msg == '成功' ? '修改成功' : res.msg,
                icon: "none",
                mask: true
            });
            if (res.msg == '成功') {
                this.mainData()
                this.showTeamDialog()
            }
        })

    },
    showWorkerDialog() {
        this.setData({
            workLeaderDialog: !this.data.workLeaderDialog
        })
    },
    selectLeader(data) {
        this.setData({
            actLeader: data.currentTarget.dataset.item
        })
    },
    async toWorker() {
        if (!this.data.actLeader) {
            wx.showToast({
                title: '未选择负责人',
                icon: 'none'
            })
        } else {
            /* this.data.billData.projectlearders = [this.data.actLeader.userid]
            const res = await api._post({
              "id": "20230208140003",
              "content": this.data.billData
            }) */
            let teams = this.data.billData.team;
            console.log(this.data.billData.team.filter(v => v.isleader != 1))
            console.log(this.data.actLeader)
            if (!teams.some(v => v.userid == this.data.actLeader.userid)) {

            }

            const res = await api._post({
                "id": "20220930103703",
                "content": {
                    ownertable: 'sa_workorder',
                    ownerid: this.data.billData.sa_workorderid,
                    "userid": this.data.actLeader.userid
                }
            })
            await api._post({
                "id": 20220930103603,
                "content": {
                    ownertable: 'sa_workorder',
                    ownerid: this.data.billData.sa_workorderid,
                    "userids": [this.data.actLeader.userid].concat(this.data.billData.team.filter(v => v.isleader != 1).map(v => v.userid)),
                    "justuserids": 1
                }
            }).then(res => {
                console.log("添加成员", res)
            })

            this.setData({
                workLeaderDialog: false
            })
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            });
            this.mainData()
            // this.confirmBillList()
        }
    },
    onLoad(options) {
        this.setData({
            id: options.id
        })
    },
    onShow() {
        this.mainData()
        this.confirmBillList()
    },
})