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

Page({

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

  async billData() {
    const res = await api._post({
      "id": "20230211105803",
      "content": {
        "sa_workorder_confirmationid": this.data.id
      }
    })

    let ishave = false

    res.data.confirmationcontent.forEach(item => {
      if (item.nextTreatmentTime) {
        ishave = true
      }
    })
    if (!ishave) {
      let arr = [{
        nextTreatmentTime: {
          confirm_value: ''
        }
      }, {
        treatment: {
          confirm_value: ''
        }
      }, {
        explain: {
          confirm_value: ''
        }
      }, {
        repairResult: {
          confirm_value: ''
        }
      }, {
        rebackTime: {
          confirm_value: ''
        }
      }, {
        nextRebackTime: {
          confirm_value: ''
        }
      }, {
        text: {
          confirm_value: ''
        }
      }]
      arr.forEach(e => {
        res.data.confirmationcontent.push(e)
      }, this)
    }
    console.log(res.data)
    this.setData({
      mainData: res.data,
      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 === 'nextTreatmentTime') {
          this.setData({
            nextTreatmentTime: e[key].confirm_value
          })
        }
        if (key === 'treatment') {
          this.setData({
            treatment: e[key].confirm_value
          })
        }
        if (key === 'explain') {
          this.setData({
            explain: e[key].confirm_value
          })
        }
        if (key === 'nextRebackTime') {
          this.setData({
            nextRebackTime: e[key].confirm_value
          })
        }
        if (key === 'text') {
          this.setData({
            text: e[key].confirm_value
          })
        }
      })
    })
  },
  async updateBill() {
    if (this.data.mainData.status !== '新建') return wx.showToast({
      title: '非新建状态,修改的数据将不会保存',
      icon: 'none'
    })
    const res = await api._post({
      "id": "20230211105703",
      "content": this.data.mainData
    })
  },



  // 改变评分
  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) {
    console.log(data)
    let name = data.currentTarget.dataset.name
    this.data.mainData.confirmationcontent.forEach(e => {
      Object.keys(e).map((key, index) => {
        if (name === key) {
          e[key].confirm_value = data.detail.value
        }
      })
    })

    this.updateBill()
  },
  showNextTimePicker(data) {
    let name = data.currentTarget.dataset.name
    this.setData({
      pickkerName: name,
      dateVisible: !this.data.dateVisible
    })
  },
  onConfirm(data) {
    let name = this.data.pickkerName
    this.data.mainData.confirmationcontent.forEach(e => {
      Object.keys(e).map((key, index) => {
        if (name === key) {
          e[key].confirm_value = data.detail.value
        }
      })
    })
    this.setData({
      mainData: this.data.mainData
    })
    this.updateBill()
    this.setInputData()
  },
  /* 查看单据 */
  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
    })
  }
})