let util = require('../../../utils/util.js');
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgurl: util.imgurl,
    baseImgUrl: util.baseImgUrl,
    data: [],
    isfirst: true,
    customs: [],
    customsindex: 0,
    customsel: '全部客户',
    us: [],
    usindex: 0,
    usname: '全部销售',
    showpay: false,
    deliveryTypeArray: [{
      key: 1,
      name: '快递'
    }, {
      key: 2,
      name: '上门'
    }, {
      key: 3,
      name: '自取'
    }],
    deliveryTypeIndex: 0,
    param: {
      pageNo: 1,
      pageSize: 99999,
      orCondition: ''
    }
  },
  tosettlement(e) {
    wx.navigateTo({
      url: '/pages/stock/settlement/settlement?id=' + e.currentTarget.dataset.id,
    })
  },

  dateChange(e) {
    console.log(e)
    var data = this.data.param
    data.dim = e.detail.type + ''
    if (data.dim === '1') {
      data.day = e.detail.year + '-' + e.detail.month + '-' + e.detail.day
      data.month = ''
      data.year = ''
    } else if (data.dim === '2') {
      data.month = e.detail.year + '-' + e.detail.month
      data.day = ''
      data.year = ''
    } else if (data.dim === '3') {
      data.year = e.detail.year
      data.month = ''
      data.day = ''
    } else if (data.dim === '4') {
      data.beginDate = e.detail.stime
      data.endDate = e.detail.etime
    }

    this.setData({
      param: data
    })
    if (this.data.nowbook) {
      this.getList()
    } else {
      this.getDefaultBook()
    }

  },
  scan(e) {
    wx.scanCode({
      success(res) {
        console.log(res)
        if (res && res.errMsg === 'scanCode:ok') {
          var param = that.data.param
          param.orCondition = res.result
          that.setData({
            names: res.result,
            param: param
          })
          that.getList()
        } else {
          util.showToast('未识别出正确的条码')
        }
      },
      fail(res) {

      }
    })
  },
  search(e) {
    var param = this.data.param
    param.orCondition = e.detail.value
    this.setData({
      names: e.detail.value,
      param: param
    })
    this.getList()
  },
  setbook(e) {
    var param = this.data.param
    param.accountBookId = this.data.books[e.detail.value].id
    delete param.userInfoId
    delete param.customId
    var uf = this.data.books[e.detail.value].userInfos

    this.setData({
      nowbook: this.data.books[e.detail.value],
      param: param,
      us: uf,
      usindex: 0
    })
    this.getList()
    that.getAllCustom()
  },
  setcourierCompany(e) {
    this.setData({
      courierCompany: e.detail.value
    })
  },
  setcourierNumber(e) {
    this.setData({
      courierNumber: e.detail.value
    })
  },
  deliveryChange(e) {
    this.setData({
      deliveryTypeIndex: e.detail.value
    })
  },
  hideSendInfo() {
    this.setData({
      showpay: false
    })
  },
  showSendInfo(e) {
    this.setData({
      showpay: true,
      courierCompany: '',
      courierNumber: '',
      noworderid: e.currentTarget.dataset.id,
      deliveryTypeIndex: 0
    })
  },
  setsend(data) {
    // if (e.currentTarget.data.iscus !== 1) {
    //   wx.navigateTo({
    //     url: '/pages/order/choiseAddress/choiseAddress',
    //   })
    //   return
    // }


    util.query('order/wxapi/setsend?orderId=' + this.data.noworderid + '&deliveryType=' + this.data.deliveryTypeArray[this.data.deliveryTypeIndex].key + '&courierCompany=' + this.data.courierCompany + '&courierNumber=' + this.data.courierNumber, {}, function(res) {
      if (res.code === 10000) {
        util.showToast('设置成功')
        var da = that.data.data
        for (var i = 0; i < da.length; i++) {
          if (da[i].id === that.data.noworderid) {
            da[i].isSend = 1
            break
          }
        }
        that.setData({
          data: da,
          showpay: false
        })

      } else {
        util.showToast('设置失败')
        that.hideSendInfo()
      }
    }, 'post')
  },
  setreceive(e) {
    util.query('order/wxapi/setreceive?orderId=' + e.currentTarget.dataset.id, {}, function(res) {
      if (res.code === 10000) {
        util.showToast('设置成功')
        var da = that.data.data
        for (var i = 0; i < da.length; i++) {
          if (da[i].id === e.currentTarget.dataset.id) {
            da[i].isReceivables = 1
            break
          }
        }
        that.setData({
          data: da
        })

      } else {
        util.showToast('设置失败')
      }
    }, 'post')
  },
  setpayed(e) {
    util.query('order/wxapi/setpayed?orderId=' + e.currentTarget.dataset.id, {}, function(res) {
      if (res.code === 10000) {
        util.showToast('设置成功')
        var da = that.data.data
        for (var i = 0; i < da.length; i++) {
          if (da[i].id === e.currentTarget.dataset.id) {
            da[i].isPayment = 1
            break
          }
        }
        that.setData({
          data: da
        })

      } else {
        util.showToast('设置失败')
      }
    }, 'post')
  },
  tochoise() {
    wx.navigateTo({
      url: '/pages/stock/choise/choise',
    })
  },
  getList() {
    console.log(this.data.param)
    util.query('order/wxapi/listpage', this.data.param, function(res) {
      if (res.code === 10000) {
        res.data.forEach(function(e) {
          e.submitDate = e.submitDate ? (e.submitDate.split(' ')[0]) : ''
        })
        that.setData({
          data: res.data
        })
      } else {
        that.setData({
          data: []
        })
      }
    })
  },

  setus(e) {
    var param = this.data.param
    if (this.data.us[e.detail.value].id === 0) {
      delete param.userInfoId
    } else {
      param.userInfoId = this.data.us[e.detail.value].id
    }
    this.setData({
      usindex: e.detail.value,
      usname: this.data.us[e.detail.value].wechatName,
      param: param
    })
    this.getList()

  },
  getDefaultBook() {
    util.query('accountbook/wxapi/getdefault', {}, function(res) {
      if (res.code === 10000) {
        res.data.userInfos.unshift({
          id: 0,
          wechatName: '全部销售'
        })
        var param = that.data.param
        param.accountBookId = res.data.id
        that.setData({
          us: res.data.userInfos,
          nowbook: res.data,
          param: param
        })
        that.getList()
        that.getBookList()
        that.getAllCustom()
      }
    })
  },
  getBookList() {
    util.query('accountbook/wxapi/list', {
      pageNo: 1,
      pageSize: 9999
    }, function(res) {
      if (res.code === 10000) {

        res.data.forEach(function(b) {
          b.userInfos.unshift({
            id: 0,
            wechatName: '全部销售'
          })
        })
        that.setData({
          books: res.data
        })
      } else {
        that.setData({
          books: []
        })
      }
    })
  },
  setcustomsel(e) {
    var param = this.data.param
    if (this.data.customs[e.detail.value].id === 0) {
      delete param.customId
    } else {
      param.customId = this.data.customs[e.detail.value].id
    }
    this.setData({
      customsindex: e.detail.value,
      customsel: this.data.customs[e.detail.value].name,
      param: param
    })
    this.getList()
  },
  getAllCustom() {
    util.query('order/wxapi/findCustomByAccountBookId', {
      accountBookId: this.data.nowbook.id
    }, function(res) {
      if (res.code === 10000) {
        var da = []
        da.push({
          id: 0,
          name: '全部客户'
        })
        res.data.forEach(function(e) {
          if (e) {
            da.push({
              id: e.id,
              onlyID: e.onlyID,
              name: e.name

            })

          }

        })
        that.setData({
          customs: da,
          customsindex: 0
        })
      } else {
        that.setData({
          customs: [{
            id: 0,
            name: '全部客户'
          }],
          customsindex: 0
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
    util.checklogin(
      function() {

      }
    )
    var ui = wx.getStorageSync('userinfo')
    if (ui) {
      this.setData({
        uid: ui.uid
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (!this.data.isfirst) {
      that.getAllCustom()
      that.getDefaultBook()
    }
  },

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

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

  },

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

  },

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

  },

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

  }
})