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

Page({

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

  async billData () {
    const res = await api._post({
      "id": "20230211105803",
      "content": {
        "sa_workorder_confirmationid":this.data.id
      }
    })
    this.setData({
      mainData:res.data,
      sa_serviceorderid:res.data.workorder.sa_serviceorderid,
      sa_orderid:res.data.workorder.sa_orderid,
      stopClick:res.data.status !== '新建'?'stopClick':this.data.stopClick
    })
    this.setInputData()
  },
  setInputData () {
    let that = this
    that.data.mainData.confirmationcontent.forEach(e=>{
      
      Object.keys(e).map((key,index)=>{
        that.setData({
          paidServiceAmount:that.data.paidServiceAmount + e[key].amount
        })
        if (key.includes('是否需要拆卸配件或整拆')) {
          that.setData({
            node1:e[key].confirm_value
          })
        }
        if(key.includes('与现场协商确认维修方案')) {
          that.setData({
            node2:e[key].confirm_value?e[key].confirm_value:e[key].textcontent
          })
        }
        if(key.includes('实施内容')) {
          that.setData({
            node3:e[key].confirm_value?e[key].confirm_value:e[key].textcontent
          })
        }
        if(key.includes('与现场协商试运行')) {
          that.setData({
            node4:e[key].confirm_value?e[key].confirm_value:e[key].textcontent
          })
        }
        if(key.includes('现场交代事项')){
          that.setData({
            node5:e[key].confirm_value?e[key].confirm_value:e[key].textcontent
          })
        }
      })
    })
  },
  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/agent/billCanUseProduct/index',
    })
  },
  // 添加物料
  async getAdd(selection) {
    let arr = selection.map(e=>{
      return {
        "sa_workorder_node_itemsid":0,
        "itemid":e.itemid,
        "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()
  },
  // 删除物料
  async deleteMaterial (data) {
    let itemid = data.detail.sa_serviceorderitemsid
    const res = await api._post({
      "id": "20230206162003", 
      "content": {
        "sa_serviceorderitemsids": [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 () {
    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()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.bindSignNameData.ownerid = options.id
    this.setData({
      id:options.id,
      bindSignNameData:this.data.bindSignNameData
    })
    this.billData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      SignName:true
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    this.setData({
      SignName:false
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})