<script setup>
import { date } from 'alga-js'
import { DatePicker } from 'tedir-calendar'
import { SelectBox } from 'tedir-select'
import { AttachBox, ThumbBox } from 'tedir-dropzone'

const props = defineProps({
  modelValue: Object,
  tab: String
})

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 loading = ref({})
const getOptions = ref({})
const setOptions = async (url, key) => {
  loading.value[key] = true
  const res = await $fetch('/api/'+url, {
    headers: useRequestHeaders(['cookie'])
  })
  if(res) {
    getOptions.value[key] = res
  }
  loading.value[key] = false
}

const form = ref({
  id: props.modelValue.id || '',
  uid: props.modelValue.uid || '',
  organization: props.modelValue.organization || organization.value?.Name || '',
  organizationId: props.modelValue.organizationId || organizationId.value || '',
  isActive: props.modelValue.isActive ?? true,
  name: props.modelValue.name || '',
  description: props.modelValue.description || '',
  value: props.modelValue.value || '',
  requestEMail: props.modelValue.requestEMail || '',
  requestFolder: props.modelValue.requestFolder || '',
  requestUser: props.modelValue.requestUser || '',
  isMultiLingualDocument: props.modelValue.isMultiLingualDocument ?? false,
  isSmtpAuthorization: props.modelValue.isSmtpAuthorization ?? false,
  isUseBetaFunctions: props.modelValue.isUseBetaFunctions ?? false,
  isPostImmediate: props.modelValue.isPostImmediate ?? false,
  isUseASP: props.modelValue.isUseASP ?? false,
  isSecureSMTP: props.modelValue.isSecureSMTP ?? false,
  language: props.modelValue.language || '',
  languageId: props.modelValue.languageId || 'en_US',
  autoArchive: props.modelValue.autoArchive || '',
  autoArchiveId: props.modelValue.autoArchiveId || 'N',
  mMPolicy: props.modelValue.mMPolicy || '',
  mMPolicyId: props.modelValue.mMPolicyId || 'F',
  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 save = () => {
  emit('update:modelValue', form.value)
  emit('save', form.value)
}
</script>

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

      <template v-if="tab === 'basic'">
        <div class="columns is-desktop">
          <div class="column is-two-thirds">
            <div class="columns is-desktop">
              <div class="column is-half">
                <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">Auto Archive</label>
                <select v-model="form.autoArchiveId" class="select" @change="save">
                  <option value="N">None</option>
                </select>
              </div>
              <div class="column is-half">
                <label class="label d-block">Language</label>
                <ClientOnly>
                  <SelectBox v-model="form.languageId" :options="getOptions?.['languages']?.records || [{id: 'en_US', Name: 'English (US)'}]" prop="Name" dataprop="id" datatype="string" :size="5" @click="setOptions('models/ad_language', 'languages')" :loading="loading?.['languages'] ?? true" @change="save" />
                </ClientOnly>

                <label class="label d-block mt-2">Material Policy</label>
                <select v-model="form.mMPolicyId" class="select" @change="save">
                  <option value="F">FiFo</option>
                </select>
              </div>
            </div>

            <label class="label d-block mt-2">Description</label>
            <textarea v-model="form.description" class="input" @keyup="save"></textarea>
          </div>
          <div class="column is-one-third">
            <label class="label d-block">Status</label>
            <div class="d-flex flexDirection-column px-3 gap-5px">
              <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.isUseBetaFunctions" class="checkInput" id="isUseBetaFunctions" :checked="form.isUseBetaFunctions" @change="save">
                <label class="checkLabel" for="isUseBetaFunctions">Use Beta Functions</label>
              </div>
              <div class="check">
                <input type="checkbox" v-model="form.isMultiLingualDocument" class="checkInput" id="isMultiLingualDocument" :checked="form.isMultiLingualDocument" @change="save">
                <label class="checkLabel" for="isMultiLingualDocument">Multi Lingual Documents</label>
              </div>
            </div>
          </div>
        </div>
      </template>
      <template v-else-if="tab === 'request'">
        <div class="columns is-desktop">
          <div class="column is-two-thirds">
            <div class="columns is-desktop">
              <div class="column is-half">
                <label class="label d-block">Mail Host</label>
                <input type="text" readonly class="input" />

                <label class="label d-block mt-2">Request EMail</label>
                <input type="text" v-model="form.requestEMail" class="input" @keyup="save" />

                <label class="label d-block mt-2">Request User</label>
                <input type="text" v-model="form.requestUser" class="input" @keyup="save" />
              </div>
              <div class="column is-half">
                <label class="label d-block">SMTP Port</label>
                <input type="text" readonly class="input" />

                <label class="label d-block mt-2">Request Folder</label>
                <input type="text" v-model="form.requestFolder" class="input" @keyup="save" />

                <label class="label d-block mt-2">Request User Password</label>
                <input type="text" readonly class="input" />
              </div>
            </div>
          </div>
          <div class="column is-one-third">
            <label class="label d-block">Request Status</label>
            <div class="d-flex flexDirection-column px-3 gap-5px">
              <div class="check">
                <input type="checkbox" v-model="form.isSmtpAuthorization" class="checkInput" id="isSmtpAuthorization" :checked="form.isSmtpAuthorization" @change="save">
                <label class="checkLabel" for="isSmtpAuthorization">SMTP Authentication</label>
              </div>
              <div class="check">
                <input type="checkbox" v-model="form.isSecureSMTP" class="checkInput" id="isSecureSMTP" :checked="form.isSecureSMTP" @change="save">
                <label class="checkLabel" for="isSecureSMTP">SMTP SSL/TLS</label>
              </div>
            </div>
          </div>
        </div>
      </template>
      <template v-else-if="tab === 'technical'">
        <div class="columns is-desktop">
          <div class="column is-one-third">
            <label class="label d-block">Model Validation Classes</label>
            <input type="text" readonly class="input" />
          </div>
          <div class="column is-one-third">
            <label class="label d-block">Replication Strategy</label>
            <input type="text" readonly class="input" />
          </div>
          <div class="column is-one-third">
            <label class="label d-block">Password Policies</label>
            <input type="text" readonly class="input" />
          </div>
        </div>
      </template>

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