<template>
  <view class="uni-calendar">
    <view v-if="!insert && show" class="uni-calendar__mask" :class="{ 'uni-calendar--mask-show': aniMaskShow }"
      @click="clean"></view>
    <view v-if="insert || show" class="uni-calendar__content"
      :class="{ 'uni-calendar--fixed': !insert, 'uni-calendar--ani-show': aniMaskShow }">
      <view v-if="!insert" class="uni-calendar__header uni-calendar--fixed-top">
        <view class="uni-calendar__header-btn-box" @click="close">
          <text class="uni-calendar__header-text uni-calendar--fixed-width">{{ cancelText }}</text>
        </view>
        <view class="uni-calendar__header-btn-box" @click="confirm">
          <text class="uni-calendar__header-text uni-calendar--fixed-width">{{ okText }}</text>
        </view>
      </view>
      <view v-if="showToolbar" class="uni-calendar__header">
        <view class="uni-calendar__header-btn-box" @click.stop="pre">
          <!-- <view class="uni-calendar__header-btn uni-calendar--left"></view> -->
          <uv-icon name="play-left-fill" size="12" :color="canPre ? '#333' : '#ccc'" />
        </view>
        <!-- <picker mode="date" :value="date" fields="month" @change="bindDateChange">
					<text class="uni-calendar__header-text">{{ (nowDate.year||'') +'年'+( nowDate.month||'')+'月'}}</text>
				</picker> -->
        <view>
          <text class="uni-calendar__header-text">{{ (nowDate.year || '') + '年' + (nowDate.month || '') + '月' }}</text>
        </view>
        <view class="uni-calendar__header-btn-box" @click.stop="next">
          <uv-icon name="play-right-fill" size="12" :color="canNext ? '#333' : '#ccc'" />
          <!-- <view class="uni-calendar__header-btn uni-calendar--right"></view> -->
        </view>
        <text v-if="highlightToday" class="uni-calendar__backtoday" @click="backToday">{{ todayText }}</text>

      </view>
      <view class="uni-calendar__box">
        <view v-if="showMonth" class="uni-calendar__box-bg">
          <text class="uni-calendar__box-bg-text">{{ nowDate.month }}</text>
        </view>
        <view class="uni-calendar__weeks uni-calendar__weeks-head">
          <template v-if="sundayFirst">
            <view class="uni-calendar__weeks-day">
              <text class="uni-calendar__weeks-day-text">{{ SUNText }}</text>
            </view>
            <view class="uni-calendar__weeks-day">
              <text class="uni-calendar__weeks-day-text">{{ monText }}</text>
            </view>
            <view class="uni-calendar__weeks-day">
              <text class="uni-calendar__weeks-day-text">{{ TUEText }}</text>
            </view>
            <view class="uni-calendar__weeks-day">
              <text class="uni-calendar__weeks-day-text">{{ WEDText }}</text>
            </view>
            <view class="uni-calendar__weeks-day">
              <text class="uni-calendar__weeks-day-text">{{ THUText }}</text>
            </view>
            <view class="uni-calendar__weeks-day">
              <text class="uni-calendar__weeks-day-text">{{ FRIText }}</text>
            </view>
            <view class="uni-calendar__weeks-day">
              <text class="uni-calendar__weeks-day-text">{{ SATText }}</text>
            </view>
          </template>
          <template v-else>
            <view class="uni-calendar__weeks-day">
              <text class="uni-calendar__weeks-day-text">{{ monText }}</text>
            </view>
            <view class="uni-calendar__weeks-day">
              <text class="uni-calendar__weeks-day-text">{{ TUEText }}</text>
            </view>
            <view class="uni-calendar__weeks-day">
              <text class="uni-calendar__weeks-day-text">{{ WEDText }}</text>
            </view>
            <view class="uni-calendar__weeks-day">
              <text class="uni-calendar__weeks-day-text">{{ THUText }}</text>
            </view>
            <view class="uni-calendar__weeks-day">
              <text class="uni-calendar__weeks-day-text">{{ FRIText }}</text>
            </view>
            <view class="uni-calendar__weeks-day">
              <text class="uni-calendar__weeks-day-text">{{ SATText }}</text>
            </view>
            <view class="uni-calendar__weeks-day">
              <text class="uni-calendar__weeks-day-text">{{ SUNText }}</text>
            </view>
          </template>
        </view>
        <view class="uni-calendar__weeks" v-for="(item, weekIndex) in filteredWeeks" :key="weekIndex">
          <view class="uni-calendar__weeks-item" v-for="(weeks, weeksIndex) in item" :key="weeksIndex">
            <slot name="calendar-item" :weeks="weeks" :calendar="calendar" :selected="selected" :lunar="lunar"
              :highlight-today="highlightToday" :week-index="weekIndex" :weeks-index="weeksIndex">
              <calendar-item class="uni-calendar-item--hook" :weeks="weeks" :calendar="calendar" :selected="selected" :showExtraInfo="showExtraInfo"
                :lunar="lunar" :highlight-today="highlightToday" @change="choiceDate" />
            </slot>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import Calendar from './util.js';
