<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: '',
  isActive: true,
  note: '',
  isSummary: false,
  isCommitment: false,
  committedAmt: 0,
  dateContract: date.now('date'),
  dateFinish: date.now('date'),
  processed: false,
  plannedAmt: 0,
  plannedQty: 0,
  plannedMarginAmt: 0,
  invoicedAmt: 0,
  invoicedQty: 0,
  projectBalanceAmt: 0,
  committedQty: 0,
  isCommitCeiling: false,
  currency: 'USD',
  currencyId: 100,
  poReference: '',
  partner: '',
  partnerId: '',
  partnerLocation: '',
  partnerLocationId: '',
  salesRep: '',
  salesRepId: '',
  priceListVersion: '',
  priceListVersionId: '',
  campaign: '',
  campaignId: '',
  user: '',
  userId: '',
  warehouse: '',
  warehouseId: '',
  projectCategory: '',
  projectCategoryId: '',
  projInvoiceRule: '',
  projInvoiceRuleId: '',
  projectLineLevel: 'Project',
  projectLineLevelId: 'P',
  partnerSR: '',
  partnerSRId: '',
  paymentTerm: '',
  paymentTermId: '',
  processing: false,
  alert: {
    status: '',
    message: ''
  }
})

const retrieve = async () => {
  const res = await $fetch('/api/projects/projects/'+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 || '',
      isActive: res?.IsActive ?? true,
      note: res?.Note || '',
      isSummary: res?.IsSummary ?? false,
      isCommitment: res?.IsCommitment ?? false,
      committedAmt: res?.CommittedAmt || 0,
      dateContract: res?.DateContract || date.now('date'),
      dateFinish: res?.DateFinish || date.now('date'),
      processed: res?.Processed ?? false,
      plannedAmt: res?.PlannedAmt || 0,
      plannedQty: res?.PlannedQty || 0,
      plannedMarginAmt: res?.PlannedMarginAmt || 0,
      invoicedAmt: res?.InvoicedAmt || 0,
      invoicedQty: res?.InvoicedQty || 0,
      projectBalanceAmt: res?.ProjectBalanceAmt || 0,
      committedQty: res?.CommittedQty || 0,
      isCommitCeiling: res?.IsCommitCeiling ?? false,
      poReference: res?.POReference || '',
      currency: res?.C_Currency_ID?.identifier || '',
      currencyId: res?.C_Currency_ID?.id || '',
      partner: res?.C_BPartner_ID?.identifier || '',
      partnerId: res?.C_BPartner_ID?.id || '',
      partnerLocation: res?.C_BPartner_Location_ID?.identifier || '',
      partnerLocationId: res?.C_BPartner_Location_ID?.id || '',
      salesRep: res?.SalesRep_ID?.identifier || '',
      salesRepId: res?.SalesRep_ID?.id || '',
      priceListVersion: res?.M_PriceList_Version_ID?.identifier || '',
      priceListVersionId: res?.M_PriceList_Version_ID?.id || '',
      campaign: res?.C_Campaign_ID?.identifier || '',
      campaignId: res?.C_Campaign_ID?.id || '',
      user: res?.AD_User_ID?.identifier || '',
      userId: res?.AD_User_ID?.id || '',
      warehouse: res?.M_Warehouse_ID?.identifier || '',
      warehouseId: res?.M_Warehouse_ID?.id || '',
      projectCategory: res?.ProjectCategory?.identifier || '',
      projectCategoryId: res?.ProjectCategory?.id || '',
      projInvoiceRule: res?.ProjInvoiceRule?.identifier || '',
      projInvoiceRuleId: res?.ProjInvoiceRule?.id || '',
      projectLineLevel: res?.ProjectLineLevel?.identifier || '',
      projectLineLevelId: res?.ProjectLineLevel?.id || '',
      partnerSR: res?.C_BPartnerSR_ID?.identifier || '',
      partnerSRId: res?.C_BPartnerSR_ID?.id || '',
      paymentTerm: res?.C_PaymentTerm_ID?.identifier || '',
      paymentTermId: res?.C_PaymentTerm_ID?.id || '',
      processing: false,
      alert: {
        status: '',
        message: ''
      }
    }
  }
}
onMounted(retrieve)

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

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: '/projects/projects', text: 'Projects'},
  { link: '#', text: 'View Project Details', active: true}
]))
</script>

<template>
  <Head>
    <Title>View Project 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>Search Key</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.value }}</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>Sales Rep.</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.salesRep }}</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>Line Level</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.projectLineLevel }}</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>Note</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.note }}</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>Contract Date</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.dateContract }}</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>Finish Date</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.dateFinish }}</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>Business Partner</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.partner }}</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>Partner Location</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.partnerLocation }}</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>Payment Term</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.paymentTerm }}</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>Warehouse</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.warehouse }}</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>Price List Version</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.priceListVersion }}</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>Partner Rep.</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.partnerSR }}</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>User/Contact</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.user }}</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>Order Reference</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.poReference }}</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>Campaign</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.campaign }}</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>Currency</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.currency }}</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>Planned Amount</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.plannedAmt }}</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>Planned Quantity</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.plannedQty }}</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>Planned Margin</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.plannedMarginAmt }}</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>Invoice Rule</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.projInvoiceRule }}</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>Committed Amount</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.committedAmt }}</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>Committed Quantity</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.committedQty }}</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>Invoiced Amount</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.invoicedAmt }}</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>Project Balance</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.projectBalanceAmt }}</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>Quantity Invoiced</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.invoicedQty }}</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="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.isSummary" class="checkInput" id="isSummary" :checked="form.isSummary" disabled>
                    <label class="checkLabel" for="isSummary">Summary</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isCommitment" class="checkInput" id="isCommitment" :checked="form.isCommitment" disabled>
                    <label class="checkLabel" for="isCommitment">Commitment</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isCommitCeiling" class="checkInput" id="isCommitCeiling" :checked="form.isCommitCeiling" disabled>
                    <label class="checkLabel" for="isCommitCeiling">Commit Ceiling</label>
                  </div>
                </div>
              </div>
            </div>
            
          </div>
          
        </div>
      </div>

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