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

  /**
   * 页面的初始数据
   */
  data: {
    singleSelect: {
      value: '',
      options: [],
    },
    keys: {
      value: 'id',
      label: 'name',
    },
    deviceId: '',
    label: '请选择联系人',
    datetimeVisible: false,
    datetime: '',
    content: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      datetime: new Date().getTime(),
      deviceId: options.deviceId,
    })
    console.log(this.data.datetime)
    this.getProjectData();
  },
  //报警联系人
  getProjectData: function()
  {
    var that = this;
    myRequest.myRequest('task/handlers', {
      deviceId:this.data.deviceId
    },'get').then(function(res){
      var handlersData = res.data.data;
      that.setData({
        'singleSelect.options': handlersData,
        'singleSelect.value': handlersData[0].id,
        label: handlersData[0].name,
      });
    }).catch(function(res){
      console.log(res);
    })
  },
  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;
    myRequest.myRequest('task/add', {
      deviceId: that.data.deviceId,
      task_content: that.data.content,
      handler: that.data.singleSelect.value,
      complete_time: that.data.datetime
    },'post').then(function(res){
      wx.showToast({
        title: '已派工',
        icon: 'none',
      })
    }).catch(function(res){
      console.log(res);
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

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

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

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

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

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

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

