<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="packageName">
          <el-input v-model="queryParams.packageName" 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="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <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="140" />
      <el-table-column label="套餐分钟数" align="center" prop="times" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.times }} 分钟</span>
        </template>
      </el-table-column>
      <el-table-column label="套餐价格" align="center" prop="price" width="120">
        <template slot-scope="scope">
          <span style="color:#E6A23C;font-weight:bold">¥{{ scope.row.price || '0.00' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" type="success" size="small">启用</el-tag>
          <el-tag v-else type="info" size="small">停用</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="formOpen" width="520px" append-to-body>
      <el-form ref="packageForm" :model="form" :rules="rules" label-width="130px" size="small">
        <el-form-item label="套餐名称" prop="packageName">
          <el-input v-model="form.packageName" placeholder="请输入套餐名称" />
        </el-form-item>
        <el-form-item label="套餐分钟数" prop="times">
          <el-input-number v-model="form.times" :min="1" :precision="0" controls-position="right" style="width:100%" />
        </el-form-item>
        <el-form-item label="套餐价格(元)" prop="price">
          <el-input-number v-model="form.price" :min="0" :precision="2" :step="1" controls-position="right" style="width:100%" />
        </el-form-item>
        <el-form-item label="超出单价(元/分钟)" prop="expirePrice">
          <el-input-number v-model="form.expirePrice" :min="0" :precision="4" :step="0.01" controls-position="right" style="width:100%" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="formSubmitting" @click="submitForm">确 定</el-button>
        <el-button @click="formOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVoicePackage, addVoicePackage } from '@/api/admin/voicePackage'

const defaultForm = () => ({
  packageName: null,
  times: null,
  price: null,
  expirePrice: null,
  status: 1,
  remark: null
})

export default {
  name: 'AdminVoicePackage',
  data() {
    return {
      loading: false,
      showSearch: true,
      total: 0,
      packageList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        packageName: null
      },
      formOpen: false,
      formSubmitting: false,
      form: defaultForm(),
      rules: {
        packageName: [{ required: true, message: '套餐名称不能为空', trigger: 'blur' }],
        times: [{ required: true, message: '套餐分钟数不能为空', trigger: 'blur' }],
        price: [{ required: true, message: '套餐价格不能为空', trigger: 'blur' }],
        status: [{ required: true, message: '请选择状态', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listVoicePackage(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()
    },
    handleAdd() {
      this.form = defaultForm()
      this.formOpen = true
      this.$nextTick(() => {
        if (this.$refs.packageForm) this.$refs.packageForm.clearValidate()
      })
    },
    submitForm() {
      this.$refs.packageForm.validate(valid => {
        if (!valid) return
        this.formSubmitting = true
        addVoicePackage({ ...this.form }).then(res => {
          if (res.code === 200) {
            this.msgSuccess('新增成功')
            this.formOpen = false
            this.getList()
          }
        }).finally(() => {
          this.formSubmitting = false
        })
      })
    }
  }
}
</script>

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