<template>
  <!-- Nội dung chính - chỉ hiển thị form cá nhân (màn hình chọn đã được xử lý ở SearchBox) -->
  <div class="columns mx-0 px-0 py-2">
    <div
      :class="['column is-narrow p-0 pr-4', store.viewport === 1 && 'px-0']"
      :style="{ borderRight: store.viewport > 1 ? '1px solid var(--bulma-border)' : 'initial' }"
    >
      <div :class="['buttons is-align-items-stretch', store.viewport > 1 && 'is-flex-direction-column']">
        <button
          v-for="(v, i) in tabs"
          :key="i"
          :class="['button is-justify-content-flex-start', v.code === tab ? 'is-primary' : 'is-light']"
          @click="changeTab(v)"
        >
          {{ isVietnamese ? v.name : v.en }}
        </button>
      </div>
    </div>
    <div :class="['column', { 'px-0': store.viewport === 1 }]">
      <CustomerForm
        v-if="tab === 'info' && record !== undefined"
        v-bind="{ row: record, pagename, application }"
        @update="update"
        @close="emit('close')"
      />
      <template v-if="record">
        <ImageGallery
          v-bind="{
            row: record,
            pagename: pagename,
            show: ['delete'],
            api: 'customerfile',
          }"
          @update="update"
          v-if="tab === 'image'"
        />
        <CustomerView
          v-bind="{ row: record, pagename: pagename }"
          @update="update"
          @close="emit('close')"
          v-if="tab === 'print'"
        />
      </template>
    </div>
  </div>
  <Modal
    v-if="showmodal"
    v-bind="showmodal"
    @close="showmodal.value = undefined"
  />
</template>
<script setup>
import CustomerForm from "~/components/customer/CustomerForm.vue";
import CustomerView from "~/components/customer/CustomerView.vue";
import Modal from "~/components/Modal.vue";
import ImageGallery from "~/components/media/ImageGallery.vue";
const { $dialog } = useNuxtApp();

const store = useStore();
const props = defineProps({
  pagename: String,
  row: Object,
  application: Object,
  isEditMode: Boolean,
  handleCustomer: Function,
});

const isVietnamese = computed(() => store.lang === "vi");
const emit = defineEmits(["modalevent", "close"]);
const tabs = [
  { code: "info", name: "1. Thông tin", en: "1. Information", active: true },
  { code: "image", name: "2. Hình ảnh", en: "2. Images", active: false },
  { code: "print", name: "3. Bản in", en: "3. Print", active: false },
];
const tab = ref("info");
let record = props.row || null;
const showmodal = ref();

function changeTab(v) {
  if (tab.value === v.code) return;
  if (!record) return $dialog("Vui lòng <b>lưu dữ liệu</b> trước khi chuyển sang mục tiếp theo", "Thông báo");
  tab.value = v.code;
}

function update(v) {
  record = {
    ...v,
    label: `${v.code} / ${v.fullname} / ${v.phone || ""}`,
  };
  emit("modalevent", { name: "dataevent", data: record });
  if (!props.isEditMode) emit("close");
}
</script>
