<script setup lang="ts">
import { ref, watch } from "vue";
import { useQuasar } from "quasar";

import http from "@/plugins/http";
import config from "@/app.config";
import { useRouter } from "vue-router";
import { useCounterMixin } from "@/stores/mixin";
import { useCommandMainStore } from "@/modules/18_command/store/Main";

import type { ListCommand } from "@/modules/18_command/interface/index/Main";

import DialogHeader from "@/components/DialogHeader.vue";

const storeCommand = useCommandMainStore();
const router = useRouter();
const $q = useQuasar();
const mixin = useCounterMixin();
const {
  showLoader,
  success,
  messageError,
  dialogConfirm,
  hideLoader,
  date2Thai,
} = mixin;

const modal = defineModel<boolean>("modal", { required: true });
const props = defineProps({
  commandTypeCode: String, // ไอดีประเภทคำสั่ง
});

const commandOp = ref<ListCommand[]>([]); // ประเภทคำสั่ง
const commandType = ref<string>(""); //ประเภทคำสั่ง
const commandNo = ref<string>(""); //คำสั่งเลขที่
const commandYear = ref<number>(new Date().getFullYear());
const commandAffectDate = ref<Date | null>(null); //วันที่ลงนาม
const commandExcecuteDate = ref<Date | null>(null); //วันที่คำสั่งมีผล

/**
 * ฟังก์ชันสร้างคำสั่งใหม่
 */
function onSubmit() {
  dialogConfirm($q, () => {});
}

/** ปิด popup */
function closeModal() {
  modal.value = false;
  clearValue();
}

function clearValue() {
  commandNo.value = "";
  commandYear.value = new Date().getFullYear();
  commandAffectDate.value = null;
  commandExcecuteDate.value = null;
}

/** ดึงข้อมูลคำสั่ง */
async function fetchCommandType() {
  const data = await storeCommand.getCommandTypes(); // ยิงไป get ที่ store
  // filter เฉพาะ code ของคำสั่งที่เลือก
  commandOp.value = await data.filter(
    (v: any) => v.code == props.commandTypeCode
  );
  commandType.value = commandOp.value[0].id;
}

watch(modal, () => {
  if (modal.value) {
    fetchCommandType();
  }
});
</script>

<template>
  <q-dialog v-model="modal" persistent>
    <q-card style="min-width: 50vw">
      <q-form greedy @submit.prevent @validation-success="onSubmit">
        <DialogHeader :tittle="'สร้างคำสั่ง'" :close="closeModal" />
        <q-separator />
        <q-card-section style="max-height: 50vh" class="scroll">
          <div class="row q-sm q-col-gutter-sm">
            <div class="col-12">
              <q-select
                v-model="commandType"
                :label="`${'ประเภทคำสั่ง'}`"
                option-label="name"
                :options="commandOp"
                option-value="id"
                dense
                emit-value
                map-options
                lazy-rules
                use-input
                hide-bottom-space
                outlined
                readonly
              >
                <template v-slot:no-option>
                  <q-item>
                    <q-item-section class="text-grey">
                      ไม่มีข้อมูล
                    </q-item-section>
                  </q-item>
                </template>
              </q-select>
            </div>
            <div class="col-6">
              <q-input
                class="inputgreen"
                outlined
                dense
                v-model="commandNo"
                :label="`${'คำสั่งเลขที่'}`"
              />
            </div>
            <label class="col-1 flex justify-center items-center text-bold"
              >/</label
            >

            <div class="col-5">
              <datepicker
                menu-class-name="modalfix"
                v-model="commandYear"
                :locale="'th'"
                autoApply
                year-picker
                :enableTimePicker="false"
                class="inputgreen"
              >
                <template #year="{ year }">{{ year + 543 }}</template>
                <template #year-overlay-value="{ value }">{{
                  parseInt(value + 543)
                }}</template>
                <template #trigger>
                  <q-input
                    dense
                    outlined
                    hide-bottom-space
                    :model-value="
                      commandYear == null ? null : commandYear + 543
                    "
                    :label="`${'พ.ศ.'}`"
                    :rules="[(val:string) => !!val || `${'กรุณากรอก พ.ศ.'}`]"
                  >
                    <template v-slot:prepend>
                      <q-icon
                        name="event"
                        class="cursor-pointer"
                        style="color: var(--q-primary)"
                      >
                      </q-icon>
                    </template>
                  </q-input>
                </template>
              </datepicker>
            </div>

            <div class="col-6">
              <datepicker
                clearable
                menu-class-name="modalfix"
                v-model="commandAffectDate"
                :locale="'th'"
                autoApply
                :enableTimePicker="false"
                class="inputgreen"
              >
                <template #year="{ year }">
                  {{ year + 543 }}
                </template>
                <template #year-overlay-value="{ value }">
                  {{ parseInt(value + 543) }}
                </template>
                <template #trigger>
                  <q-input
                    clearable
                    dense
                    outlined
                    hide-bottom-space
                    :model-value="
                      commandAffectDate == null
                        ? null
                        : date2Thai(commandAffectDate)
                    "
                    :label="`${'วันที่ลงนาม'}`"
                    @clear="() => (commandAffectDate = null)"
                  >
                    <template v-slot:prepend>
                      <q-icon
                        name="event"
                        class="cursor-pointer"
                        style="color: var(--q-primary)"
                      >
                      </q-icon>
                    </template>
                  </q-input>
                </template>
              </datepicker>
            </div>

            <div class="col-6">
              <datepicker
                clearable
                menu-class-name="modalfix"
                v-model="commandExcecuteDate"
                :locale="'th'"
                autoApply
                :enableTimePicker="false"
                class="inputgreen"
              >
                <template #year="{ year }">
                  {{ year + 543 }}
                </template>
                <template #year-overlay-value="{ value }">
                  {{ parseInt(value + 543) }}
                </template>
                <template #trigger>
                  <q-input
                    clearable
                    dense
                    outlined
                    hide-bottom-space
                    class="inputgreen"
                    :model-value="
                      commandExcecuteDate == null
                        ? null
                        : date2Thai(commandExcecuteDate)
                    "
                    :label="`${'วันที่คำสั่งมีผล'}`"
                    @clear="() => (commandExcecuteDate = null)"
                  >
                    <template v-slot:prepend>
                      <q-icon
                        name="event"
                        class="cursor-pointer"
                        style="color: var(--q-primary)"
                      >
                      </q-icon>
                    </template>
                  </q-input>
                </template>
              </datepicker>
            </div>
          </div>
        </q-card-section>

        <q-separator />

        <q-card-actions align="right">
          <!-- <q-btn
          label="บันทึกและเลือกรายชื่อต่อ"
          @click="() => onSubmit(false)"
          :disable="
            selectCreate === 'NEW' ? commandType == '' : selected.length == 0
          "
          color="blue"
        >
          <q-tooltip>บันทึกและเลือกรายชื่อต่อ</q-tooltip>
        </q-btn> -->
          <q-btn
            label="บันทึกและไปยังหน้าคำสั่ง"
            :disable="commandType == ''"
            type="submit"
            color="public"
          >
            <q-tooltip>บันทึกและไปยังหน้าคำสั่ง</q-tooltip>
          </q-btn>
        </q-card-actions>
      </q-form>
    </q-card>
  </q-dialog>
</template>

<style scoped></style>
