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

  /**
   * 页面的初始数据
   */
  data: {
    deviceId: '',
    deviceCode: '',
    deviceName: '',
    projectId: '',
    content: '',
    deviceData: [],
    equipmentData: [],
    formData: [],
    options: {
      styleIsolation: 'apply-shared',
    },
    mode: '',
    datetimeVisible: false,
    fileList: [],
    gridConfig: {
      column: 4,
      width: 160,
      height: 160,
    },
    filePath: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      projectId: wx.getStorageSync('projectId'),
      completeTimeText: utils.getTime2(),
      completeTime: utils.getTime2(),
      'formData.completeTime': utils.getTime2(),
    })
    var deviceId = options.deviceId;
    var deviceName = options.deviceName;
    if (deviceId !== '') {
      this.setData({
        deviceId: deviceId,
        deviceCodeValue: deviceId,
        'formData.deviceCode': deviceId,
        deviceCodeText: deviceName,
        showDetail: true,
      })
    }
    // this.getDeviceList();
    this.getDictData();
  },
  search() {
    wx.navigateTo({
      url: '../search-site/search-site?type=xianxia',
    })
  },
  //站点列表
  getDeviceList()
  {
    var that = this;
    myRequest.myRequest('task/devices', {
      projectId:this.data.projectId
    },'get').then(function(res){
      var data = res.data.data;
      var deviceData = [];
      for (let i = 0; i < data.length; i++) { 
        deviceData.push({label: data[i].name, value: data[i].code, id: data[i].id})
      }
      that.setData({
        deviceData: deviceData,
      })
    }).catch(function(res){
      console.log(res);
    })
  },
  //字典数据
  getDictData: function()
  {
    var that = this;
    myRequest.myRequest('repair/dict', {
      
    },'get').then(function(res){
      var data = res.data.data.repairType;
      var repairTypeData = [];
      for (let i = 0; i < data.length; i++) {
        repairTypeData.push({label: data[i].dataValue, value: data[i].dataCode})
      }
      that.setData({
        repairTypeData: repairTypeData,
      })
    }).catch(function(res){
      console.log(res);
    })
  },
  //保修设备
  getEquipmentsData() {
    var that = this;
    myRequest.myRequest('repair/equipments', {
      deviceCode: that.data.formData.deviceCode
    },'get').then(function(res){
      var data = res.data.data;
      var equipmentIdData = [];
      for (let i = 0; i < data.length; i++) {
        equipmentIdData.push({label: data[i].name, value: data[i].id})
      }
      that.setData({
        equipmentIdData: equipmentIdData,
      })
    }).catch(function(res){
      console.log(res);
    })
  },
  handleAdd(e) {
    var that = this;
    const { files } = e.detail;
    var fileLists = this.data.fileList;
    var filePath = this.data.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({
          filePath: filePath + res + ',',
          fileList: fileLists,
        })
        console.log(1, that.data.filePath);
      }).catch(function(res){
        console.log(res);
      })
    }
  },
  handleRemove(e) {
    const { index } = e.detail;
    const { fileList } = this.data;
    fileList.splice(index, 1);
    this.setData({
      fileList,
    });
  },
  onConfirm(e) {
    const { value } = e?.detail;
    const { mode } = this.data;
    this.setData({
      [mode]: value,
      [`${mode}Text`]: value,
      [`formData.${mode}`]: value
    });
    this.hidePicker();
  },
  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]
    });
    // this.getEquipmentsData();
  },

  dispatch() {
    var that = this;
    var formData  = this.data.formData;
    console.log(this.data.formData);
    myRequest.myRequest('repair/add', {
      deviceCode: formData.deviceCode,
      equipmentId: formData.equipmentId,
      repairType: formData.repairType,
      repairImg: that.data.filePath,
    },'post').then(function(res){
      wx.showToast({
        title: '提交成功',
        icon: 'none',
      })
      wx.navigateTo({
        url: '../guarantee/guarantee',
      })
    }).catch(function(res){
      console.log(res);
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      [`formData.deviceCode`]: this.data.deviceCode
    })
    if (this.data.deviceCode) {
      this.getEquipmentsData();
    }
  },

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

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

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

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

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