import CalendarItem from './uni-calendar-item.vue'

import { initVueI18n } from '@dcloudio/uni-i18n'
import i18nMessages from './i18n/index.js'
const { t } = initVueI18n(i18nMessages)

/**
 * Calendar 日历
 * @description 日历组件可以查看日期，选择任意范围内的日期，打点操作。常用场景如：酒店日期预订、火车机票选择购买日期、上下班打卡等
 * @tutorial https://ext.dcloud.net.cn/plugin?id=56
 * @property {String} date 自定义当前时间，默认为今天
 * @property {Boolean} lunar 显示农历
 * @property {String} startDate 日期选择范围-开始日期
 * @property {String} endDate 日期选择范围-结束日期
 * @property {Boolean} range 范围选择
 * @property {Boolean} insert = [true|false] 插入模式,默认为false
 * 	@value true 弹窗模式
 * 	@value false 插入模式
 * @property {Boolean} clearDate = [true|false] 弹窗模式是否清空上次选择内容
 * @property {Array} selected 打点，期待格式[{date: '2019-06-27', info: '签到', data: { custom: '自定义信息', name: '自定义消息头',xxx:xxx... }}]
 * @property {Boolean} showMonth 是否选择月份为背景
 * @event {Function} change 日期改变，`insert :ture` 时生效
 * @event {Function} confirm 确认选择`insert :false` 时生效
 * @event {Function} monthSwitch 切换月份时触发
 * @example <uni-calendar :insert="true":lunar="true" :start-date="'2019-3-2'":end-date="'2019-5-20'"@change="change" />
 */
