<template>
  <div class="app-container period-workspace">
    <el-page-header
      class="workspace-page-header"
      @back="$emit('back')"
      content="返回营期列表"
    />
    <el-card class="period-info-card" shadow="never">
      <div class="period-info-body">
        <div class="period-info-main">
          <h2 class="period-info-name">{{ workspace.periodName || '—' }}</h2>
          <p class="period-info-line">
            <span class="label">开始时间</span>
            <span class="value">{{ formatPeriodTime(workspace.startTime) }}</span>
          </p>
          <p class="period-info-line">
            <span class="label">结束时间</span>
            <span class="value">{{ formatPeriodTime(workspace.endTime) }}</span>
          </p>
          <p v-if="workspace.campName" class="period-info-line camp">
            <span class="label">所属训练营</span>
            <span class="value">{{ workspace.campName }}</span>
          </p>
        </div>
        <div class="period-info-cover-col">
          <el-image
            v-if="workspace.periodImgUrl"
            :src="workspace.periodImgUrl"
            fit="cover"
            class="period-info-cover-img"
            :preview-src-list="[workspace.periodImgUrl]"
          >
            <div slot="error" class="period-info-cover-placeholder">
              <span>加载失败</span>
            </div>
          </el-image>
          <div v-else class="period-info-cover-placeholder">
            <i class="el-icon-picture-outline" />
            <span>暂无营期图片</span>
          </div>
        </div>
      </div>
    </el-card>
    <el-tabs v-model="activeTab" class="workspace-tabs">
      <el-tab-pane label="直播间" name="live">
        <el-row class="mb8">
          <el-col :span="24" align="right">
            <el-button
              v-hasPermi="['live:live:add', 'live:trainingCamp:live:add']"
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="$emit('add-live')"
            >新增</el-button>
          </el-col>
        </el-row>
        <el-table
          v-loading="liveLoading"
          :data="liveList"
          border
          size="small"
        >
          <el-table-column label="直播间ID" prop="liveId" width="100" align="center" show-overflow-tooltip />
          <el-table-column label="直播名称" prop="liveName" min-width="140" show-overflow-tooltip />
          
          <el-table-column label="开始时间" min-width="158" align="center" show-overflow-tooltip>
            <template slot-scope="scope">{{ formatPeriodTime(scope.row.startTime) }}</template>
          </el-table-column>
          <el-table-column label="结束时间" min-width="158" align="center" show-overflow-tooltip>
            <template slot-scope="scope">{{ formatPeriodTime(scope.row.finishTime) }}</template>
          </el-table-column>
          <el-table-column label="状态" width="96" align="center">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status == 1" size="mini">待直播</el-tag>
              <el-tag v-else-if="scope.row.status == 2" size="mini" type="danger">直播中</el-tag>
              <el-tag v-else-if="scope.row.status == 3" size="mini" type="info">已结束</el-tag>
              <el-tag v-else-if="scope.row.status == 4" size="mini" type="success">回放中</el-tag>
              <span v-else>—</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200" align="center" fixed="right">
            <template slot-scope="scope">
              <el-button
                v-hasPermi="['live:live:edit']"
                type="text"
                size="mini"
                icon="el-icon-edit"
                @click="$emit('edit-live', scope.row)"
              >修改</el-button>
              <el-button
                v-hasPermi="['live:live:query']"
                type="text"
                size="mini"
                @click="goLiveConfig(scope.row)"
              >配置</el-button>
              <el-button
                type="text"
                size="mini"
                @click="openConsole(scope.row)"
              >控制台</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="liveTotal > 0"
          small
          :total="liveTotal"
          :page.sync="liveQuery.pageNum"
          :limit.sync="liveQuery.pageSize"
          @pagination="loadLiveList"
        />
      </el-tab-pane>
      <el-tab-pane label="数据概览" name="overview">
        <live-statistics-panel
          v-if="activeTab === 'overview' && workspace.periodId"
          :training-period-id="workspace.periodId"
          :key="'camp-stats-' + workspace.periodId"
        />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { listTrainingLive } from '@/api/live/trainingCamp'
