<template>
  <div>
    <div v-if="formState.ready">
      <gz-error :error-box-message="formState.errorBoxMessage"></gz-error>
      <v-form ref="form">
        <v-row>
          <v-col cols="12">
            <div class="text-h4 primary--text">
              {{ $ay.t("BusinessSettings") }}
            </div>
          </v-col>

          <v-col cols="12" sm="6" lg="4" xl="3">
            <gz-pick-list
              :aya-type="ayaTypes().TaxCode"
              show-edit-icon
              v-model="obj.taxPartPurchaseId"
              :readonly="formState.readOnly"
              :label="$ay.t('GlobalTaxPartPurchaseID')"
              ref="taxPartPurchaseId"
              data-cy="taxPartPurchaseId"
              :error-messages="form().serverErrors(this, `taxPartPurchaseId`)"
              @input="fieldValueChanged(`taxPartPurchaseId`)"
            ></gz-pick-list>
          </v-col>

          <v-col cols="12" sm="6" lg="4" xl="3">
            <gz-pick-list
              :aya-type="ayaTypes().TaxCode"
              show-edit-icon
              v-model="obj.taxRateSaleId"
              :readonly="formState.readOnly"
              :label="$ay.t('GlobalTaxRateSaleID')"
              ref="taxRateSaleId"
              data-cy="taxRateSaleId"
              :error-messages="form().serverErrors(this, `taxRateSaleId`)"
              @input="fieldValueChanged(`taxRateSaleId`)"
            ></gz-pick-list>
          </v-col>

          <v-col cols="12" sm="6" lg="4" xl="3">
            <gz-pick-list
              :aya-type="ayaTypes().TaxCode"
              show-edit-icon
              v-model="obj.taxPartSaleId"
              :readonly="formState.readOnly"
              :label="$ay.t('GlobalTaxPartSaleID')"
              ref="taxPartSaleId"
              data-cy="taxPartSaleId"
              :error-messages="form().serverErrors(this, `taxPartSaleId`)"
              @input="fieldValueChanged(`taxPartSaleId`)"
            ></gz-pick-list>
          </v-col>

          <v-col cols="12" sm="6" lg="4" xl="3">
            <v-checkbox
              v-model="obj.searchCaseSensitiveOnly"
              :readonly="formState.readOnly"
              :label="$ay.t('GlobalSearchCaseSensitiveOnly')"
              ref="searchCaseSensitiveOnly"
              data-cy="searchCaseSensitiveOnly"
              @change="fieldValueChanged('searchCaseSensitiveOnly')"
            ></v-checkbox>
          </v-col>

          <v-col cols="12" sm="6" lg="4" xl="3">
            <v-checkbox
              v-model="obj.useInventory"
              :readonly="formState.readOnly"
              :label="$ay.t('GlobalUseInventory')"
              ref="useInventory"
              data-cy="useInventory"
              @change="fieldValueChanged('useInventory')"
            ></v-checkbox>
          </v-col>

          <v-col cols="12" sm="6" lg="4" xl="3">
            <gz-duration-picker
              v-model="obj.workOrderCompleteByAge"
              :readonly="formState.readOnly"
              :label="$ay.t('GlobalWorkOrderCompleteByAge')"
              :show-seconds="false"
              ref="workOrderCompleteByAge"
              data-cy="workOrderCompleteByAge"
              :error-messages="
                form().serverErrors(this, 'workOrderCompleteByAge')
              "
              @input="fieldValueChanged('workOrderCompleteByAge')"
            ></gz-duration-picker>
          </v-col>

          <v-col cols="12" sm="6" lg="4" xl="3">
            <v-checkbox
              v-model="obj.allowScheduleConflicts"
              :readonly="formState.readOnly"
              :label="$ay.t('GlobalAllowScheduleConflicts')"
              ref="allowScheduleConflicts"
              data-cy="allowScheduleConflicts"
              @change="fieldValueChanged('allowScheduleConflicts')"
            ></v-checkbox>
          </v-col>

          <v-col cols="12" sm="6" lg="4" xl="3">
            <v-text-field
              v-model="obj.workLaborScheduleDefaultMinutes"
              :readonly="formState.readOnly"
              :label="$ay.t('GlobalLaborSchedUserDfltTimeSpan')"
              ref="workLaborScheduleDefaultMinutes"
              data-cy="workLaborScheduleDefaultMinutes"
              :rules="[
                form().integerValid(this, 'workLaborScheduleDefaultMinutes'),
                form().required(this, 'workLaborScheduleDefaultMinutes')
              ]"
              :error-messages="
                form().serverErrors(this, 'workLaborScheduleDefaultMinutes')
              "
              @input="fieldValueChanged('workLaborScheduleDefaultMinutes')"
            ></v-text-field>
          </v-col>

          <v-col cols="12" sm="6" lg="4" xl="3">
            <v-text-field
              v-model="obj.workOrderTravelDefaultMinutes"
              :readonly="formState.readOnly"
              :label="$ay.t('GlobalTravelDfltTimeSpan')"
              ref="workOrderTravelDefaultMinutes"
              data-cy="workOrderTravelDefaultMinutes"
              :rules="[
                form().integerValid(this, 'workOrderTravelDefaultMinutes'),
                form().required(this, 'workOrderTravelDefaultMinutes')
              ]"
              :error-messages="
                form().serverErrors(this, 'workOrderTravelDefaultMinutes')
              "
              @input="fieldValueChanged('workOrderTravelDefaultMinutes')"
            ></v-text-field>
          </v-col>
          <v-col cols="12" sm="6" lg="4" xl="3">
            <v-btn to="adm-global-seeds">{{
              $ay.t("GlobalNextSeeds")
            }}</v-btn></v-col
          >
          <v-col cols="12" class="mt-8">
            <span class="text-h4 primary--text">
              {{ $ay.t("UserInterfaceSettings") }}</span
            >
          </v-col>
          <v-col cols="12" sm="6" lg="4" xl="3">
            <v-btn to="adm-global-logo">{{ $ay.t("GlobalLogo") }}</v-btn>
          </v-col>
          <v-col cols="12" sm="6" lg="4" xl="3">
            <v-btn to="adm-global-select-templates">{{
              $ay.t("PickListTemplates")
            }}</v-btn></v-col
          >

          <v-col cols="12" class="mt-4">
            <div class="text-subtitle-1">
              {{ $ay.t("CustomerSignature") }}
            </div>
          </v-col>

          <v-col cols="12" sm="6" lg="4" xl="3">
            <v-text-field
              v-model="obj.signatureTitle"
              :readonly="formState.readOnly"
              :label="$ay.t('GlobalSignatureTitle')"
              ref="signatureTitle"
              data-cy="signatureTitle"
              :error-messages="form().serverErrors(this, 'signatureTitle')"
              @input="fieldValueChanged('signatureTitle')"
            ></v-text-field>
          </v-col>

          <v-col cols="12" sm="6" lg="4" xl="3">
            <v-text-field
              v-model="obj.signatureHeader"
              :readonly="formState.readOnly"
              :label="$ay.t('GlobalSignatureHeader')"
              ref="signatureHeader"
              data-cy="signatureHeader"
              :error-messages="form().serverErrors(this, 'signatureHeader')"
              @input="fieldValueChanged('signatureHeader')"
            ></v-text-field>
          </v-col>

          <v-col cols="12" sm="6" lg="4" xl="3">
            <v-text-field
              v-model="obj.signatureFooter"
              :readonly="formState.readOnly"
              :label="$ay.t('GlobalSignatureFooter')"
              ref="signatureFooter"
              data-cy="signatureFooter"
              :error-messages="form().serverErrors(this, 'signatureFooter')"
              @input="fieldValueChanged('signatureFooter')"
            ></v-text-field>
          </v-col>

          <v-col cols="12" class="mt-8">
            <div class="text-h4 primary--text">
              {{ $ay.t("CustomerAccessSettings") }}
            </div>
          </v-col>
          <!-- ######################################################## -->
          <v-col cols="12">
            <div class="text-subtitle-1">
              {{ $ay.t("CustomerServiceRequestList") }}
            </div>
          </v-col>
          <v-col cols="12" sm="6" lg="4" xl="3">
            <v-checkbox
              v-model="obj.customerAllowCSR"
              :readonly="formState.readOnly"
              :label="$ay.t('Active')"
              ref="customerAllowCSR"
              data-cy="customerAllowCSR"
              :error-messages="form().serverErrors(this, 'customerAllowCSR')"
              @change="fieldValueChanged('customerAllowCSR')"
            ></v-checkbox>
          </v-col>
          <v-col cols="12" sm="6" lg="4">
            <gz-tag-picker
              v-model="obj.customerAllowCSRInTags"
              :readonly="formState.readOnly"
              :label="$ay.t('TaggedWith')"
              ref="customerAllowCSRInTags"
              data-cy="customerAllowCSRInTags"
              :error-messages="
                form().serverErrors(this, 'customerAllowCSRInTags')
              "
              @input="fieldValueChanged('customerAllowCSRInTags')"
            ></gz-tag-picker>
          </v-col>
          <v-col cols="12" sm="6" lg="4">
            <gz-tag-picker
              v-model="obj.customerAllowCSROutTags"
              :readonly="formState.readOnly"
              :label="$ay.t('NotTaggedWith')"
              ref="customerAllowCSROutTags"
              data-cy="customerAllowCSROutTags"
              :error-messages="
                form().serverErrors(this, 'customerAllowCSROutTags')
              "
              @input="fieldValueChanged('customerAllowCSROutTags')"
            ></gz-tag-picker>
          </v-col>
          <v-col cols="12">
            <v-textarea
              v-model="obj.customerServiceRequestInfoHTML"
              :readonly="formState.readOnly"
              :label="$ay.t('CSRInfoHTML')"
              :error-messages="
                form().serverErrors(this, 'customerServiceRequestInfoHTML')
              "
              ref="customerServiceRequestInfoHTML"
              data-cy="customerServiceRequestInfoHTML"
              @input="fieldValueChanged('customerServiceRequestInfoHTML')"
              auto-grow
            ></v-textarea>
          </v-col>
          <!-- ######################################################## -->
          <v-col cols="12">
            <div class="text-subtitle-1">
              {{ $ay.t("WorkOrderList") }}
            </div>
          </v-col>
          <v-col cols="12" sm="6" lg="4" xl="3">
            <v-checkbox
              v-model="obj.customerAllowViewWO"
              :readonly="formState.readOnly"
              :label="$ay.t('Active')"
              ref="customerAllowViewWO"
              data-cy="customerAllowViewWO"
              :error-messages="form().serverErrors(this, 'customerAllowViewWO')"
              @change="fieldValueChanged('customerAllowViewWO')"
            ></v-checkbox>
          </v-col>
          <v-col cols="12" sm="6" lg="4">
            <gz-tag-picker
              v-model="obj.customerAllowViewWOInTags"
              :readonly="formState.readOnly"
              :label="$ay.t('TaggedWith')"
              ref="customerAllowViewWOInTags"
              data-cy="customerAllowViewWOInTags"
              :error-messages="
                form().serverErrors(this, 'customerAllowViewWOInTags')
              "
              @input="fieldValueChanged('customerAllowViewWOInTags')"
            ></gz-tag-picker>
          </v-col>
          <v-col cols="12" sm="6" lg="4">
            <gz-tag-picker
              v-model="obj.customerAllowViewWOOutTags"
              :readonly="formState.readOnly"
              :label="$ay.t('NotTaggedWith')"
              ref="customerAllowViewWOOutTags"
              data-cy="customerAllowViewWOOutTags"
              :error-messages="
                form().serverErrors(this, 'customerAllowViewWOOutTags')
              "
              @input="fieldValueChanged('customerAllowViewWOOutTags')"
            ></gz-tag-picker>
          </v-col>
          <v-col cols="12">
            <gz-pick-list
              :aya-type="ayaTypes().Report"
              :variant="ayaTypes().WorkOrder.toString()"
              show-edit-icon
              v-model="obj.customerDefaultWorkOrderReportId"
              :readonly="formState.readOnly"
              :label="$ay.t('CustomerAccessWorkOrderReport')"
              ref="customerDefaultWorkOrderReportId"
              data-cy="customerDefaultWorkOrderReportId"
              :error-messages="
                form().serverErrors(this, 'customerDefaultWorkOrderReportId')
              "
              @input="fieldValueChanged('customerDefaultWorkOrderReportId')"
            ></gz-pick-list>
          </v-col>

          <!-- ######################################################## -->
          <v-col cols="12">
            <div class="text-subtitle-1">
              {{ $ay.t("CustomerAccessWorkOrderWiki") }}
            </div>
          </v-col>
          <v-col cols="12" sm="6" lg="4" xl="3">
            <v-checkbox
              v-model="obj.customerAllowWOWiki"
              :readonly="formState.readOnly"
              :label="$ay.t('Active')"
              ref="customerAllowWOWiki"
              data-cy="customerAllowWOWiki"
              :error-messages="form().serverErrors(this, 'customerAllowWOWiki')"
              @change="fieldValueChanged('customerAllowWOWiki')"
            ></v-checkbox>
          </v-col>
          <v-col cols="12" sm="6" lg="4">
            <gz-tag-picker
              v-model="obj.customerAllowWOWikiInTags"
              :readonly="formState.readOnly"
              :label="$ay.t('TaggedWith')"
              ref="customerAllowWOWikiInTags"
              data-cy="customerAllowWOWikiInTags"
              :error-messages="
                form().serverErrors(this, 'customerAllowWOWikiInTags')
              "
              @input="fieldValueChanged('customerAllowWOWikiInTags')"
            ></gz-tag-picker>
          </v-col>
          <v-col cols="12" sm="6" lg="4">
            <gz-tag-picker
              v-model="obj.customerAllowWOWikiOutTags"
              :readonly="formState.readOnly"
              :label="$ay.t('NotTaggedWith')"
              ref="customerAllowWOWikiOutTags"
              data-cy="customerAllowWOWikiOutTags"
              :error-messages="
                form().serverErrors(this, 'customerAllowWOWikiOutTags')
              "
              @input="fieldValueChanged('customerAllowWOWikiOutTags')"
            ></gz-tag-picker>
          </v-col>

          <!-- ######################################################## -->
          <v-col cols="12">
            <div class="text-subtitle-1">
              {{ $ay.t("UserSettings") }}
            </div>
          </v-col>
          <v-col cols="12" sm="6" lg="4" xl="3">
            <v-checkbox
              v-model="obj.customerAllowUserSettings"
              :readonly="formState.readOnly"
              :label="$ay.t('Active')"
              ref="customerAllowUserSettings"
              data-cy="customerAllowUserSettings"
              :error-messages="
                form().serverErrors(this, 'customerAllowUserSettings')
              "
              @change="fieldValueChanged('customerAllowUserSettings')"
            ></v-checkbox>
          </v-col>
          <v-col cols="12" sm="6" lg="4">
            <gz-tag-picker
              v-model="obj.customerAllowUserSettingsInTags"
              :readonly="formState.readOnly"
              :label="$ay.t('TaggedWith')"
              ref="customerAllowUserSettingsInTags"
              data-cy="customerAllowUserSettingsInTags"
              :error-messages="
                form().serverErrors(this, 'customerAllowUserSettingsInTags')
              "
              @input="fieldValueChanged('customerAllowUserSettingsInTags')"
            ></gz-tag-picker>
          </v-col>
          <v-col cols="12" sm="6" lg="4">
            <gz-tag-picker
              v-model="obj.customerAllowUserSettingsOutTags"
              :readonly="formState.readOnly"
              :label="$ay.t('NotTaggedWith')"
              ref="customerAllowUserSettingsOutTags"
              data-cy="customerAllowUserSettingsOutTags"
              :error-messages="
                form().serverErrors(this, 'customerAllowUserSettingsOutTags')
              "
              @input="fieldValueChanged('customerAllowUserSettingsOutTags')"
            ></gz-tag-picker>
          </v-col>

          <!-- ######################################################## -->
          <v-col cols="12">
            <div class="text-h6 mb-8">
              {{ $ay.t("NotifySubscriptionList") }}
            </div>
            <div class="text-subtitle-1">
              {{ $ay.t("NotifyEventCustomerServiceImminent") }}
            </div>
          </v-col>
          <v-col cols="12" sm="6" lg="4" xl="3">
            <v-checkbox
              v-model="obj.customerAllowNotifyServiceImminent"
              :readonly="formState.readOnly"
              :label="$ay.t('Active')"
              ref="customerAllowNotifyServiceImminent"
              data-cy="customerAllowNotifyServiceImminent"
              :error-messages="
                form().serverErrors(this, 'customerAllowNotifyServiceImminent')
              "
              @change="fieldValueChanged('customerAllowNotifyServiceImminent')"
            ></v-checkbox>
          </v-col>
          <v-col cols="12" sm="6" lg="4">
            <gz-tag-picker
              v-model="obj.customerAllowNotifyServiceImminentInTags"
              :readonly="formState.readOnly"
              :label="$ay.t('TaggedWith')"
              ref="customerAllowNotifyServiceImminentInTags"
              data-cy="customerAllowNotifyServiceImminentInTags"
              :error-messages="
                form().serverErrors(
                  this,
                  'customerAllowNotifyServiceImminentInTags'
                )
              "
              @input="
                fieldValueChanged('customerAllowNotifyServiceImminentInTags')
              "
            ></gz-tag-picker>
          </v-col>
          <v-col cols="12" sm="6" lg="4">
            <gz-tag-picker
              v-model="obj.customerAllowNotifyServiceImminentOutTags"
              :readonly="formState.readOnly"
              :label="$ay.t('NotTaggedWith')"
              ref="customerAllowNotifyServiceImminentOutTags"
              data-cy="customerAllowNotifyServiceImminentOutTags"
              :error-messages="
                form().serverErrors(
                  this,
                  'customerAllowNotifyServiceImminentOutTags'
                )
              "
              @input="
                fieldValueChanged('customerAllowNotifyServiceImminentOutTags')
              "
            ></gz-tag-picker>
          </v-col>

          <!-- ######################################################## -->
          <v-col cols="12">
            <div class="text-subtitle-1">
              {{ $ay.t("NotifyEventCSRAccepted") }}
            </div>
          </v-col>
          <v-col cols="12" sm="6" lg="4" xl="3">
            <v-checkbox
              v-model="obj.customerAllowNotifyCSRAccepted"
              :readonly="formState.readOnly"
              :label="$ay.t('Active')"
              ref="customerAllowNotifyCSRAccepted"
              data-cy="customerAllowNotifyCSRAccepted"
              :error-messages="
                form().serverErrors(this, 'customerAllowNotifyCSRAccepted')
              "
              @change="fieldValueChanged('customerAllowNotifyCSRAccepted')"
            ></v-checkbox>
          </v-col>
          <v-col cols="12" sm="6" lg="4">
            <gz-tag-picker
              v-model="obj.customerAllowNotifyCSRAcceptedInTags"
              :readonly="formState.readOnly"
              :label="$ay.t('TaggedWith')"
              ref="customerAllowNotifyCSRAcceptedInTags"
              data-cy="customerAllowNotifyCSRAcceptedInTags"
              :error-messages="
                form().serverErrors(
                  this,
                  'customerAllowNotifyCSRAcceptedInTags'
                )
              "
              @input="fieldValueChanged('customerAllowNotifyCSRAcceptedInTags')"
            ></gz-tag-picker>
          </v-col>
          <v-col cols="12" sm="6" lg="4">
            <gz-tag-picker
              v-model="obj.customerAllowNotifyCSRAcceptedOutTags"
              :readonly="formState.readOnly"
              :label="$ay.t('NotTaggedWith')"
              ref="customerAllowNotifyCSRAcceptedOutTags"
              data-cy="customerAllowNotifyCSRAcceptedOutTags"
              :error-messages="
                form().serverErrors(
                  this,
                  'customerAllowNotifyCSRAcceptedOutTags'
                )
              "
              @input="
                fieldValueChanged('customerAllowNotifyCSRAcceptedOutTags')
              "
            ></gz-tag-picker>
          </v-col>

          <!-- ######################################################## -->
          <v-col cols="12">
            <div class="text-subtitle-1">
              {{ $ay.t("NotifyEventCSRRejected") }}
            </div>
          </v-col>
          <v-col cols="12" sm="6" lg="4" xl="3">
            <v-checkbox
              v-model="obj.customerAllowNotifyCSRRejected"
              :readonly="formState.readOnly"
              :label="$ay.t('Active')"
              ref="customerAllowNotifyCSRRejected"
              data-cy="customerAllowNotifyCSRRejected"
              :error-messages="
                form().serverErrors(this, 'customerAllowNotifyCSRRejected')
              "
              @change="fieldValueChanged('customerAllowNotifyCSRRejected')"
            ></v-checkbox>
          </v-col>
          <v-col cols="12" sm="6" lg="4">
            <gz-tag-picker
              v-model="obj.customerAllowNotifyCSRRejectedInTags"
              :readonly="formState.readOnly"
              :label="$ay.t('TaggedWith')"
              ref="customerAllowNotifyCSRRejectedInTags"
              data-cy="customerAllowNotifyCSRRejectedInTags"
              :error-messages="
                form().serverErrors(
                  this,
                  'customerAllowNotifyCSRRejectedInTags'
                )
              "
              @input="fieldValueChanged('customerAllowNotifyCSRRejectedInTags')"
            ></gz-tag-picker>
          </v-col>
          <v-col cols="12" sm="6" lg="4">
            <gz-tag-picker
              v-model="obj.customerAllowNotifyCSRRejectedOutTags"
              :readonly="formState.readOnly"
              :label="$ay.t('NotTaggedWith')"
              ref="customerAllowNotifyCSRRejectedOutTags"
              data-cy="customerAllowNotifyCSRRejectedOutTags"
              :error-messages="
                form().serverErrors(
                  this,
                  'customerAllowNotifyCSRRejectedOutTags'
                )
              "
              @input="
                fieldValueChanged('customerAllowNotifyCSRRejectedOutTags')
              "
            ></gz-tag-picker>
          </v-col>

          <!-- ######################################################## -->
          <v-col cols="12">
            <div class="text-subtitle-1">
              {{ $ay.t("NotifyEventWorkorderCreatedForCustomer") }}
            </div>
          </v-col>
          <v-col cols="12" sm="6" lg="4" xl="3">
            <v-checkbox
              v-model="obj.customerAllowNotifyWOCreated"
              :readonly="formState.readOnly"
              :label="$ay.t('Active')"
              ref="customerAllowNotifyWOCreated"
              data-cy="customerAllowNotifyWOCreated"
              :error-messages="
                form().serverErrors(this, 'customerAllowNotifyWOCreated')
              "
              @change="fieldValueChanged('customerAllowNotifyWOCreated')"
            ></v-checkbox>
          </v-col>
          <v-col cols="12" sm="6" lg="4">
            <gz-tag-picker
              v-model="obj.customerAllowNotifyWOCreatedInTags"
              :readonly="formState.readOnly"
              :label="$ay.t('TaggedWith')"
              ref="customerAllowNotifyWOCreatedInTags"
              data-cy="customerAllowNotifyWOCreatedInTags"
              :error-messages="
                form().serverErrors(this, 'customerAllowNotifyWOCreatedInTags')
              "
              @input="fieldValueChanged('customerAllowNotifyWOCreatedInTags')"
            ></gz-tag-picker>
          </v-col>
          <v-col cols="12" sm="6" lg="4">
            <gz-tag-picker
              v-model="obj.customerAllowNotifyWOCreatedOutTags"
              :readonly="formState.readOnly"
              :label="$ay.t('NotTaggedWith')"
              ref="customerAllowNotifyWOCreatedOutTags"
              data-cy="customerAllowNotifyWOCreatedOutTags"
              :error-messages="
                form().serverErrors(this, 'customerAllowNotifyWOCreatedOutTags')
              "
              @input="fieldValueChanged('customerAllowNotifyWOCreatedOutTags')"
            ></gz-tag-picker>
          </v-col>

          <!-- ######################################################## -->
          <v-col cols="12">
            <div class="text-subtitle-1">
              {{ $ay.t("NotifyEventWorkorderCompleted") }}
            </div>
          </v-col>
          <v-col cols="12" sm="6" lg="4" xl="3">
            <v-checkbox
              v-model="obj.customerAllowNotifyWOCompleted"
              :readonly="formState.readOnly"
              :label="$ay.t('Active')"
              ref="customerAllowNotifyWOCompleted"
              data-cy="customerAllowNotifyWOCompleted"
              :error-messages="
                form().serverErrors(this, 'customerAllowNotifyWOCompleted')
              "
              @change="fieldValueChanged('customerAllowNotifyWOCompleted')"
            ></v-checkbox>
          </v-col>
          <v-col cols="12" sm="6" lg="4">
            <gz-tag-picker
              v-model="obj.customerAllowNotifyWOCompletedInTags"
              :readonly="formState.readOnly"
              :label="$ay.t('TaggedWith')"
              ref="customerAllowNotifyWOCompletedInTags"
              data-cy="customerAllowNotifyWOCompletedInTags"
              :error-messages="
                form().serverErrors(
                  this,
                  'customerAllowNotifyWOCompletedInTags'
                )
              "
              @input="fieldValueChanged('customerAllowNotifyWOCompletedInTags')"
            ></gz-tag-picker>
          </v-col>
          <v-col cols="12" sm="6" lg="4">
            <gz-tag-picker
              v-model="obj.customerAllowNotifyWOCompletedOutTags"
              :readonly="formState.readOnly"
              :label="$ay.t('NotTaggedWith')"
              ref="customerAllowNotifyWOCompletedOutTags"
              data-cy="customerAllowNotifyWOCompletedOutTags"
              :error-messages="
                form().serverErrors(
                  this,
                  'customerAllowNotifyWOCompletedOutTags'
                )
              "
              @input="
                fieldValueChanged('customerAllowNotifyWOCompletedOutTags')
              "
            ></gz-tag-picker>
          </v-col>

          <!-- **************************************************** -->
        </v-row>
      </v-form>
    </div>
    <v-overlay :value="!formState.ready || formState.loading">
      <v-progress-circular indeterminate :size="64" />
    </v-overlay>
  </div>
