<template>
  <div class="app-container">
    <el-card shadow="never" class="mb16 filter-card">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" size="small" label-width="100px">
        <el-form-item label="租户名称" prop="tenantId">
          <inline-tenant-selector :key="tenantSelectorKey" @change="handleTenantChange" />
        </el-form-item>
        <el-form-item label="视频标题" prop="videoTitle">
          <el-input v-model="queryParams.videoTitle" placeholder="请输入视频标题" clearable size="small" @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-table border size="small" style="width:100%" v-loading="loading" :data="list">
      <el-table-column label="租户名称" prop="companyName" min-width="120" />
      <el-table-column label="视频标题" prop="title" :show-overflow-tooltip="true" />
      <el-table-column label="直播ID" prop="liveId" min-width="100" />
      <el-table-column label="播放地址" prop="playUrl" :show-overflow-tooltip="true" />
      <el-table-column label="时长(秒)" prop="duration" min-width="80" />
      <el-table-column label="状态" prop="status" min-width="80" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 1 ? 'success' : 'info'" size="mini">
            {{ scope.row.status === 1 ? '已生成' : '处理中' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime" min-width="150" align="center" />
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import { listAllLiveVideos } from '@/api/admin/liveVideo'
import InlineTenantSelector from '@/components/InlineTenantSelector'

export default {
  name: 'AdminLiveVideo',
  components: { InlineTenantSelector },
  data() {
    return {
      loading: false,
      showSearch: true,
      list: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tenantId: null,
        videoTitle: null
      },
      tenantSelectorKey: 0
    }
  },
  methods: {
    getList() {
      if (!this.queryParams.tenantId) {
        this.list = []
        this.total = 0
        this.loading = false
        return
      }
      this.loading = true
      listAllLiveVideos(this.queryParams).then(res => {
        this.list = res.rows
        this.total = res.total
        this.loading = false
      }).catch(() => { this.loading = false })
    },
    handleTenantChange(val) {
      this.queryParams.tenantId = val || null
      if (val) {
        this.handleQuery()
      }
    },
    handleQuery() {
      if (!this.queryParams.tenantId) {
        this.$message.warning('请先选择租户')
        return
      }
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.$store.dispatch('tenant/setTenantId', null)
      this.resetForm('queryForm')
      this.queryParams.tenantId = null
      this.list = []
      this.total = 0
      this.tenantSelectorKey++
    }
  }
}
</script>

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