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

  /**
   * 页面的初始数据
   */
  data: {
    index: 1,
    imageurl: '',
    id: 0,
    custom: {},
    levelname: '',
    addresslist: [],
    showpay: false,
    payWayarray: [],
    payWayindex: 0,
    payWay: 1,
    payAmount: 0,
    paytime: '',
    isfirst: true,
    totalAmount: 0,
    totalDebt: 0,
    totalFreight: 0,
    totalPayed: 0,
    userName: ''
  },

  hidepay() {
    this.setData({
      showpay: false,

    })
  },
  setpayDate(e) {
    this.setData({
      paytime: e.detail.value
    })
  },
  payback() {
    this.setData({
      showpay: true,
      paytime: util.formatShortTime(new Date()),
      payWayindex: 0,
      payWay: 1,
      payAmount: 0
    })
  },
  savepay() {
    util.query('customerpayback/wxapi/create', {
      customer: {
        id: this.data.id,
        onlyID: this.data.onlyID
      },
      payAmount: this.data.payAmount,
      payWay: this.data.payWay,
      createTime: this.data.paytime + ' 00:00:00'
    }, function(res) {
      if (res.code === 10000) {
        util.showToast('保存成功')
        that.getDetail()
        that.hidepay()
      } else {
        util.showToast('保存失败')
      }
    }, 'POST')
  },
  setprice(e) {
    this.setData({
      payAmount: util.PointNum(e.detail.value)
    })
  },
  payWayChange(e) {
    this.setData({
      payWayindex: e.detail.value,
      payWay: this.data.payWayarray[e.detail.value].key,
      payWayname: this.data.payWayarray[e.detail.value].name
    })
  },
  setindex(e) {
    this.setData({
      index: e.currentTarget.dataset.index
    })
  },
  toaddress() {
    wx.navigateTo({
      url: '/pages/order/addAddress/addAddress?customerOnlyId=' + this.data.custom.onlyID
    })
  },
  toediteaddress(e) {
    wx.navigateTo({
      url: '/pages/order/addAddress/addAddress?id=' + e.currentTarget.dataset.id
    })
  },
  getDetail() {
    util.query('customer/wxapi/findbyid', {
      id: this.data.id
    }, function(res) {
      if (res.code === 10000) {
        // var levelname = ''
        // switch (res.data.customerLevel) {
        //   case '1':
        //     levelname = ''
        //     break;

        // }
        var orderda = []
        res.orderList.forEach(function(e) {
          var payWayname = ''
          var pw = that.data.payWayarray
          for (var i = 0; i < pw.length; i++) {
            if (pw[i].key === e.payWay) {
              payWayname = pw[i].name
              break;
            }
          }


          orderda.push({
            time: e.submitDate ? (e.submitDate.split(' ')[0]) : '',
            realReceive: e.realReceive ? e.realReceive : 0,
            debt: e.debt ? e.debt : 0,
            salesPrice: e.salesPrice ? e.salesPrice : 0,
            freight: e.freight ? e.freight : 0,
            payWayname: payWayname
          })
        })
        var payBackList = []
        res.payBackList.forEach(function(e) {
          var payWayname = ''
          var pw = that.data.payWayarray
          for (var i = 0; i < pw.length; i++) {
            if (pw[i].key === e.payWay) {
              payWayname = pw[i].name
              break;
            }
          }

          payBackList.push({
            time: e.createTime ? e.createTime.split(' ')[0] : '',
            payAmount: e.payAmount,
            payWayname: payWayname,
            username: e.userName
          })
        })
        that.setData({
          custom: res.data,
          orderList: orderda,
          payBackList: payBackList,
          totalAmount: res.totalAmount,
          totalDebt: res.totalDebt,
          totalFreight: res.totalFreight,
          totalPayed: res.totalPayed,
        })
        that.getAddressList()
      } else {
        util.showToast('未找到该客户信息')
      }
    })

  },
  getAddressList() {
    util.query('customeraddress/wxapi/listpage', {
      customerOnlyId: this.data.custom.onlyID,
      pageNo: 1,
      pageSize: 9999
    }, function(res) {
      if (res.code === 10000) {
        that.setData({
          addresslist: res.data.content
        })
      } else {
        that.setData({
          addresslist: []
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
    util.checklogin(
      function() {
        var ui = wx.getStorageSync('userinfo')
        if (ui) {
          that.setData({
            user: ui
          })
        }
        if (options.id) {
          that.setData({
            id: options.id,
            onlyID: options.onlyID
          })
          that.getDetail()
        }
        util.getPayWay(function(e) {
          that.setData({
            payWayarray: e
          })
        })
      }
    )

  },


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

  },

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

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

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

  },

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

  },

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

  },

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

  }
})