<template>
  <div class="app-container">
    <div class="title">近期直播</div>
    <div class="live-container">
      <div class="live-card" v-for="live in displayLives" :key="live.id">
        <!-- 直播状态 -->
        <div class="status">已结束</div>

        <!-- 直播信息 -->
        <div class="content">
          <img :src="live.image" alt="直播封面" class="cover-image" />
          <div class="info">
            <h3 class="live-title">{{ live.title }}</h3>
            <p class="time">开播时间: {{ live.time }}</p>
          </div>
        </div>

        <!-- 直播数据 -->
        <div class="stats">
          <div class="stat-item">
            <p class="label">直播间浏览量</p>
            <p class="value">{{ live.views }}</p>
          </div>
          <div class="stat-item">
            <p class="label">直播间访客数</p>
            <p class="value">{{ live.visitors }}</p>
          </div>
        </div>
      </div>
    </div>
    <!-- 直播趋势统计 -->
    <div class="trend-section">
      <div class="trend-header">
        <h3>直播趋势</h3>
        <div class="filter-container">
        <!-- 时间范围选择（下拉框） -->
          <span class="filter-label">时间筛选：</span>
          <el-select v-model="selectedTimeRange" placeholder="选择时间范围" @change="changeTimeRange" style="width: 180px">
            <el-option label="自然天" value="day"></el-option>
            <el-option label="自然周" value="week"></el-option>
            <el-option label="自然月" value="month"></el-option>
          </el-select>

          <!-- 日期选择器 -->
          <!-- 选择日期 -->
          <el-date-picker
            v-model="selectedDate"
            :type="datePickerType"
            :format="selectedTimeRange === 'week' ? weekRange : dateFormat"
            :value-format="valueFormat"
            placeholder="选择日期"
            style="width: 280px"
            @change="changeDate"
          ></el-date-picker>
        </div>
      </div>

      <div class="trend-cards">
        <div
          class="trend-card"
          v-for="item in trendData"
          :key="item.id"
          :class="{ 'active': selectedMetric.id === item.id }"
          @click="changeMetric(item)"
        >
          <div class="trend-header">
            <p class="trend-title">{{ item.title }}</p>
            <el-tooltip class="item" effect="dark" :content="item.tip" placement="top">
              <i class="el-icon-info"></i>
            </el-tooltip>
          </div>
          <p class="trend-value">{{ item.value }}</p>
          <p class="trend-change" :class="{ 'up': computedChange(item) > 0, 'down': computedChange(item) < 0 }">
            {{ changeLabel }} {{ computedChange(item) }}%
          </p>
        </div>
      </div>

      <!-- 直播趋势折线图 -->
      <div id="liveChart" class="chart"></div>
    </div>
    <div class="top10-section">
      <h3>直播TOP10排行榜</h3>
      <div class="ranking-container">
          <!-- 红榜 -->
          <div class="ranking-section red-rank">
            <span class="rank-title">红榜</span>
            <div class="rank-filters">
              <button
                v-for="item in redRankTypes"
                :key="item.value"
                :class="{ active: selectedRank === item.value }"
                @click="selectRank(item.value)"
              >
                {{ item.label }}
              </button>
            </div>
          </div>

          <!-- 黑榜 -->
          <div class="ranking-section black-rank">
            <div class="rank-filters">
              <button
                v-for="item in blackRankTypes"
                :key="item.value"
                :class="{ active: selectedRank === item.value }"
                @click="selectRank(item.value)"
              >
                {{ item.label }}
              </button>
            </div>
            <span class="rank-title">黑榜</span>
          </div>
      </div>
      <el-table :data="top10List" border style="width: 100%">
        <el-table-column prop="rank" label="排名" width="80"></el-table-column>
        <el-table-column prop="name" label="直播名称">
          <template #default="scope">
            <div class="live-name">
              <img :src="scope.row.cover" class="live-cover" alt="封面">
              <span class="live-title">{{ scope.row.name }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="pv" label="直播间浏览量(PV)"></el-table-column>
        <el-table-column prop="uv" label="直播间访客数(UV)"></el-table-column>
        <el-table-column prop="watchPV" label="累计观看人次(PV)"></el-table-column>
        <el-table-column prop="watchUV" label="累计观看人数(UV)"></el-table-column>
        <el-table-column prop="avgTime" label="人均观看时长"></el-table-column>
        <el-table-column prop="maxOnline" label="最高在线人数"></el-table-column>
      </el-table>
    </div>
  </div>
</template>


<script>
  import * as echarts from "echarts";
  import { listLive, getLive, delLive, addLive, updateLive, exportLive } from "@/api/live/live";
  import Editor from '@/components/Editor/wang';
  import articleDetails from "@/views/components/his/doctorArticleDetails.vue";

  export default {
    name: "Live",
    components: { Editor },
    data() {
      return {
        top10List: [
          {
            rank: 1,
            name: "弘珍医药年末会员福利专场！",
            cover:"https://cos.his.cdwjyyh.com/fs/20250304/710ea5b1896749b58438b76baf881d05.jpeg",
            pv: 88332,
            uv: 32674,
            watchPV: 41866,
            watchUV: 24714,
            avgTime: "00:13:12",
            maxOnline: 4317,
          },
          {
            rank: 2,
            name: "立秋养生大作战",
            cover:"https://cos.his.cdwjyyh.com/fs/20250304/710ea5b1896749b58438b76baf881d05.jpeg",
            pv: 55092,
            uv: 15066,
            watchPV: 33522,
            watchUV: 12343,
            avgTime: "01:18:30",
            maxOnline: 3160,
          },
          {
            rank: 3,
            name: "寒露养生大挑战：防寒防燥防疾病",
            cover:"https://cos.his.cdwjyyh.com/fs/20250304/710ea5b1896749b58438b76baf881d05.jpeg",
            pv: 36044,
            uv: 12205,
            watchPV: 26056,
            watchUV: 11086,
            avgTime: "01:16:08",
            maxOnline: 3232,
          },
        ],
        selectedRank: 'highFlow',
        redRankTypes: [
          { label: '高流量', value: 'highFlow' },
          { label: '高观看', value: 'highView' },
          { label: '高时长', value: 'highTime' },
        ],
        blackRankTypes: [
          { label: '低时长', value: 'lowTime' },
          { label: '低观看', value: 'lowView' },
          { label: '低流量', value: 'lowFlow' },
        ],
        chart: null,
        selectedTimeRange: "day", // 默认选中“自然天”
        datePickerType: "date", // 默认 date 类型
        trendData: [], // 直播趋势数据
        weekRange: "", // 存储选择周的时间范围
        selectedMetric: {}, // 选中的指标
        selectedDate: new Date().toISOString().split("T")[0], // 默认今天
        selectedMetric: "views", // 默认选中浏览量
        lives: [
          {
            id: 1,
            image: "https://cos.his.cdwjyyh.com/fs/20250117/d9e3b268e3834a2497e0bb1f900eac90.jpg",
            title: "李悦的直播",
            time: "2024-10-10 15:00:00",
            views: 352821,
            visitors: 14505,
          },
          {
            id: 2,
            image: "https://cos.his.cdwjyyh.com/fs/20250117/d9e3b268e3834a2497e0bb1f900eac90.jpg",
            title: "小付的直播",
            time: "2024-10-12 18:00:00",
            views: 284210,
            visitors: 10234,
          },
          {
            id: 3,
            image: "https://cos.his.cdwjyyh.com/fs/20250117/d9e3b268e3834a2497e0bb1f900eac90.jpg",
            title: "生活妙招分享",
            time: "2024-11-05 20:00:00",
            views: 182401,
            visitors: 8734,
          },
          {
            id: 4,
            image: "https://cos.his.cdwjyyh.com/fs/20250117/d9e3b268e3834a2497e0bb1f900eac90.jpg",
            title: "家庭健康指南",
            time: "2024-12-01 14:00:00",
            views: 254321,
            visitors: 12345,
          },
          {
            id: 5,
            image: "https://cos.his.cdwjyyh.com/fs/20250117/d9e3b268e3834a2497e0bb1f900eac90.jpg",
            title: "额外的直播",
            time: "2024-12-15 19:00:00",
            views: 100000,
            visitors: 5000,
          }
        ],
        trendData: [
          {
            id: "views",
            title: "直播间浏览量",
            tip: "直播间总浏览量",
            value: 352421,
            dailyChange:1,
            weeklyChange:2,
            monthlyChange:3,
            change: 5.2,
            dates: ["02-02", "02-04", "02-06", "02-08", "02-10"],
            data: [10, 20, 15, 25, 30],
          },
          {
            id: "visitors",
            title: "直播间访客数",
            tip: "直播间访客数",
            value: 14505,
            dailyChange:1,
            weeklyChange:2,
            monthlyChange:3,
            change: 5.2,
            dates: ["02-02", "02-04", "02-06", "02-08", "02-10"],
            data: [5, 8, 6, 10, 12],
          },
          {
            id: "streams",
            title: "创建直播数",
            tip: "每日直播场次",
            value: 20,
            dailyChange:1,
            weeklyChange:2,
            monthlyChange:3,
            change: 5.2,
            dates: ["02-02", "02-04", "02-06", "02-08", "02-10"],
            data: [1, 2, 1, 3, 2],
          },
          {
            id: "pv",
            title: "累计观看人次（PV）",
            tip: "累计观看人次",
            value: 5000,
            dailyChange:1,
            weeklyChange:2,
            monthlyChange:3,
            change: 5.2,
            dates: ["02-02", "02-04", "02-06", "02-08", "02-10"],
            data: [0, 0, 1, 0, 0],
          },
          {
            id: "uv",
            title: "累计观看人数（UV）",
            tip: "累计观看人数",
            value: 3000,
            dailyChange:1,
            weeklyChange:2,
            monthlyChange:3,
            change: 5.2,
            dates: ["02-02", "02-04", "02-06", "02-08", "02-10"],
            data: [0, 0, 0, 1, 0],
          },
        ],
        selectedMetric: {}, // 选中的卡片数据
        chart: null, // ECharts 实例
      };
    },
    computed: {
      changeLabel() {
        switch (this.selectedTimeRange) {
          case 'day': return '比前一天';
          case 'week': return '比上周';
          case 'month': return '比上月';
          default: return '变化';
        }
      },
      displayLives() {
        return this.lives.slice(0, 4); // 最多只显示前 4 条数据
      },
      /*datePickerType() {
        return this.selectedTimeRange === "day"
          ? "date"
          : this.selectedTimeRange === "week"
            ? "week"
            : "month";
      },*/
      dateFormat() {
        return this.selectedTimeRange === "day"
          ? "yyyy-MM-dd"
          : this.selectedTimeRange === "week"
            ? "yyyy-MM-dd 至 yyyy-MM-dd"
            : "yyyy-MM";
      },
      valueFormat() {
        return this.selectedTimeRange === "day"
          ? "yyyy-MM-dd"
          : this.selectedTimeRange === "week"
            ? "yyyy-MM-dd"
            : "yyyy-MM";
      },
    },
    created() {
      //this.getList();
    },
    mounted() {
      this.selectedMetric = this.trendData[0]; // 默认选中第一个
      this.initChart(); // 初始化折线图
    },
    methods: {
      changeDate(value) {
        if (this.selectedTimeRange === "week" && value) {
          /*console.log("?? 监听到 selectedWeek 变化:", newVal);*/
          this.weekRange = this.getWeekRange(value);
        } else {
          this.weekRange = "";
        }
        console.log("选择的时间:", value, "筛选方式:", this.selectedTimeRange);
      },
      getWeekRange(selectedWeek) {
        console.log("?? selectedWeek 输入值:", selectedWeek); // 检查传入值
        let date = new Date(selectedWeek);

        if (isNaN(date.getTime())) {
          console.error("Invalid Date:", selectedWeek);
          return "日期错误";
        }

        let dayOfWeek = date.getDay(); // 0（星期天）- 6（星期六）

        // 计算周一
        let startDate = new Date(date); // 创建一个新的 Date 实例
        startDate.setDate(date.getDate() - dayOfWeek + (dayOfWeek === 0 ? -6 : 1));

        // 计算周日（确保 `endDate` 是新实例）
        let endDate = new Date(startDate.getTime()); // 复制 `startDate`
        endDate.setDate(startDate.getDate() + 6);

        console.log("周一:", this.formatDate(startDate));
        console.log("周日:", this.formatDate(endDate));

        return `${this.formatDate(startDate)} 至 ${this.formatDate(endDate)}`;
      },
      formatDate(date) {
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, "0");
        const day = String(date.getDate()).padStart(2, "0");
        return `${year}-${month}-${day}`;
      },
      computedChange(item) {
        switch (this.selectedTimeRange) {
          case 'day': return item.dailyChange;
          case 'week': return item.weeklyChange;
          case 'month': return item.monthlyChange;
          default: return 0;
        }
      },
      selectRank(type) {
        this.selectedRank = type;
        console.log(`选中的排行方式: ${type}`);
      },
      selectRank(type) {
        this.selectedRank = type; // 只允许一个按钮被选中
        console.log(`选中的排行方式: ${type}`);
      },
      fetchRankingData() {
        // 这里根据 `selectedRedRank` 和 `selectedBlackRank` 来请求不同的排行榜数据
        console.log(`获取排行榜数据: 红榜-${this.selectedRedRank}, 黑榜-${this.selectedBlackRank}`);
      },
      // 切换时间范围
      changeTimeRange() {
        // 根据选中的时间范围修改 datePicker 类型
        if (this.selectedTimeRange === "day") {
          this.datePickerType = "date";
        } else if (this.selectedTimeRange === "month") {
          this.datePickerType = "month";
        } else if (this.selectedTimeRange === "week") {
          this.datePickerType = "week";
        }
        this.selectedDate = null; // 重置已选日期
      },
      // 切换时间（但目前只是选日期，没有请求后端）
      /*changeDate() {
        console.log("选择日期:", this.selectedDate);
        // 这里可以做后端请求：this.fetchTrendData();
      },*/
      // 获取直播趋势数据
      async fetchTrendData() {
        try {
          const response = await axios.get("/api/trend-data", { params: { date: this.selectedDate } });
          this.trendData = response.data;

          // 默认选择第一个指标
          if (this.trendData.length > 0) {
            this.changeMetric(this.trendData[0]);
          }
        } catch (error) {
          console.error("获取直播数据失败:", error);
        }
      },
      // 切换日期时更新数据
      updateTrendData() {
        console.log(`更新 ${this.selectedDate} 的直播数据`);
        // 这里可以加接口请求，获取新的数据
        this.updateChart();
      },
      // 切换指标
      changeMetric(metric) {
        this.selectedMetric = metric;
        this.updateChart();
      },
      updateTrendData() {
        console.log("选择的时间：", this.selectedDate);
        this.trendData = this.trendData.map(item => ({
          ...item,
          value: Math.floor(Math.random() * 50),
          change: (Math.random() * 10 - 5).toFixed(2)
        }));
        this.updateChart();
      },
      initChart() {
        let chartDom = document.getElementById("liveChart");
        if (!chartDom) return;
        this.chart = echarts.init(chartDom);
        this.updateChart();
      },
      // 更新折线图
      updateChart() {
        if (!this.chart) return;

        let options = {
          tooltip: { trigger: "axis", backgroundColor: "rgba(0, 0, 0, 0.7)", textStyle: { color: "#fff" } },
          grid: { left: "5%", right: "5%", bottom: "10%", top: "10%", containLabel: true },
          xAxis: { type: "category", data: this.selectedMetric.dates || [], axisLine: { lineStyle: { color: "#ddd" } } },
          yAxis: { type: "value", splitLine: { lineStyle: { type: "dashed", color: "#ddd" } } },
          series: [
            {
              name: this.selectedMetric.title,
              data: this.selectedMetric.data || [],
              type: "line",
              smooth: true,
              symbol: "circle",
              symbolSize: 8,
              itemStyle: { color: "#007BFF", borderColor: "#fff", borderWidth: 2 },
              lineStyle: { width: 3, color: "#007BFF" },
              areaStyle: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  { offset: 0, color: "rgba(0, 123, 255, 0.5)" },
                  { offset: 1, color: "rgba(0, 123, 255, 0)" },
                ]),
              },
              emphasis: {
                focus: "series",
                label: { show: true, formatter: "{c}", fontSize: 14, color: "#333", backgroundColor: "#fff", padding: [4, 6] },
              },
              animationDuration: 1200,
              animationEasing: "quadraticOut",
            },
          ],
        };

        this.chart.setOption(options);
      },
      changeMetric(metric) {
        this.selectedMetric = metric;
        this.updateChart();
      },
    }
  };
