// pages/t-repair/t-repair.js
var myRequest = require("../../utils/api.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    projectId: '',
    deviceCodeData: [],
    deviceTypeData: [],
    repairItemData: [],
    repairTypeData: [],
    equipmentNameData: [],
    fileList: {
      repairImg: [],
    },
    formData: [],
    gridConfig: {
      column: 4,
      width: 160,
      height: 160,
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.projectId = wx.getStorageSync('projectId');
    
    this.getDeviceData();
    this.getDictData();
  },
  //获取站点
  getDeviceData() {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    myRequest.myRequest('task/devices', {
      projectId: that.data.projectId
    },'get').then(function(res){
      var deviceData = res.data.data;
      var arr = [];
      for (let i = 0; i < deviceData.length; i++) {
        arr.push({label: deviceData[i].name, value: deviceData[i].code})
      }
      that.setData({
        deviceCodeData: arr,
      });
      wx.hideLoading();
    }).catch(function(res){
      console.log(res);
    })
  },
  // 获取字典数据
  getDictData() {
    var that = this;
    myRequest.myRequest('repair/dict', {}).then(function(res){
      var dictData = res.data.data;
      var equipmentNameData = [];
      for (let i = 0; i < dictData.equipmentName.length; i++) {
        equipmentNameData.push({label: dictData.equipmentName[i].dataValue, value: dictData.equipmentName[i].dataCode})
      }
      console.log(equipmentNameData);
      that.setData({
        repairTypeData: dictData.repairType,
        equipmentNameData: equipmentNameData,
      })
      
      console.log(that.data.repairTypeData);
    }).catch(function(res){
      console.log(res);
    })
  },
  onPicker(e) {
    var filed = e.currentTarget.dataset.filed;
    console.log(filed)
    this.setData({
      [`${filed}Visible`]: true,
    });
  },
  onPickerChange(e) {
    const { key } = e.currentTarget.dataset;
    const { value, label } = e.detail;
    this.setData({
      [`${key}Visible`]: false,
      [`${key}Value`]: value,
      [`${key}Text`]: label.join(' '),
      [`formData.${key}`]: value[0]
    });
    
  },
  onPickerChange(e) {
    const { key } = e.currentTarget.dataset;
    const { value, label } = e.detail;

    console.log('picker change:', e.detail);
    this.setData({
      [`${key}Visible`]: false,
      [`${key}Value`]: value,
      [`${key}Text`]: label.join(' '),
      [`formData.${key}`]: value[0]
    });
    console.log(this.data.formData)
  },
  onRepairTypeChange(e) {
    this.setData({
      'formData.repairType': e.detail.value
    });
  },
  onCityPicker(e) {
    var filed = e.currentTarget.dataset.filed;
    console.log(filed)
    this.setData({
      [`${filed}Visible`]: true,
    });
  },
  handleAdd(e) {
    var filed = e.currentTarget.dataset.filed;
    var that = this;
    const { files } = e.detail;
    var fileLists = this.data.fileList[filed] == undefined ? [] : this.data.fileList[filed];
    var filePath = this.data.formData[filed] == undefined ? '' : this.data.formData[filed];
    if (filePath != '') {
      filePath = filePath + ','
    }
    for (let i = 0; i < files.length; i++) {
      myRequest.updateImg({file:files[i].url}).then(function(res){
        fileLists.push({url:files[0].url,type:'image',percent:100});
        that.setData({
          ['formData.'+filed+'']: filePath + res,
          ['fileList.'+filed+'']: fileLists,
        })
      }).catch(function(res){
        console.log(res);
      })
    }
  },
  handleRemove(e) {
    var filed = e.currentTarget.dataset.filed;
    const { index } = e.detail;
    const { fileList } = this.data;
    fileList[filed].splice(index, 1);
    this.setData({
      fileList,
    });
  },
  submitForm() {
    var pream = this.data.formData;
    myRequest.myRequest('repair/add', pream,'post').then(function(res){
      wx.showToast({
        title: '提交成功',
        icon: 'none',
      })
      wx.navigateTo({
        url: '/pages/guarantee/guarantee',
      })
    }).catch(function(res){
      console.log(res);
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

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

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

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

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

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

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

