<script setup>
import axios from 'axios';
import { computed, ref, toRefs } from 'vue';
import { useI18n } from 'vue-i18n';
import { useVnConfirm } from 'composables/useVnConfirm';
import { useRoute } from 'vue-router';
import { useAcl } from 'src/composables/useAcl';
import { useArrayData } from 'src/composables/useArrayData';
import VnConfirm from 'src/components/ui/VnConfirm.vue';
import VnChangePassword from 'src/components/common/VnChangePassword.vue';
import useNotify from 'src/composables/useNotify.js';

const $props = defineProps({
    hasAccount: {
        type: Boolean,
        default: false,
        required: true,
    },
});
const { t } = useI18n();
const { hasAccount } = toRefs($props);
const { openConfirmationModal } = useVnConfirm();
const route = useRoute();
const { notify } = useNotify();
const account = computed(() => useArrayData('AccountId').store.data[0]);
account.value.hasAccount = hasAccount.value;
const entityId = computed(() => +route.params.id);

async function updateStatusAccount(active) {
    if (active) {
        await axios.post(`Accounts`, { id: entityId.value });
    } else {
        await axios.delete(`Accounts/${entityId.value}`);
    }

    account.value.hasAccount = active;
    const status = active ? 'enable' : 'disable';
    notify({
        message: t(`account.card.${status}Account.success`),
        type: 'positive',
    });
}
async function updateStatusUser(active) {
    await axios.patch(`VnUsers/${entityId.value}`, { active });
    account.value.active = active;
    const status = active ? 'activate' : 'deactivate';
    notify({
        message: t(`account.card.actions.${status}User.success`),
        type: 'positive',
    });
}
const showSyncDialog = ref(false);
const syncPassword = ref(null);
const shouldSyncPassword = ref(false);
async function sync() {
    const params = { force: true };
    if (shouldSyncPassword.value) params.password = syncPassword.value;
    await axios.patch(`Accounts/${account.value.name}/sync`, {
        params,
    });
    notify({
        message: t('account.card.actions.sync.success'),
        type: 'positive',
    });
}
</script>
<template>
    <VnChangePassword
        ref="changePassRef"
        :ask-old-pass="true"
        :submit-fn="
            async (newPassword, oldPassword) => {
                await axios.patch(`Accounts/change-password`, {
                    userId: entityId,
                    newPassword,
                    oldPassword,
                });
            }
        "
    />
    <VnConfirm
        v-model="showSyncDialog"
        :message="t('account.card.actions.sync.message')"
        :title="t('account.card.actions.sync.title')"
        :promise="sync"
    >
        <template #customHTML>
            {{ shouldSyncPassword }}
            <QCheckbox
                :label="t('account.card.actions.sync.checkbox')"
                v-model="shouldSyncPassword"
                class="full-width"
                clearable
                clear-icon="close"
            >
                <QIcon style="padding-left: 10px" color="primary" name="info" size="sm">
                    <QTooltip>{{ t('account.card.actions.sync.tooltip') }}</QTooltip>
                </QIcon></QCheckbox
            >
            <QInput
                v-if="shouldSyncPassword"
                :label="t('login.password')"
                v-model="syncPassword"
                class="full-width"
                clearable
                clear-icon="close"
                type="password"
            />
        </template>
    </VnConfirm>
    <QItem
        v-if="
            entityId == account.id &&
            useAcl().hasAny([{ model: 'Account', props: '*', accessType: 'WRITE' }])
        "
        v-ripple
        clickable
        @click="$refs.changePassRef.show()"
    >
        <QItemSection>{{ t('globals.changePass') }}</QItemSection>
    </QItem>
    <QItem
        v-if="account.hasAccount"
        v-ripple
        clickable
        @click="
            openConfirmationModal(
                t('account.card.actions.disableAccount.title'),
                t('account.card.actions.disableAccount.subtitle'),
                () => updateStatusAccount(false)
            )
        "
    >
        <QItemSection>{{ t('account.card.actions.disableAccount.name') }}</QItemSection>
    </QItem>

    <QItem
        v-if="!account.active"
        v-ripple
        clickable
        @click="
            openConfirmationModal(
                t('account.card.actions.activateUser.title'),
                t('account.card.actions.activateUser.title'),
                () => updateStatusUser(true)
            )
        "
    >
        <QItemSection>{{ t('account.card.actions.activateUser.name') }}</QItemSection>
    </QItem>
    <QItem
        v-if="account.active"
        v-ripple
        clickable
        @click="
            openConfirmationModal(
                t('account.card.actions.deactivateUser.title'),
                t('account.card.actions.deactivateUser.title'),
                () => updateStatusUser(false)
            )
        "
    >
        <QItemSection>{{ t('account.card.actions.deactivateUser.name') }}</QItemSection>
    </QItem>
    <QItem v-ripple clickable @click="showSyncDialog = true">
        <QItemSection>{{ t('account.card.actions.sync.name') }}</QItemSection>
    </QItem>
    <QSeparator />
</template>
