const api = require("../api/api")

Page({

    /**
     * 页面的初始数据
     */
    data: {
        timerid: null,
        bindSignNameData: {
            "ownertable": "sa_workorder_confirmation",
            "ownerid": 0,
            "usetype": "signature",
        }
    },

    async billData() {
        const res = await api._post({
            "id": "20230211105803",
            "content": {
                "sa_workorder_confirmationid": this.data.id
            }
        })
        console.log('详情', res)
        this.setData({
            mainData: res.data,
            sa_serviceorderid: res.data.workorder.sa_serviceorderid || '0',
            sa_orderid: res.data.workorder.sa_orderid || '0',
            stopClick: res.data.status !== '新建' ? 'stopClick' : this.data.stopClick
        })
        this.setInputData()
    },
    setInputData() {
        this.data.mainData.confirmationcontent.forEach(e => {
            Object.keys(e).map((key, index) => {
                if (key.includes('现场培训内容')) {
                    this.setData({
                        trainerContent: e[key].confirm_value
                    })
                } else if (key.includes('现场互动及测试培训效果')) {

                    this.setData({
                        trainerResult: e[key].confirm_value
                    })
                } else {

                }
            })
        })
    },
    async updateBill() {
        if (this.data.mainData.status !== '新建') return wx.showToast({
            title: '非新建状态,修改的数据将不会保存',
            icon: 'none'
        })
        const res = await api._post({
            "id": "20230211105703",
            "content": this.data.mainData
        })
    },
    // 跳转到物料添加
    addProduct() {
        /*  wx.navigateTo({
           url: '/Eservice/serviceMaterial/index',
         }) */
        wx.navigateTo({
            url: '/Eservice/agent/billCanUseProduct/index?id=' + this.data.mainData.sa_workorderid
        })
    },
    // 跳转到添加人员
    addTrainers() {
        wx.navigateTo({
            url: '/Eservice/addTrainers/index',
        })
    },
    // 添加人员
    async getAddTrainers(data) {
        data.sa_workorder_node_teamid = 0
        this.data.mainData.trainers.push(data)
        const res = await api._post({
            "id": "20230215202103",
            "content": {
                "sa_workorder_confirmationid": this.data.mainData.sa_workorder_confirmationid,
                "sa_workorderid": this.data.mainData.workorder.sa_workorderid,
                "trainers": this.data.mainData.trainers
            }
        })
        this.billData()
    },
    // 删除人员
    async deleteMenber(data) {
        let id = data.detail.sa_workorder_node_teamid
        const res = await api._post({
            "id": "20230215202203",
            "version": 1,
            "content": {
                "sa_workorder_node_teamids": [id]
            }
        })
        this.billData()
    },
    // 添加物料
    async getAdd(selection) {
        console.log(selection)
        let arr = selection.map(e => {
            return {
                "sa_workorder_node_itemsid": 0,
                "itemid": e,
                "qty": 1
            }
        })
        this.data.mainData.trainertitems = this.data.mainData.trainertitems.concat(arr)
        this.updateMaterial(this.data.mainData.trainertitems)
    },
    async updateMaterial(trainertitems) {
        const res = await api._post({
            "id": "20230215201903",
            "content": {
                "sa_workorder_confirmationid": this.data.mainData.sa_workorder_confirmationid,
                "sa_workorderid": this.data.mainData.workorder.sa_workorderid,
                "iteminfos": trainertitems
            }
        })
        this.billData()
    },
    // 修改物料数量
    qtyChange(data) {
        clearTimeout(this.data.timerid)
        this.data.timerid = setTimeout(() => {
            this.data.mainData.trainertitems.forEach(e => {
                if (e.itemid == data.detail.itemid) {
                    e.qty = data.detail.qty
                }
            })
            this.updateMaterial(this.data.mainData.trainertitems)
        }, 1000)
        this.setData({
            timerid: this.data.timerid
        })

    },
    // 删除物料
    async deleteMaterial(data) {
        let itemid = data.detail.sa_workorder_node_itemsid
        const res = await api._post({
            "id": "20230215202003",
            "content": {
                "sa_workorder_node_itemsids": [itemid]
            }
        })
        this.billData()
    },
    // 改变评分
    onChange(e) {
        this.data.mainData[e.currentTarget.dataset.name] = e.detail.value
        this.setData({
            mainData: this.data.mainData
        });
        this.updateBill()
    },

    async submit() {
        this.createImage(true);
        const res = await api._post({
            "id": "20230211110003",
            "content": {
                "sa_workorder_confirmationid": this.data.mainData.sa_workorder_confirmationid
            }
        })
        if (res.code == 1) {
            wx.showToast({
                title: '提交成功',
                icon: "success"
            })
            this.billData()
        }

    },
    async delete() {
        const res = await api._post({
            "id": "20230211110103",
            "content": {
                "sa_workorder_confirmationids": [this.data.mainData.sa_workorder_confirmationid]
            }
        })
        if (res.code == 1) {
            wx.navigateBack()
        }
    },
    inputChange(data) {
        let keyValue = data.currentTarget.dataset.name
        this.data.mainData.confirmationcontent.forEach(e => {
            Object.keys(e).map((key, index) => {
                if (key.includes(keyValue)) {
                    console.log(e)
                    e[key].confirm_value = data.detail.value
                }
            })
        })
        this.updateBill()
    },
    /* 查看单据 */
    checkImage() {
        let attinfos = this.data.mainData.attinfos.map(v => v.usetype);
        let index = attinfos.lastIndexOf("comfirmbill");
        //没有查询到有效单据 跳转生成
        if (index == -1) return this.createImage(true);
        wx.previewImage({
            urls: [this.data.mainData.attinfos[index].url],
        })
    },
    createImage(active = false) {
        let token = wx.getStorageSync('userMsg').token
        let parentid = wx.getStorageSync('siteP').appfolderid
        let url = `${getApp().globalData.http.baseUrl}/yosweb/#/printTable?token=${token}&parentid=${parentid}&ownerid=${this.data.mainData.sa_workorder_confirmationid}${active==true?'&action=1':''}`
        this.setData({
            url: url
        })
        wx.navigateTo({
            url: '/Eservice/webView/index',
        })
    },
    onLoad(options) {
        this.data.bindSignNameData.ownerid = options.id
        this.setData({
            stopClick: options.class,
            id: options.id,
            bindSignNameData: this.data.bindSignNameData
        })
    },
    onShow() {
        this.setData({
            SignName: true
        })
        this.billData()
    },
    onHide() {
        this.setData({
            SignName: false
        })
    },
})