const api = require("../api/api");
const _Http = getApp().globalData.http;

import Dialog from 'tdesign-miniprogram/dialog/index';
Page({
  data: {

  },
  // 跳转到物料添加
  addProduct() {
    wx.navigateTo({
      url: '/Eservice/agent/billCanUseProduct/index?id=' + this.data.billData.sa_workorderid
    })
  },
  toServiceBillDetail() {
    console.log("查看来源申请单")
    wx.navigateTo({
      url: '/Eservice/saler/serviceBillDetail/index?id=' + this.data.billData.sa_serviceorderid,
    })
  },
  // 添加物料
  async getAdd(selection) {
    let arr = selection.map(e => {
      return {
        "sa_workorder_node_itemsid": 0,
        "itemid": e,
        "qty": 1
      }
    })
    this.data.billData.titems = this.data.billData.titems.concat(arr)
    this.updateMaterial(this.data.billData.titems)
  },
  //修改物料
  changeItem({
    detail
  }) {
    let index = this.data.billData.titems.findIndex(v => v.sa_workorder_node_itemsid == detail.sa_workorder_node_itemsid)
    if (index != -1) this.setData({
      [`billData.titems[${index}]`]: detail
    })
  },
  /* 移除物料 */
  deleteMaterial({
    detail
  }) {
    wx.showModal({
      title: '提示',
      content: `是否确认删除“${detail.itemname}”`,
      complete: (res) => {
        if (res.confirm) api._post({
          "id": "20230215202003",
          "version": 1,
          "content": {
            "sa_workorder_node_itemsids": [detail.sa_workorder_node_itemsid]
          }
        }).then(res => {
          wx.showToast({
            title: res.msg == '成功' ? '删除成功' : res.msg,
            icon: "none",
            mask: true
          });
          this.setData({
            "billData.titems": this.data.billData.titems.filter(v => v.sa_workorder_node_itemsid != detail.sa_workorder_node_itemsid)
          })
        })
      }
    })
  },
  async updateMaterial(titems) {
    console.log(this.data.confirmBills[0])
    const res = await api._post({
      "id": "20230215201903",
      "content": {
        "sa_workorder_confirmationid": 0,
        "sa_workorderid": this.data.billData.sa_workorderid,
        "iteminfos": titems
      }
    })
    this.mainData()
  },
  /* 修改团队 */
  checkTeam(e) {
    const {
      status,
      sa_workorderid
    } = this.data.billData;
    console.log("工单状态", status)
    wx.navigateTo({
      url: `/Universal/dataTeam/index?ownertable=sa_workorder&ownerid=${sa_workorderid}`,
    })
  },
  /* 是否阻止跳转 */
  isStopToPage() {
    if (this.data.billData.status != '进行中') {
      wx.showToast({
        title: '当前工单状态不支持操作！',
        icon: "none"
      })
    }
  },
  async mainData() {
    const res = await api._post({
      "id": "20230208140103",
      "content": {
        nocache: true,
        "sa_workorderid": this.data.id
      }
    })
    console.log("详情", res)
    this.setData({
      billData: res.data
    })
  },
  changeBillStatus(data) {
    let id = data.currentTarget.dataset.id;
    if (id == 20230209144903 && this.data.billData.titems.length == 0 && this.data.billData.actiontype === "非工序模板") return wx.showToast({
      title: '还未添加产品，不可完结',
      icon: "none",
      mask: true
    })
    const dialogConfig = {
      context: this,
      title: '提示',
      content: '确认执行当前操作吗?',
      confirmBtn: '确定',
      cancelBtn: '取消',
    };

    Dialog.confirm(dialogConfig)
      .then(async () => {
        if (id == '20230209144503') {
          getApp().globalData.handelSelect = this
          wx.navigateTo({
            url: '/Eservice/workBillConfirmInfo/index',
          })
        } else {
          const res = await api._post({
            "id": id,
            "content": {
              "sa_workorderid": this.data.id
            }
          })
          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.actiontype === "非工序模板") {
      wx.navigateTo({
        url: '/Eservice/materielConfirmBill/index?id=' + res.data.sa_workorder_confirmationid,
      })
    } else 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 {
      wx.navigateTo({
        url: '/Eservice/serviceConfirmBill/index?id=' + res.data.sa_workorder_confirmationid,
      })
    }

  },
  toConfirmBill(data) {
    if (this.data.billData.actiontype === "非工序模板") {
      wx.navigateTo({
        url: '/Eservice/materielConfirmBill/index?id=' + data.currentTarget.dataset.item.sa_workorder_confirmationid + '&class=' + this.data.stopClick,
      })
    } else if (this.data.billData.type === '安装培训') {
      wx.navigateTo({
        url: '/Eservice/trainConfirmBill/index?id=' + data.currentTarget.dataset.item.sa_workorder_confirmationid + '&class=' + this.data.stopClick,
      })
    } else if (this.data.billData.type === '安装调试') {
      wx.navigateTo({
        url: '/Eservice/installConfirmBill/index?id=' + data.currentTarget.dataset.item.sa_workorder_confirmationid + '&class=' + this.data.stopClick,
      })
    } else {
      wx.navigateTo({
        url: '/Eservice/serviceConfirmBill/index?id=' + data.currentTarget.dataset.item.sa_workorder_confirmationid + '&class=' + this.data.stopClick,
      })
    }
  },
  async confirmBillList() {
    const res = await api._post({
      "id": "20230211105903",
      "content": {
        "where": {
          "condition": "",
          "sa_workorderid": this.data.id
        }
      }
    })
    this.setData({
      confirmBills: res.data
    })
  },

  toWorkConfirmInfo() {
    getApp().globalData.handelSelect = this
    wx.navigateTo({
      url: '/Eservice/workBillConfirmInfo/index?class=' + this.data.stopClick,
    })
  },
  onLoad(options) {
    this.setData({
      id: options.id,
      stopClick: options.class
    })

  },
  onShow() {
    this.mainData()
    this.confirmBillList()
  },
})