<script setup>
const router = useRouter()
const route = useRoute()

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

const form = ref({
  id: '',
  organization: '',
  organizationId: '',
  client: '',
  clientId: '',
  name: '',
  description: '',
  isActive: true,
  help: '',
  processing: false,
  alert: {
    status: '',
    message: ''
  }
})

const retrieve = async () => {
  const res = await $fetch('/api/rma/rma-types/'+route.params.id, {
    headers: useRequestHeaders(['cookie'])
  })
  if(res) {
    form.value = {
      id: route.params.id,
      organization: res?.AD_Org_ID?.identifier || '',
      organizationId: res?.AD_Org_ID?.id || '',
      client: res?.AD_Client_ID?.identifier || '',
      clientId: res?.AD_Client_ID?.id || '',
      isActive: res?.IsActive ?? true,
      name: res?.Name || '',
      help: res?.Help || '',
      description: res?.Description || ''
    }
  }
}
onMounted(retrieve)

const back = () => {
  router.push('/settings/rma-types')
}

const save = async (record, id = '') => {
  const res = await $fetch('/api/rma/rma-types/update', {
    method: 'PUT',
    headers: useRequestHeaders(['cookie']),
    body: {
      id: record.id,
      organizationId: record.organizationId,
      name: record.name,
      description: record.description,
      isActive: record.isActive,
      help: record.help
    }
  })

  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: '#', text: 'Settings'},
  { link: '/settings/rma-types', text: 'RMA Types'},
  { link: '#', text: 'Add RMA Type', active: true}
]))
</script>

<template>
  <Head>
    <Title>Add RMA Type</Title>
  </Head>

  <NuxtLayout name="new-dash">
    <NewBreadCrumb :items="breadCrumb" />
    <div>
      <div class="banner">
        <div class="d-flex justify-between mb-3">
          <h4></h4>
          <div class="d-flex gap-0.75rem">
            <button type="button" @click="back" class="button">
              <span class="tf-icon ti ti-chevron-left ti-xs me-1 color-inherit"></span> Back
            </button>
          </div>
        </div>
        <div>
          <div class="columns">
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Name</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.name }}</div>
              </div>
            </div>
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Orgaanization</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.organization }}</div>
              </div>
            </div>
            
          </div>
          <div class="columns">
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Description</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.description }}</div>
              </div>
            </div>
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Tenant</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.client }}</div>
              </div>
            </div>
            
          </div>
          <div class="columns">
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Comment/Help</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.help }}</div>
              </div>
            </div>
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Status</strong>
                  <strong>:</strong>
                </div>
                <div class="check">
                  <input type="checkbox" v-model="form.isActive" class="checkInput" id="isActive" :checked="form.isActive" disabled>
                  <label class="checkLabel" for="isActive">Active</label>
                </div>
              </div>
            </div>
            
          </div>

        </div>
      </div>

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