<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="companyName">
          <el-input v-model="queryParams.companyName" placeholder="请输入租户名称" clearable @keyup.enter.native="handleQuery" />
        </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="订单号" align="center" prop="orderNo" min-width="180" />
      <el-table-column label="所属租户" align="center" prop="companyName" min-width="140" />
      <el-table-column label="套餐名称" align="center" prop="packageName" min-width="140" />
      <el-table-column label="金额" align="center" prop="amount" width="120">
        <template slot-scope="scope">
          <span style="color:#E6A23C;font-weight:bold">¥{{ scope.row.amount || '0.00' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="购买时间" align="center" prop="createTime" width="160" />
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === '0' || scope.row.status === 0" type="success" size="small">已支付</el-tag>
          <el-tag v-else-if="scope.row.status === '1' || scope.row.status === 1" type="warning" size="small">待支付</el-tag>
          <el-tag v-else-if="scope.row.status === '2' || scope.row.status === 2" type="info" size="small">已取消</el-tag>
          <el-tag v-else size="small">{{ 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" />
  </div>
</template>

<script>
import { listVoiceOrder, exportVoiceOrder } from '@/api/admin/voiceOrder'

export default {
  name: 'AdminVoiceOrder',
  data() {
    return {
      loading: false,
      exportLoading: false,
      showSearch: true,
      total: 0,
      orderList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: null
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listVoiceOrder(this.queryParams).then(res => {
        this.orderList = res.rows || []
        this.total = res.total || 0
        this.loading = false
      }).catch(() => { this.loading = false })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleExport() {
      this.exportLoading = true
      exportVoiceOrder(this.queryParams).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>
