<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="88px">
      <el-form-item label="企业ID" prop="companyId">
        <el-input v-model="queryParams.companyId" clearable placeholder="可选" style="width: 140px" />
      </el-form-item>
      <el-form-item label="审核状态" prop="isAudit">
        <el-select v-model="queryParams.isAudit" clearable placeholder="全部" style="width: 120px">
          <el-option label="待审核" :value="0" />
          <el-option label="已通过" :value="1" />
          <el-option label="已驳回" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="直播名称" prop="liveName">
        <el-input v-model="queryParams.liveName" clearable placeholder="模糊" style="width: 160px" />
      </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-table v-loading="loading" :data="list" border size="small">
      <el-table-column label="直播间ID" prop="liveId" width="96" align="center" />
      <el-table-column label="直播名称" prop="liveName" min-width="140" show-overflow-tooltip />
      <el-table-column label="企业" prop="companyName" min-width="120" show-overflow-tooltip />
      <el-table-column label="训练营" prop="campName" min-width="100" show-overflow-tooltip />
      <el-table-column label="营期" prop="periodName" min-width="100" show-overflow-tooltip />
      <el-table-column label="审核" width="88" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isAudit === 0" size="mini" type="warning">待审</el-tag>
          <el-tag v-else-if="scope.row.isAudit === 1" size="mini" type="success">通过</el-tag>
          <el-tag v-else-if="scope.row.isAudit === 2" size="mini" type="danger">驳回</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="开播时间" prop="startTime" width="158" align="center" />
      <el-table-column label="操作" width="200" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.isAudit === 0 || scope.row.isAudit === 2"
            v-hasPermi="['live:trainingCampAdmin:audit:edit']"
            type="text"
            size="mini"
            @click="handleAudit(scope.row, true)"
          >通过</el-button>
          <el-button
            v-if="scope.row.isAudit === 0"
            v-hasPermi="['live:trainingCampAdmin:audit:edit']"
            type="text"
            size="mini"
            @click="openReject(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="rejectOpen" width="420px" append-to-body @close="rejectRemark = ''">
      <el-input v-model="rejectRemark" type="textarea" rows="4" maxlength="500" show-word-limit placeholder="选填，写入直播间备注" />
      <div slot="footer" class="dialog-footer">
        <el-button @click="rejectOpen = false">取 消</el-button>
        <el-button type="primary" @click="confirmReject">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTrainingLiveAudit, auditTrainingLive } from '@/api/live/trainingCampAdmin'

export default {
  name: 'TrainingLiveAudit',
  data() {
    return {
      loading: false,
      list: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: undefined,
        isAudit: 0,
        liveName: undefined
      },
      rejectOpen: false,
      rejectRemark: '',
      rejectRow: null
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      const q = { ...this.queryParams }
      if (q.companyId === '' || q.companyId === undefined) {
        delete q.companyId
      } else {
        q.companyId = Number(q.companyId)
      }
      if (q.isAudit === '' || q.isAudit === undefined) {
        delete q.isAudit
      }
      listTrainingLiveAudit(q).then(res => {
        this.list = res.rows || []
        this.total = res.total || 0
        this.loading = false
      }).catch(() => { this.loading = false })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        companyId: undefined,
        isAudit: 0,
        liveName: undefined
      }
      this.getList()
    },
    handleAudit(row, passed) {
      this.$confirm(passed ? '确认审核通过该直播间？' : '确认驳回？', '提示').then(() => {
        return auditTrainingLive({
          liveId: row.liveId,
          passed,
          remark: passed ? undefined : this.rejectRemark
        })
      }).then(() => {
        this.$message.success('操作成功')
        this.getList()
      }).catch(() => {})
    },
    openReject(row) {
      this.rejectRow = row
      this.rejectRemark = ''
      this.rejectOpen = true
    },
    confirmReject() {
      if (!this.rejectRow) return
      auditTrainingLive({
        liveId: this.rejectRow.liveId,
        passed: false,
        remark: this.rejectRemark || undefined
      }).then(() => {
        this.$message.success('已驳回')
        this.rejectOpen = false
        this.getList()
      })
    }
  }
}
</script>
