<script setup>
const router = useRouter()

definePageMeta({
  middleware: ['auth', 'org']
})

const form = ref({
  id: '',
  organizationId: '',
  isActive: true,
  code: '',
  name: '',
  isDefault: false,
  processing: false,
  alert: {
    status: '',
    message: ''
  }
})

const back = () => {
  router.push('/admin/print-colors')
}

const save = async (record, id = '') => {
  const res = await $fetch('/api/admin/print-colors/store', {
    method: 'POST',
    headers: useRequestHeaders(['cookie']),
    body: {
      organizationId: record.organizationId,
      isActive: record.isActive,
      code: record.code,
      name: record.name,
      isDefault: record.isDefault,
    }
  })

  if(Number(res.status) === 200) {
    back()
  } else {
    form.value.alert.status = res.status
    form.value.alert.message = res.message
    form.value.processing = false
  }
}

const breadCrumb = computed(() => ([
  { link: '/admin', text: 'Admin'},
  { link: '/admin/print-colors', text: 'Print Colors'},
  { link: '#', text: 'Add Print Color', active: true}
]))
</script>

<template>
  <Head>
    <Title>Add Print Color</Title>
  </Head>

  <NuxtLayout name="admin">
    <div>
      <BreadCrumb :items="breadCrumb" class="mb-1.5rem" />

      <div class="card card-action mb-5">
        <div class="card-alert"></div>
        <div class="card-header">
          <h4 class="card-action-title">Add Print Color</h4>
          <div class="card-action-element">
            <button type="button" @click="back" class="btn btn-ms btn-secondary waves-effect waves-light">
              <span class="tf-icon ti ti-chevron-left ti-xs me-1"></span> Back
            </button>
          </div>
        </div>
        <div class="collapse show">
          <AdminPrintColorForm v-model="form" @save="save"/>
        </div>
      </div>

    </div>
  </NuxtLayout>
</template>