<script setup>
import { SelectBox } from 'tedir-select'

const props = defineProps({
  modelValue: Object
})

const emit = defineEmits(['update:modelValue', 'save'])

const organizations = useCookie('logship_organizations')
const organization = useCookie('logship_organization')
const organizationId = useCookie('logship_organization_id')

const clientName = ref('')
onMounted(() => {
  const getClient = localStorage.getItem('logship_clients')
  if(getClient) {
    const clientObj = JSON.parse(getClient)
    clientName.value = clientObj[0].name
  }
})

const form = ref({
  id: props.modelValue.id || '',
  uid: props.modelValue.uid || '',
  client: props.modelValue.client || '',
  clientId: props.modelValue.clientId || '',
  organization: props.modelValue.organization || organization.value?.Name || '',
  organizationId: props.modelValue.organizationId || organizationId.value || '',
  name: props.modelValue.name || '',
  description: props.modelValue.description || '',
  isActive: props.modelValue.isActive ?? true,
  help: props.modelValue.help || '',
  isOwned: props.modelValue.isOwned ?? false,
  isDepreciated: props.modelValue.isDepreciated ?? false,
  isOneAssetPerUOM: props.modelValue.isOneAssetPerUOM ?? false,
  isDefault: props.modelValue.isDefault ?? false,
  isCreateAsActive: props.modelValue.isCreateAsActive ?? false,
  isTrackIssues: props.modelValue.isTrackIssues ?? false,
  isFixedAsset: props.modelValue.isFixedAsset ?? false,
  processing: props.modelValue.processing ?? false,
  alert: {
    status: props.modelValue.alert.status || '',
    message: props.modelValue.alert.message || '',
    processing: props.modelValue.alert.processing ?? false,
  }
})

watch(() => props.modelValue, () => {
  form.value = props.modelValue
})

const debounce = (func, timeout = 300) => {
  let timer;
  return (...args) => {
    clearTimeout(timer)
    timer = setTimeout(() => { func.apply(this, args); }, timeout)
  }
}

const save = debounce(() => {
  emit('update:modelValue', form.value)
  emit('save', form.value)
})
</script>

<template>
  <div @mouseleave.self="save">
    <div class="my-5 mx-5">
      <AlertAction v-model='form.alert.status' :alert="form.alert" />

      <div class="columns is-desktop">
        <div class="column is-two-thirds">
          <label class="label d-block">Name *</label>
          <input type="text" v-model="form.name" class="input" @keyup="save" />

          <label class="label d-block mt-2">Description</label>
          <textarea v-model="form.description" class="input" @keyup="save"></textarea>

          <label class="label d-block mt-2">Comment/Help</label>
          <textarea v-model="form.help" class="input" @keyup="save"></textarea>
        </div>
        <div class="column is-one-third">
          <label class="label d-block">Organization *</label>
          <SelectBox v-model="form.organizationId" :options="getOptions?.['orgs']?.records || [{id: form.organizationId, Name: form.organization}]" prop="Name" dataprop="id" datatype="string" :size="5" @click="setOptions('models/ad_org', 'orgs')" @change="(val, opt) => {form.organization = opt.Name; save();}" :loading="loading?.['orgs'] ?? true" />

          <label class="label d-block mt-2">Tenant *</label>
          <input type="text" :value="clientName" class="input" readonly />

          <label class="label d-block mt-2">Status</label>
          <div class="d-flex flexDirection-column">
            <div class="check">
              <input type="checkbox" v-model="form.isActive" class="checkInput" id="isActive" :checked="form.isActive" @change="save">
              <label class="checkLabel" for="isActive">Active</label>
            </div>
            <div class="check">
              <input type="checkbox" v-model="form.isDefault" class="checkInput" id="isDefault" :checked="form.isDefault" @change="save">
              <label class="checkLabel" for="isDefault">Default</label>
            </div>
            <div class="check">
              <input type="checkbox" v-model="form.isOwned" class="checkInput" id="isOwned" :checked="form.isOwned">
              <label class="checkLabel" for="isOwned">Owned</label>
            </div>
            <div class="check">
              <input type="checkbox" v-model="form.isDepreciated" class="checkInput" id="isDepreciated" :checked="form.isDepreciated" @change="save">
              <label class="checkLabel" for="isDepreciated">Depreciated</label>
            </div>
            <div class="check">
              <input type="checkbox" v-model="form.isOneAssetPerUOM" class="checkInput" id="isOneAssetPerUOM" :checked="form.isOneAssetPerUOM" @change="save">
              <label class="checkLabel" for="isOneAssetPerUOM">One Asset per UOM</label>
            </div>
            <div class="check">
              <input type="checkbox" v-model="form.isCreateAsActive" class="checkInput" id="isCreateAsActive" :checked="form.isCreateAsActive" @change="save">
              <label class="checkLabel" for="Create as Active">Create as Active</label>
            </div>
            <div class="check">
              <input type="checkbox" v-model="form.isTrackIssues" class="checkInput" id="isTrackIssues" :checked="form.isTrackIssues" @change="save">
              <label class="checkLabel" for="isTrackIssues">Track Issues</label>
            </div>
            <div class="check">
              <input type="checkbox" v-model="form.isFixedAsset" class="checkInput" id="isFixedAsset" :checked="form.isFixedAsset" @change="save">
              <label class="checkLabel" for="isFixedAsset">Is Fixed Asset</label>
            </div>
          </div>
        </div>
      </div>

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