<template>
  <div class="user-order-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input v-model.trim="queryForm.userId" clearable placeholder="请输入User ID" />
          </el-form-item>
          <el-form-item>
            <el-input v-model.trim="queryForm.outTradeNo" clearable placeholder="请输入订单号" />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-table v-loading="listLoading" :data="list" :element-loading-text="elementLoadingText">
      <el-table-column align="center" label="订单号" prop="outTradeNo" show-overflow-tooltip />

      <el-table-column align="center" label="用户信息">
        <template #default="{ row }">
          <div v-if="row.detail" style="display: flex; align-items: center; justify-content: center">
            <img
              v-if="row.detail.avatar"
              :src="row.detail.avatar"
              style="width: 40px; height: 40px; border-radius: 50%; margin-right: 10px; object-fit: cover"
            />
            <div style="text-align: left">
              <div>{{ row.detail.nickname || row.userId }}</div>
              <div style="font-size: 12px; color: #909399">
                ID:
                <el-link type="primary" :underline="false" @click="goToUser(row.userId)">{{ row.userId }}</el-link>
              </div>
            </div>
          </div>
          <el-link v-else type="primary" :underline="false" @click="goToUser(row.userId)">{{ row.userId }}</el-link>
        </template>
      </el-table-column>

      <el-table-column align="center" label="商品描述" prop="description" show-overflow-tooltip />

      <el-table-column align="center" label="金额" prop="totalAmount" width="120">
        <template #default="{ row }">
          <span style="color: #f56c6c; font-weight: bold">¥{{ (row.totalAmount / 100).toFixed(2) }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="支付渠道">
        <template #default="{ row }">
          <el-tag v-if="row.provider === 'wechat'" size="small" type="success">微信</el-tag>
          <el-tag v-else-if="row.provider === 'alipay'" size="small" type="primary">支付宝</el-tag>
          <el-tag v-else-if="row.provider === 'douyin'" size="small" type="warning">抖音</el-tag>
          <span v-else>{{ row.provider }}</span>
          <div style="font-size: 12px; color: #909399; margin-top: 4px">{{ row.channel }}</div>
        </template>
      </el-table-column>

      <el-table-column align="center" label="状态" prop="tradeState" width="120">
        <template #default="{ row }">
          <el-tag v-if="row.tradeState === 'SUCCESS'" type="success">支付成功</el-tag>
          <el-tag v-else-if="row.tradeState === 'REFUND'" type="warning">已退款</el-tag>
          <el-tag v-else-if="row.tradeState === 'NOTPAY'" type="info">未支付</el-tag>
          <el-tag v-else-if="row.tradeState === 'CLOSED'" type="info">已关闭</el-tag>
          <el-tag v-else-if="row.tradeState === 'PAYERROR'" type="danger">支付失败</el-tag>
          <el-tag v-else>{{ row.tradeState }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="VIP计划" prop="vipPlanId" show-overflow-tooltip width="140">
        <template #default="{ row }">
          {{ row.vipPlanId || '-' }}
        </template>
      </el-table-column>

      <el-table-column align="center" label="创建时间" prop="createdAt" width="170">
        <template #default="{ row }">
          {{ formatTime(row.createdAt) }}
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
  import { getUserOrderList } from '@/api/spring/user'
  import { formatTime } from '@/utils'

  export default {
    name: 'UserOrder',
    data() {
      return {
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          userId: '',
          outTradeNo: '',
        },
      }
    },
    created() {
      if (this.$route.query.userId) {
        this.queryForm.userId = this.$route.query.userId
      }
      this.fetchData()
    },
    methods: {
      formatTime,
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        try {
          const { data } = await getUserOrderList(this.queryForm)
          this.list = data.list
          this.total = data.totalCount
        } catch (error) {
          console.error(error)
        } finally {
          this.listLoading = false
        }
      },
      goToUser(userId) {
        this.$router.push({
          path: '/spring/user/user',
          query: { userId },
        })
      },
    },
  }
</script>

<style scoped>
  .user-order-container {
    padding: 20px;
  }
</style>