</script>

<style scoped>
  .app-container {
    padding: 20px;
  }

  .title {
    font-size: 18px;
    font-weight: bold;
    margin-bottom: 10px;
  }
  .live-container {
    display: flex;
    gap: 15px; /* 控制卡片之间的间距 */
    overflow-x: auto; /* 允许横向滚动 */
    padding-bottom: 10px;
  }

  /* 卡片样式 */
  .live-card {
    width: 380px;
    height: 225px;
    background: rgb(250,250,250);
    border-radius: 10px;
    padding: 15px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    display: flex;
    flex-direction: column;
    align-items: flex-start;
  }

  /* 直播状态 */
  .status {
    background: #d3d3d3;
    color: #333;
    font-size: 14px;
    padding: 4px 8px;
    border-radius: 5px;
    display: inline-block;


  }

  /* 直播信息部分 */
  .content {
    background: rgb(250,250,250);
    display: flex;
    gap: 10px;
    align-items: center;
    width: 90%;
    margin-top: 0px;
  }

  /* 直播封面图片 */
  .cover-image {
    width: 70px;
    height: 70px;
    border-radius: 10px;
    object-fit: cover;
  }

  /* 直播文本信息 */
  .info {
    flex: 1;
  }

  .live-title {
    font-size: 16px;
    font-weight: bold;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 250px; /* 防止标题过长 */
  }

  .time {
    font-size: 14px;
    color: #666;
  }

  /* 直播数据部分 */
  .stats {
    display: flex;
    justify-content: space-between;
    width: 100%;
    flex-shrink: 0;
    margin-top: -40px;
  }

  .stat-item {
    text-align: center;
    flex: 1;
  }

  .label {
    font-size: 14px;
    color: #888;
  }

  .value {
    font-size: 18px;
    font-weight: bold;
    color: #333;
  }
  /* 直播趋势 */
  .trend-section {
    margin-top: 30px;
  }
  .trend-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .trend-cards {
    display: flex;
    gap: 15px;
  }
  .trend-card {
    width: 280px;
    hight: 100px;
    border: 1px solid #ddd;
    border-radius: 8px;
    padding: 16px;
    transition: all 0.3s;
    position: relative;
  }

  .trend-card.active {
    border-color: #007bff; /* 选中时边框变蓝 */
    box-shadow: 0px 0px 10px rgba(0, 123, 255, 0.3);
  }

  .trend-card.active::after {
    content: "✔";
    position: absolute;
    bottom: 8px;
    right: 8px;
    color: white;
    background: #007bff;
    border-radius: 50%;
    width: 20px;
    height: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 14px;
    font-weight: bold;
  }
  .trend-value {
    font-size: 24px;
    font-weight: bold;
  }

  /* 折线图 */
  .chart {
    width: 100%;
    height: 300px;
    margin-top: 20px;
  }
  .top10-section {
    margin-top: 20px;
    background: #fff;
    padding: 16px;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  }
  .live-name {
    display: flex;
    align-items: center;
  }

  .live-cover {
    width: 80px;  /* 直播封面宽度 */
    height: 45px; /* 直播封面高度 */
    border-radius: 4px;
    margin-right: 10px;
    object-fit: cover;
  }

  .live-title {
    font-size: 14px;
    color: #333;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .ranking-tabs {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 20px;
  }

  .ranking-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 10px 0;
    border-radius: 8px;
    overflow: hidden;
  }

  .ranking-section {
    display: flex;
    align-items: center;
    width: 50%;
    padding: 10px 15px;
  }

  .red-rank {
    background: linear-gradient(90deg, #ff6b6b, #ff8e8e);
    justify-content: flex-start;
  }

  .black-rank {
    background: linear-gradient(90deg, #4a4a4a, #6b6b6b);
    justify-content: flex-end;
  }

  .rank-title {
    font-size: 18px;
    font-weight: bold;
    color: white;
    margin: 0 15px;
  }

  .rank-filters {
    display: flex;
  }

  button {
    background: rgba(255, 255, 255, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.5);
    color: white;
    font-size: 14px;
    padding: 5px 12px;
    margin: 0 5px;
    border-radius: 15px;
    cursor: pointer;
    transition: all 0.3s ease;
  }

  button.active {
    background: white;
    color: #ff4d4f;
  }
  .filter-container {
    display: flex;
    align-items: center;
    gap: 8px; /* 控制两个组件的间距 */
  }

  .filter-label {
    font-weight: bold;
    white-space: nowrap; /* 防止换行 */
  }
</style>