import LiveStatisticsPanel from '@/views/live/liveStatistics/LiveStatisticsPanel.vue'

export default {
  name: 'PeriodWorkspace',
  components: { LiveStatisticsPanel },
  props: {
    workspace: {
      type: Object,
      required: true,
      default: () => ({
        periodId: null,
        periodName: '',
        campName: '',
        periodImgUrl: '',
        startTime: null,
        endTime: null
      })
    }
  },
  data() {
    return {
      activeTab: 'live',
      liveLoading: false,
      liveList: [],
      liveTotal: 0,
      liveQuery: { pageNum: 1, pageSize: 10 }
    }
  },
  watch: {
    workspace: {
      deep: true,
      immediate: true,
      handler(val) {
        if (val && val.periodId) {
          this.liveQuery.pageNum = 1
          this.activeTab = 'live'
          this.loadLiveList()
        } else {
          this.liveList = []
          this.liveTotal = 0
        }
      }
    }
  },
  methods: {
    formatPeriodTime(val) {
      if (!val) return '—'
      const s = String(val).replace('T', ' ')
      if (s.length >= 19) {
        return s.slice(0, 10) + ' ' + s.slice(11, 19)
      }
      return s.length >= 16 ? s.slice(0, 16) : s
    },
    loadLiveList() {
      if (!this.workspace.periodId) return
      this.liveLoading = true
      listTrainingLive({
        ...this.liveQuery,
        trainingPeriodId: this.workspace.periodId
      }).then(res => {
        this.liveList = res.rows || []
        this.liveTotal = res.total || 0
        this.liveLoading = false
      }).catch(() => {
        this.liveLoading = false
      })
    },
    /** 父组件在新建直播间成功后调用 */
    refreshLiveList() {
      this.loadLiveList()
    },
    goLiveConfig(row) {
      this.$router.push('/live/liveConfig/' + row.liveId)
    },
    openConsole(row) {
      const r = this.$router.resolve({ path: '/live/liveConsole/' + row.liveId })
      window.open(r.href, '_blank')
    }
  }
}
</script>

<style scoped>
.workspace-page-header {
  margin-bottom: 16px;
}
.period-info-card {
  margin-bottom: 16px;
  border-radius: 8px;
}
.period-info-card >>> .el-card__body {
  padding: 20px 24px;
}
.period-info-body {
  display: flex;
  align-items: stretch;
  justify-content: space-between;
  gap: 24px;
  min-height: 132px;
}
.period-info-main {
  flex: 1;
  min-width: 0;
}
.period-info-name {
  margin: 0 0 16px 0;
  font-size: 20px;
  font-weight: 600;
  color: #303133;
  line-height: 1.3;
}
.period-info-line {
  margin: 0 0 10px 0;
  font-size: 14px;
  color: #606266;
  line-height: 1.5;
}
.period-info-line:last-child {
  margin-bottom: 0;
}
.period-info-line .label {
  display: inline-block;
  width: 88px;
  color: #909399;
}
.period-info-line .value {
  color: #303133;
}
.period-info-line.camp .value {
  font-weight: 500;
}
.period-info-cover-col {
  flex-shrink: 0;
  width: 220px;
  height: 132px;
}
.period-info-cover-img {
  width: 220px;
  height: 132px;
  border-radius: 8px;
  border: 1px solid #ebeef5;
  overflow: hidden;
}
.period-info-cover-img >>> .el-image__inner {
  width: 220px;
  height: 132px;
  object-fit: cover;
}
.period-info-cover-placeholder {
  width: 220px;
  height: 132px;
  border-radius: 8px;
  border: 1px dashed #dcdfe6;
  background: #fafafa;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: #c0c4cc;
  font-size: 13px;
}
.period-info-cover-placeholder i {
  font-size: 36px;
}
.workspace-tabs {
  margin-top: 8px;
}
/* 数据概览内嵌统计页：避免双层 app-container 顶距过大 */
.workspace-tabs >>> .live-statistics-panel-root.app-container {
  padding-top: 0;
}
.mb8 {
  margin-bottom: 8px;
}
</style>
