<template>
  <div class="app-container">
    <el-card shadow="never" class="mb16 filter-card">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px" size="small">
      <el-form-item label="租户名称" prop="companyId">
        <el-select
          v-model="queryParams.companyId"
          placeholder="选择租户"
          clearable
          filterable
          size="small"
          style="width: 200px"
        >
          <el-option
            v-for="item in companyList"
            :key="item.companyId"
            :label="item.companyName"
            :value="item.companyId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="直播标题" prop="liveTitle">
        <el-input
          v-model="queryParams.liveTitle"
          placeholder="请输入直播标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="直播状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择直播状态" clearable size="small">
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </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"
          v-hasPermi="['admin:live:list']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-bar-chart"
          size="mini"
          @click="showStatistics = true"
        >直播统计</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border size="small" style="width:100%" v-loading="loading" :data="liveList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="租户名称" align="center" prop="companyName" min-width="120" />
      <el-table-column label="直播标题" align="center" prop="liveTitle" min-width="150" show-overflow-tooltip />
      <el-table-column label="直播封面" align="center" prop="coverUrl" min-width="100">
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.coverUrl"
            style="width: 80px; height: 60px"
            :src="scope.row.coverUrl"
            fit="contain" />
        </template>
      </el-table-column>
      <el-table-column label="主播名称" align="center" prop="anchorName" min-width="90" />
      <el-table-column label="观看人数" align="center" prop="viewerCount" min-width="80" />
      <el-table-column label="开始时间" align="center" prop="startTime" min-width="150" />
      <el-table-column label="直播时长" align="center" prop="duration" min-width="90" />
      <el-table-column label="直播状态" align="center" prop="status" min-width="90">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 0" type="success">直播中</el-tag>
          <el-tag v-else-if="scope.row.status == 1" type="warning">未开始</el-tag>
          <el-tag v-else type="info">已结束</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            plain
            @click="handleView(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="viewOpen" width="600px" append-to-body>
      <el-form :model="viewForm" label-width="100px">
        <el-form-item label="租户名称">
          <span>{{ viewForm.companyName }}</span>
        </el-form-item>
        <el-form-item label="直播标题">
          <span>{{ viewForm.liveTitle }}</span>
        </el-form-item>
        <el-form-item label="直播封面">
          <el-image v-if="viewForm.coverUrl" style="width: 120px; height: 90px" :src="viewForm.coverUrl" fit="contain" />
        </el-form-item>
        <el-form-item label="主播名称">
          <span>{{ viewForm.anchorName }}</span>
        </el-form-item>
        <el-form-item label="观看人数">
          <span>{{ viewForm.viewerCount }}</span>
        </el-form-item>
        <el-form-item label="开始时间">
          <span>{{ viewForm.startTime }}</span>
        </el-form-item>
        <el-form-item label="直播时长">
          <span>{{ viewForm.duration }}</span>
        </el-form-item>
        <el-form-item label="直播状态">
          <el-tag v-if="viewForm.status == 0" type="success">直播中</el-tag>
          <el-tag v-else-if="viewForm.status == 1" type="warning">未开始</el-tag>
          <el-tag v-else type="info">已结束</el-tag>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 统计弹窗 -->
    <el-dialog title="直播统计" :visible.sync="showStatistics" width="600px" append-to-body>
      <el-card>
        <el-row :gutter="20">
          <el-col :span="12">
            <div class="stat-item">
              <span class="stat-value">{{ statistics.totalCount }}</span>
              <span class="stat-label">直播总数</span>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="stat-item">
              <span class="stat-value">{{ statistics.totalViewerCount }}</span>
              <span class="stat-label">总观看人数</span>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="stat-item">
              <span class="stat-value">{{ statistics.livingCount }}</span>
              <span class="stat-label">直播中</span>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="stat-item">
              <span class="stat-value">{{ statistics.endedCount }}</span>
              <span class="stat-label">已结束</span>
            </div>
          </el-col>
        </el-row>
      </el-card>
    </el-dialog>
  </div>
</template>

<script>
import { listAllLives, getLiveInfo, getLiveStatistics, exportAllLives } from '@/api/admin/live'
import { listAllCompanies } from '@/api/admin/sysCompany'

export default {
  name: 'LiveAdmin',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 直播列表
      liveList: [],
      // 租户列表
      companyList: [],
      // 统计数据
      statistics: {
        totalCount: 0,
        totalViewerCount: 0,
        livingCount: 0,
        endedCount: 0
      },
      // 状态选项
      statusOptions: [
        { value: 0, label: '直播中' },
        { value: 1, label: '未开始' },
        { value: 2, label: '已结束' }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        liveTitle: null,
        status: null
      },
      // 详情弹窗
      viewOpen: false,
      viewForm: {},
      // 统计弹窗
      showStatistics: false
    }
  },
  created() {
    this.getCompanyList()
    this.getList()
  },
  methods: {
    /** 获取租户列表 */
    getCompanyList() {
      listAllCompanies({ pageSize: 9999 }).then(response => {
        this.companyList = response.rows || []
      })
    },
    /** 查询列表 */
    getList() {
      this.loading = true
      listAllLives(this.queryParams).then(response => {
        this.liveList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryParams.companyId = null
      this.handleQuery()
    },
    /** 查看详情 */
    handleView(row) {
      getLiveInfo(row.liveId).then(response => {
        this.viewForm = response.data
        this.viewOpen = true
      })
    },
    /** 查看统计 */
    showStatisticsDialog() {
      getLiveStatistics().then(response => {
        this.statistics = response.data
        this.showStatistics = true
      })
    },
    /** 导出按钒操作 */
    handleExport() {
      this.exportLoading = true
      exportAllLives(this.queryParams).then(response => {
        this.download(response.msg)
        this.exportLoading = false
      }).catch(() => { this.exportLoading = false })
    }
  }
}
</script>

<style scoped>
.stat-item {
  text-align: center;
  padding: 20px;
  background: #f5f7fa;
  border-radius: 8px;
}
.stat-value {
  font-size: 24px;
  font-weight: bold;
  color: #1890ff;
}
.stat-label {
  display: block;
  margin-top: 5px;
  font-size: 14px;
  color: #666;
}
.mb16 { margin-bottom: 16px; }
.filter-card { padding-bottom: 0; }
</style>