<script lang="ts" setup>
import { Calendar } from "@element-plus/icons-vue";
import dayjs from "dayjs";
import { ref } from "vue";
const timeValue = ref("");
const dropdownShow = ref(false);
const yearTime = ref<any>(dayjs(new Date()).format("YYYY"));
const decreaseYear = () => {
  yearTime.value = dayjs().year(yearTime.value).subtract(1, "year").year(); // 减一年
};

const increaseYear = () => {
  yearTime.value = dayjs().year(yearTime.value).add(1, "year").year(); // 加一年
};
const focusShow = () => {};
const blurShow = () => {};
// 季度
const monthOne = item => {
  let start = "";
  let end = "";

  switch (item) {
    case "一季度":
      start = `${yearTime.value}-01-01`;
      end = `${yearTime.value}-03-31`;
      break;
    case "二季度":
      start = `${yearTime.value}-04-01`;
      end = `${yearTime.value}-06-30`;
      break;
    case "三季度":
      start = `${yearTime.value}-07-01`;
      end = `${yearTime.value}-09-30`;
      break;
    case "四季度":
      start = `${yearTime.value}-10-01`;
      end = `${yearTime.value}-12-31`;
      break;
    case "上半年":
      start = `${yearTime.value}-01-01`;
      end = `${yearTime.value}-06-30`;
      break;
    case "下半年":
      start = `${yearTime.value}-07-01`;
      end = `${yearTime.value}-12-31`;
      break;
  }
  timeValue.value = `${yearTime.value}年${item}`;
  console.log(start, end);
  return `从 ${start} 到 ${end}`; // 设置时间区间
};
// 季度 半年
const quarterValue = ref(false);
const open = value => {
  console.log(value);
  timeValue.value = "";
  if (value === "季度") {
    quarterValue.value = true;
  } else {
    quarterValue.value = false;
  }
};
defineExpose({
  open
});
</script>
<template>
  <div class="">
    <el-dropdown trigger="click">
      <span class="el-dropdown-link navbar-bg-hover select-none">
        <el-input
          v-model="timeValue"
          style="width: 240px"
          placeholder="请选择日期"
          :prefix-icon="Calendar"
          @focus="focusShow"
          @blur="blurShow"
        />
      </span>
      <template #dropdown>
        <el-dropdown-menu class="setting" style="width: 300px">
          <div class="flex justify-between align-center text-base mt-4">
            <div class="cursor-pointer ml-4 text-xs pt-2" @click="decreaseYear">
              <el-icon><DArrowLeft /></el-icon>
            </div>
            <div>{{ yearTime }}</div>
            <div class="cursor-pointer mr-4 text-xs pt-2" @click="increaseYear">
              <el-icon><DArrowRight /></el-icon>
            </div>
          </div>
          <div
            v-if="quarterValue"
            class="flex justify-center align-center mt-5 mb-5"
          >
            <el-dropdown-item @click="monthOne('一季度')">
              一季度
            </el-dropdown-item>
            <el-dropdown-item @click="monthOne('二季度')">
              二季度
            </el-dropdown-item>
            <el-dropdown-item @click="monthOne('三季度')">
              三季度
            </el-dropdown-item>
            <el-dropdown-item @click="monthOne('四季度')">
              四季度
            </el-dropdown-item>
          </div>
          <div v-else class="flex justify-around align-center mt-5 mb-5">
            <el-dropdown-item @click="monthOne('上半年')">
              上半年
            </el-dropdown-item>
            <el-dropdown-item @click="monthOne('下半年')">
              下半年
            </el-dropdown-item>
          </div>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>
