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

  /**
   * 页面的初始数据
   */
  data: {
    content: '',
    deviceCode: '',
    HandlersData: [],
    levelData: [],
    tqImeiData: [],
    repairId: '',
    formData: [],
    deviceCodeText: '',
    options: {
      styleIsolation: 'apply-shared',
    },
    mode: '',
    datetimeVisible: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      projectId: wx.getStorageSync('projectId'),
      completeTimeText: utils.getTime2(),
      completeTime: utils.getTime2(),
      'formData.completeTime': utils.getTime2(),
      repairId: options.id,
      deviceCode: options.deviceCode,
      deviceCodeText: options.deviceName,
    })
    this.getHandlersData();
    this.getTqData();
    this.getLevelData();
  },
  //报警用户
  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);
    })
  },
  //任务级别字典数据
  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);
    })
  },
  //途强车辆设备列表
  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]
    });
  },

  getContent(e) {
    this.setData({
      'formData.taskContent': e.detail.value
    })
  },

  dispatch() {
    var that = this;
    var formData  = this.data.formData;
    console.log(this.data.formData);
    myRequest.myRequest('repair/toTask', {
      repairId: that.data.repairId,
      taskContent: formData.taskContent,
      handler: formData.handler,
      completeTime: formData.completeTime,
      taskLevel: formData.taskLevel,
      tqImei: formData.tqImei,
    },'post').then(function(res){
      wx.showToast({
        title: '提交成功',
        icon: 'none',
      })
      wx.navigateTo({
        url: '../guarantee/guarantee',
      })
    }).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 () {
    
  },

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

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

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

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

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

