<script setup>
import { date } from 'alga-js'
const router = useRouter()
const route = useRoute()

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

const form = ref({
  id: '',
  client: '',
  clientId: '',
  organization: '',
  organizationId: '',
  name: '',
  value: '',
  description: '',
  help: '',
  isActive: true,
  marketplace: '',
  marketplaceId: '',
  marketplaceUrl: '',
  marketplaceKey: '',
  marketplaceSecret: '',
  marketplaceToken: '',
  startingOrderId: '',
  startingOrderDate: date.now('date'),
  priceList: '',
  priceListId: '',
  priceListVersion: '',
  priceListVersionId: '',
  syncProductToIdempiere: false,
  syncProductFromIdempiere: false,
  syncOrderToIdempiere: false,
  syncOrderFromIdempiere: false,
  syncShipmentToIdempiere: false,
  syncShipmentFromIdempiere: false,
  isExcludetrackingmail: false,
  processing: false,
  alert: {
    status: '',
    message: '',
    processing: false
  }
})

const retrieve = async () => {
  const res = await $fetch('/api/settings/order-sources/'+route.params.id, {
    headers: useRequestHeaders(['cookie'])
  })
  if(res) {
    form.value = {
      id: route.params.id,
      client: res?.AD_Client_ID?.identifier || '',
      clientId: res?.AD_Client_ID?.id || '',
      organization: res?.AD_Org_ID?.identifier || '',
      organizationId: res?.AD_Org_ID?.id || '',
      name: res?.Name || '',
      value: res?.Value || '',
      description: res?.description || '',
      help: res?.Help || '',
      isActive: res?.IsActive ?? true,
      marketplace: res?.Marketplace?.identifier || '',
      marketplaceId: res?.Marketplace?.id || '',
      marketplaceUrl: res?.marketplace_url || '',
      marketplaceKey: res?.marketplace_key || '',
      marketplaceSecret: res?.marketplace_secret || '',
      marketplaceToken: res?.marketplace_token || '',
      startingOrderId: res?.starting_order_id || '',
      startingOrderDate: res?.starting_order_date || date.now('date'),
      priceList: res?.M_PriceList_ID?.identifier || '',
      priceListId: res?.M_PriceList_ID?.id || '',
      priceListVersion: res?.M_PriceList_Version_ID?.identifier || '',
      priceListVersionId: res?.M_PriceList_Version_ID?.id || '',
      syncProductToIdempiere: res?.syncProductToIdempiere ?? false,
      syncProductFromIdempiere: res?.syncProductFromIdempiere ?? false,
      syncOrderToIdempiere: res?.syncOrderToIdempiere ?? false,
      syncOrderFromIdempiere: res?.syncOrderFromIdempiere ?? false,
      syncShipmentToIdempiere: res?.syncShipmentToIdempiere ?? false,
      syncShipmentFromIdempiere: res?.syncShipmentFromIdempiere ?? false,
      isExcludetrackingmail: res?.isExcludetrackingmail ?? res?.IsExcludetrackingmail ?? false,
      processing: false,
      alert: {
        status: '',
        message: '',
        processing: false
      }
    }
  }
}
onMounted(retrieve)

const back = () => {
  router.push('/settings/order-sources')
}
const edit = () => {
  router.push(`/settings/order-sources/${route.params.id}/edit`)
}
const destroy = async () => {
  const confirmation = confirm('Are you sure want to delete this record?')
  if(confirmation) {
    await $fetch('/api/settings/order-sources/'+route.params.id, {
      method: 'DELETE',
      headers: useRequestHeaders(['cookie'])
    })
    router.push(`/settings/order-sources`)
  }
}

const dropDownHandler = (e) => {
  if(e.target.parentElement.classList.contains('show')) {
    e.target.parentElement.classList.remove('show')
  } else {
    e.target.parentElement.classList.add('show')
  }
}

const dropItemHandler = (e) => {
  if(e.target.parentElement.parentElement.classList.contains('show')) {
    e.target.parentElement.parentElement.classList.remove('show')
  } else {
    e.target.parentElement.parentElement.classList.add('show')
  }
}

const breadCrumb = computed(() => ([
  { link: '/settings/order-sources', text: 'Order Sources'},
  { link: '#', text: 'View Order Source Details', active: true}
]))
</script>

<template>
  <Head>
    <Title>View Order Source Details</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">
            <div>
              <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 class="dropDown">
              <button type="button" class="button dropDownToggler" @click="dropDownHandler">
                <span class="tf-icon ti ti-settings ti-xs me-1 color-inherit"></span> Action
              </button>
              <div class="dropDownBackdrop" @click="dropDownHandler"></div>
              <div class="dropDownMenu dropDownRight mnw-80px">
                <span class="dropDownItem" @click="dropItemHandler($event); edit();">Edit</span>
                <span class="dropDownItem" @click="dropItemHandler($event); destroy();">Delete</span>
              </div>
            </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>Tenant</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.client }}</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>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>Search Key</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.value }}</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>Comment/Help</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.help }}</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>Status</strong>
                  <strong>:</strong>
                </div>
                <div class="d-flex flexDirection-column">
                  <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 class="check">
                    <input type="checkbox" v-model="form.isShopware6" class="checkInput" id="isShopware6" :checked="form.isShopware6" disabled>
                    <label class="checkLabel" for="isShopware6">Is Shopware 6</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isWebshopToIdempiere" class="checkInput" id="isWebshopToIdempiere" :checked="form.isWebshopToIdempiere" disabled>
                    <label class="checkLabel" for="isWebshopToIdempiere">Is Shopware To Idempiere</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isIdempiereToWebshop" class="checkInput" id="isIdempiereToWebshop" :checked="form.isIdempiereToWebshop" disabled>
                    <label class="checkLabel" for="isIdempiereToWebshop">Is Idempiere To Shopware</label>
                  </div>-->
                </div>
              </div>
            </div>
            <div class="column is-half"></div>
          </div>
          
        </div>
      </div>

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