<template>
  <header class="report-header" :class="['report-header__' + size]">
    <!-- 左侧区域：标题 -->
    <div class="left-content">
      <div class="top-title">{{ topTitle }}</div>
    </div>

    <!-- 右侧区域：全屏按钮 -->
    <div class="right-content">
      <el-button type="text" class="return-btn" :title="'返回'" @click="handleReturn">
        <svg-icon style="color: #0B58FF;" icon-class="returnIcon" />
      </el-button>
      <el-button type="text" class="screen-btn" :title="isFullScreen ? '退出全屏' : '全屏'" @click="changeFullScreen">
        <svg-icon style="color: #0B58FF;" v-if="isFullScreen" icon-class="unFullScreenView" />
        <svg-icon style="color: #0B58FF;" v-else icon-class="fullScreenView" />
      </el-button>
    </div>

    <!-- 时间选择区域：日/月/年按钮 + 日期选择器 -->
    <div class="timeType">
      <div class="item" v-for="(item, index) in timeTypes" :key="index" @click="activeTime = index"
        :class="{ 'no-skew': activeTime === index }">
        <span class="item-text">{{ item.text }}</span>
      </div>
      <div class="dateP">
        <div class="label">
          <span class="label-text">日期选择</span>
        </div>
        <el-date-picker v-model="date" :type="getPickerType" :placeholder="getPickerPlaceholder"
          class="custom-date-picker" value-format="yyyy-MM-dd" :clearable="false" style="width: 132px;height: 29px;"
          @change="emitTimeRange" />
      </div>
    </div>
  </header>
</template>

<script>
import moment from 'moment'; // 引入moment

export default {
  name: 'Header',
  props: {
    isFullScreen: { type: Boolean, default: false },
    topTitle: { type: String, default: '' },
    size: { type: String, default: 'basic' },
  },
  data() {
    return {
      currentTime: '',
      timeTimer: null,
      date: undefined, // 存储选择的日期（字符串格式：yyyy-MM-dd/yyyy-MM/yyyy）
      activeTime: 1, // 默认月维度（0=日，1=月，2=年）
      timeTypes: [
        { text: '日', pickerType: 'date', placeholder: '选择日期' },
        { text: '月', pickerType: 'month', placeholder: '选择月份' },
        { text: '年', pickerType: 'year', placeholder: '选择年份' }
      ]
    }
  },
  computed: {
    getPickerType() {
      return this.timeTypes[this.activeTime].pickerType;
    },
    getPickerPlaceholder() {
      return this.timeTypes[this.activeTime].placeholder;
    }
  },
  watch: {
    activeTime(newVal, oldVal) {
      if (newVal !== oldVal) {
        this.date = undefined;
        // this.emitTimeRange();
      }
    }
  },
  mounted() {
    // 初始化默认日期（当前月，格式：yyyy-MM）
    console.log(this.$router);
    this.date = moment().format('YYYY-MM');
    this.$nextTick(() => this.emitTimeRange());
  },
  methods: {
    changeFullScreen() {
      this.$emit('screenfullChange');
    },
    handleReturn() {
      console.log(this.$router);

      this.$router.go(-1);
    },
    exportPDF() {
      this.$emit('exportPDF');
    },
    /**
     * 核心方法：用moment计算时间范围（时间戳格式）
     * 日：选择日00:00:00 → 次日00:00:00
     * 月：当月1日00:00:00 → 次月1日00:00:00
     * 年：当年1月1日00:00:00 → 次年1月1日00:00:00
     */
    calculateTimeRange() {
      let startTime = 0;
      let endTime = 0;
      const mode = this.activeTime + 1; // 1=日，2=月，3=年
      const defaultMoment = moment(); // 默认当前时间

      const targetMoment = this.date
        ? moment(this.date, this.getPickerType === 'date' ? 'YYYY-MM-DD' : (this.getPickerType === 'month' ? 'YYYY-MM' : 'YYYY'))
        : defaultMoment;

      if (!targetMoment.isValid()) {
        console.error('无效日期:', this.date);
        return { startTime, endTime, mode };
      }

      // 1. 日维度：00:00:00 → 23:59:59（无毫秒）
      if (this.activeTime === 0) {
        startTime = targetMoment.startOf('day').millisecond(0).valueOf();
        endTime = targetMoment.endOf('day').millisecond(0).valueOf();
      }

      // 2. 月维度：当月1日00:00:00 → 当月最后一天23:59:59（无毫秒）
      else if (this.activeTime === 1) {
        startTime = targetMoment.startOf('month').millisecond(0).valueOf();
        endTime = targetMoment.endOf('month').millisecond(0).valueOf();
      }

      // 3. 年维度：当年1月1日00:00:00 → 当年最后一天23:59:59（无毫秒）
      else if (this.activeTime === 2) {
        startTime = targetMoment.startOf('year').millisecond(0).valueOf();
        endTime = targetMoment.endOf('year').millisecond(0).valueOf();
      }

      // 调试输出：验证是否去掉毫秒
      console.log('时间范围计算结果:', {
        mode,
        startTime: moment(startTime * 1000).format('YYYY-MM-DD HH:mm:ss'), // 格式：2025-11-30 00:00:00
        endTime: moment(endTime * 1000).format('YYYY-MM-DD HH:mm:ss'),     // 格式：2025-11-30 23:59:59（无毫秒）
        startTimeStamp: startTime, // 秒级时间戳（如：1764422400）
        endTimeStamp: endTime      // 秒级时间戳（如：1764508799）
      });

      return { startTime, endTime, mode };
    },

    // 传递时间范围给父组件
    emitTimeRange() {
      const timeRange = this.calculateTimeRange();
      this.$emit('timeRangeChange', timeRange);
    }
  }
}
</script>

