import request from '@/helpers/request'
import { state } from '@/state'
import { Tab, Tabs } from 'vant'
import { defineComponent } from 'vue'
import styles from './index.module.less'
import List from './list'

export default defineComponent({
  name: 'coupon-container',
  data() {
    return {
      couponCount: {
        total: 0,
        useState: 'USABLE'
      } as any
    }
  },
  async mounted() {
    try {
      // 判断是哪个端
      const url =
        state.platformType === 'STUDENT' ? '/api-student' : '/api-teacher'
      const res = await request.get(`${url}/couponInfo/statInfo`)
      const result = res.data || []
      this.couponCount = result.find(result => result.useState === 'USABLE')
      console.log(result.find(result => result.useState === 'USABLE'))
    } catch {
      // TODO: handle
    }
  },
  render() {
    return (
      <div class={styles.coupons}>
        <Tabs color="#01C1B5" lineWidth={44} sticky>
          <Tab
            title={`可使用${
              this.couponCount.total > 0
                ? '(' + this.couponCount.total + '张)'
                : ''
            }`}
          >
            <List />
          </Tab>
          <Tab title="已使用">
            <List useState="USED" />
          </Tab>
          <Tab title="已失效">
            <List useState="EXPIRED" />
          </Tab>
        </Tabs>
      </div>
    )
  }
})
