<template>
  <div class="form-background p-3">
    <b-form novalidate @submit.prevent="handleSubmit">
      <b-form-group
        :label="
          $t('pageServerPowerOperations.bootSettings.bootSettingsOverride')
        "
        label-for="boot-option"
        class="mb-3"
      >
        <b-form-select
          id="boot-option"
          v-model="form.bootOption"
          :disabled="bootSourceOptions.length === 0 || isNotAdmin"
          :options="bootSourceOptionsReplaced"
          @change="onChangeSelect"
        >
        </b-form-select>
      </b-form-group>
      <b-form-checkbox
        v-model="form.oneTimeBoot"
        class="mb-4"
        :disabled="form.bootOption === 'Отключено' || isNotAdmin"
        @change="$v.form.oneTimeBoot.$touch()"
      >
        {{ $t('pageServerPowerOperations.bootSettings.enableOneTimeBoot') }}
      </b-form-checkbox>
      <b-form-group :label="$t('pageServerPowerOperations.serverBootMode')">
        <b-form-radio-group
          id="boot-settings-mode"
          v-model="form.bootMode"
          :disabled="isNotAdmin"
          stacked
        >
          <b-form-radio class="mb-1" :value="bootModeOptions[0]">
            {{ `${bootModeOptions[0]}` }}
          </b-form-radio>

          <b-form-radio class="mb-1" :value="bootModeOptions[1]">
            {{ `${bootModeOptions[1]}` }}
          </b-form-radio>
        </b-form-radio-group>
      </b-form-group>
      <b-form-group
        :label="$t('pageServerPowerOperations.bootSettings.tpmRequiredPolicy')"
      >
        <b-form-text id="tpm-required-policy-help-block">
          {{
            $t('pageServerPowerOperations.bootSettings.tpmRequiredPolicyHelper')
          }}
        </b-form-text>
        <b-form-checkbox
          id="tpm-required-policy"
          v-model="form.tpmPolicyOn"
          aria-describedby="tpm-required-policy-help-block"
          :disabled="isNotAdmin"
          @change="$v.form.tpmPolicyOn.$touch()"
        >
          {{ $t('global.status.enabled') }}
        </b-form-checkbox>
      </b-form-group>
      <b-button
        variant="primary"
        type="submit"
        class="mb-3"
        :disabled="isNotAdmin"
      >
        {{ $t('global.action.save') }}
      </b-button>
    </b-form>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import BVToastMixin from '@/components/_sila/Mixins/BVToastMixin';
import LoadingBarMixin from '@/components/_sila/Mixins/LoadingBarMixin';
import {
  replaceNone,
  replaceRevert,
  replaceNoneArr,
} from '@/utilities/_sila/transformBootOption.js';

export default {
  name: 'BootSettings',
  mixins: [BVToastMixin, LoadingBarMixin],
  data() {
    return {
      form: {
        bootOption: replaceNone(
          this.$store.getters['serverBootSettings/bootSource']
        ),
        bootMode: this.$store.getters['serverBootSettings/bootMode'],
        oneTimeBoot: this.$store.getters['serverBootSettings/overrideEnabled'],
        tpmPolicyOn: this.$store.getters['serverBootSettings/tpmEnabled'],
      },
    };
  },
  computed: {
    ...mapState('serverBootSettings', [
      'bootSourceOptions',
      'bootModeOptions',
      'bootSource',
      'bootMode',
      'overrideEnabled',
      'tpmEnabled',
    ]),
    isNotAdmin() {
      return this.$store.getters['authentication/role'] === 'ReadOnly';
    },
    bootSourceOptionsReplaced() {
      return replaceNoneArr(this.bootSourceOptions);
    },
  },
  watch: {
    bootSource: function (value) {
      this.form.bootOption = replaceNone(value);
    },
    bootMode: function (value) {
      this.form.bootMode = value;
    },
    overrideEnabled: function (value) {
      this.form.oneTimeBoot = value;
    },
    tpmEnabled: function (value) {
      this.form.tpmPolicyOn = value;
    },
  },
  validations: {
    // Empty validations to leverage vuelidate form states
    // to check for changed values
    form: {
      bootOption: {},
      bootMode: {},
      oneTimeBoot: {},
      tpmPolicyOn: {},
    },
  },
  created() {
    this.$store
      .dispatch('serverBootSettings/getTpmPolicy')
      .finally(() =>
        this.$root.$emit('server-power-operations-boot-settings-complete')
      );
  },
  methods: {
    handleSubmit() {
      this.startLoader();
      const tpmPolicyChanged = this.$v.form.tpmPolicyOn.$dirty;
      let settings;
      let bootSource = replaceRevert(this.form.bootOption);
      let overrideEnabled = this.form.oneTimeBoot;
      let bootMode = this.form.bootMode;
      let tpmEnabled = null;

      if (tpmPolicyChanged) tpmEnabled = this.form.tpmPolicyOn;
      settings = { bootSource, overrideEnabled, tpmEnabled, bootMode };

      this.$store
        .dispatch('serverBootSettings/saveSettings', settings)
        .then((message) => this.successToast(message))
        .catch(({ message }) => this.errorToast(message))
        .finally(() => {
          this.$v.form.$reset();
          this.endLoader();
        });
    },
    onChangeSelect(selectedOption) {
      this.$v.form.bootOption.$touch();
      // Disable one time boot if selected boot option is 'None'
      if (selectedOption === 'None') this.form.oneTimeBoot = false;
    },
  },
};
</script>
