<script setup lang="ts">
import useUserStore from 'src/stores/user';
import { onMounted } from 'vue';

const userStore = useUserStore();

const hqId = defineModel<string | null | undefined>('hqId');
const brId = defineModel<string | null | undefined>('brId');
const userType = defineModel<string>('userType');
const userRole = defineModel<string>('userRole');
const username = defineModel<string | null | undefined>('username');
const userCode = defineModel<string>('userCode');

defineProps<{
  dense?: boolean;
  outlined?: boolean;
  readonly?: boolean;
  separator?: boolean;
  usernameReadonly?: boolean;
}>();

async function selectHq(id: string) {
  if (!id) return;
  brId.value = '';
  userStore.userOption.brOpts = [];
  await userStore.fetchBrOption(id);
  if (userStore.userOption.brOpts.length === 1) {
    brId.value = userStore.userOption.brOpts[0].value;
  }
}

onMounted(async () => {
  if (userStore.userOption.hqOpts[0].value)
    await userStore.fetchBrOption(userStore.userOption.hqOpts[0].value);
});
</script>
<template>
  <div class="col-3 app-text-muted">
    • {{ $t('formDialogTitleInformation') }}
  </div>
  <div class="col-9 row q-col-gutter-md">
    <q-select
      id="select-hq-id"
      :dense="dense"
      outlined
      :readonly="readonly"
      :hide-dropdown-icon="readonly"
      emit-value
      map-options
      options-dense
      hide-bottom-space
      class="col-4"
      v-model="hqId"
      option-label="label"
      option-value="value"
      :label="$t('formDialogInputCode')"
      :options="userStore.userOption.hqOpts"
      :rules="[(val: string) => !!val || $t('formDialogInputHqIdValidate')]"
      @update:model-value="(val: string) => selectHq(val)"
    />
    <q-select
      id="select-br-id"
      :dense="dense"
      outlined
      :readonly="readonly"
      :hide-dropdown-icon="readonly"
      emit-value
      map-options
      options-dense
      clearable
      hide-bottom-space
      class="col-4"
      v-model="brId"
      :label="$t('formDialogInputBrId')"
      option-label="label"
      option-value="value"
      :options="userStore.userOption.brOpts"
    />
    <q-input
      for="input-username"
      :dense="dense"
      outlined
      :readonly="usernameReadonly"
      emit-value
      map-options
      options-dense
      hide-bottom-space
      class="col-4"
      :label="$t('formDialogInputUsername')"
      v-model="username"
      :rules="[
        (val: string) =>
          val.length > 2 || $t('formDialogInputUsernameValidate'),
      ]"
    />
    <q-select
      id="select-user-type"
      :dense="dense"
      outlined
      :readonly="readonly"
      :hide-dropdown-icon="readonly"
      emit-value
      map-options
      options-dense
      hide-bottom-space
      class="col-6"
      option-value="value"
      :label="$t('formDialogInputUserType')"
      v-model="userType"
      :options="userStore.userOption.userTypeOpts"
      :rules="[(val: string) => !!val || $t('formDialogInputUserTypeValidate')]"
    >
      <template v-slot:option="scope">
        <q-item v-if="scope.opt" v-bind="scope.itemProps">
          {{ $t(scope.opt.label) }}
        </q-item>
      </template>

      <template v-slot:selected-item="scope">
        <div v-if="scope.opt">
          {{ $t(scope.opt.label) }}
        </div>
      </template>
    </q-select>
    <q-select
      id="select-user-role"
      :dense="dense"
      outlined
      :readonly="readonly"
      :hide-dropdown-icon="readonly"
      emit-value
      map-options
      options-dense
      hide-bottom-space
      class="col-6"
      :label="$t('formDialogInputUserRole')"
      option-label="label"
      option-value="value"
      v-model="userRole"
      :options="userStore.userOption.roleOpts"
      :rules="[(val: string) => !!val || $t('formDialogInputUserRoleValidate')]"
    />
    <!-- <q-input
      id="input-user-code"
      :dense="dense"
      :outlined="readonly ? false : outlined"
      :borderless="readonly"
      readonly
      hide-bottom-space
      class="col-3"
      :label="$t('formDialogInputUserCode')"
      v-model="userCode"
    /> -->
  </div>
  <q-separator
    v-if="separator"
    class="col-12 q-mb-md"
    style="padding-block: 0.5px; margin-top: 32px"
  />
</template>
