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

const mixin = useCounterMixin();
const { dialogConfirm } = mixin;
const $q = useQuasar();

const title = ref<string>("");
const amount = ref<number>();
const props = defineProps({
  modal: Boolean,
  save: {
    type: Function,
    default: () => console.log("not function"),
  },
  close: {
    type: Function,
    default: () => console.log("not function"),
  },
  insigniadata: {
    type: Object,
    default: [],
  },
  actionType: {
    type: String,
  },
});
watch(props, () => {
  if (props.modal === true) {
    amount.value = props.insigniadata.total;
    if (props.actionType === "insignia") {
      title.value = props.insigniadata.insignia;
    } else title.value = props.insigniadata.organization;
  }
});
const closeModal = () => {
  props.close();
};
const clickSave = () => {
  console.log(props.insigniadata);

  dialogConfirm($q, () => {
    if (props.actionType === "insignia") {
      props.save(
        props.insigniadata.id,
        props.insigniadata.insigniaId,
        amount.value,
        props.insigniadata.year
      );
    } else {
      props.save(props.insigniadata.id, 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-xs-12 col-sm-6">
              <q-input
                hide-bottom-space
                outlined
                class="inputgreen"
                v-model="title"
                dense
                lazy-rules
                type="text"
                label="จำนวน"
                disable
              />
            </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="จำนวน"
              />
            </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>

<style lang="scss" scoped></style>