<style scoped lang="scss">
/* 字体引入 */
@font-face {
  font-family: "YouSheBiaoTiHei";
  src: url('../../../assets/fonts/YouSheBiaoTiHe.ttf') format('truetype');
}

/* 头部容器基础样式 */
.report-header {
  height: 117px;
  width: 100%;
  display: flex;
  justify-content: space-between;
  box-sizing: border-box;
  position: relative;

  &__basic {
    background: url(../../../assets/img/topBg.png) no-repeat;
    background-size: cover;
    background-position: 0 0;
  }

  &__psi {
    background: url(../../../assets/img/psiTopTitle.png) no-repeat;
    background-size: 100% 100%;
    background-position: 0 0;
  }

  /* 左侧标题区域 */
  .left-content {
    margin-top: 11px;
    margin-left: 350px;
    height: 55px;
    display: flex;
    align-items: center;
    gap: 16px;
  }

  .top-title {
    height: 55px;
    font-family: "YouSheBiaoTiHei", sans-serif;
    font-size: 42px;
    color: #1E1651;
    line-height: 55px;
    letter-spacing: 6px;
    text-align: left;
  }

  /* 时间选择区域 */
  .timeType {
    position: absolute;
    display: flex;
    align-items: center;
    top: 42px;
    right: 0px;
    margin-top: 18px;
    gap: 0;
  }

  .timeType .item {
    width: 50px;
    height: 28px;
    background: rgba(236, 244, 254, 1);
    transform: skew(-25deg);
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 14px;
    color: rgba(11, 88, 255, 1);
    line-height: 28px;
    letter-spacing: 2px;
    text-align: center;
    cursor: pointer;
    overflow: hidden;
  }

  .timeType .item .item-text {
    display: inline-block;
    transform: skew(25deg);
    transition: all 0.2s ease;
  }

  .timeType .item.no-skew {
    background: rgba(11, 88, 255, 1);
    color: rgba(249, 252, 255, 1);
    transform: skew(-25deg) !important;
    box-shadow: 0 2px 8px rgba(11, 88, 255, 0.3);
  }

  .timeType .item.no-skew .item-text {
    transform: skew(25deg) !important;
  }

  .dateP {
    position: relative;
    margin-left: 10px;
    display: flex;
    align-items: center;
    gap: 0;
  }

  .dateP .label {
    width: 70px;
    height: 28px;
    background: rgba(236, 244, 254, 1);
    transform: skew(-25deg);
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 14px;
    color: #0B58FF;
    line-height: 28px;
    text-align: center;
    overflow: hidden;
  }

  .dateP .label-text {
    display: inline-block;
    transform: skew(25deg);
  }

  /* 右侧全屏按钮区域 */
    .right-content {
      display: flex;
      // flex-direction: column;
      margin-top: 12px;
      margin-right: 10px;
      gap: 21px;
    }

    // .current-time {
    //   color: #FFFFFF;
    //   font-family: PingFangSC, PingFang SC;
    //   font-weight: 500;
    //   font-size: 22px;
    //   line-height: 24px;
    //   letter-spacing: 1px;
    // }

    .screen-btn {
      width: 26px;
      height: 26px;
      color: #00fff0;
      font-size: 26px;
      padding: 0;
    }

    .home-btn {
      width: 26px;
      height: 26px;
      // margin-left: 300px;
      color: #00fff0;
      font-size: 26px;
      padding: 0;
    }

    .return-btn {
      width: 26px;
      height: 26px;
      // margin-left: 300px;
      color: #00fff0;
      font-size: 26px;
      padding: 0;
    }
}

/* 日期选择器自定义样式 */
::v-deep .custom-date-picker {
  position: absolute;
  right: 8px;
  width: 132px !important;
  height: 28px !important;
  position: relative;
  margin: 0 !important;

  .el-input__inner {
    height: 28px !important;
    width: 132px !important;
    text-align: center;
    padding-left: 15px !important;
    padding-right: 32px !important;
    font-size: 14px !important;
    line-height: 28px !important;
    color: rgba(237, 245, 253, 1) !important;
    vertical-align: middle !important;
    clip-path: polygon(18px 0, 100% 0, 100% 100%, 0 100%);
    border: none !important;
    box-shadow: none !important;
    background-color: rgba(11, 88, 255, 1) !important;
    border-left: 1px solid rgba(255, 255, 255, 0.2);
  }

  .el-input__prefix {
    left: auto !important;
    right: 8px !important;
    top: 50% !important;
    transform: translateY(-50%) !important;
    display: inline-flex !important;
    align-items: center !important;
    height: 28px !important;
  }

  .el-input__icon {
    color: #ffffff !important;
    font-size: 16px !important;
    line-height: 28px !important;
    vertical-align: middle !important;
  }

  .el-icon-date::before {
    color: #ffffff !important;
    font-size: 16px !important;
    line-height: inherit !important;
  }
}
</style>
