// pages/maintenance/index.js
var myRequest = require("../../utils/api.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    desc:'',
    show:false,
    orderId:'',
    accessory:[
    ],
    searchValue:'',
    accessorySearchList:'',
    faultImg:[],
    accessoryList:[


    ],
    autosize:{
      minHeight: 70
   },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
 
    let info = JSON.parse(options.item)
    console.log(info)
    this.setData({
      orderId :info.orderId
    })
    let device = info.device
    this.getdeviceParts(device.id)
  },
  getdeviceParts(deviceVersion){
    let that =this;
    myRequest.myRequest('deviceParts/list/'+deviceVersion,{
    },'get').then(res=>{
  
      this.setData({
        accessory:res.data,
        accessorySearchList:res.data
      })

   
    })
  },

  onClose(){
    this.setData({
      show:false,
      searchValue:''
    })
  },
  delAccessory(e){
  console.log(e)
  let index = e.currentTarget.dataset.index
  let accessoryList = this.data.accessoryList
  accessoryList.splice(index,1)
  this.setData({
    accessoryList
  })
  },
  searchList(e){
    console.log(e.detail)
    let accessory = []
    let list =this.data.accessorySearchList
    if(e.detail){
     
      accessory = list.filter(item=>{
         return item.partsName.indexOf(e.detail)!=-1
      })
    }else{
      accessory = list
    }
    this.setData({
      accessory
    })



  },
  saveComplete(){
    console.log(this.data.accessoryList)
    if(this.data.desc==''){
      wx.showToast({
        title: '请输入维修事项',
        icon:'none'
      })
      return 
    }
    if(this.data.faultImg.length<3){
      wx.showToast({
        title: '请上传维修图片，不少于3张',
        icon:'none'
      })
      return 
    }
    let parts = this.data.accessoryList.map(item=>{
      return {
        partsId:item.partsId,
        quantity:item.num,
        price:item.price
      }
    })
    let img= this.data.faultImg.map(item=>{
      return item.url
    })
    let parma = {
      parts,
      desc:this.data.desc,
      img
    }

    let orderId = this.data.orderId
    myRequest.myRequest('deviceRepairOrder/supporter/complete/'+this.data.orderId,parma,'post').then(res=>{
      wx.showToast({
        title: '提交成功',
        icon:'none'
      })
      setTimeout(() => {
        wx.navigateBack({
          delta: 1,
          success: function (e) {
            var page = getCurrentPages().pop();
            if (page == undefined || page == null) return;
            page.onLoad({id:orderId}); // 刷新数据
          }
        });
      }, 1000);
    
    
    })
  },
  onChangeDesc(e){
  this.setData({
    desc:e.detail
  })
  },
  selectItem(e){

    let data = e.currentTarget.dataset.item
    console.log(data)
    let accessoryList = this.data.accessoryList
    accessoryList.push({
      name:data.partsName,
      type:data.partsModel,
      price:data.price,
      partsId:data.partsId,
      num:'1'
    })
    this.setData({accessoryList,show:false,searchValue:''})
  },
  onChangeNum(e){
    console.log(e.currentTarget.dataset.index)
    let index = e.currentTarget.dataset.index
    let accessoryList =this.data.accessoryList
    accessoryList[index].num = e.detail
    this.setData({
      accessoryList
    })
  },
  addAccessory(){
    this.setData({
      show:true
    })
  },
  bindDel(e){
    console.log(e)
    let faultImg= this.data.faultImg
    let delList =  faultImg.splice(e.detail.index,1)
    myRequest.myRequest('wx/oss/'+delList[0].ossId,{
    },'delete').then(res=>{
     this.setData({faultImg})
   })

  },
  uploadFile(url) {
    return new Promise((resolve, reject) => {
      myRequest.updateImg({
        file: url
      }).then(res => {
        resolve(res)
      })
    })

  },
  afterRead(event) {
    const {
      file
    } = event.detail;
    let that = this;
    let upload = []
    file.forEach(element => {
      upload.push(this.uploadFile(element.url))
    })
     Promise.all(upload).then(res => {

      let faultImg = that.data.faultImg
      faultImg=[...faultImg,...res]
      that.setData({
        faultImg
      });
    })
  },
  onChange(){},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  clickCell(){

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

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

  },

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

  },

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

  },

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

  }
})