<template>
  <div class="app-container">
    <!-- 搜索栏 -->
    <el-card shadow="never" class="mb16 filter-card">
      <el-form :model="queryParams" ref="queryForm" :inline="true" size="small">
        <el-form-item label="租户名称" prop="tenantId">
          <inline-tenant-selector @change="handleTenantChange" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 操作栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <!-- 表格 -->
    <el-table v-loading="loading" :data="orderList" border size="small" style="width:100%">
      <el-table-column label="订单ID" align="center" prop="orderId" min-width="70" />
      <el-table-column label="所属租户" align="center" prop="companyName" min-width="120" />
      <el-table-column label="套餐名称" align="center" prop="packageName" min-width="120" />
      <el-table-column label="购买条数" align="center" prop="smsCount" min-width="90">
        <template slot-scope="scope">
          <span style="font-weight:bold">{{ scope.row.smsCount || 0 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="金额" align="center" prop="amount" min-width="100">
        <template slot-scope="scope">
          <span style="color:#fa8c16;font-weight:bold">¥{{ scope.row.amount ? Number(scope.row.amount).toFixed(2) : '0.00' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="购买时间" align="center" prop="createTime" min-width="150" />
      <el-table-column label="状态" align="center" min-width="85">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === '0' || scope.row.status === 0" type="success" size="mini">成功</el-tag>
          <el-tag v-else-if="scope.row.status === '1' || scope.row.status === 1" type="warning" size="mini">待支付</el-tag>
          <el-tag v-else-if="scope.row.status === '2' || scope.row.status === 2" type="danger" size="mini">已取消</el-tag>
          <el-tag v-else type="info" size="mini">{{ scope.row.status }}</el-tag>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 详情弹窗 -->
    <el-dialog title="订单详情" :visible.sync="detailVisible" width="550px" append-to-body>
      <el-descriptions :column="2" border size="small">
        <el-descriptions-item label="订单ID">{{ detail.orderId }}</el-descriptions-item>
        <el-descriptions-item label="所属租户">{{ detail.companyName || '-' }}</el-descriptions-item>
        <el-descriptions-item label="套餐名称">{{ detail.packageName || '-' }}</el-descriptions-item>
        <el-descriptions-item label="购买条数">
          <span style="font-weight:bold">{{ detail.smsCount || 0 }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="金额">
          <span style="color:#fa8c16;font-weight:bold">¥{{ detail.amount ? Number(detail.amount).toFixed(2) : '0.00' }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="状态">
          <el-tag v-if="detail.status === '0' || detail.status === 0" type="success" size="mini">成功</el-tag>
          <el-tag v-else-if="detail.status === '1' || detail.status === 1" type="warning" size="mini">待支付</el-tag>
          <el-tag v-else-if="detail.status === '2' || detail.status === 2" type="danger" size="mini">已取消</el-tag>
          <el-tag v-else type="info" size="mini">{{ detail.status }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="购买时间">{{ detail.createTime || '-' }}</el-descriptions-item>
        <el-descriptions-item label="支付方式">{{ detail.payMethod || '-' }}</el-descriptions-item>
        <el-descriptions-item label="交易流水号" :span="2">{{ detail.txnNo || '-' }}</el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">{{ detail.remark || '-' }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/utils/request'
import InlineTenantSelector from '@/components/InlineTenantSelector'

export default {
  name: 'AdminSmsOrder',
  components: { InlineTenantSelector },
  data() {
    return {
      loading: false,
      exportLoading: false,
      showSearch: true,
      total: 0,
      orderList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tenantId: null
      },
      detailVisible: false,
      detail: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      if (!this.queryParams.tenantId) {
        this.orderList = []
        this.total = 0
        this.loading = false
        return
      }
      this.loading = true
      request({
        url: '/admin/sms-order/list',
        method: 'get',
        params: this.queryParams
      }).then(res => {
        this.orderList = res.rows || []
        this.total = res.total || 0
        this.loading = false
      }).catch(() => { this.loading = false })
    },
    handleQuery() {
      if (!this.queryParams.tenantId) {
        this.$message.warning('请先选择租户')
        return
      }
      this.queryParams.pageNum = 1
      this.getList()
    },
    handleTenantChange(val) {
      this.queryParams.tenantId = val || null
      this.handleQuery()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.queryParams.tenantId = null
      this.handleQuery()
    },
    handleDetail(row) {
      request({
        url: '/admin/sms-order/' + row.orderId,
        method: 'get'
      }).then(res => {
        this.detail = res.data || {}
        this.detailVisible = true
      })
    },
    handleExport() {
      this.exportLoading = true
      request({
        url: '/admin/sms-order/export',
        method: 'get',
        params: this.queryParams,
        responseType: 'blob'
      }).then(res => {
        const blob = new Blob([res], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
        const link = document.createElement('a')
        link.href = URL.createObjectURL(blob)
        link.download = '短信订单.xlsx'
        link.click()
        URL.revokeObjectURL(link.href)
      }).catch(() => {
        this.$message.error('导出失败')
      }).finally(() => { this.exportLoading = false })
    }
  }
}
</script>

<style scoped>
.mb8 { margin-bottom: 8px; }
.mb16 { margin-bottom: 16px; }
.filter-card { padding-bottom: 0; }
</style>