</template>

<script>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*  Xeslint-disable    */
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
const FORM_KEY = "global-settings-edit";
const API_BASE_URL = "global-biz-setting/";
//const FORM_CUSTOM_TEMPLATE_KEY = "Global"; //<-- Should always be CoreBizObject AyaType name here where possible

export default {
  async created() {
    let vm = this;

    try {
      await initForm(vm);

      vm.rights = window.$gz.role.getRights(window.$gz.type.Global);
      vm.formState.readOnly = !vm.rights.change;
      window.$gz.eventBus.$on("menu-click", clickHandler);
      //NOTE: slightly different in this form as there is only ever a single global object so no need for a bunch of code
      //is there already an obj from a prior operation?
      if (this.$route.params.obj) {
        //yes, no need to fetch it
        this.obj = this.$route.params.obj;
        window.$gz.form.setFormState({
          vm: vm,
          loading: false
        });
      } else {
        await vm.getDataFromApi(); //let getdata handle loading
      }
      window.$gz.form.setFormState({
        vm: vm,
        dirty: false,
        valid: true
      });
      generateMenu(vm);
    } catch (error) {
      window.$gz.errorHandler.handleFormError(error, vm);
    } finally {
      vm.formState.ready = true;
    }
  },
  async beforeRouteLeave(to, from, next) {
    if (!this.formState.dirty) {
      next();
      return;
    }
    if ((await window.$gz.dialog.confirmLeaveUnsaved()) === true) {
      next();
    } else {
      next(false);
    }
  },
  beforeDestroy() {
    window.$gz.eventBus.$off("menu-click", clickHandler);
  },
  data() {
    return {
      // formCustomTemplateKey: FORM_CUSTOM_TEMPLATE_KEY,

      obj:
        //IMPORTANT NOTE: Fields that are NON NULLABLE in the schema for the table but *are* hideable **MUST** have a default value set here or else there will be no way to save the record
        //I.E. Serial, usertype fields, ACTIVE
        //Also, if it's a non-nullable Enum backed field then it should have a valid selection i.e. not zero if there is no zero
        {
          id: 0,
          concurrency: 0,
          searchCaseSensitiveOnly: false,
          useInventory: true,
          taxPartPurchaseId: null,
          taxPartSaleId: null,
          taxRateSaleId: null,
          workOrderCompleteByAge: "00:00:00",
          allowScheduleConflicts: false,
          workOrderTravelDefaultMinutes: 0,
          workLaborScheduleDefaultMinutes: 0,
          customerDefaultWorkOrderReportId: null,
          customerServiceRequestInfoHTML: null,
          signatureTitle: null,
          signatureHeader: null,
          signatureFooter: null,
          customerAllowCSR: false,
          customerAllowCSRInTags: [],
          customerAllowCSROutTags: [],
          customerAllowViewWO: false,
          customerAllowViewWOInTags: [],
          customerAllowViewWOOutTags: [],
          customerAllowWOWiki: false,
          customerAllowWOWikiInTags: [],
          customerAllowWOWikiOutTags: [],
          customerAllowUserSettings: false,
          customerAllowUserSettingsInTags: [],
          customerAllowUserSettingsOutTags: [],
          customerAllowNotifyServiceImminent: false,
          customerAllowNotifyServiceImminentInTags: [],
          customerAllowNotifyServiceImminentOutTags: [],
          customerAllowNotifyCSRAccepted: false,
          customerAllowNotifyCSRAcceptedInTags: [],
          customerAllowNotifyCSRAcceptedOutTags: [],
          customerAllowNotifyCSRRejected: false,
          customerAllowNotifyCSRRejectedInTags: [],
          customerAllowNotifyCSRRejectedOutTags: [],
          customerAllowNotifyWOCreated: false,
          customerAllowNotifyWOCreatedInTags: [],
          customerAllowNotifyWOCreatedOutTags: [],
          customerAllowNotifyWOCompleted: false,
          customerAllowNotifyWOCompletedInTags: [],
          customerAllowNotifyWOCompletedOutTags: []
        },
      formState: {
        ready: false,
        dirty: false,
        valid: true,
        readOnly: false,
        loading: true,
        errorBoxMessage: null,
        appError: null,
        serverError: {}
      },
      rights: window.$gz.role.defaultRightsObject(),
      ayaType: window.$gz.type.Project
    };
  },
  //WATCHERS
  watch: {
    formState: {
      handler: function(val) {
        //,oldval is available here too if necessary
        if (this.formState.loading) {
          return;
        }

        //enable / disable save button
        if (val.dirty && val.valid && !val.readOnly) {
          window.$gz.eventBus.$emit("menu-enable-item", FORM_KEY + ":save");
        } else {
          window.$gz.eventBus.$emit("menu-disable-item", FORM_KEY + ":save");
        }
      },
      deep: true
    }
  },
  methods: {
    canSave: function() {
      return this.formState.valid && this.formState.dirty;
    },

    ayaTypes: function() {
      return window.$gz.type;
    },
    form() {
      return window.$gz.form;
    },
    fieldValueChanged(ref) {
      if (
        this.formState.ready &&
        !this.formState.loading &&
        !this.formState.readOnly
      ) {
        window.$gz.form.fieldValueChanged(this, ref);
      }
    },
    async getDataFromApi() {
      let vm = this;
      window.$gz.form.setFormState({
        vm: vm,
        loading: true
      });

      let url = API_BASE_URL;
      try {
        window.$gz.form.deleteAllErrorBoxErrors(vm);

        let res = await window.$gz.api.get(url);

        if (res.error) {
          //Not found?
          if (res.error.code == "2010") {
            window.$gz.form.handleObjectNotFound(vm);
          }
          vm.formState.serverError = res.error;
          window.$gz.form.setErrorBoxErrors(vm);
        } else {
          vm.obj = res.data;
          //modify the menu as necessary
          generateMenu(vm);
          //Update the form status
          window.$gz.form.setFormState({
            vm: vm,
            dirty: false,
            valid: true,
            loading: false
          });
        }
      } catch (error) {
        window.$gz.errorHandler.handleFormError(error, vm);
      } finally {
        window.$gz.form.setFormState({
          vm: vm,
          loading: false
        });
      }
    },
    async submit() {
      let vm = this;
      if (vm.canSave == false) {
        return;
      }

      try {
        window.$gz.form.setFormState({
          vm: vm,
          loading: true
        });
        let url = API_BASE_URL;
        //clear any errors vm might be around from previous submit
        window.$gz.form.deleteAllErrorBoxErrors(vm);

        let res = await window.$gz.api.put(url, vm.obj);

        if (res.error) {
          vm.formState.serverError = res.error;
          window.$gz.form.setErrorBoxErrors(vm);
        } else {
          //PUT - only concurrency token is returned (**warning, if server changes object other fields then this needs to act more like POST above but is more efficient this way**)
          //Handle "put" of an existing record (UPDATE)
          vm.obj.concurrency = res.data.concurrency;

          //Update the form status
          window.$gz.form.setFormState({
            vm: vm,
            dirty: false,
            valid: true
          });

          //refresh the local global settings cache so user can try their settings right away
          //get the client version of the global settings object values
          let gsets = await window.$gz.api.get("global-biz-setting/client");
          if (!gsets.error) {
            window.$gz.store.commit("setGlobalSettings", gsets.data);
          }
        }
      } catch (ex) {
        window.$gz.errorHandler.handleFormError(ex, vm);
      } finally {
        window.$gz.form.setFormState({
          vm: vm,
          loading: false
        });
      }
    }
    //end methods
  }
};

