<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"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" border size="small" style="width:100%">
      <el-table-column label="坐席ID" align="center" prop="callerId" width="80" />
      <el-table-column label="所属租户" align="center" prop="companyName" min-width="140" show-overflow-tooltip />
      <el-table-column label="坐席号码" align="center" prop="callerNumber" min-width="140" />
      <el-table-column label="坐席名称" align="center" prop="callerName" min-width="120" show-overflow-tooltip />
      <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="danger">禁用</el-tag>
          <el-tag v-else type="success">启用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="绑定时间" align="center" prop="bindTime" width="160" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
          >详情</el-button>
        </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="detailOpen" width="600px" append-to-body>
      <el-form :model="detailForm" label-width="100px" size="small">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="坐席ID">
              <span>{{ detailForm.callerId }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属租户">
              <span>{{ detailForm.companyName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="坐席号码">
              <span>{{ detailForm.callerNumber }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="坐席名称">
              <span>{{ detailForm.callerName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-tag v-if="detailForm.status === 0 || detailForm.status === '0'" type="danger">禁用</el-tag>
              <el-tag v-else type="success">启用</el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="绑定时间">
              <span>{{ detailForm.bindTime }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注">
              <span>{{ detailForm.remark || '-' }}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listCompanyVoiceCaller, getCompanyVoiceCaller, exportCompanyVoiceCaller } from '@/api/company/companyVoiceCaller'

export default {
  name: 'AdminVoiceSeat',
  data() {
    return {
      loading: true,
      exportLoading: false,
      showSearch: true,
      total: 0,
      dataList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: null
      },
      detailOpen: false,
      detailForm: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listCompanyVoiceCaller(this.queryParams).then(response => {
        this.dataList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleDetail(row) {
      getCompanyVoiceCaller(row.callerId).then(response => {
        this.detailForm = response.data
        this.detailOpen = true
      })
    },
    handleExport() {
      this.exportLoading = true
      exportCompanyVoiceCaller(this.queryParams).then(response => {
        this.download(response.msg)
        this.exportLoading = false
      }).catch(() => {
        this.exportLoading = false
      })
    }
  }
}
</script>

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