<template>
  <div class="app-container">
    <el-card shadow="never" class="mb16 filter-card">
      <el-form :model="queryParams" ref="queryForm" :inline="true" size="small" label-width="100px">
        <el-form-item label="租户名称" prop="tenantId">
          <inline-tenant-selector @change="handleTenantChange" />
        </el-form-item>
        <el-form-item label="任务名称" prop="taskName">
          <el-input
            v-model="queryParams.taskName"
            placeholder="请输入任务名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
            <el-option label="待启动" :value="0" />
            <el-option label="运行中" :value="1" />
            <el-option label="已停止" :value="2" />
            <el-option label="已完成" :value="3" />
          </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">
      <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="任务名称" align="center" prop="taskName" min-width="140" show-overflow-tooltip />
      <el-table-column label="所属租户" align="center" prop="companyName" 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" type="info">待启动</el-tag>
          <el-tag v-else-if="scope.row.status === 1" type="success">运行中</el-tag>
          <el-tag v-else-if="scope.row.status === 2" type="danger">已停止</el-tag>
          <el-tag v-else-if="scope.row.status === 3" type="warning">已完成</el-tag>
          <el-tag v-else type="info">未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="外呼总数" align="center" prop="callTotal" width="100" />
      <el-table-column label="成功数" align="center" prop="callSuccess" width="100" />
      <el-table-column label="创建时间" align="center" prop="createTime" 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="650px" append-to-body>
      <el-form :model="detailForm" label-width="100px" size="small">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="任务名称">
              <span>{{ detailForm.taskName }}</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="状态">
              <el-tag v-if="detailForm.status === 0" type="info">待启动</el-tag>
              <el-tag v-else-if="detailForm.status === 1" type="success">运行中</el-tag>
              <el-tag v-else-if="detailForm.status === 2" type="danger">已停止</el-tag>
              <el-tag v-else-if="detailForm.status === 3" type="warning">已完成</el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="外呼总数">
              <span>{{ detailForm.callTotal }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="成功数">
              <span>{{ detailForm.callSuccess }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间">
              <span>{{ detailForm.createTime }}</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 request from '@/utils/request'
import InlineTenantSelector from '@/components/InlineTenantSelector'

// admin专用：走 /admin/ 前缀，代理到 fs-admin 8004
function listRobotic(query) {
  return request({ url: '/admin/voice-robotic/list', method: 'get', params: query })
}
function getRobotic(id) {
  return request({ url: '/admin/voice-robotic/' + id, method: 'get' })
}
function exportRobotic(query) {
  return request({ url: '/admin/voice-robotic/export', method: 'get', params: query })
}

export default {
  name: 'AdminVoice',
  components: { InlineTenantSelector },
  data() {
    return {
      loading: true,
      exportLoading: false,
      showSearch: true,
      total: 0,
      dataList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskName: null,
        tenantId: null,
        status: null
      },
      detailOpen: false,
      detailForm: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      if (!this.queryParams.tenantId) {
        this.dataList = []
        this.total = 0
        this.loading = false
        return
      }
      this.loading = true
      listRobotic(this.queryParams).then(response => {
        this.dataList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    handleQuery() {
      if (!this.queryParams.tenantId) {
        this.$message.warning('请先选择租户')
        return
      }
      this.queryParams.pageNum = 1
      this.getList()
    },
    handleTenantChange(val) {
      this.queryParams.tenantId = val || null
      this.handleQuery()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.queryParams.tenantId = null
      this.handleQuery()
    },
    handleDetail(row) {
      getRobotic(row.id).then(response => {
        this.detailForm = response.data
        this.detailOpen = true
      })
    },
    handleExport() {
      if (!this.queryParams.tenantId) {
        this.$message.warning('请先选择租户')
        return
      }
      this.exportLoading = true
      exportRobotic(this.queryParams).then(response => {
        this.download(response.msg)
        this.exportLoading = false
      }).catch(() => {
        this.exportLoading = false
      })
    }
  }
}
</script>
