import OEmpty from '@/components/o-empty'
import { orderStatus } from '@/constant'
import { moneyFormat } from '@/helpers/utils'
import { Button, Cell, CellGroup, Grid, GridItem, Image, List } from 'vant'
import { defineComponent, onMounted, reactive, ref } from 'vue'
import { useRoute } from 'vue-router'
import request from '../../request-music'
import styles from '../index.module.less'

export default defineComponent({
  name: 'order',
  emits: ['next'],
  setup() {
    const route = useRoute()
    const form = reactive({
      isClick: false,
      list: [] as any,
      listState: {
        dataShow: true, // 判断是否有数据
        loading: false,
        finished: false
      },
      params: {
        page: 1,
        rows: 20
      }
    })

    // userPaymentOrder/page
    const getList = async () => {
      try {
        if (form.isClick) return
        form.isClick = true
        const res = await request.post('/api-student/userPaymentOrder/page', {
          data: {
            ...form.params,
            bizId: route.query.id
          }
        })
        form.listState.loading = false
        const result = res.data || {}
        // 处理重复请求数据
        if (form.list.length > 0 && result.current === 1) {
          return
        }
        const rows = result.rows || []
        rows.goodsInfos &&
          rows.goodsInfos.forEach((item: any) => {
            const img = item.goodsUrl ? item.goodsUrl.split(',')[0] : ''
            item.goodsUrl = img
          })
        form.list = form.list.concat(rows)
        form.listState.finished = result.current >= result.pages
        form.params.page = result.current + 1
        form.listState.dataShow = form.list.length > 0
        form.isClick = false
      } catch {
        form.listState.dataShow = false
        form.listState.finished = true
        form.isClick = false
      }
    }

    onMounted(() => {
      getList()
    })

    return () => (
      <div style={{ paddingTop: '20px' }}>
        {form.listState.dataShow ? (
          <List
            v-model:loading={form.listState.loading}
            finished={form.listState.finished}
            finishedText=" "
            class={[styles.liveList]}
            onLoad={getList}
            immediateCheck={false}
          >
            {form.list.map((item: any) => (
              <CellGroup inset class={styles.orderCellGroup}>
                <Cell
                  title={item.createTime}
                  titleClass={styles.payTime}
                  value={orderStatus[item.status]}
                  valueClass={
                    item.status === 'WAIT_PAY'
                      ? styles.payStatus
                      : item.status === 'PAID'
                      ? styles.paySuccess
                      : ''
                  }
                ></Cell>
                <Cell border={false} class={styles.imgGroup}>
                  {{
                    title: () => (
                      <Grid border={false}>
                        {item.goodsInfos &&
                          item.goodsInfos.map((goods: any) => (
                            <GridItem>
                              <Image class={styles.img} src={goods.goodsUrl} />
                            </GridItem>
                          ))}
                      </Grid>
                    )
                  }}
                </Cell>
                <Cell class={styles.btnGroup} center>
                  {{
                    title: () => (
                      <span class={styles.btnPrice}>¥{moneyFormat(item.currentPrice)}</span>
                    ),
                    value: () => (
                      <div class={styles.btns}>
                        {/* {item.status === 'WAIT_PAY' && (
                          <>
                            <Button round plain color="#AAAAAA">
                              修改订单
                            </Button>
                            <Button round plain color="#FF8057">
                              继续支付
                            </Button>
                          </>
                        )} */}
                      </div>
                    )
                  }}
                </Cell>
              </CellGroup>
            ))}
          </List>
        ) : (
          <OEmpty btnStatus={false} classImgSize="SMALL" tips="暂无订单" />
        )}
      </div>
    )
  }
})
