<template>
  <os-card>
    <h2 class="title">{{ $t('admin.donations.name') }}</h2>
    <form @submit.prevent="submit" novalidate>
      <p class="ds-text show-donations-checkbox">
        <input id="showDonations" type="checkbox" v-model="showDonations" />
        <label for="showDonations">
          {{ $t('admin.donations.showDonationsCheckboxLabel') }}
        </label>
      </p>
      <ocelot-input
        id="donations-goal"
        class="donations-data"
        model="goal"
        :label="$t('admin.donations.goal')"
        placeholder="15000"
        icon="money"
        :disabled="!showDonations"
        data-test="donations-goal"
      />
      <ocelot-input
        id="donations-progress"
        class="donations-data"
        model="progress"
        :label="$t('admin.donations.progress')"
        placeholder="1200"
        icon="money"
        :disabled="!showDonations"
        data-test="donations-progress"
      />
      <os-button class="donations-info-button" variant="primary" appearance="filled" type="submit">
        {{ $t('actions.save') }}
      </os-button>
    </form>
  </os-card>
</template>

<script>
import { OsButton, OsCard } from '@ocelot-social/ui'
import { DonationsQuery, UpdateDonations } from '~/graphql/Donations'
import formValidation from '~/mixins/formValidation'
import OcelotInput from '~/components/OcelotInput/OcelotInput.vue'

export default {
  mixins: [formValidation],
  components: { OsButton, OsCard, OcelotInput },
  data() {
    return {
      formData: {
        goal: null,
        progress: null,
      },
      // TODO: Our styleguide does not support checkmarks.
      // Integrate showDonations into `this.formData` once we
      // have checkmarks available.
      showDonations: false,
    }
  },
  methods: {
    submit() {
      const { showDonations } = this
      let { goal, progress } = this.formData
      goal = typeof goal === 'string' && goal.length > 0 ? goal : '15000'
      progress = typeof progress === 'string' && progress.length > 0 ? progress : '0'
      this.$apollo
        .mutate({
          mutation: UpdateDonations(),
          variables: {
            showDonations,
            goal: parseInt(goal),
            progress: parseInt(progress) < parseInt(goal) ? parseInt(progress) : parseInt(goal),
          },
          update: (_store, { data }) => {
            if (!data || !data.UpdateDonations) return
            const { showDonations, goal, progress } = data.UpdateDonations
            this.showDonations = showDonations
            this.formData = {
              goal: goal.toString(10),
              progress: progress < goal ? progress.toString(10) : goal.toString(10),
            }
          },
        })
        .then(() => {
          this.$toast.success(this.$t('admin.donations.successfulUpdate'))
        })
        .catch((error) => this.$toast.error(error.message))
    },
  },
  apollo: {
    Donations: {
      query() {
        return DonationsQuery()
      },
      update({ Donations }) {
        if (!Donations) return
        const { showDonations, goal, progress } = Donations
        this.showDonations = showDonations
        this.formData = {
          goal: goal.toString(10),
          progress: progress < goal ? progress.toString(10) : goal.toString(10),
        }
      },
    },
  },
}
</script>

<style lang="scss" scoped>
.show-donations-checkbox {
  margin-top: $space-base;
  margin-bottom: $space-small;
}

.donations-data {
  margin-left: $space-small;
}

.donations-info-button {
  margin-top: $space-small;
}
</style>
