<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="status">
          <el-select v-model="queryParams.status" placeholder="全部" clearable style="width:120px">
            <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </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">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <!-- 表格 -->
    <el-table v-loading="loading" :data="packageList" border size="small" style="width:100%">
      <el-table-column label="套餐ID" align="center" prop="packageId" width="80" />
      <el-table-column label="套餐名称" align="center" prop="packageName" min-width="160" />
      <el-table-column label="短信条数" align="center" prop="smsCount" width="120">
        <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="price" width="120">
        <template slot-scope="scope">
          <span style="color:#fa8c16;font-weight:bold">¥{{ scope.row.price ? Number(scope.row.price).toFixed(2) : '0.00' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单价" align="center" width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.smsCount > 0">¥{{ (scope.row.price / scope.row.smsCount).toFixed(4) }}/条</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" width="100">
        <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 type="info" size="mini">停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160" />
    </el-table>

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

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

export default {
  name: 'AdminSmsPackage',
  data() {
    return {
      loading: false,
      showSearch: true,
      total: 0,
      packageList: [],
      statusOptions: [
        { value: '0', label: '启用' },
        { value: '1', label: '停用' }
      ],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: null
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      request({
        url: '/admin/sms-package/list',
        method: 'get',
        params: this.queryParams
      }).then(res => {
        this.packageList = 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()
    }
  }
}
</script>

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