const Api = require("../../../../utils/util.js"),
  app = getApp();
Page({
  data: {
    keyWords: '', //关键字
    currentTab: 0,
    tabList: [{
      name: '全部',
      siteStatus: '4,5,6,7',
      num: 0
    }, {
      name: '待接单',
      siteStatus: '4',
      num: 0
    }, {
      name: '服务中',
      siteStatus: '5,6',
      num: 0
    }, {
      name: '已完工',
      siteStatus: '7',
      num: 0
    }],
    pageNum: 1,
    pageSize: 10,   
  },
  onLoad: function (options) {
    var that = this;   
    if(options.tab){
      this.setData({
        currentTab: options.tab
      })
    }
    that.getOrder()    
    that.getNum();   
  },
  onShow: function () {
    var that = this;
    this.setData({
      userId: wx.getStorageSync('userId'),
    })
  }, 
  //搜索
  keyWordsSearch: function (e) {
    this.setData({
      keyWords: e.detail,
      pageNum: 1,
      order: []
    })
    this.getOrder()
  },
  getNum: function () {
    var that = this;
    var data = {}    
    Api.req('sf/operationOrder/orderHomeIndex', data, 'get')
      .then(res => {
        if(res.code == 200){
          that.setData({
            [`tabList[0].num`]: res.data.allCount, //全部
            [`tabList[1].num`]: res.data.djdCount, //待接单
            [`tabList[2].num`]: res.data.fwzCount, //服务中
            [`tabList[3].num`]: res.data.ywcCount, //完成 
          })
        }
      })

  },
  // 全部工单
  getOrder: function () {
    var that = this
    var data = {
      current: this.data.pageNum,
      size: this.data.pageSize,
      smallCondition: this.data.keyWords,
      siteStatus: this.data.tabList[this.data.currentTab].siteStatus,
    }
    wx.showLoading({
      title: '加载中...',
    }) 
    Api.req('sf/operationOrder/page', data, 'get').then(res => {
      wx.stopPullDownRefresh()
      wx.hideLoading()
      if(res.code== 200){
        this.data.loadEnd = res.data.records.length < this.data.pageSize
        if (this.data.pageNum == 1) {
          this.data.records = res.data.records
        } else {
          this.data.records = this.data.records.concat(res.data.records)
        }
        if (!this.data.loadEnd) {
          this.data.pageNum++;
        }
        this.setData({
          order: this.data.records,
          loadEnd: this.data.loadEnd,
        })
      }else{
        wx.showToast({
          title: res.errMsg,
          icon: 'none'
        })
      }
    })
  },
  // 点击切换工单
  swichNav: function (e) {
    this.setData({
      currentTab: e.currentTarget.dataset.current,
      pageNum:1,
      order: []
    })
    this.getOrder()
  },  
  //拨打电话
  call: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.tel
    })
  },
  //订单详情
  orderDetail: function (e) {
    var status = e.currentTarget.dataset.status,
      id = e.currentTarget.dataset.id,
      promiseTime = e.currentTarget.dataset.promisetime,
      jie = e.currentTarget.dataset.jie,
      no = e.currentTarget.dataset.no;
      wx.navigateTo({
        url: '../orderDetail/orderDetail?id=' + id + '&status=' + status + '&promiseTime=' + promiseTime,
      })    
  },
  //上门
  door: function (e) {
    var that = this;
    wx.showModal({
      title: '上门',
      content: '确认上门？',
      success: function (res) {
        if (res.cancel) {} else {
          var data = {
            orderId: e.currentTarget.dataset.id,
            siteId: wx.getStorageSync('siteId'),
            userId: wx.getStorageSync('userId'),
          }
          Api.req('main/order/toVisit', data, 'get')
            .then(res => {
              console.log(res)
              if (res.data.code == '200') {
                that.getOrder4(0, [])
                that.getOrder(0, [])
              } else {
                wx.showToast({
                  title: '上门失败',
                  image: '../../img/error.png',
                  mask: true
                })
              }
            })
        }
      },
      fail: function (res) {}, //接口调用失败的回调函数
      complete: function (res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
    })
  },
  //接单
  jie: function (e) {
    var that = this;
    wx.showModal({
      title: '接单',
      content: '确认接单？',
      success: function (res) {
        if (res.cancel) {} else {
          var data = {
            orderId: e.currentTarget.dataset.id,
          }
          wx.showLoading({
            title: '提交中...',
          }) 
          Api.req('sf/operationOrderSiteDispatch/empReceiveOrde', data, 'post')
            .then(res => {
              wx.hideLoading()
              if (res.code == '200') { 
                wx.showToast({
                  title: '操作成功',
                })
                that.setData({
                  pageNum: 1,
                  order: [],
                });
                that.getOrder()
                that.getNum()
              }else {
                wx.showToast({
                  title: res.msg,
                  icon: 'none'
                })
              }
            })
        }
      },
      fail: function (res) {}, //接口调用失败的回调函数
      complete: function (res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
    })

  },
  
  //反馈
  feedback(e){
    var id = e.currentTarget.dataset.id     
    wx.navigateTo({
      url: '../orderDetail2/orderDetail2?id=' + id,
    })
  },
  /** 下拉刷新 **/
  onPullDownRefresh(e) {
    this.setData({
      pageNum: 1,
      pageSize: 10,
      loadEnd: false,
    })
    this.getOrder() 
    this.getNum();
  },
  /** 页面触底上拉刷新 **/
  onReachBottom(e) {
    if (!this.data.loadEnd) {       
      this.getOrder()
    } else {
      wx.showToast({
        title: '暂无更多数据',
        icon: 'none',
        duration: 500
      })
    }
  },
})