<script>
import { mapActions, mapState, mapGetters } from 'vuex';
import { GlCard, GlDeprecatedButton, GlLoadingIcon } from '@gitlab/ui';
import Tracking from '~/tracking';
import { mapComputed } from '~/vuex_shared/bindings';
import {
  UPDATE_SETTINGS_ERROR_MESSAGE,
  UPDATE_SETTINGS_SUCCESS_MESSAGE,
} from '../../shared/constants';
import ExpirationPolicyFields from '../../shared/components/expiration_policy_fields.vue';
import { SET_CLEANUP_POLICY_BUTTON, CLEANUP_POLICY_CARD_HEADER } from '../constants';

export default {
  components: {
    GlCard,
    GlDeprecatedButton,
    GlLoadingIcon,
    ExpirationPolicyFields,
  },
  mixins: [Tracking.mixin()],
  labelsConfig: {
    cols: 3,
    align: 'right',
  },
  i18n: {
    CLEANUP_POLICY_CARD_HEADER,
    SET_CLEANUP_POLICY_BUTTON,
  },
  data() {
    return {
      tracking: {
        label: 'docker_container_retention_and_expiration_policies',
      },
      formIsValid: true,
    };
  },
  computed: {
    ...mapState(['formOptions', 'isLoading']),
    ...mapGetters({ isEdited: 'getIsEdited' }),
    ...mapComputed([{ key: 'settings', getter: 'getSettings' }], 'updateSettings'),
    isSubmitButtonDisabled() {
      return !this.formIsValid || this.isLoading;
    },
    isCancelButtonDisabled() {
      return !this.isEdited || this.isLoading;
    },
  },
  methods: {
    ...mapActions(['resetSettings', 'saveSettings']),
    reset() {
      this.track('reset_form');
      this.resetSettings();
    },
    submit() {
      this.track('submit_form');
      this.saveSettings()
        .then(() => this.$toast.show(UPDATE_SETTINGS_SUCCESS_MESSAGE, { type: 'success' }))
        .catch(() => this.$toast.show(UPDATE_SETTINGS_ERROR_MESSAGE, { type: 'error' }));
    },
  },
};
</script>

<template>
  <form ref="form-element" @submit.prevent="submit" @reset.prevent="reset">
    <gl-card>
      <template #header>
        {{ $options.i18n.CLEANUP_POLICY_CARD_HEADER }}
      </template>
      <template #default>
        <expiration-policy-fields
          v-model="settings"
          :form-options="formOptions"
          :is-loading="isLoading"
          @validated="formIsValid = true"
          @invalidated="formIsValid = false"
        />
      </template>
      <template #footer>
        <div class="d-flex justify-content-end">
          <gl-deprecated-button
            ref="cancel-button"
            type="reset"
            class="mr-2 d-block"
            :disabled="isCancelButtonDisabled"
          >
            {{ __('Cancel') }}
          </gl-deprecated-button>
          <gl-deprecated-button
            ref="save-button"
            type="submit"
            :disabled="isSubmitButtonDisabled"
            variant="success"
            class="d-flex justify-content-center align-items-center js-no-auto-disable"
          >
            {{ $options.i18n.SET_CLEANUP_POLICY_BUTTON }}
            <gl-loading-icon v-if="isLoading" class="ml-2" />
          </gl-deprecated-button>
        </div>
      </template>
    </gl-card>
  </form>
</template>
