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:''}}]
      arr.forEach(e=>{
        res.data.confirmationcontent.push(e)
      },this)
    }
    
    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
          })
        }
      })
    })
  },
  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 () {
    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 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()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.bindSignNameData.ownerid = options.id
    this.setData({
      stopClick:options.class,
      id:options.id,
      bindSignNameData:this.data.bindSignNameData
    })
    this.billData()
  },

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

  },

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

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

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

  },

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

  },

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

  },

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

  }
})