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

  /**
   * 页面的初始数据
   */
  data: {
    taskData: {
      id: '',
      name: '',
      time: '',
      content: '',
      handname: '',
      taskLevel: '',
      handid: '',
      deviceId: '',
      deviceCode: ''
    },
    deviceId: '',
    datetimeVisible: false,
    datetime: '',
    content: '',
    handlersData: [],
    statusData: [
      {
        value: 1,
        label: '已处理'
      },
      {
        value: 0,
        label: '未处理'
      }
    ],
    fileList: [],
    gridConfig: {
      column: 4,
      width: 160,
      height: 160,
    },
    filePath: '',
    status: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      taskData: options,
      handlerValue: options.handid,
      datetime: new Date().getTime(),
    })
    console.log(this.data.taskData)
  },
  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]
    });
    
  },
 
  setSelectLabel(value) {
    var options = this.data.singleSelect.options;
    for (let i = 0; i < options.length; i++) {
      if(options[i].id == value) {
        this.setData({
          label: options[i].name
        })
      }
    }
  },
  getContent(e) {
    this.setData({
      content: e.detail.value
    })
    console.log(e.detail.value)
  },
  dispatch() {
    var that = this;
    if (that.data.status == '') {
      wx.showToast({
        title: '请选择处理状态',
        icon: 'none',
      })
      return false;
    }
    if (that.data.content == '') {
      wx.showToast({
        title: '请输入处理内容',
        icon: 'none',
      })
      return false;
    }
    if (that.data.filePath == '') {
      wx.showToast({
        title: '请上传图片',
        icon: 'none',
      })
      return false;
    }
    myRequest.myRequest('task/feedback', {
      id: parseInt(that.data.taskData.deviceId),
      feedbackText: that.data.content,
      status: ""+that.data.status+"",
      feedbackImg: that.data.filePath
    },'post').then(function(res){
      wx.showToast({
        title: '反馈已提交',
        icon: 'none',
      })
      wx.navigateTo({
        url: '../t-dispatch/t-dispatch',
      })
    }).catch(function(res){
      console.log(res);
    })
  },
  changeAbnormal(e) {
    this.setData({
      status: e.detail.value,
    })
  },
  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,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

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

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

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

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

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

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

