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

  /**
   * 页面的初始数据
   */
  data: {
    deviceCode: '',
    alarmRecordId: '',
    content: '',
    projectId: '',
    HandlersData: [],
    tqImeiData: [],
    deviceData: [],
    LevelData: [],
    formData: [],
    options: {
      styleIsolation: 'apply-shared',
    },
    mode: '',
    datetimeVisible: false,
    isDeviceShow: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      alarmRecordId: options.alarmRecordId,
      projectId: wx.getStorageSync('projectId'),
      completeTimeText: utils.getTime2(),
      completeTime: utils.getTime2(),
      'formData.completeTime': utils.getTime2(),
    })
    var deviceCode = options.deviceCode;
    var deviceName = options.deviceName;
    if(deviceCode != '') {
      this.setData({
        deviceCode: deviceCode,
        deviceCodeText: deviceName,
        'formData.deviceCode': deviceCode,
        isDeviceShow: true
      })
      this.getHandlersData();
    } else {
      this.setData({
        isDeviceShow: false
      })
    }
    this.getTqData();
    this.getLevelData();
  },
  //紧急程度
  getLevelData() {
    var that = this;
    myRequest.myRequest('task/taskLevel', {
      
    },'get').then(function(res){
      var data = res.data.data;
      var LevelData = [];
      for (let i = 0; i < data.length; i++) {
        LevelData.push({label: data[i].dataValue, value: data[i].dataCode})
      }
      that.setData({
        LevelData: LevelData,
      })
    }).catch(function(res){
      console.log(res);
    })
  },
  //报警用户
  getHandlersData() {
    var that = this;
    myRequest.myRequest('task/handlers', {
      deviceCode: that.data.formData.deviceCode
    },'get').then(function(res){
      var data = res.data.data;
      var HandlersData = [];
      for (let i = 0; i < data.length; i++) {
        HandlersData.push({label: data[i].name, value: data[i].id})
      }
      that.setData({
        HandlersData: HandlersData,
      })
    }).catch(function(res){
      console.log(res);
    })
  },
  //途强车辆设备列表
  getTqData() {
    var that = this;
    myRequest.myRequest('task/tqDevices', {
      projectId: that.data.projectId
    },'get').then(function(res){
      var data = res.data.data;
      var tqImeiData = [];
      for (let i = 0; i < data.length; i++) {
        tqImeiData.push({label: data[i].num, value: data[i].imei})
      }
      that.setData({
        tqImeiData: tqImeiData,  
      })
    }).catch(function(res){
      console.log(res);
    })
  },
  showPicker(e) {
    const { mode } = e?.currentTarget?.dataset;
    this.setData({
      mode,
      [`${mode}Visible`]: true,
    });
  },
  hidePicker() {
    const { mode } = this.data;
    this.setData({
      [`${mode}Visible`]: false,
    });
  },
  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]
    });
    if(key == 'deviceCode') {
      this.getHandlersData();
    }
  },
  getContent(e) {
    this.setData({
      'formData.content': e.detail.value
    })
    console.log(this.data.formData)
  },

  dispatch() {
    var that = this;
    const { deviceCode, content, handler, completeTime, tqImei, alarmLevel }  = this.data.formData;
    var alarmRecordId = parseInt(that.data.alarmRecordId);
    console.log(deviceCode);
    if (alarmRecordId.length <= 0) {
      wx.showToast({
        title: '任务ID不能为空',
        icon: 'none',
      })
      return false;
    }
    if (deviceCode == undefined) {
      wx.showToast({
        title: '请选择站点',
        icon: 'none',
      })
      return false;
    }
    if (completeTime == undefined) {
      wx.showToast({
        title: '日期不能为空',
        icon: 'none',
      })
      return false;
    }
    if (alarmLevel == undefined) {
      wx.showToast({
        title: '紧急程度不能为空',
        icon: 'none',
      })
      return false;
    }
    if (handler == undefined) {
      wx.showToast({
        title: '处理人员不能为空',
        icon: 'none',
      })
      return false;
    }
    if (tqImei == undefined) {
      wx.showToast({
        title: '调用车辆不能为空',
        icon: 'none',
      })
      return false;
    }
    if (content == undefined) {
      wx.showToast({
        title: '任务内容不能为空',
        icon: 'none',
      })
      return false;
    }
    myRequest.myRequest('alarm/toTask', {
      deviceCode: deviceCode,
      alarmRecordId: alarmRecordId,
      taskContent: content,
      handler: handler,
      completeTime: completeTime,
      tqImei: tqImei,
      alarmLevel: alarmLevel,
    },'post').then(function(res){
      wx.showToast({
        title: '提交成功',
        icon: 'none',
      })
      wx.navigateTo({
        url: '../t-dispatch/t-dispatch',
      })
    }).catch(function(res){
      console.log(res);
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

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

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

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

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

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

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

