const util = require('../../../utils/util.js');
const app = getApp();
Component({
  properties:{
    show: {
      type: Boolean,
      observer: function (val) {
        this.setData({ show: val });
      }
    },
    info:{
      type:Object,
      observer:function(options){
        if (util.isObjEmpty(options)) {
          return false;
        }
        if (options.packIds && options.aid && options.sid) {
          let data = {};
          data.meals = options.packIds;
          data.aid = options.aid;
          data.sid = options.sid;
          data.cid = options.cid;
          data.vid = options.vid != -1 ? options.vid : "";
          this.setData({ data: data });
        }
        this.getlist(false);
      }
    },
    status:{
      type: Number,
      observer: function (val) {
        this.setData({ presellstatus : val });
      }
    },
    type:{
      type: Number,
      observer: function (val) {
        this.setData({ type: val });
      }
    }
  },
  data: {
    show:false,
    list: [],
    data: Object,
    selectId: 0,
    isLoadMore: true,
    pageIndex: 1,
    pageSize: 10,
    tabStatus:1,
    presellstatus:0,
    type:0,
  },
  methods:{
    getlist: function (load) {
      var self = this,
        url = "v2/order/pay_vouchers",
        data = { "pageIndex": self.data.pageIndex, "pageSize": self.data.pageSize };
      if (self.data.data.meals) {
        url = "v2/order/pay_vouchers";
        data = self.data.data;
        data.pageIndex = self.data.pageIndex;
        data.pageSize = self.data.pageSize;
        data.status = self.data.tabStatus;
        if (self.data.presellstatus != 0) {
          data.otype = self.data.type == 1 ? 2 : 1;
        }else{
          data.otype = 0
        }
      }
      util.ajax({
        func: url,
        data: data,
        load: load
      }, function (res) {
        if (res.code == 0) {
          let isLoadMore = true;
          let newData = self.data.data.meals ? res.data.list : res.data.collects;
          let oldData = !load ? newData : [...self.data.list, ...newData];
          if (newData.length < self.data.pageSize && oldData.length > self.data.pageSize) {
            isLoadMore = false;
          }
          for (let i = 0; i < oldData.length; i++) {
            oldData[i].description = oldData[i].description.replace("<b>", "<b class='b'>");
          }
          self.setData({ list: oldData, isLoadMore: isLoadMore, selectId: data.vid || '' });
        } else
          util.showTips(res.reason);
      });
    },
    onbottom() {
      var self = this;
      if (self.data.isLoadMore == false) return false;
      setTimeout(function () {
        self.setData({ pageIndex: self.data.pageIndex + 1 });
        self.getlist(true);
      }, 1000);
    },
    radioChange: function (e) {
      if (!this.data.data.meals) return false;
      this.setData({ show:false})
      this.triggerEvent("change", e.detail.value)
    },
    notUse(){
      this.setData({ show: false });
      this.triggerEvent("close", this.data.show);
      this.triggerEvent("change", -1);
    },
    tabsChange(e) {
      let tabStatus = e.currentTarget.dataset.status;
      if (tabStatus == this.data.tabStatus) return false;
      this.setData({ tabStatus, list: [], pageIndex:1});
      this.getlist(false)
    },
    close() {
      this.setData({ show: false });
      this.triggerEvent("close", this.data.show);
    }
  },
})