// pages/order_tabs/orderTabs.js
const getReq = require('../../config.js').getReq
import recordSource from '../../utils/recordSource'
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasmore: false,
    orders: [],
    addTimes: [],
    curpage: 1,
    tabs: [
      {
        name: "全部",
        type: ""
      },
      {
        name: "待付款",
        type: 'state_new'
      },
      {
        name: "待发货",
        type: 'state_pay'
      },
      {
        name: "待收货",
        type: 'state_send'
      },
      {
        name: "已收货",
        type: 'state_success'
      }
    ],
    selectedIndex: 0,
    state_type: '',
    tabs_title: '',
    loading: false,
    fromSource: '',
    firstLoad: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let state_type = options.state_type || this.data.state_type;
    switch (state_type) {
      case '': {
        this.setData({
          selectedIndex: 0,
          state_type,
          tabs_title: '全部'
        })
        break;
      }
      case 'state_new': {
        this.setData({
          selectedIndex: 1,
          state_type,
          tabs_title: '待付款'
        })
        break;
      }
      case 'state_pay': {
        this.setData({
          selectedIndex: 2,
          state_type,
          tabs_title: '待发货'
        })
        break;
      }
      case 'state_send': {
        this.setData({
          selectedIndex: 3,
          state_type,
          tabs_title: '待收货'
        })
        break;
      }
      case 'state_success': {
        this.setData({
          selectedIndex: 4,
          state_type,
          tabs_title: '已收货'
        })
        break;
      }
    }
    this.req_datas(state_type);
  },

  req_datas(type) {
    let fromSource = recordSource(app, `act=member_order&op=list&page=15&curpage=${this.data.curpage}&state_type=${type}&client_type=mini`)
    this.setData({
      loading: true,
      fromSource: app.globalData.fromSource
    });
    wx.showLoading({
      title: '加载中',
    });
    if (type != this.data.state_type) {
      this.setData({
        hasmore: false,
        orders: [],
        addTimes: [],
        curpage: 1,
        state_type: type
      });
    }
    let self = this;
    getReq(app,{
      act: 'member_order',
      op: 'list',
      page: 15,
      curpage: this.data.curpage,
      state_type: type,
      from: fromSource
    }, function (res) {
      let orders = res.datas.orders;
      let addTimes = [];
      let hasmore = false;
      let curpage = self.data.curpage
      for (let item of orders) {
        addTimes.push(self.timeFormat(item.order_info.add_time));
      }
      if (res.datas.mobile_page.hasmore) {
        hasmore = true;
        curpage = self.data.curpage + 1;
      }
      else {
        hasmore = false;
      }
      self.setData({
        hasmore,
        curpage,
        orders: self.data.orders.concat(orders),
        addTimes: self.data.addTimes.concat(addTimes),
        loading: false,
        firstLoad: false
      });

      wx.hideLoading();
    })
  },

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

  },

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

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasmore) {
      this.req_datas(this.data.state_type);
    }
  },

  timeFormat(time) {
    let date = new Date(time * 1000);
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    return `${year}-${month}-${day}`;
  },

  selectTab(e) {
    let index = e.target.dataset.index;
    let type = e.target.dataset.type;
    if (type == this.data.state_type) {
      return;
    }
    else {
      switch (type) {
        case '': {
          this.setData({
            selectedIndex: index,
            tabs_title: '全部'
          })
          break;
        }
        case 'state_new': {
          this.setData({
            selectedIndex: index,
            tabs_title: '待付款'
          })
          break;
        }
        case 'state_pay': {
          this.setData({
            selectedIndex: index,
            tabs_title: '待发货'
          })
          break;
        }
        case 'state_send': {
          this.setData({
            selectedIndex: index,
            tabs_title: '待收货'
          })
          break;
        }
        case 'state_success': {
          this.setData({
            selectedIndex: index,
            tabs_title: '已收货'
          })
          break;
        }
      }
      this.req_datas(type);
    }
  },
  change_order_state(action, order_id, index) {
    let self = this;
    getReq(app,{
      act: 'member_order',
      op: 'change_state',
      act_type: action,
      order_id: order_id
    }, function (res) {
      if (res.code != 200) {
        wx.showToast({
          title: res.message,
          icon: 'none'
        })
      }
      else {
        let orders = self.data.orders;
        let addTimes = self.data.addTimes;
        orders.splice(index, 1);
        addTimes.splice(index, 1);
        self.setData({
          orders,
          addTimes
        })
      }
    })
  },
  order_action(e) {
    const action = e.target.dataset.action;
    const order_id = e.target.dataset.orderid;
    const order_sn = e.target.dataset.sordersn;
    const pay_sn = e.target.dataset.paysn;
    const index = e.target.dataset.index;
    let self = this;
    switch (action) {
      case "if_delete": {
        wx.showModal({
          title: "提示",
          content: "您确定要删除该订单吗？",
          success: function (res) {
            if (res.confirm) {
              self.change_order_state(action, order_id, index);
            }
            else if (res.cancel) {
              return;
            }
          }
        })
        break;
      }
      case "if_deliver": {
        let url = encodeURIComponent("https://passport.lrlz.com/mobile/index.php?act=member_order&op=search_deliver&order_id=");
        app.navigateto(`/pages/webView/webView?url=${url}${order_id}`)
        break;
      }
      case "if_cancel": {
        wx.showModal({
          title: "提示",
          content: "您确定要取消该订单吗？",
          success: function (res) {
            if (res.confirm) {
              self.change_order_state(action, order_id, index);
            }
            else if (res.cancel) {
              return;
            }
          }
        })
        break;
      }
      case "if_receive": {
        wx.showModal({
          title: "提示",
          content: "您确定收货吗？",
          success: function (res) {
            if (res.confirm) {
              self.change_order_state(action, order_id, index);
            }
            else if (res.cancel) {
              return;
            }
          }
        })
        break;
      }
      case "if_payment": {
        app.navigateto(`/pages/orderPaySn/orderPaySn?pay_sn=${pay_sn}`)
      }

    }
  }
})