<template>
  <div>
    <div class="text-subtitle2 text-weight-bold">{{$t('v103.scheduledTripDetails.dateoption')}}</div>
    <div class="text-grey-6 f12 q-my-md">{{$t('v103.scheduledTripDetails.pleasedateoption')}}</div>
    <div class="rounded-borders" style="border: 1px solid #eee">
      <div class="q-px-lg q-py-sm row items-center">
        <div class="text-subtitle2 text-weight-bold col">
          {{ months[currentMonth].display }}
        </div>
        <div>
          <q-btn
            color="primary"
            round
            icon="iconfont iconpreviewleft"
            flat
            @click="changeShowMonthHandler(-1)"
            class="q-mr-sm"
            :disable="currentMonth == 0"
          />
          <q-btn
            color="primary"
            round
            icon="iconfont iconpreviewright"
            flat
            @click="changeShowMonthHandler(1)"
            :disable="currentMonth == months.length - 1"
          />
        </div>
      </div>
      <div class="q-pa-sm non-selectable" style="border-top: 1px solid #eee">
        <div class="row items-center">
          <div
            class="col text-weight-bold text-center"
            style="padding: 10px 0 20px"
            v-for="(x, i) in chineseWeek"
            :key="i"
          >
            {{ x }}
          </div>
        </div>
        <div class="row wrap">
          <div
            class="rounded-borders text-center text-weight-bold"
            :class="{
              'cursor-pointer hover-date': x.value != '' && x.price,
              'cursor-not-allowed': !x.price && x.value != '',
              'bg-primary': x.value != '' && x.value == currentDate,
            }"
            style="padding: 8px 4px; width: 14.285%"
            v-for="(x, i) in col"
            :key="i"
            @click="chosenDateHandler(x)"
          >
            <div
              class="text-dark"
              :class="{ 'text-white': x.value == currentDate }"
              v-if="x.price"
            >
              {{ x.display }}
            </div>
            <div class="text-grey-6" v-else>{{ x.display }}</div>
            <div
              class="q-mt-sm f12"
              :class="{
                'text-grey-1': x.value == currentDate,
                ' text-grey-6': x.value != currentDate,
              }"
            >
              {{
                x.price ? x.price.originalB2BPrice : "&nbsp;"
              }}
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { date,useQuasar } from "quasar";
import { inject, provide, reactive, toRefs, defineComponent,onMounted,ref,watch, getCurrentInstance  } from 'vue';
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { DirtionmaryHelper } from 'src/config/dictionary'
import scheduledTripService from 'src/api/scheduledTrip'
import { ApiResult } from 'src/@types/enumHelper'
import message from 'src/utils/message'
import { moneyFormat } from 'src/utils/tools'
import { useHotel } from '../../utils/hotelRate'
export default defineComponent({
  components: {  },
  props: ['priceList'],
  setup(props, context) {
    const $router = useRouter()
    const { t } = useI18n()
    const $q = useQuasar()
    const qDateProxy = ref(null) as any
    let {ctx:that, proxy} = getCurrentInstance()
    watch(props.priceList,(n,o)=>{
      data.prices = n;
      methods.init();
    })
    const data= reactive({
      chineseWeek: [t('dayOfWeek.sun'), t('dayOfWeek.mon'), t('dayOfWeek.tues'), t('dayOfWeek.wed'), t('dayOfWeek.thur'), t('dayOfWeek.fri'), t('dayOfWeek.sat')],
      chineseMonth: [] as String,
      data: [],
      months: [],
      currentMonth: 0,
      col: [],
      prices: props.priceList,
      currentDate: "",
    })
    data.chineseMonth = useHotel.getMonthType()
    data.prices = props.priceList;
    const methods = {
      init() {
        methods.createChosenDateHandler();
        let firstMonth = date.formatDate(
          new Date(data.prices[0].startDate),
          "YYYY/MM"
        );
        data.currentMonth =
          data.months.findIndex((x) => x.monthValue == firstMonth) ?? 0;
        methods.changeMonthHandler();
      },
      createChosenDateHandler() {
        let beginDate = new Date();
        let endDate = date.addToDate(beginDate, { years: 1 });
        while (beginDate <= endDate) {
          let i = parseInt(date.formatDate(beginDate, "M")) - 1;
          data.months.push({
            value: date.formatDate(beginDate, "YYYY/MM/DD"),
            monthValue: date.formatDate(beginDate, "YYYY/MM"),
            display: `${date.formatDate(beginDate, "YYYY")} ${
              data.chineseMonth[i]
            }`,
          });
          beginDate = date.addToDate(beginDate, { months: 1 });
        }
      },
      changeShowMonthHandler(i) {
        data.currentMonth += i;
        methods.changeMonthHandler();
      },
      changeMonthHandler() {
        data.col = [];
        let newDate = new Date(data.months[data.currentMonth].monthValue);
        let newBeginDate = date.startOfDate(newDate, "month");
        let newEndDate = date.endOfDate(newDate, "month");
        for (let i = 0; i < date.formatDate(newBeginDate, "d"); i++) {
          data.col.push({
            value: "",
          });
        }
        while (newBeginDate <= newEndDate) {
          let value = date.formatDate(newBeginDate, "YYYY-MM-DD");
          let price = data.prices.find((x) => x.startDate == value && x.remainNum>0);
          data.col.push({
            value,
            display: date.formatDate(newBeginDate, "DD"),
            price,
          });
          newBeginDate = date.addToDate(newBeginDate, { days: 1 });
        }
        let after = 6 - date.formatDate(newBeginDate, "d");
        for (let i = 0; i < after; i++) {
          data.col.push({
            value: "",
          });
        }
      },
      chosenDateHandler(item) {
        if (item.price) {
          data.currentDate = item.value;
          context.emit("change", item);
        }
      },
      reset() {
        data.currentDate = "";
      },
    }
    
    methods.init();
    return {
      ...toRefs(data),
      ...methods,
      moneyFormat
    }
  }
})
</script>

<style>
.hover-date:hover {
  background: #eee;
}
</style>
