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

  /**
   * 页面的初始数据
   */
  data: {
    deviceId: '',
    deviceCode: '',
    projectId: '',
    content: '',
    typeData: [],
    levelData: [],
    deviceData: [],
    taskLevelData: [],
    HandlersData: [],
    tqImeiData: [],
    formData: [],
    showDetail: false,
    options: {
      styleIsolation: 'apply-shared',
    },
    mode: '',
    datetimeVisible: false,
    deviceName: '',
    deviceId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  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.getDeviceData();
    }
    this.getDeviceList();
    this.getDictData();
    this.getTqData();
  },
  search() {
    wx.navigateTo({
      url: '../search-site/search-site?type=xunjian',
    })
  },
  //站点列表
  getDeviceList()
  {
    var that = this;
    myRequest.myRequest('task/devices?projectId=14', {
      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);
    })
  },
  //站点详情
  getDeviceData: function()
  {
    var that = this;
    myRequest.myRequest('device/details', {
      deviceId: that.data.deviceId
    },'get').then(function(res){
      that.setData({
        deviceDetail: res.data.data,
        deviceCode: res.data.data.code,
        showDetail: true,
      })
      that.getHandlersData();
    }).catch(function(res){
      console.log(res);
    })
  },
  //字典数据
  getDictData: function()
  {
    var that = this;
    myRequest.myRequest('task/taskLevel', {
      
    },'get').then(function(res){
      var data = res.data.data;
      var taskLevelData = [];
      for (let i = 0; i < data.length; i++) {
        taskLevelData.push({label: data[i].dataValue, value: data[i].dataCode})
      }
      that.setData({
        taskLevelData: taskLevelData,
      })
    }).catch(function(res){
      console.log(res);
    })
  },
  //报警用户
  getHandlersData() {
    var that = this;
    myRequest.myRequest('task/handlers', {
      deviceCode: that.data.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;
    console.log(mode)
    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') {
      var deviceData = this.data.deviceData;
      for (let i = 0; i < deviceData.length; i++) {
        if (deviceData[i].value == value[0]) {
          var deviceId = deviceData[i].id
          this.setData({
            deviceId: deviceId,
          })
          this.getDeviceData();
        }
      }
      
      // this.getDeviceData();
    }
  },

  getContent(e) {
    this.setData({
      'formData.content': e.detail.value
    })
    console.log(this.data.formData)
  },

  dispatch() {
    var that = this;
    var formData  = this.data.formData;
    console.log(123, formData);
    if (that.data.deviceCode == '') {
      wx.showToast({
        title: '请选择站点',
        icon: 'none',
      })
      return false;
    }
    if (formData.taskLevel == undefined) {
      wx.showToast({
        title: '请选择紧急程度',
        icon: 'none',
      })
      return false;
    }
    if (formData.completeTime == undefined) {
      wx.showToast({
        title: '请选择时间',
        icon: 'none',
      })
      return false;
    }
    if (formData.content == undefined) {
      wx.showToast({
        title: '请输入任务内容',
        icon: 'none',
      })
      return false;
    }
    if (formData.handler == undefined) {
      wx.showToast({
        title: '请选择处理人',
        icon: 'none',
      })
      return false;
    }
    if (formData.tqImei == undefined) {
      wx.showToast({
        title: '请选择车辆',
        icon: 'none',
      })
      return false;
    }
    myRequest.myRequest('task/add', {
      deviceCode: that.data.deviceCode,
      taskContent: formData.content,
      handler: formData.handler,
      completeTime: formData.completeTime,
      tqImei: formData.tqImei,
      taskLevel: formData.taskLevel,
    },'post').then(function(res){
      wx.showToast({
        title: '提交成功',
        icon: 'none',
      })
      wx.navigateTo({
        url: '../t-dispatch/t-dispatch',
      })
    }).catch(function(res){
      console.log(res);
    })
  },
  navigation(e) {
    let latitude = e.currentTarget.dataset.latitude;
    let longitude = e.currentTarget.dataset.longitude;
    let name = e.currentTarget.dataset.name;
    wx.openLocation({
      latitude: latitude,
      longitude: longitude,
      name: name,
      address: name,
      scale: 28
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

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

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

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

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

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

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

