<template>
  <div class="orders-page">
    <div class="filter-bar">
      <el-radio-group v-model="activeFilter" @change="doFilter">
        <el-radio-button value="全部">全部</el-radio-button>
        <el-radio-button value="未支付">未支付</el-radio-button>
        <el-radio-button value="已取消">已取消</el-radio-button>
        <el-radio-button value="已完成">已完成</el-radio-button>
      </el-radio-group>
    </div>

    <div v-loading="orderStore.loading" class="order-list">
      <div v-for="order in orderStore.orders" :key="order.id" class="order-card">
        <div class="order-top">
          <span class="order-title">{{ order.postTitle }}</span>
          <el-tag :type="statusType(order.status)" size="small">{{ order.status }}</el-tag>
        </div>
        <div class="order-meta">
          <span>{{ order.expertName }}</span>
          <span class="order-amount">¥{{ order.amount }}</span>
        </div>
        <div class="order-bottom">
          <span class="order-time">{{ order.createdAt }}</span>
          <el-button v-if="order.status === '未支付'" size="small" type="primary">去支付</el-button>
        </div>
      </div>
      <EmptyState v-if="!orderStore.loading && orderStore.orders.length === 0" description="暂无订单" />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useOrderStore } from '../store'
import EmptyState from '../components/EmptyState.vue'

const orderStore = useOrderStore()
const activeFilter = ref('全部')

function doFilter() {
  orderStore.fetchOrders(activeFilter.value)
}

function statusType(status: string): string {
  if (status === '已完成') return 'success'
  if (status === '未支付') return 'warning'
  if (status === '已取消') return 'info'
  return ''
}

onMounted(() => {
  orderStore.fetchOrders()
})
</script>

<style scoped>
.orders-page {
  padding: 0;
}
.filter-bar {
  padding: 12px 16px;
  overflow-x: auto;
}
.order-list {
  padding: 0 16px;
}
.order-card {
  background: var(--color-card, #fff);
  border-radius: 8px;
  padding: 12px 16px;
  margin-bottom: 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}
.order-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
}
.order-title {
  font-size: 14px;
  font-weight: 600;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-right: 8px;
}
.order-meta {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  color: var(--color-text-secondary, #999);
  margin-bottom: 6px;
}
.order-amount {
  color: var(--color-primary, #db2777);
  font-weight: 600;
}
.order-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.order-time {
  font-size: 12px;
  color: var(--color-text-secondary, #999);
}
</style>
