<script setup lang="ts">
import { ref, watch } from "vue";
import { QForm, useQuasar } from "quasar";
import { useCounterMixin } from "@/stores/mixin";

import { useRouter } from "vue-router";
const $q = useQuasar();
const myForm = ref<QForm>();
const mixin = useCounterMixin();
const { dialogConfirm } = mixin;
const router = useRouter();
const routeName = router.currentRoute.value.name;
const amount = ref<number | null>();
const Org = ref<string | null>("");
const grandCross = ref<string | null>("");
const closeModal = () => {
  props.close();
};

const props = defineProps({
  modal: Boolean,
  save: {
    type: Function,
    default: () => console.log("not function"),
  },
  close: {
    type: Function,
    default: () => console.log("not function"),
  },
  insigniaList: {
    type: Array,
    default: [],
  },
});

watch(props, () => {
  if (props.modal === false) {
    grandCross.value = "";
    Org.value = "";
    amount.value = null;
  }
});

const clickSave = () => {
  dialogConfirm($q, () => props.save(grandCross.value, amount.value));
};
</script>

<template>
  <q-dialog v-model="props.modal" persistent>
    <q-card style="width: 800px">
      <q-toolbar class="q-py-sm">
        <q-toolbar-title class="text-h6">จัดสรรเครื่องราชฯ </q-toolbar-title>
        <q-btn
          icon="close"
          unelevated
          round
          dense
          @click="closeModal"
          style="color: #ff8080; background-color: #ffdede"
        />
      </q-toolbar>
      <q-separator />

      <q-form ref="myForm">
        <div class="q-pa-md">
          <div
            class="row col-12 items-center q-col-gutter-x-xs q-col-gutter-y-xs"
          >
            <div class="col-6">
              <q-select
                hide-bottom-space
                :options="insigniaList"
                dense
                borderless
                option-label="name"
                option-value="id"
                emit-value
                map-options
                outlined
                options-cover
                v-model="grandCross"
                :label="
                  routeName == 'insigniaAllocate'
                    ? `เครื่องราชฯ`
                    : `เลือกหน่วยงาน`
                "
              />
            </div>
            <div class="col-xs-12 col-sm-6">
              <q-input
                hide-bottom-space
                outlined
                class="inputgreen"
                v-model="amount"
                dense
                lazy-rules
                type="number"
                label="จำนวน"
              />
              <!-- :rules="[(val:any) =>val.length != 13 ||`${'กรุณากรอกเลขบัตรประจำตัวประชาชนให้ครบ'}`,]" -->
            </div>
          </div>
        </div>
      </q-form>

      <q-separator />
      <div class="row justify-end q-py-sm">
        <div class="q-px-md">
          <q-btn label="บันทึก" @click="clickSave" color="public" />
        </div>
      </div>
    </q-card>
  </q-dialog>
</template>
