<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 @change="handleTenantChange" />
        </el-form-item>
        <el-form-item label="视频名称" prop="videoName">
          <el-input v-model="queryParams.videoName" placeholder="请输入视频名称" clearable size="small" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label-width="0px">
          <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" width="160" />
      <el-table-column label="视频名称" prop="videoName" :show-overflow-tooltip="true" />
      <el-table-column label="视频类型" prop="videoType" width="100" />
      <el-table-column label="播放地址" prop="playUrl" :show-overflow-tooltip="true" />
      <el-table-column label="时长(秒)" prop="duration" width="100" />
      <el-table-column label="状态" prop="status" 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" width="160" 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 { listAllVideoResources } from '@/api/admin/videoResource'
import InlineTenantSelector from '@/components/InlineTenantSelector'

export default {
  name: 'AdminVideoResource',
  components: { InlineTenantSelector },
  data() {
    return {
      loading: false,
      showSearch: true,
      list: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tenantId: null,
        videoName: null
      }
    }
  },
  methods: {
    getList() {
      if (!this.queryParams.tenantId) {
        this.list = []
        this.total = 0
        this.loading = false
        return
      }
      this.loading = true
      listAllVideoResources(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
      this.handleQuery()
    },
    handleQuery() {
      if (!this.queryParams.tenantId) {
        this.$message.warning('请先选择租户')
        return
      }
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.queryParams.tenantId = null
      this.handleQuery()
    }
  }
}
</script>

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