<script setup>
import { date } from 'alga-js'
import { toast } from 'bulma-toast'

const router = useRouter()
const route = useRoute()
const activeSave = useActiveSave()

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

const form = ref({
  id: '',
  client: '',
  clientId: '',
  organization: '',
  organizationId: '',
  name: '',
  value: '',
  description: '',
  isActive: true,
  isDefault: false,
  isConfidentialInfo: false,
  creditWatchPercent: 0,
  priceMatchTolerance: 0,
  printColor: '',
  printColorId: '',
  priorityBase: 'Lower',
  priorityBaseId: 'L',
  priceList: '',
  priceListId: '',
  poPriceList: '',
  poPriceListId: '',
  discountSchema: '',
  discountSchemaId: '',
  poDiscountSchema: '',
  poDiscountSchemaId: '',
  dunning: '',
  dunningId: '',
  alert: {
    status: '',
    message: '',
    processing: false
  }
})

const back = () => {
  if(activeSave.value) {
    toast({
      message: 'Save button is active, please submit the data before leaving!',
      type: 'is-warning',
      duration: 2000
    })
  } else {
    router.push('/partners/partner-groups')
  }
}

const save = async (record) => {
  form.value = record
  
  if(form.value.name !== '') {
    activeSave.value = true
  } else {
    activeSave.value = false
  }
}

const submit = async (saveType = '') => {
  form.value.alert.processing = true
  activeSave.value = true

  const res = await $fetch('/api/partners/partner-groups/store', {
    method: 'POST',
    headers: useRequestHeaders(['cookie']),
    body: {
      organizationId: form.value.organizationId,
      name: form.value.name,
      isActive: form.value.isActive,
      value: form.value.value,
      description: form.value.description,
      isDefault: form.value.isDefault,
      isConfidentialInfo: form.value.isConfidentialInfo,
      creditWatchPercent: form.value.creditWatchPercent,
      priceMatchTolerance: form.value.priceMatchTolerance,
      printColorId: form.value.printColorId,
      priorityBaseId: form.value.priorityBaseId,
      priceListId: form.value.priceListId,
      poPriceListId: form.value.poPriceListId,
      discountSchemaId: form.value.discountSchemaId,
      poDiscountSchemaId: form.value.poDiscountSchemaId,
      dunningId: form.value.dunningId
    }
  })

  if(Number(res.status) === 200) {
    form.value.alert.processing = false
    activeSave.value = false
    if(saveType === 'preview') {
      router.push('/partners/partner-groups/'+res.id+'/view')
    } else if(saveType === 'changes') {
      router.push('/partners/partner-groups/'+res.id+'/edit')
    } else if(saveType === 'onlysave') {
      // Do nothing
      router.push('/partners/partner-groups/'+res.id+'/edit')
      toast({
        message: 'Saved!',
        type: 'is-success',
        duration: 2000
      })
    } else {
      back()
    }
  } else {
    //form.value.alert.status = res.status
    //form.value.alert.message = res.message
    form.value.alert.processing = false
    activeSave.value = false
    toast({
      message: res.message || 'Save error',
      type: 'is-danger',
      duration: 2000
    })
  }
}

const breadCrumb = computed(() => ([
  { link: '/partners/partners', text: 'Partners'},
  { link: '/partners/partner-groups', text: 'Partner Groups'},
  { link: '#', text: 'Add Partner Group', active: true}
]))
</script>

<template>
  <Head>
    <Title>Add Partner Group</Title>
  </Head>

  <NuxtLayout name="new-dash">
    <NewBreadCrumb :items="breadCrumb" />
    
    <div class="bannercontrol">
      <div class="d-flex gap-10px">
        <button type="button" @click="submit('onlysave')" class="button" :class="(form.name === '' || form.alert.processing) ? 'pointerEvents-none opacity-0.6' : ''">Save</button>
      </div>
      <div>
        <div class="d-flex gap-10px">
          <button v-if="activeSave" type="button" @click="activeSave = false; back();" class="button">
            <span class="tf-icon ti ti-square-x ti-xs me-1 color-inherit"></span> Abort
          </button>
          <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="banner">
      <div>
        <PartnersPartnerGroupForm v-model="form" @save="save" />
      </div>
      <div class="opacity-0.6 pointerEvents-none">
        <PartnersPartnerGroupDetailRecord action="create" :info="form" />
      </div>
    </div>
  </NuxtLayout>
</template>