<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="接口名称" prop="apiName">
        <el-input
          v-model="queryParams.apiName"
          placeholder="请输入接口名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="接口类型" prop="apiType">
        <el-select v-model="queryParams.apiType" placeholder="请选择类型" clearable size="small">
          <el-option label="SIP" :value="0" />
          <el-option label="网关" :value="1" />
          <el-option label="API" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="启用" :value="1" />
          <el-option label="禁用" :value="0" />
        </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-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="list" border style="width: 100%">
      <el-table-column label="接口ID" align="center" prop="apiId" width="90" />
      <el-table-column label="接口名称" align="center" prop="apiName" min-width="100" show-overflow-tooltip />
      <el-table-column label="接口类型" align="center" prop="apiType" width="120">
        <template slot-scope="scope">
          <span>{{ getApiTypeLabel(scope.row.apiType) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="价格(元/分钟)" align="center" prop="salePrice" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.salePrice != null ? scope.row.salePrice : '未定价' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="优先级" align="center" prop="priority" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.priority != null ? scope.row.priority : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="主线路" align="center" prop="isPrimary" width="80">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isPrimary === 1" type="success" size="small">是</el-tag>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0 || scope.row.status === '0'" type="danger" size="small">禁用</el-tag>
          <el-tag v-else type="success" size="small">启用</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
        width="300"
        show-overflow-tooltip
        :formatter="formatRemark"
      />
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </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 { listCompanyVoiceApi } from '@/api/company/companyVoiceApi'

export default {
  name: 'CompanyVoiceApi',
  data() {
    return {
      loading: false,
      showSearch: true,
      total: 0,
      list: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        apiName: null,
        apiType: null,
        status: null
      },
      apiTypeMap: { 0: 'SIP', 1: '网关', 2: 'API', '0': 'SIP', '1': '网关', '2': 'API' }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getApiTypeLabel(type) {
      return this.apiTypeMap[type] || '未知'
    },
    formatRemark(row) {
      return row.remark || '-'
    },
    getList() {
      this.loading = true
      listCompanyVoiceApi(this.queryParams).then(response => {
        this.list = response.rows || []
        this.total = response.total || 0
      }).finally(() => {
        this.loading = false
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
