<template>
  <div v-if="value != null" class="mt-8">
    <v-row dense>
      <v-col cols="12">
        <v-menu offset-y max-width="600px">
          <template v-slot:activator="{ on, attrs }">
            <div class="text-h6">
              <v-icon large :color="hasData ? 'primary' : null" class="mr-2"
                >$ayiLuggageCart</v-icon
              >
              {{ $ay.t("OutsideServiceList") }}
              <v-btn v-if="!parentDeleted" large icon v-bind="attrs" v-on="on">
                <v-icon small color="primary">$ayiEllipsisV</v-icon>
              </v-btn>
            </div>
          </template>
          <v-list>
            <v-list-item v-if="canAdd" @click="newItem">
              <v-list-item-icon>
                <v-icon>$ayiPlus</v-icon>
              </v-list-item-icon>
              <v-list-item-title>{{ $ay.t("New") }}</v-list-item-title>
            </v-list-item>
            <v-list-item v-if="canDelete && isDeleted" @click="unDeleteItem">
              <v-list-item-icon>
                <v-icon>$ayiTrashRestoreAlt</v-icon>
              </v-list-item-icon>
              <v-list-item-title>{{ $ay.t("Undelete") }}</v-list-item-title>
            </v-list-item>
            <v-list-item v-if="canDelete && !isDeleted" @click="deleteItem">
              <v-list-item-icon>
                <v-icon>$ayiTrashAlt</v-icon>
              </v-list-item-icon>
              <v-list-item-title>{{ $ay.t("SoftDelete") }}</v-list-item-title>
            </v-list-item>
            <v-list-item v-if="canDeleteAll" @click="deleteAllItem">
              <v-list-item-icon>
                <v-icon>$ayiDumpster</v-icon>
              </v-list-item-icon>
              <v-list-item-title>{{
                $ay.t("SoftDeleteAll")
              }}</v-list-item-title>
            </v-list-item>
          </v-list>
        </v-menu>
      </v-col>

      <template v-if="hasData">
        <!-- ############################################################### -->
        <v-col cols="12" class="mb-10">
          <v-data-table
            v-model="selectedRow"
            :headers="headerList"
            :items="itemList"
            item-key="index"
            class="elevation-1"
            disable-pagination
            disable-filtering
            disable-sort
            hide-default-footer
            data-cy="outsideServicesTable"
            dense
            :item-class="itemRowClasses"
            :show-select="$vuetify.breakpoint.xs"
            single-select
            @click:row="handleRowClick"
          >
          </v-data-table>
        </v-col>
      </template>
      <template v-if="hasData && hasSelection">
        <div ref="outsideservicetopform"></div>
        <v-btn
          v-if="canDelete && isDeleted"
          large
          color="primary"
          @click="unDeleteItem"
          >{{ $ay.t("Undelete")
          }}<v-icon right large>$ayiTrashRestoreAlt</v-icon></v-btn
        >

        <v-col
          v-if="form().showMe(this, 'WorkOrderItemOutsideServiceUnit')"
          cols="12"
          sm="6"
          lg="4"
          xl="3"
        >
          <gz-pick-list
            :ref="
              `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].unitId`
            "
            v-model="
              value.items[activeWoItemIndex].outsideServices[activeItemIndex]
                .unitId
            "
            :aya-type="$ay.ayt().Unit"
            show-edit-icon
            :readonly="formState.readOnly || isDeleted"
            :disabled="isDeleted"
            :label="$ay.t('Unit')"
            data-cy="outsideServices.unitId"
            :rules="[
              form().required(
                this,
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].unitId`
              )
            ]"
            :error-messages="
              form().serverErrors(
                this,
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].unitId`
              )
            "
            @input="
              fieldValueChanged(
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].unitId`
              )
            "
            @update:name="unitChange"
          ></gz-pick-list>
        </v-col>

        <v-col
          v-if="
            form().showMe(this, 'WorkOrderItemOutsideServiceVendorSentToID')
          "
          cols="12"
          sm="6"
          lg="4"
          xl="3"
        >
          <gz-pick-list
            :ref="
              `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].vendorSentToId`
            "
            v-model="
              value.items[activeWoItemIndex].outsideServices[activeItemIndex]
                .vendorSentToId
            "
            :aya-type="$ay.ayt().Vendor"
            show-edit-icon
            :readonly="formState.readOnly || isDeleted"
            :disabled="isDeleted"
            :label="$ay.t('WorkOrderItemOutsideServiceVendorSentToID')"
            data-cy="outsideServices.vendorSentToId"
            :error-messages="
              form().serverErrors(
                this,
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].vendorSentToId`
              )
            "
            @input="
              fieldValueChanged(
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].vendorSentToId`
              )
            "
            @update:name="vendorSentToChange"
          ></gz-pick-list>
        </v-col>

        <v-col
          v-if="form().showMe(this, 'WorkOrderItemOutsideServiceRMANumber')"
          cols="12"
          sm="6"
          lg="4"
          xl="3"
        >
          <v-text-field
            :ref="
              `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].rmaNumber`
            "
            v-model="
              value.items[activeWoItemIndex].outsideServices[activeItemIndex]
                .rmaNumber
            "
            dense
            :readonly="formState.readOnly"
            :disabled="isDeleted"
            :label="$ay.t('WorkOrderItemOutsideServiceRMANumber')"
            :error-messages="
              form().serverErrors(
                this,
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].rmaNumber`
              )
            "
            @input="
              fieldValueChanged(
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].rmaNumber`
              )
            "
          ></v-text-field>
        </v-col>
        <v-col
          v-if="form().showMe(this, 'WorkOrderItemOutsideServiceRepairCost')"
          cols="12"
          sm="6"
          lg="4"
          xl="3"
        >
          <gz-currency
            :ref="
              `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].repairCost`
            "
            v-model="
              value.items[activeWoItemIndex].outsideServices[activeItemIndex]
                .repairCost
            "
            can-clear
            :readonly="formState.readOnly || isDeleted"
            :disabled="isDeleted"
            :label="$ay.t('WorkOrderItemOutsideServiceRepairCost')"
            data-cy="outsideServicepriceoverride"
            :error-messages="
              form().serverErrors(
                this,
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].repairCost`
              )
            "
            :rules="[
              form().decimalValid(
                this,
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].repairCost`
              )
            ]"
            @input="
              fieldValueChanged(
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].repairCost`
              )
            "
          ></gz-currency>
        </v-col>

        <v-col
          v-if="form().showMe(this, 'WorkOrderItemOutsideServiceRepairPrice')"
          cols="12"
          sm="6"
          lg="4"
          xl="3"
        >
          <gz-currency
            :ref="
              `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].repairPrice`
            "
            v-model="
              value.items[activeWoItemIndex].outsideServices[activeItemIndex]
                .repairPrice
            "
            can-clear
            :readonly="formState.readOnly || isDeleted"
            :disabled="isDeleted"
            :label="$ay.t('WorkOrderItemOutsideServiceRepairPrice')"
            data-cy="outsideServicepriceoverride"
            :error-messages="
              form().serverErrors(
                this,
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].repairPrice`
              )
            "
            :rules="[
              form().decimalValid(
                this,
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].repairPrice`
              )
            ]"
            @input="
              fieldValueChanged(
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].repairPrice`
              )
            "
          ></gz-currency>
        </v-col>

        <v-col
          v-if="
            form().showMe(this, 'WorkOrderItemOutsideServiceVendorSentViaID')
          "
          cols="12"
          sm="6"
          lg="4"
          xl="3"
        >
          <gz-pick-list
            :ref="
              `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].vendorSentViaId`
            "
            v-model="
              value.items[activeWoItemIndex].outsideServices[activeItemIndex]
                .vendorSentViaId
            "
            :aya-type="$ay.ayt().Vendor"
            show-edit-icon
            :readonly="formState.readOnly || isDeleted"
            :disabled="isDeleted"
            :label="$ay.t('WorkOrderItemOutsideServiceVendorSentViaID')"
            data-cy="outsideServices.vendorSentViaId"
            :error-messages="
              form().serverErrors(
                this,
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].vendorSentViaId`
              )
            "
            @input="
              fieldValueChanged(
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].vendorSentViaId`
              )
            "
            @update:name="vendorSentViaChange"
          ></gz-pick-list>
        </v-col>

        <v-col
          v-if="
            form().showMe(this, 'WorkOrderItemOutsideServiceTrackingNumber')
          "
          cols="12"
          sm="6"
          lg="4"
          xl="3"
        >
          <v-text-field
            :ref="
              `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].trackingNumber`
            "
            v-model="
              value.items[activeWoItemIndex].outsideServices[activeItemIndex]
                .trackingNumber
            "
            dense
            :readonly="formState.readOnly"
            :disabled="isDeleted"
            :label="$ay.t('WorkOrderItemOutsideServiceTrackingNumber')"
            :error-messages="
              form().serverErrors(
                this,
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].trackingNumber`
              )
            "
            @input="
              fieldValueChanged(
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].trackingNumber`
              )
            "
          ></v-text-field>
        </v-col>
        <v-col
          v-if="form().showMe(this, 'WorkOrderItemOutsideServiceShippingCost')"
          cols="12"
          sm="6"
          lg="4"
          xl="3"
        >
          <gz-currency
            :ref="
              `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].shippingCost`
            "
            v-model="
              value.items[activeWoItemIndex].outsideServices[activeItemIndex]
                .shippingCost
            "
            can-clear
            :readonly="formState.readOnly || isDeleted"
            :disabled="isDeleted"
            :label="$ay.t('WorkOrderItemOutsideServiceShippingCost')"
            data-cy="outsideServicepriceoverride"
            :error-messages="
              form().serverErrors(
                this,
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].shippingCost`
              )
            "
            :rules="[
              form().decimalValid(
                this,
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].shippingCost`
              )
            ]"
            @input="
              fieldValueChanged(
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].shippingCost`
              )
            "
          ></gz-currency>
        </v-col>

        <v-col
          v-if="form().showMe(this, 'WorkOrderItemOutsideServiceShippingPrice')"
          cols="12"
          sm="6"
          lg="4"
          xl="3"
        >
          <gz-currency
            :ref="
              `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].shippingPrice`
            "
            v-model="
              value.items[activeWoItemIndex].outsideServices[activeItemIndex]
                .shippingPrice
            "
            can-clear
            :readonly="formState.readOnly || isDeleted"
            :disabled="isDeleted"
            :label="$ay.t('WorkOrderItemOutsideServiceShippingPrice')"
            data-cy="outsideServicepriceoverride"
            :error-messages="
              form().serverErrors(
                this,
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].shippingPrice`
              )
            "
            :rules="[
              form().decimalValid(
                this,
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].shippingPrice`
              )
            ]"
            @input="
              fieldValueChanged(
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].shippingPrice`
              )
            "
          ></gz-currency>
        </v-col>

        <v-col
          v-if="form().showMe(this, 'WorkOrderItemOutsideServiceDateSent')"
          cols="12"
          sm="6"
          lg="4"
          xl="3"
        >
          <gz-date-time-picker
            :ref="
              `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].sentDate`
            "
            v-model="
              value.items[activeWoItemIndex].outsideServices[activeItemIndex]
                .sentDate
            "
            :label="$ay.t('WorkOrderItemOutsideServiceDateSent')"
            :readonly="formState.readOnly"
            :disabled="isDeleted"
            data-cy="outsideServiceSentDate"
            :error-messages="
              form().serverErrors(
                this,
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].sentDate`
              )
            "
            @input="
              fieldValueChanged(
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].sentDate`
              )
            "
          ></gz-date-time-picker>
        </v-col>

        <v-col
          v-if="form().showMe(this, 'WorkOrderItemOutsideServiceDateETA')"
          cols="12"
          sm="6"
          lg="4"
          xl="3"
        >
          <gz-date-time-picker
            :ref="
              `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].etaDate`
            "
            v-model="
              value.items[activeWoItemIndex].outsideServices[activeItemIndex]
                .etaDate
            "
            :label="$ay.t('WorkOrderItemOutsideServiceDateETA')"
            :readonly="formState.readOnly"
            :disabled="isDeleted"
            data-cy="outsideServiceEtaDate"
            :error-messages="
              form().serverErrors(
                this,
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].etaDate`
              )
            "
            @input="
              fieldValueChanged(
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].etaDate`
              )
            "
          ></gz-date-time-picker>
        </v-col>

        <v-col
          v-if="form().showMe(this, 'WorkOrderItemOutsideServiceDateReturned')"
          cols="12"
          sm="6"
          lg="4"
          xl="3"
        >
          <gz-date-time-picker
            :ref="
              `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].returnDate`
            "
            v-model="
              value.items[activeWoItemIndex].outsideServices[activeItemIndex]
                .returnDate
            "
            :label="$ay.t('WorkOrderItemOutsideServiceDateReturned')"
            :readonly="formState.readOnly"
            :disabled="isDeleted"
            data-cy="outsideServiceDateReturned"
            :error-messages="
              form().serverErrors(
                this,
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].returnDate`
              )
            "
            @input="
              fieldValueChanged(
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].returnDate`
              )
            "
          ></gz-date-time-picker>
        </v-col>

        <v-col
          v-if="form().showMe(this, 'WorkOrderItemOutsideServiceTaxCode')"
          cols="12"
          sm="6"
          lg="4"
          xl="3"
        >
          <gz-pick-list
            :ref="
              `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].taxCodeId`
            "
            v-model="
              value.items[activeWoItemIndex].outsideServices[activeItemIndex]
                .taxCodeId
            "
            :aya-type="$ay.ayt().TaxCode"
            show-edit-icon
            :readonly="formState.readOnly || isDeleted"
            :disabled="isDeleted"
            :label="$ay.t('TaxCode')"
            data-cy="outsideServiceTaxCodeSaleId"
            :error-messages="
              form().serverErrors(
                this,
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].taxCodeId`
              )
            "
            @input="
              fieldValueChanged(
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].taxCodeId`
              )
            "
            @update:name="taxCodeChange"
          ></gz-pick-list>
        </v-col>

        <v-col
          v-if="form().showMe(this, 'WorkOrderItemOutsideServiceNotes')"
          cols="12"
        >
          <v-textarea
            :ref="
              `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].notes`
            "
            v-model="
              value.items[activeWoItemIndex].outsideServices[activeItemIndex]
                .notes
            "
            dense
            :readonly="formState.readOnly"
            :disabled="isDeleted"
            :label="$ay.t('WorkOrderItemOutsideServiceNotes')"
            :error-messages="
              form().serverErrors(
                this,
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].notes`
              )
            "
            data-cy="outsideServiceNotes"
            auto-grow
            @input="
              fieldValueChanged(
                `Items[${activeWoItemIndex}].outsideServices[${activeItemIndex}].notes`
              )
            "
          ></v-textarea>
        </v-col>
      </template>
    </v-row>
  </div>
