//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    routers: [],
    imgUrls: [],
    notice: '',
    showNoticeNav: true,
    userInfo:{},
    bannerList:[],
    ordersNum:{},
    oneCall:null,
    yhyj:null,
    complain:null,
  },
  onShow: function () {
    var that = this;
    app.ajaxReadyCallback = res => {  //各个接口统一回调方法
      var apiname = res.data.apiname;
      if(apiname == 'icon'){
        var icons = res.data.data;
        var iconsarr = [];
        if (icons.length > 0) {
          for (var o in icons) {
            if(Number(icons[o].mode) == 42){
              var arr1 = {
                'id': icons[o].id,
                'name': icons[o].name,
                'mode': icons[o].mode,
                'icon': icons[o].path,
                'url': icons[o].url
              };
              that.setData({
                oneCall:arr1,
              })
            }
            // else if(Number(icons[o].mode) == 4){
            //   var arr2 = {
            //     'id': icons[o].id,
            //     'name': icons[o].name,
            //     'mode': icons[o].mode,
            //     'icon': icons[o].path,
            //     'url': icons[o].url
            //   };
            //   that.setData({
            //     yhyj:arr2,
            //   })
            // }else if(Number(icons[o].mode) == 12){
            //   var arr3 = {
            //     'id': icons[o].id,
            //     'name': icons[o].name,
            //     'mode': icons[o].mode,
            //     'icon': icons[o].path,
            //     'url': icons[o].url
            //   };
            //   that.setData({
            //     complain:arr3,
            //   })
            // }
            else{
              var oc = {
                'id': icons[o].id,
                'name': icons[o].name,
                'mode': icons[o].mode,
                'icon': icons[o].path,
                'url': icons[o].url
              };
              iconsarr.push(oc);
            }
     
          }
  
          that.setData({
            routers: iconsarr,
          });
        }
      }else if (apiname == 'userInfo'){
          that.setData({
              userInfo:res.data.data
          })
      }else if(apiname == 'banner'){
        that.setData({
          bannerList:res.data.data
        })
      }else if(apiname == 'ordersNums'){
        that.setData({
          ordersNum:res.data.data
        })
      }
    }
    this.ordersNum();
  },

  onLoad:function(options){
    app.getScene(options);
    if (!app.globalData.userinfo.userId || app.globalData.userinfo.userId == undefined) {
      app.globalData.userinfo = {
        userId: wx.getStorageSync("userId"),
        token: wx.getStorageSync("token"),
        orgId: wx.getStorageSync("orgId"),
        orgName: wx.getStorageSync("orgName")
      };
      app.globalData.isTourist = false;
      if (app.globalData.userinfo.userId == -110){ //游客
        app.globalData.isTourist = true;
      }
    }
    this.showData();
    this.getUserInfo();
    this.bannerList();
    this.ordersNum();
  },

  onPullDownRefresh() {
    this.showData();
  },

  iconTap:function(event){
    var that = this;
    var url = event.currentTarget.dataset.url;
    var name = event.currentTarget.dataset.name;
    var mode = event.currentTarget.dataset.mode;
    if(url){ //url不为空跳转到webview页
      console.log('跳转到webview页');
      return false;
    }
    console.log('mode',mode);
    switch(mode.toString()){
      case '1': //保洁发单
        wx.navigateTo({
          url: '/pages/order/add/index?mode=2&name='+name,
        })
        break;
      case '2': //维修发单
        wx.navigateTo({
          url: '/pages/order/add/index?mode=1&name='+name,
        })
        break;
      case '3': //运送发单
        wx.navigateTo({
          url: '/pages/order/addys/index?mode=3&name='+name,
        })
        break;
      case '4': //隐患发单
        wx.navigateTo({
          url: '/pages/order/add/index?mode=4&name='+name,
        })
        break;
      case '12': //发起投诉
        wx.navigateTo({
          url: '/pages/complain/add/index?name='+name,
        })
        break;
      case '15': //我的申请
        wx.navigateTo({
          url: '/pages/order/index?title='+name,
        })
        break;
      case '42': //一键呼叫
        wx.navigateTo({
            url: '/pages/order/add/index?mode=42&name='+name,
        })
        break;
      case '48': //发起投诉
        wx.navigateTo({
          url: '/pages/hiddendanger/index',
        })
        break;
    }
    
  },

  
  //首页接口数据
  showData:function(){
    app.ajax({
      url: app.globalData.serverUrl + 'appIcon/wxlist',
      type: 'POST',
      apiname: 'icon',
    });
  },
  //轮播题
  bannerList:function(){
    app.ajax({
      url: app.globalData.serverUrl + 'banner/list',
      type: 'POST',
      apiname: 'banner',
    });
  },

  //订单状态数量
  ordersNum:function(){
    app.ajax({
      url: app.globalData.serverUrl + 'Orders/getOrdersCount',
      type: 'POST',
      apiname: 'ordersNums',
    });
  },

  //设备台账接口
  deviceData: function (code) {
    app.ajax({
      url: app.globalData.serverUrl + 'server/deviceInsp/sweepCode',
      type: 'POST',
      apiname: 'device',
      data: {
        code: code
      }
    });
  },

  getNotice: function () {
    var userId = app.globalData.userinfo.userId;
    var org = app.globalData.userinfo.org.orgId;
    app.ajax({
      url: app.globalData.serverUrl + 'server/notice/queryNewNotice',
      type: 'POST',
      apiname: 'notice',
      data: {
        userId: userId,
        orgId: org
      }
    });
  },

  getNoticeDetail(event) {
    var id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/notice/detail/detail?id=' + id
    })
  },

  handleCloseNoticeNav: function () {
    this.setData({
      showNoticeNav: false,
    })
  },

  goOrder(){
    app.gotopage('/pages/repair/repair', { mode: 2 , name: '一键呼叫'});
  },

  goMeeting(){
    wx.navigateTo({
      url: '/pages/meeting/index',
    })
  },

  getUserInfo(){
      app.ajax({
          url: app.globalData.serverUrl + '/User/info',
          type: 'POST',
          apiname:'userInfo',
          data: {

          }
      });
  },

  goOrdersList(e){
    var status = e.currentTarget.dataset.status;
    wx.navigateTo({
      url: '/pages/order/index?status='+status
    })
  }
  
})