export default {
  components: {
    CalendarItem
  },
  emits: ['close', 'confirm', 'change', 'monthSwitch', 'change-week'],
  props: {
    date: {
      type: String,
      default: ''
    },
    selected: {
      type: Array,
      default() {
        return []
      }
    },
    lunar: {
      type: Boolean,
      default: false
    },
    startDate: {
      type: String,
      default: ''
    },
    endDate: {
      type: String,
      default: ''
    },
    range: {
      type: Boolean,
      default: false
    },
    insert: {
      type: Boolean,
      default: true
    },
    showMonth: {
      type: Boolean,
      default: true
    },
    clearDate: {
      type: Boolean,
      default: true
    },
    readonly: {
      type: Boolean,
      default: false
    },
    // 新增参数：控制周日位置
    sundayFirst: {
      type: Boolean,
      default: false
    },
    // 新增参数：控制显示模式
    displayMode: {
      type: String,
      default: 'month', // 'month' | 'week'
      validator: (value) => ['month', 'week'].includes(value)
    },
    // 新增参数：按周显示时的周数
    weekNumber: {
      type: Number,
      default: 1
    },
    // 新增参数：控制是否高亮今天
    highlightToday: {
      type: Boolean,
      default: true
    },
    // 新增参数：控制是否显示toolbar
    showToolbar: {
      type: Boolean,
      default: true
    },
    showExtraInfo: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      show: false,
      weeks: [],
      calendar: {},
      nowDate: '',
      aniMaskShow: false
    }
  },
  computed: {
    /**
     * for i18n
     */

    okText() {
      return t("uni-calender.ok")
    },
    cancelText() {
      return t("uni-calender.cancel")
    },
    todayText() {
      return t("uni-calender.today")
    },
    monText() {
      return t("uni-calender.MON")
    },
    TUEText() {
      return t("uni-calender.TUE")
    },
    WEDText() {
      return t("uni-calender.WED")
    },
    THUText() {
      return t("uni-calender.THU")
    },
    FRIText() {
      return t("uni-calender.FRI")
    },
    SATText() {
      return t("uni-calender.SAT")
    },
    SUNText() {
      return t("uni-calender.SUN")
    },
    canNext() {
      if (!this.endDate) return true
      const currentMonth = this.nowDate.month
      const endMonth = new Date(this.endDate).getMonth() + 1
      return currentMonth !== endMonth
    },
    canPre() {
      if (!this.startDate) return true
      const currentMonth = this.nowDate.month
      const startMonth = new Date(this.startDate).getMonth() + 1
      return currentMonth !== startMonth
    },
    // 新增计算属性：过滤后的周数据
    filteredWeeks() {
      if (this.displayMode === 'week') {
        // 按周显示，显示所有周但只有指定周正常显示，其他周置灰
        const weekIndex = this.weekNumber - 1
        const filteredWeeks = {}

        // 遍历weeks对象
        Object.keys(this.weeks).forEach(key => {
          const index = parseInt(key)
          const week = this.weeks[key]

          if (index === weekIndex) {
            // 指定周正常显示
            filteredWeeks[key] = week
          } else {
            // 其他周置灰显示
            filteredWeeks[key] = week.map(date => ({
              ...date,
              disable: true,
              isWeekModeDisabled: true // 新增标识，用于样式区分
            }))
          }
        })

        return filteredWeeks
      }
      // 按月显示，返回所有周数据
      return this.weeks
    }
  },
  watch: {
    date(newVal) {
      // this.cale.setDate(newVal)
      this.init(newVal)
    },
    startDate(val) {
      this.cale.resetSatrtDate(val)
      this.cale.setDate(this.nowDate.fullDate)
      this.weeks = this.cale.weeks
    },
    endDate(val) {
      this.cale.resetEndDate(val)
      this.cale.setDate(this.nowDate.fullDate)
      this.weeks = this.cale.weeks
    },
    selected(newVal) {
      this.cale.setSelectInfo(this.nowDate.fullDate, newVal)
      this.weeks = this.cale.weeks
    },
    displayMode(newVal) {
      // 显示模式变化时重新计算weeks
      this.weeks = this.cale.weeks
    },
    weekNumber(newVal) {
      // 周数变化时重新计算weeks
      this.weeks = this.cale.weeks
    }
  },
  created() {
    this.cale = new Calendar({
      selected: this.selected,
      startDate: this.startDate,
      endDate: this.endDate,
      range: this.range,
      sundayFirst: this.sundayFirst,
    })
    this.init(this.date)
  },
  methods: {
    // 取消穿透
    clean() { },
    bindDateChange(e) {
      const value = e.detail.value + '-1'
      this.setDate(value)

      const { year, month } = this.cale.getDate(value)
      this.$emit('monthSwitch', {
        year,
        month
      })
    },
    /**
     * 初始化日期显示
     * @param {Object} date
     */
    init(date) {
      this.cale.setDate(date)
      this.weeks = this.cale.weeks
      this.nowDate = this.calendar = this.cale.getInfo(date)
    },
    /**
     * 打开日历弹窗
     */
    open() {
      // 弹窗模式并且清理数据
      if (this.clearDate && !this.insert) {
        this.cale.cleanMultipleStatus()
        // this.cale.setDate(this.date)
        this.init(this.date)
      }
      this.show = true
      this.$nextTick(() => {
        setTimeout(() => {
          this.aniMaskShow = true
        }, 50)
      })
    },
    /**
     * 关闭日历弹窗
     */
    close() {
      this.aniMaskShow = false
      this.$nextTick(() => {
        setTimeout(() => {
          this.show = false
          this.$emit('close')
        }, 300)
      })
    },
    /**
     * 确认按钮
     */
    confirm() {
      this.setEmit('confirm')
      this.close()
    },
    /**
     * 变化触发
     */
    change() {
      if (!this.insert) return
      this.setEmit('change')
    },
    /**
     * 选择月份触发
     */
    monthSwitch() {
      let {
        year,
        month
      } = this.nowDate
      this.$emit('monthSwitch', {
        year,
        month: Number(month)
      })
    },
    /**
     * 派发事件
     * @param {Object} name
     */
    setEmit(name) {
      let {
        year,
        month,
        date,
        fullDate,
        lunar,
        extraInfo
      } = this.calendar
      this.$emit(name, {
        range: this.cale.multipleStatus,
        year,
        month,
        date,
        fulldate: fullDate,
        lunar,
        extraInfo: extraInfo || {}
      })
    },
    /**
     * 选择天触发
     * @param {Object} weeks
     */
    choiceDate(weeks) {
      if (this.readonly) return
      if (weeks.disable) return
      this.calendar = weeks
      // 设置多选
      this.cale.setMultiple(this.calendar.fullDate)
      this.weeks = this.cale.weeks
      this.change()
    },
    /**
     * 回到今天
     */
    backToday() {
      if (this.calendar.fullDate === this.cale.date.fullDate && this.cale.selectDate.fullDate === this.cale.date.fullDate) {
        return;
      }
      const nowYearMonth = `${this.nowDate.year}-${this.nowDate.month}`
      const date = this.cale.getDate(new Date())
      const todayYearMonth = `${date.year}-${date.month}`

      this.init(date.fullDate)

      if (nowYearMonth !== todayYearMonth) {
        this.monthSwitch()
      }

      this.change()
      this.$emit('change-month', {
        date: date.fullDate,
        month: Number(this.nowDate.month)
      })
    },
    /**
     * 上个月
     */
    pre() {
      if (!this.canPre) return
      const preDate = this.cale.getDate(this.nowDate.fullDate, -1, 'month').fullDate
      this.setDate(preDate)
      this.monthSwitch()
      this.$emit('change-month', {
        date: preDate,
        month: Number(this.nowDate.month)
      })
    },
    /**
     * 下个月
     */
    next() {
      if (!this.canNext) return
      const nextDate = this.cale.getDate(this.nowDate.fullDate, +1, 'month').fullDate
      this.setDate(nextDate)
      this.monthSwitch()
      this.$emit('change-month', {
        date: nextDate,
        month: Number(this.nowDate.month)
      })
    },
    /**
     * 设置日期
     * @param {Object} date
     */
    setDate(date) {
      this.cale.setDate(date)
      this.weeks = this.cale.weeks
      this.nowDate = this.cale.getInfo(date)
    },
    /**
     * 上一周
     */
    preWeek() {
      if (this.displayMode !== 'week') return
      const totalWeeks = Object.keys(this.weeks).length
      if (this.weekNumber > 1) {
        this.weekNumber--
      } else {
        // 如果已经是第一周，切换到上个月的最后一周
        const preDate = this.cale.getDate(this.nowDate.fullDate, -1, 'month').fullDate
        this.setDate(preDate)
        this.weekNumber = Object.keys(this.weeks).length
      }
      this.$emit('change-week', {
        weekNumber: this.weekNumber,
        totalWeeks: Object.keys(this.weeks).length,
        date: this.nowDate.fullDate
      })
    },
    /**
     * 下一周
     */
    nextWeek() {
      if (this.displayMode !== 'week') return
      const totalWeeks = Object.keys(this.weeks).length
      if (this.weekNumber < totalWeeks) {
        this.weekNumber++
      } else {
        // 如果已经是最后一周，切换到下个月的第一周
        const nextDate = this.cale.getDate(this.nowDate.fullDate, +1, 'month').fullDate
        this.setDate(nextDate)
        this.weekNumber = 1
      }
      this.$emit('change-week', {
        weekNumber: this.weekNumber,
        totalWeeks: Object.keys(this.weeks).length,
        date: this.nowDate.fullDate
      })
    },
    /**
     * 设置周数
     * @param {Number} weekNumber
     */
    setWeekNumber(weekNumber) {
      if (this.displayMode !== 'week') return
      const totalWeeks = Object.keys(this.weeks).length
      if (weekNumber >= 1 && weekNumber <= totalWeeks) {
        this.weekNumber = weekNumber
        this.$emit('change-week', {
          weekNumber: this.weekNumber,
          totalWeeks: totalWeeks,
          date: this.nowDate.fullDate
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
$uni-bg-color-mask: rgba($color: #000000, $alpha: 0.4);
$uni-border-color: #EDEDED;
$uni-text-color: #333;
$uni-bg-color-hover: #f1f1f1;
$uni-font-size-base: 14px;
$uni-text-color-placeholder: #808080;
$uni-color-subtitle: #555555;
$uni-text-color-grey: #999;

.uni-calendar {
  /* #ifndef APP-NVUE */
  display: flex;
  /* #endif */
  flex-direction: column;
}

.uni-calendar__mask {
  position: fixed;
  bottom: 0;
  top: 0;
  left: 0;
  right: 0;
  background-color: $uni-bg-color-mask;
  transition-property: opacity;
  transition-duration: 0.3s;
  opacity: 0;
  /* #ifndef APP-NVUE */
  z-index: 99;
  /* #endif */
}

.uni-calendar--mask-show {
  opacity: 1
}

.uni-calendar--fixed {
  position: fixed;
  /* #ifdef APP-NVUE */
  bottom: 0;
  /* #endif */
  left: 0;
  right: 0;
  transition-property: transform;
  transition-duration: 0.3s;
  transform: translateY(460px);
  /* #ifndef APP-NVUE */
  bottom: calc(var(--window-bottom));
  z-index: 99;
  /* #endif */
}

.uni-calendar--ani-show {
  transform: translateY(0);
}

.uni-calendar__content {
  background-color: #fff;
}

.uni-calendar__header {
  position: relative;
  /* #ifndef APP-NVUE */
  display: flex;
  /* #endif */
  flex-direction: row;
  justify-content: center;
  align-items: center;
  height: 50px;
  // border-bottom-color: $uni-border-color;
  // border-bottom-style: solid;
  // border-bottom-width: 1px;
}

.uni-calendar--fixed-top {
  /* #ifndef APP-NVUE */
  display: flex;
  /* #endif */
  flex-direction: row;
  justify-content: space-between;
  border-top-color: $uni-border-color;
  border-top-style: solid;
  border-top-width: 1px;
}

.uni-calendar--fixed-width {
  width: 50px;
}

.uni-calendar__backtoday {
  position: absolute;
  right: 0;
  top: 25rpx;
  padding: 0 5px;
  padding-left: 10px;
  height: 25px;
  line-height: 25px;
  font-size: 12px;
  border-top-left-radius: 25px;
  border-bottom-left-radius: 25px;
  color: $uni-text-color;
  background-color: $uni-bg-color-hover;
}

.uni-calendar__header-text {
  display: block;
  text-align: center;
  width: 116px;
  // font-size: $uni-font-size-base;
  color: $uni-text-color;
  font-size: 30rpx;
  font-weight: bold;
}

.uni-calendar__header-btn-box {
  /* #ifndef APP-NVUE */
  display: flex;
  /* #endif */
  flex-direction: row;
  align-items: center;
  justify-content: center;
  width: 50px;
  height: 50px;
}

.uni-calendar__header-btn {
  width: 6px;
  height: 6px;
  border-left-color: $uni-text-color-placeholder;
  border-left-style: solid;
  border-left-width: 2px;
  border-top-color: $uni-color-subtitle;
  border-top-style: solid;
  border-top-width: 2px;
}

.uni-calendar--left {
  transform: rotate(-45deg);
}

.uni-calendar--right {
  transform: rotate(135deg);
}


.uni-calendar__weeks {
  position: relative;
  /* #ifndef APP-NVUE */
  display: flex;
  /* #endif */
  flex-direction: row;
}

.uni-calendar__weeks-head {
  color: #B3B3B3;
  font-size: 30rpx;
}

.uni-calendar__weeks+.uni-calendar__weeks {
  margin-top: 26rpx;
}

.uni-calendar__weeks-item {
  flex: 1;
}

.uni-calendar__weeks-day {
  flex: 1;
  /* #ifndef APP-NVUE */
  display: flex;
  /* #endif */
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 45px;
  // border-bottom-color: #F5F5F5;
  // border-bottom-style: solid;
  // border-bottom-width: 1px;
}

.uni-calendar__weeks-day-text {
  font-size: 14px;
}

.uni-calendar__box {
  position: relative;
  padding: 0 40rpx;
}

.uni-calendar__box-bg {
  /* #ifndef APP-NVUE */
  display: flex;
  /* #endif */
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.uni-calendar__box-bg-text {
  font-size: 200px;
  font-weight: bold;
  color: $uni-text-color-grey;
  opacity: 0.1;
  text-align: center;
  /* #ifndef APP-NVUE */
  line-height: 1;
  /* #endif */
}
</style>
