import { getHeaderHeight } from '../../utils';
import services from './service';
import { createSubscribe } from 'applet-page-component';
Component(createSubscribe({
  onShow() {
    // 从详情页面返回，默认查询一次
    this.queryFilter();
  }

})({
  props: {
    list: [{
      title: '全部',
      type: 'ALL'
    }, {
      title: '待付款',
      type: 'WAIT_PAY'
    }, {
      title: '待发货',
      type: 'UNSEND'
    }, {
      title: '待提/收货',
      type: 'UNDELIVERED'
    }, {
      title: '已提/收货',
      type: 'DELIVERED'
    }, {
      title: '退款',
      type: 'REFUND'
    }, {
      title: '交易关闭',
      type: 'CLOSE'
    }],
    onChange: () => null
  },

  didMount() {
    const {
      windowHeight,
      titleBarHeight,
      statusBarHeight
    } = my.getSystemInfoSync();
    this.setData({
      contentHeight: windowHeight - titleBarHeight - statusBarHeight
    });
    this.fetchDefaultList();
  },

  data: {
    windowHeight: 0,
    activeTab: 0,
    orderCenterList: [],
    contentHeight: 0,
    headerHeight: getHeaderHeight(),
    tabHeight: 65,
    tabBarHight: 0,
    // 底部提示栏高度
    blockHeight: 24,
    filters: ''
  },
  methods: {
    // 查询一次筛选栏
    queryFilter() {
      const {
        activeTab
      } = this.data;
      this.setData({
        activeTab
      });
      this.onFilterHandel(this.props.list[activeTab].type);
    },

    // 查询默认列表
    fetchDefaultList() {
      const {
        list
      } = this.props;
      const {
        orderStatus = 'ALL'
      } = getCurrentPages().pop().options || {};
      const activeTab = list.findIndex(item => item.type === orderStatus);
      this.setData({
        activeTab,
        filters: orderStatus
      });
    },

    /* 切换筛选栏 */
    handleTabClick({
      index
    }) {
      const {
        activeTab
      } = this.data;
      /* 这里防止重复点击 */

      if (activeTab === index) return;
      this.setData({
        activeTab: index
      });
      this.onFilterHandel(this.props.list[index].type);
    },

    onFilterHandel(filters) {
      this.setData({
        filters
      });
      this.flatRef.refresh();
    },

    saveRef(ref) {
      this.flatRef = ref;
    },

    /* 分页逻辑 */
    async onService(params) {
      const {
        filters
      } = this.data;
      return services.queryForOrderList({
        status: filters,
        ...params
      });
    } // // 获取高度
    // queryHight() {
    //   my.createSelectorQuery()
    //     .select('.tabbar-box.fixed')
    //     .boundingClientRect()
    //     .exec((ret) => {
    //       const tabBarHight = ret[0] ? ret[0].height : 0;
    //       this.setData({
    //         tabBarHight: `${tabBarHight}`,
    //       });
    //     });
    // },


  }
}));