</template>
<script>
export default {
  props: {
    value: {
      default: null,
      type: Object
    },
    pvm: {
      default: null,
      type: Object
    },
    activeWoItemIndex: {
      default: null,
      type: Number
    },
    gotoIndex: {
      default: null,
      type: Number
    }
  },
  data() {
    return {
      activeItemIndex: null,
      selectedRow: []
    };
  },
  computed: {
    isDeleted: function() {
      if (
        this.value.items[this.activeWoItemIndex].outsideServices[
          this.activeItemIndex
        ] == null
      ) {
        this.setDefaultView();
        return true;
      }
      return (
        this.value.items[this.activeWoItemIndex].outsideServices[
          this.activeItemIndex
        ].deleted === true
      );
    },
    parentDeleted: function() {
      return this.value.items[this.activeWoItemIndex].deleted === true;
    },

    headerList: function() {
      const headers = [];

      if (this.form().showMe(this, "WorkOrderItemOutsideServiceUnit")) {
        headers.push({
          text: this.$ay.t("Unit"),
          align: "left",
          value: "unitViz"
        });
      }

      if (
        this.form().showMe(this, "WorkOrderItemOutsideServiceVendorSentToID")
      ) {
        headers.push({
          text: this.$ay.t("WorkOrderItemOutsideServiceVendorSentToID"),
          align: "left",
          value: "vendorSentToViz"
        });
      }

      if (this.form().showMe(this, "WorkOrderItemOutsideServiceRMANumber")) {
        headers.push({
          text: this.$ay.t("WorkOrderItemOutsideServiceRMANumber"),
          align: "left",
          value: "rmaNumber"
        });
      }

      if (this.form().showMe(this, "WorkOrderItemOutsideServiceRepairCost")) {
        headers.push({
          text: this.$ay.t("WorkOrderItemOutsideServiceRepairCost"),
          align: "right",
          value: "repairCost"
        });
      }

      if (this.form().showMe(this, "WorkOrderItemOutsideServiceRepairPrice")) {
        headers.push({
          text: this.$ay.t("WorkOrderItemOutsideServiceRepairPrice"),
          align: "right",
          value: "repairPrice"
        });
      }

      if (
        this.form().showMe(this, "WorkOrderItemOutsideServiceVendorSentViaID")
      ) {
        headers.push({
          text: this.$ay.t("WorkOrderItemOutsideServiceVendorSentViaID"),
          align: "left",
          value: "vendorSentViaViz"
        });
      }

      if (
        this.form().showMe(this, "WorkOrderItemOutsideServiceTrackingNumber")
      ) {
        headers.push({
          text: this.$ay.t("WorkOrderItemOutsideServiceTrackingNumber"),
          align: "left",
          value: "trackingNumber"
        });
      }

      if (this.form().showMe(this, "WorkOrderItemOutsideServiceShippingCost")) {
        headers.push({
          text: this.$ay.t("WorkOrderItemOutsideServiceShippingCost"),
          align: "right",
          value: "shippingCost"
        });
      }

      if (
        this.form().showMe(this, "WorkOrderItemOutsideServiceShippingPrice")
      ) {
        headers.push({
          text: this.$ay.t("WorkOrderItemOutsideServiceShippingPrice"),
          align: "right",
          value: "shippingPrice"
        });
      }

      if (this.form().showMe(this, "WorkOrderItemOutsideServiceDateSent")) {
        headers.push({
          text: this.$ay.t("WorkOrderItemOutsideServiceDateSent"),
          align: "right",
          value: "sentDate"
        });
      }

      if (this.form().showMe(this, "WorkOrderItemOutsideServiceDateETA")) {
        headers.push({
          text: this.$ay.t("WorkOrderItemOutsideServiceDateETA"),
          align: "right",
          value: "etaDate"
        });
      }

      if (this.form().showMe(this, "WorkOrderItemOutsideServiceDateReturned")) {
        headers.push({
          text: this.$ay.t("WorkOrderItemOutsideServiceDateReturned"),
          align: "right",
          value: "returnDate"
        });
      }

      if (this.form().showMe(this, "WorkOrderItemOutsideServiceNotes")) {
        headers.push({
          text: this.$ay.t("WorkOrderItemOutsideServiceNotes"),
          align: "left",
          value: "notes"
        });
      }

      if (this.form().showMe(this, "OutsideServiceCostViz")) {
        headers.push({
          text: this.$ay.t("Cost"),
          align: "right",
          value: "costViz"
        });
      }

      if (this.form().showMe(this, "OutsideServicePriceViz")) {
        headers.push({
          text: this.$ay.t("Price"),
          align: "right",
          value: "priceViz"
        });
      }

      if (this.form().showMe(this, "OutsideServiceNetViz")) {
        headers.push({
          text: this.$ay.t("NetPrice"),
          align: "right",
          value: "netViz"
        });
      }

      if (this.form().showMe(this, "WorkOrderItemOutsideServiceTaxCode")) {
        headers.push({
          text: this.$ay.t("Tax"),
          align: "left",
          value: "taxCodeViz"
        });
      }

      if (this.form().showMe(this, "OutsideServiceTaxAViz")) {
        headers.push({
          text: this.$ay.t("TaxAAmt"),
          align: "right",
          value: "taxAViz"
        });
      }

      if (this.form().showMe(this, "OutsideServiceTaxBViz")) {
        headers.push({
          text: this.$ay.t("TaxBAmt"),
          align: "right",
          value: "taxBViz"
        });
      }

      if (this.form().showMe(this, "OutsideServiceLineTotalViz")) {
        headers.push({
          text: this.$ay.t("LineTotal"),
          align: "right",
          value: "lineTotalViz"
        });
      }
      return headers;
    },
    itemList: function() {
      return this.value.items[this.activeWoItemIndex].outsideServices.map(
        (x, i) => {
          return {
            index: i,
            id: x.id,
            unitViz: x.unitViz,
            vendorSentToViz: x.vendorSentToViz,
            rmaNumber: x.rmaNumber,
            repairCost: window.$gz.locale.currencyLocalized(
              x.repairCost,
              this.pvm.languageName,
              this.pvm.currencyName
            ),
            repairPrice: window.$gz.locale.currencyLocalized(
              x.repairPrice,
              this.pvm.languageName,
              this.pvm.currencyName
            ),
            vendorSentViaViz: x.vendorSentViaViz,
            trackingNumber: x.trackingNumber,
            shippingCost: window.$gz.locale.currencyLocalized(
              x.shippingCost,
              this.pvm.languageName,
              this.pvm.currencyName
            ),
            shippingPrice: window.$gz.locale.currencyLocalized(
              x.shippingPrice,
              this.pvm.languageName,
              this.pvm.currencyName
            ),
            sentDate: window.$gz.locale.utcDateToShortDateAndTimeLocalized(
              x.sentDate,
              this.pvm.timeZoneName,
              this.pvm.languageName,
              this.pvm.hour12
            ),
            etaDate: window.$gz.locale.utcDateToShortDateAndTimeLocalized(
              x.etaDate,
              this.pvm.timeZoneName,
              this.pvm.languageName,
              this.pvm.hour12
            ),
            returnDate: window.$gz.locale.utcDateToShortDateAndTimeLocalized(
              x.returnDate,
              this.pvm.timeZoneName,
              this.pvm.languageName,
              this.pvm.hour12
            ),
            notes: window.$gz.util.truncateString(
              x.notes,
              this.pvm.maxTableNotesLength
            ),
            costViz: window.$gz.locale.currencyLocalized(
              x.costViz,
              this.pvm.languageName,
              this.pvm.currencyName
            ),
            priceViz: window.$gz.locale.currencyLocalized(
              x.priceViz,
              this.pvm.languageName,
              this.pvm.currencyName
            ),
            taxCodeViz: x.taxCodeViz,
            netViz: window.$gz.locale.currencyLocalized(
              x.netViz,
              this.pvm.languageName,
              this.pvm.currencyName
            ),
            taxAViz: window.$gz.locale.currencyLocalized(
              x.taxAViz,
              this.pvm.languageName,
              this.pvm.currencyName
            ),
            taxBViz: window.$gz.locale.currencyLocalized(
              x.taxBViz,
              this.pvm.languageName,
              this.pvm.currencyName
            ),
            lineTotalViz: window.$gz.locale.currencyLocalized(
              x.lineTotalViz,
              this.pvm.languageName,
              this.pvm.currencyName
            )
          };
        }
      );
    },
    formState: function() {
      return this.pvm.formState;
    },
    formCustomTemplateKey: function() {
      return this.pvm.formCustomTemplateKey;
    },
    hasData: function() {
      return (
        this.value.items[this.activeWoItemIndex].outsideServices.length > 0
      );
    },
    hasSelection: function() {
      return this.activeItemIndex != null;
    },
    canAdd: function() {
      return this.pvm.rights.change && !this.value.userIsRestrictedType;
    },
    canDelete: function() {
      return (
        this.activeItemIndex != null &&
        this.canDeleteAll &&
        !this.value.userIsRestrictedType
      );
    },
    canDeleteAll: function() {
      return this.pvm.rights.change && !this.value.userIsRestrictedType;
    }

    //----
  },
  watch: {
    activeWoItemIndex(val, oldVal) {
      if (val != oldVal) {
        this.setDefaultView();
      }
    },
    gotoIndex(val, oldVal) {
      if (val != oldVal) {
        let gotoIndex = val;
        if (val < 0) {
          //it's a create request
          gotoIndex = this.newItem();
        }
        this.selectedRow = [{ index: gotoIndex }];
        this.activeItemIndex = gotoIndex;
        this.$nextTick(() => {
          const el = this.$refs.outsideservicetopform;
          if (el) {
            el.scrollIntoView({ behavior: "smooth" });
          }
        });
      }
    }
  },
  created() {
    this.setDefaultView();
  },
  methods: {
    unitChange(newName) {
      this.value.items[this.activeWoItemIndex].outsideServices[
        this.activeItemIndex
      ].unitViz = newName;
    },
    vendorSentToChange(newName) {
      this.value.items[this.activeWoItemIndex].outsideServices[
        this.activeItemIndex
      ].vendorSentToViz = newName;
    },
    vendorSentViaChange(newName) {
      this.value.items[this.activeWoItemIndex].outsideServices[
        this.activeItemIndex
      ].vendorSentViaViz = newName;
    },
    taxCodeChange(newName) {
      this.value.items[this.activeWoItemIndex].outsideServices[
        this.activeItemIndex
      ].taxCodeViz = newName;
    },
    newItem() {
      const newIndex = this.value.items[this.activeWoItemIndex].outsideServices
        .length;

      this.value.items[this.activeWoItemIndex].outsideServices.push({
        id: 0,
        concurrency: 0,
        unitId: 0, //zero to break rule on new
        notes: null,
        vendorSentToId: null,
        vendorSentViaId: null,
        rmaNumber: null,
        trackingNumber: null,
        repairCost: 0,
        repairPrice: 0,
        shippingCost: 0,
        shippingPrice: 0,
        sentDate: window.$gz.locale.nowUTC8601String(),
        etaDate: null,
        returnDate: null,
        taxCodeId: null,
        isDirty: true,
        pmItemId: this.value.items[this.activeWoItemIndex].id,
        uid: Date.now(),
        unitViz: null,
        vendorSentToViz: null,
        vendorSentViaViz: null,
        taxCodeViz: null
      });
      this.$emit("change");
      this.selectedRow = [{ index: newIndex }];
      this.activeItemIndex = newIndex;
      //trigger rule breaking / validation
      this.$nextTick(() => {
        this.value.items[this.activeWoItemIndex].outsideServices[
          this.activeItemIndex
        ].unitId = null;
        this.fieldValueChanged(
          `Items[${this.activeWoItemIndex}].outsideServices[${this.activeItemIndex}].unitId`
        );
      });
      return newIndex; //for create new on goto
    },
    unDeleteItem() {
      this.value.items[this.activeWoItemIndex].outsideServices[
        this.activeItemIndex
      ].deleted = false;
      this.setDefaultView();
    },
    deleteItem() {
      this.value.items[this.activeWoItemIndex].outsideServices[
        this.activeItemIndex
      ].deleted = true;
      this.setDefaultView();
      this.$emit("change");
    },
    deleteAllItem() {
      this.value.items[this.activeWoItemIndex].outsideServices.forEach(
        z => (z.deleted = true)
      );
      this.setDefaultView();
      this.$emit("change");
    },
    setDefaultView: function() {
      //if only one record left then display it otherwise just let the datatable show what the user can click on
      if (
        this.value.items[this.activeWoItemIndex].outsideServices.length == 1
      ) {
        this.selectedRow = [{ index: 0 }];
        this.activeItemIndex = 0;
      } else {
        this.selectedRow = [];
        this.activeItemIndex = null; //select nothing in essence resetting a child selects and this one too clearing form
      }
    },
    handleRowClick: function(item) {
      this.activeItemIndex = item.index;
      this.selectedRow = [{ index: item.index }];
    },
    form() {
      return window.$gz.form;
    },
    fieldValueChanged(ref) {
      if (!this.formState.loading && !this.formState.readonly) {
        //flag this record dirty so it gets picked up by save
        this.value.items[this.activeWoItemIndex].outsideServices[
          this.activeItemIndex
        ].isDirty = true;
        window.$gz.form.fieldValueChanged(this.pvm, ref);
      }
    },
    itemRowClasses: function(item) {
      let ret = "";
      const isDeleted =
        this.value.items[this.activeWoItemIndex].outsideServices[item.index]
          .deleted === true;

      const hasError = this.form().childRowHasError(
        this,
        `Items[${this.activeWoItemIndex}].OutsideServices[${item.index}].`
      );

      if (isDeleted) {
        ret += this.form().tableRowDeletedClass();
      }
      if (hasError) {
        ret += this.form().tableRowErrorClass();
      }
      return ret;
    }
    //---
  }
};
</script>