/////////////////////////////
//
//
async function clickHandler(menuItem) {
  if (!menuItem) {
    return;
  }
  let m = window.$gz.menu.parseMenuItem(menuItem);
  if (m.owner == FORM_KEY && !m.disabled) {
    switch (m.key) {
      case "save":
        m.vm.submit();
        break;

      default:
        window.$gz.eventBus.$emit(
          "notify-warning",
          FORM_KEY + "::context click: [" + m.key + "]"
        );
    }
  }
}

//////////////////////
//
//
function generateMenu(vm) {
  let menuOptions = {
    isMain: false,
    readOnly: vm.formState.readOnly,
    icon: "$ayiCogs",
    //title: vm.$ay.t("AdministrationGlobalSettings"),
    title: "AdministrationGlobalSettings",
    helpUrl: "adm-global-settings",
    formData: {
      ayaType: window.$gz.type.Project,
      recordId: vm.$route.params.recordid,
      recordName: vm.$ay.t("AdministrationGlobalSettings")
      //,
      //formCustomTemplateKey: FORM_CUSTOM_TEMPLATE_KEY,
    },
    menuItems: []
  };

  if (vm.rights.change) {
    menuOptions.menuItems.push({
      title: "Save",
      icon: "$ayiSave",
      surface: true,
      key: FORM_KEY + ":save",
      vm: vm
    });
  }

  menuOptions.menuItems.push({ divider: true, inset: false });

  window.$gz.eventBus.$emit("menu-change", menuOptions);
}

