<template>
  <header class="report-header">
    <!-- 左侧区域：logo + 标题 -->
    <div class="left-content">
      <img style="height: 36px;" src="../../../assets/img/cnbm.png" alt="benmaLogo">
      <div class="top-title">{{ topTitle }}</div>
    </div>

    <!-- 右侧区域：全屏按钮 -->
    <div class="right-content">
      <el-dropdown class="avatar-container right-menu-item hover-effect" trigger="click">
        <div class="avatar-wrapper">
          <img :src="require(`../../../assets/images/choicepart/avatar.png`)" class="user-avatar">
          <span v-if="nickname" class="user-nickname">{{ nickname }}</span>
          <i class="el-icon-caret-bottom" />
        </div>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item @click.native="logout">
            <span>退出登录</span>
          </el-dropdown-item>
           <el-dropdown-item divided  @click.native='handleToggle'>切换账号</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <el-button type="text" class="screen-btn" @click="changeHomeSider">
        <svg-icon style="color: #0B58FF;" v-if="openSider" icon-class="closeSider" />
        <svg-icon style="color: #0B58FF;" v-else icon-class="openSider" />
      </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>

    <!-- 时间选择区域：日/月/年按钮 + label + 日期选择器 -->
    <div class="timeType">
      <div class="dateP">
        <div class="label">
          <span class="label-text">月份选择</span>
        </div>
        <el-date-picker :clearable="false" v-model="date" type="month" placeholder="请选择月份" class="custom-date-picker"
          value-format="timestamp"
          style="width: 132px;height: 29px;"
          @change="emitTimeRange"
          />
      </div>
    </div>
  </header>
</template>

<script>
import { mapGetters } from 'vuex'
import moment from 'moment'
import {getPath} from "@/utils/ruoyi";
export default {
  name: 'Header',
  props: {
    isFullScreen: { type: Boolean, default: false },
    openSider: { type: Boolean, default: false },
    topTitle: { type: String, default: '' },
    dateData: {
      type: Object,
      default: () => ({})
    },
  },
  computed:{
    ...mapGetters(['nickname']),
  },
  data() {
    return {
      currentTime: '',
      timeTimer: null,
      date: Date.now(), // 使用时间戳格式
      activeTime: 1, // 0=日，1=月，2=年（默认选中"月"）
      pageRoutes: [
        { text: '营业收入', path: '/operatingRevenue/operatingRevenueIndex' },
        { text: '利润分析', path: '/profitAnalysis' },
        { text: '产销率库存分析', path: '/PSIAnal' },
        { text: '成本分析', path: '/cost/cost' },
        { text: '驾驶舱报表', path: '/cockpit' }
      ],
    }
  },
  methods: {
    goToPage(path, index) {
      this.$router.push(path);
    },
    changeHomeSider() { this.$emit('siderOpenChange') },
    changeFullScreen() { this.$emit('screenfullChange') },
    padZero(num) { return num < 10 ? '0' + num : num },

    /**
     * 计算时间区间
     * @returns {Object} 包含 startTime、endTime、mode、targetMonth 的区间对象
     */
    calculateTimeRange() {
      // 固定为月维度
      const mode = 2;
      // 初始化时间戳为0（兜底值）
      let startTime = 0;
      let endTime = 0;
      // 存储目标月份
      let targetMonth = '';

      try {
        // 使用 this.date（时间戳）创建 moment 对象
        let targetMoment = this.date
          ? moment(this.date)  // 解析时间戳
          : moment(); // 如果 date 无效，使用当前时间

        // 验证日期是否有效
        if (!targetMoment.isValid()) {
          console.warn('无效的日期，已使用当前月份:', this.date);
          targetMoment = moment();
        }

        // 获取月份（数字格式，1-12）
        targetMonth = targetMoment.format('M');
        console.log('当前选择的月份:', targetMonth);

        // 计算当月第一天00:00:00的时间戳
        startTime = targetMoment.startOf('month').valueOf();
        // 计算当月最后一天23:59:59的时间戳
        endTime = targetMoment.clone()
          .endOf('month')
          .set({
            hour: 23,
            minute: 59,
            second: 59,
            millisecond: 0  // 毫秒设为0
          })
          .valueOf();

        // 调试输出
        console.log('月份时间范围计算结果:', {
          startTime: moment(startTime).format('YYYY-MM-DD HH:mm:ss'),
          endTime: moment(endTime).format('YYYY-MM-DD HH:mm:ss'),
          startTimeStamp: startTime,
          endTimeStamp: endTime,
          targetMonth: targetMonth
        });
      } catch (error) {
        console.error('计算月份时间范围时出错：', error);
      }

      return {
        startTime,
        endTime,
        mode,
        targetMonth
      };
    },

    /**
     * 传递时间区间给父组件
     */
    emitTimeRange() {
      const timeRange = this.calculateTimeRange();
      this.$emit('timeRangeChange', timeRange);
      console.log('触发时间范围变化:', timeRange);
    },

    async logout() {
      this.$modal.confirm('确定注销并退出系统吗？', '提示').then(() => {
        this.$store.dispatch('LogOut').then(() => {
          location.href = getPath('/index');
        })
      }).catch(() => {});
    },
    handleToggle() {
      this.$store.dispatch('LogOut').then(() => {
        location.href = getPath('/index');
      })
    }
  },
  watch: {
    // 维度切换时：清空选择的日期
    activeTime(newVal, oldVal) {
      if (newVal !== oldVal) {
        this.date = Date.now(); // 重置为当前时间戳
        this.emitTimeRange();
      }
    },
    dateData: {
      immediate: true, // 初始化时立即执行
      handler(newVal) {
        console.log('dateData 变化:', newVal);
        if (newVal && (newVal.startTime || newVal.endTime)) {
          // 优先使用 startTime
          const timeStamp = newVal.startTime || newVal.endTime;
          if (timeStamp && timeStamp !== 0) {
            console.log('设置日期选择器时间为:', timeStamp);
            this.date = timeStamp; // 直接使用时间戳
          }
        }
      }
    }
  },
  mounted() {
    // 初始化默认日期为当前月份
    console.log('初始化日期选择器，当前时间戳:', this.date);
    this.$nextTick(() => {
      this.emitTimeRange();
    });
  }
}
</script>

