//index.js
//获取应用实例
const app = getApp()
let listData = [];

Page({
  data: {
    list: [],
    selectId: 0,
    type: ''
  },
  onLoad: function (options) {
    var that = this;
    if(options.hasOwnProperty('type')){
        that.setData({
            type:options.type
        })
    }
    wx.getStorage({ //获取页面传递过来的数据
      key: 'gotopage',
      success(res) {
        wx.setNavigationBarTitle({
          title: res.data.title
        });
        that.setData({
          type: res.data.type
        });
        console.log(res.data)
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2];  //上一个页面
          if(res.data.type=='dep'){
              var id = prevPage.data.depId;
          }else if(res.data.type=='conveyType'){
              var id = prevPage.data.conveyTypeId;
          }else if(res.data.type=='conveyAddrStart'){
              var id = prevPage.data.conveyStart;
          }else if(res.data.type=='conveyAddrEnd'){
              var id = prevPage.data.conveyEnd;
          }else if(res.data.type=='conveyDevice'){
              var id = prevPage.data.conveyDevice;
          }else if(res.data.type=='priority'){
              var id = prevPage.data.priority;
          }else if(res.data.type=='repairPriority'){
              var id = prevPage.data.repairPriority;
          }

          that.setData({
              selectId: id
          });

      },
      fail(res) {
        wx.navigateBack({ delta: 1 });
        return false;
      }
    });
  },
  
  onShow:function(){
    var that = this;
    console.log(that.data.type)
    app.ajaxReadyCallback = res => {
      that.setData({
        list: res.data.data
      });
      listData = res.data.data;
    }
    this.showData();
  },

  showData: function () {
    var org = app.globalData.userinfo.org;
    console.log(this.data.type)
      if(this.data.type=='dep'){
          app.ajax({
              url: app.globalData.serverUrl + 'orders/getDep',
              type: 'POST',
              data: {

              }
          });
      }else if(this.data.type=='conveyType'){
          app.ajax({
              url: app.globalData.serverUrl + 'OrderConveyType/getCate',
              type: 'POST',
              data: {

              }
          });

      }else if(this.data.type=='conveyAddrStart'){
          app.ajax({
              url: app.globalData.serverUrl + 'OrderConveyType/getAddress',
              type: 'POST',
              data: {

              }
          });
      }else if(this.data.type=='conveyAddrEnd'){
          app.ajax({
              url: app.globalData.serverUrl + 'OrderConveyType/getAddress',
              type: 'POST',
              data: {

              }
          });
      }else if(this.data.type=='conveyDevice'){
          app.ajax({
              url: app.globalData.serverUrl + 'OrderConveyType/getDevice',
              type: 'POST',
              data: {

              }
          });
      }else if(this.data.type=='priority'){
          app.ajax({
              url: app.globalData.serverUrl + 'OrderConveyType/priority',
              type: 'POST',
              data: {

              }
          });
      }else if(this.data.type=='repairPriority'){
          app.ajax({
              url: app.globalData.serverUrl + 'Orders/repair_priority',
              type: 'POST',
              data: {

              }
          });
      }


  },

  selectBtn: function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    var title = e.currentTarget.dataset.title;
    var cate = e.currentTarget.dataset.cate;
    var info = e.currentTarget.dataset.info;
    this.setData({
      selectId: id
    });

    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面

    wx.navigateBack({
      delta: 1,
      success: function (res) {
          if(that.data.type=='dep'){
              prevPage.setData({
                  depId: id,
                  depName: title
              });
          }else if(that.data.type=='conveyType'){
              prevPage.setData({
                  conveyTypeId: id,
                  conveyTypeName: title,
                  conveyTypeCate: cate,
                  priority: info.priority,
                  priorityName: info.priorityName,
                  xqTime: info.xqTime,
                  ywcTime: info.ywcTime,
              });
              if(info.ends!=null){
                  prevPage.setData({
                      conveyEnd: info.ends[0].id,
                      conveyEndName: info.ends[0].title,
                  });
              }
          }else if(that.data.type=='conveyAddrStart'){
              prevPage.setData({
                  conveyStart: id,
                  conveyStartName: title
              });

          }else if(that.data.type=='conveyAddrEnd'){
              prevPage.setData({
                  conveyEnd: id,
                  conveyEndName: title
              });
          }else if(that.data.type=='conveyDevice'){
              prevPage.setData({
                  conveyDevice: id,
                  conveyDeviceName: title
              });
          }else if(that.data.type=='priority'){
              prevPage.setData({
                  priority: id,
                  priorityName: title
              });
          }else if(that.data.type=='repairPriority'){
              prevPage.setData({
                  repairPriority: id,
                  repairPriorityName: title
              });
          }

      }
    })
  },

  showInput: function () {
    this.setData({
     inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
     inputVal: "",
     inputShowed: false,
     list: listData
    });
  },
  clearInput: function () {
    this.setData({
     inputVal: "",
     list: listData
    });
  },
  inputTyping: function (e) {
    //搜索数据
    this.setData({
     inputVal: e.detail.value
    });
    if (e.detail.value == '') {
      this.setData({
        list: listData
      });
      return ''; 
    }

    let arr = [];
    for(var i in listData) {
      if (listData[i].title.indexOf( e.detail.value) > -1) {
        arr.push(listData[i]);
      }
    }
    this.setData({
      list: arr
    })
  }
  
})