/////////////////////////////////
//
//
async function initForm(vm) {
  await fetchTranslatedText(vm);
  //await window.$gz.formCustomTemplate.get(FORM_CUSTOM_TEMPLATE_KEY, vm);
}

//////////////////////////////////////////////////////////
//
// Ensures UI translated text is available
//
async function fetchTranslatedText(vm) {
  await window.$gz.translation.cacheTranslations([
    "TaggedWith",
    "NotTaggedWith",
    "CustomerServiceRequestList",
    "WorkOrderList",
    "UserSettings",
    "UserInterfaceSettings",
    "BusinessSettings",
    "CustomerAccessSettings",
    "PickListTemplates",
    "GlobalLogo",
    "GlobalUseInventory",
    "GlobalSearchCaseSensitiveOnly",
    "GlobalTaxPartPurchaseID",
    "GlobalTaxPartSaleID",
    "GlobalTaxRateSaleID",
    "GlobalNextSeeds",
    "GlobalWorkOrderCompleteByAge",
    "GlobalLaborSchedUserDfltTimeSpan",
    "GlobalTravelDfltTimeSpan",
    "CustomerAccessWorkOrderWiki",
    "NotifySubscriptionList",
    "NotifyEventCustomerServiceImminent",
    "NotifyEventCSRAccepted",
    "NotifyEventCSRRejected",
    "NotifyEventWorkorderCreatedForCustomer",
    "NotifyEventWorkorderCompleted",
    "CustomerAccessWorkOrderReport",
    "CSRInfoHTML",
    "CustomerSignature",
    "GlobalSignatureFooter",
    "GlobalSignatureHeader",
    "GlobalSignatureTitle",
    "GlobalAllowScheduleConflicts"
  ]);
}
</script>
