const config = require('../../config/config.js')
const Http = require('../../utils/HttpBasics.js')
const util = require('../../utils/util.js')
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    couponOrderStatus:0,
    idss: 1,
    pageNum: 1,
    seekFlag:"",
    selectArray: [
    {
      "id": "0",
      "value": "全部"
    }, 
    {
      "id": "1",
      "value": "商品名称搜索"
    }, {
      "id": "2",
      "text": "用户手机号搜索"
    }],
    tabTxt: [{
        title: '待发货',
        idss: 1,
        couponorderstatus:0,
      },
      {
        title: '已发货',
        idss: 2,
        couponorderstatus: 0,
      },
      {
        title: '已完成',
        idss: 3,
        couponorderstatus: 1,
      },
      {
        title: '全部',
        idss: 100,
        couponorderstatus: "",
      }
    ],
    list:[],
    pageNum: 1,
    title:"",
    cUserPhone:"",
    value:"",
  },
  setValue(e){
    let value = e.detail.value
    if(this.data.seekFlag==0){
      this.setData({
        title: "",
        cUserPhone: "",
      })
    }else if (this.data.seekFlag == 1){
      this.setData({
        title: value,
        cUserPhone: "",
      })
    }else{
      this.setData({
        title: "",
        cUserPhone: value,
      })
    }
  },
  gokuaidi(e) {
    let nu = e.currentTarget.dataset.nu
    wx.navigateToMiniProgram({
      appId: 'wx6885acbedba59c14',
      path: `pages/result/result?nu=${nu}&com=&querysource=third_xcx`
    })
  },
  search(e){
    if (e.currentTarget.dataset.pagenum == 1) {
      this.setData({
        pageNum: 1
      })
    }
    this.getList()
  },
  getList(){
    let data = {
      pageNum: this.data.pageNum,
      pageSize: 7,
      shippingStatus:this.data.idss,
      couponOrderStatus: this.data.couponOrderStatus
    }
    if (this.data.seekFlag==1){
      data.title = this.data.title 
    } else if (this.data.seekFlag == 2){
      data.cUserPhone = this.data.cUserPhone
    }
    Http.get({
      url: config.api.goodsList,
      data
    }).then(res=>{
      let tempList = res.data.list
      tempList.map(item=>{
        item.createDate = util.formatTime(item.createDate, 'yyyy-MM-dddd hh:mm:ss')
        if (item.deliveryInfo){
          item.deliveryInfo = JSON.parse(item.deliveryInfo)
          
        }
        
      })
      let thenList = this.data.list
      let allLsit = [...thenList,...tempList]
      if(this.data.pageNum>1){
        this.setData({
          list: allLsit
        })

      }else{
        // tempList.map(item => {
        //   item.createDate = util.formatTime(item.createDate, 'yyyy-MM-dddd hh:mm:ss')
        // })
        this.setData({
          list: tempList
        })
      }
    }).catch(err=>{
      wx.showToast({
        title: err.message,
        icon:"none"
      })
    })
  },

  select: function(e) {

    this.setData({
      seekFlag:e.detail.id,
      title: "",
      cUserPhone: "",
      value:"",
    })    
  },

  // 选项卡
  filterTab: function(e) {
    let that = this;
    that.setData({
      idss: e.target.dataset.idss || e.currentTarget.dataset.idss,
      pageNum: 1,
      title: "",
      cUserPhone: "",
      value: "",
      pageNum:1,
      couponOrderStatus: e.currentTarget.dataset.couponorderstatus
    })
    this.getList()
  },
  goShipments(e){
    let id = e.currentTarget.dataset.id 
    wx.navigateTo({
      url: `/pages/deliveryDetails/deliveryDetails?id=${id}`,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    console.log("到底了")
    this.setData({
      pageNum: this.data.pageNum+1
    })
    this.getList()
  },

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

  }
})