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

  /**
   * 页面的初始数据
   */
  data: {
    singleSelect: {
      value: '',
      options: [],
    },
    keys: {
      value: 'id',
      label: 'name',
    },
    page: 1,
    size: 10,
    projectId: '',
    status: 0,
    taskData: [],
    isFromSearch: false,
    label: '请选择站点',
    deviceCode: '',
    deviceName: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
    var projectId = wx.getStorageSync('projectId');
    this.setData({
      projectId: projectId,
      taskData: [],
    })
    // this.getDevicesData()
    this.getTaskData()
  },
  search() {
    wx.navigateTo({
      url: '../search-site/search-site?type=xianxia',
    })
  },
  //站点列表
  getDevicesData: function()
  {
    var that = this;
    myRequest.myRequest('repair/deviceList', {projectId:that.data.projectId}, 'get').then(function(res){
      var devicesData = res.data.data;
      that.setData({
        'singleSelect.options': devicesData,
      });
    }).catch(function(res){
      console.log(res);
    })
  },
  //上拉
  onReachBottom() {
    this.setData({
      page: that.data.page+1,
      isFromSearch: false
    });
    this.getTaskData()
  },
  getTaskData() {
    var that = this;
    // var deviceName = that.data.label != '请选择站点' ? that.data.label : '';
    var deviceName = that.data.deviceName;
    myRequest.myRequest('repair/list', {
      projectId: that.data.projectId,
      page: that.data.page,
      size: that.data.size,
      deviceName: deviceName
      // status: that.data.status
    }, 'get').then(function(res){
      var siteList = res.data.data.items;
      if (siteList.length != 0){
        var list = []; 
        //如果isFromSearch是true从data中取出数据，否则先从原来的数据继续添加 
        that.data.isFromSearch ? list = siteList : list = that.data.taskData.concat(siteList) 
        that.setData({ 
          taskData: list, //获取数据数组 
        });
      }
      that.setData({ 
        searchLoadingComplete: true, //把“没有数据”设为true，显示 
        searchLoading: false //把"上拉加载"的变量设为false，隐藏 
       });
    }).catch(function(res){
      console.log(res);
    })
  },
  //派工
  dispatch(e) {
    var deviceId = this.data.singleSelect.value;
    var id = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    var time = e.currentTarget.dataset.time;
    var content = e.currentTarget.dataset.content;
    var handname = e.currentTarget.dataset.handname;
    var handid = e.currentTarget.dataset.handid;
    var taskLevel = e.currentTarget.dataset.tasklevel;
    var code = e.currentTarget.dataset.code;
    wx.navigateTo({
      url: '../c-dispatch/c-dispatch?id=' + id + '&name=' + name + '&time=' + time + '&content=' + content + '&handname=' + handname + '&deviceId=' + id + '&handid=' + handid + '&taskLevel=' + taskLevel + '&deviceCode=' + code,
    })
  },
  add() {
    var deviceId = this.data.singleSelect.value;
    var deviceName = this.data.label;
    wx.navigateTo({
      url: '../guaranteeadd/guaranteeadd?deviceId=' + deviceId + '&deviceName=' + deviceName,
    })
  },
  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
    })
  },
  handleSingleSelect(e) {
    this.setData({
      'singleSelect.value': e.detail.value,
      taskData: [],
    });
    this.setSelectLabel(e.detail.value)
    this.getTaskData()
  },
  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
        })
      }
    }
    console.log(this.data.label)
  },
  toTask(e) {
    let id = e.currentTarget.dataset.id;
    let deviceCode = e.currentTarget.dataset.code;
    let deviceName = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: '../gutotask/gutotask?id=' + id + '&deviceCode=' + deviceCode + '&deviceName=' + deviceName,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },


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

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

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

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

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

