import { connect } from 'herculex';
import { prefixPagePath, dynamicPageName } from "../../../../core/utils";
import { scenicOrderList } from '../../services';
import { cancellationOfOrder, payImmediately } from './utils'; // 我的订单
// 状态对应的颜色和说明

const status = [{
  typeTitle: '全部',
  value: '',
  color: '#ff6430',
  title: '全部'
}, {
  typeTitle: '待付款',
  value: '0',
  color: '#ff6430',
  title: '待付款'
}, {
  typeTitle: '出票中',
  value: '1',
  color: '#999',
  title: '处理中'
}, {
  typeTitle: '待使用',
  value: '2',
  color: '#999',
  title: '未出行'
}, {
  typeTitle: '已使用',
  value: '3',
  color: '#ff6430',
  title: '售后'
}, {
  typeTitle: '已取消',
  value: '4',
  color: '#999',
  title: '售后'
}, {
  typeTitle: '退票审核中',
  value: '5',
  color: '#999',
  title: '售后'
}, {
  typeTitle: '已退票',
  value: '6',
  color: '#999',
  title: '售后'
}, {
  typeTitle: '已过期',
  value: '7',
  color: '#999',
  title: '未出行'
}];
const tabs = status.filter((item, i, arr) => i === arr.findIndex(record => record.title === item.title));
Component(connect({
  mapStateToProps: {
    titleBarHeight: state => state.$global.titleBarHeight // axml文件用

  }
})({
  data: {
    activeTab: 0,
    tabs,
    status
  },
  methods: {
    saveRef(ref) {
      this.scrollFetch = ref;
    },

    getTypes() {
      const currentType = tabs[this.data.activeTab].title;
      return status.filter(item => item.title === currentType).map(item => item.value).join('');
    },

    // 列表查询
    onService({ ...parameter
    }) {
      return scenicOrderList({ ...parameter,
        type: this.getTypes()
      });
    },

    // 点击选项卡查询数据
    async onTabClick({
      index
    }) {
      await this.setData({
        activeTab: index
      });
      my.pageScrollTo({
        scrollTop: 0
      });
      this.scrollFetch.fetchList({
        pageIndex: 1,
        type: this.getTypes()
      }, true, true);
    },

    // 取消订单
    async onCancel({
      target: {
        dataset: {
          record
        }
      }
    }) {
      await cancellationOfOrder({
        orderId: record.orderId
      });
      this.onTabClick({
        index: this.data.activeTab
      });
    },

    // 立即支付
    async onPayment({
      target: {
        dataset: {
          record
        }
      }
    }) {
      await payImmediately({
        orderId: record.orderId
      });
      this.onTabClick({
        index: this.data.activeTab
      });
    },

    // 去详情页
    onDetails({
      target: {
        dataset: {
          record
        }
      }
    }) {
      my.navigateTo({
        url: `${prefixPagePath}/${dynamicPageName}/index?orderId=${record.orderId}&pageCode=scenicOrderDetail`
      });
    }

  }
}));