<style scoped lang="scss">
/* 原有样式不变，仅补充label文字的倾斜抵消样式 */
@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;
  background: url('../../../assets/img/topTitle.png') no-repeat;
  background-size: cover;
  background-position: 0 0;
  box-sizing: border-box;
  position: relative;

  .left-content {
    margin-top: 11px;
    margin-left: 44px;
    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;
  }

  .center-content {
    display: flex;
    gap: 8px;
    margin-top: 18px;
    margin-left: 70px;

    .item {
      width: 180px;
      height: 50px;
      background: #E1EEFC;
      transform: skew(-20deg);
      font-family: PingFangSC, PingFang SC;
      font-weight: 400;
      font-size: 20px;
      color: #1E1651;
      line-height: 50px;
      letter-spacing: 2px;
      text-align: center;
      cursor: pointer;
      overflow: hidden;
      box-shadow: 0px 13px 16px 0px rgba(179, 217, 255, 0.43),
        0px 2px 4px 0px rgba(92, 140, 255, 0.25),
        inset 0px -43px 13px 0px rgba(255, 255, 255, 0.51);

      .item-text {
        display: inline-block;
        transform: skew(20deg);
      }
    }

    .item.no-skew {
      background: none !important;
      transform: none !important;
      box-shadow: none !important;
      color: #1E1651;

      .item-text {
        transform: none !important;
      }
    }
  }

  .timeType {
    position: absolute;
    display: flex;
    align-items: center;
    top: 42px;
    right: 10px;
    margin-top: 18px;
    gap: 0;
  }

  .timeType .item {
    width: 40px;
    height: 28px;
    background: rgba(236, 244, 254, 1);
    transform: skew(-20deg);
    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(20deg);
    transition: all 0.2s ease;
  }

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

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

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

  .dateP .label {
    width: 165px;
    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;
    margin-top: 12px;
    margin-right: 16px;
    justify-content: flex-end;
    gap: 21px;
    height: 35px;
  }

  .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;
    color: #00fff0;
    font-size: 26px;
    padding: 0;
    margin: 0;
  }
  .avatar-container {
      margin-right: 30px;

      .avatar-wrapper {
        display: flex;
        justify-content: center;
        align-items: center;
        position: relative;

        .user-avatar {
          cursor: pointer;
          width: 32px;
          height: 32px;
          border-radius: 50%;
        }
        .user-nickname{
          margin-left: 5px;
          font-size: 14px;
        }

        .el-icon-caret-bottom {
          cursor: pointer;
          position: absolute;
          right: -20px;
          top: 10px;
          font-size: 12px;
        }
      }
    }
}

/* 日期选择器样式保持不变 */
::v-deep .custom-date-picker {
  position: absolute;
  right: 8px;
  width: 165px !important;
  height: 28px !important;
  position: relative;
  margin: 0 !important;

  .el-input__inner {
    height: 28px !important;
    width: 165px !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>
