<script setup>
import axios from 'axios';
import { computed, ref, toRefs } from 'vue';
import { useQuasar } from 'quasar';
import { useI18n } from 'vue-i18n';
import { useVnConfirm } from 'composables/useVnConfirm';
import { useRoute } from 'vue-router';
import { useArrayData } from 'src/composables/useArrayData';
import CustomerChangePassword from 'src/pages/Customer/components/CustomerChangePassword.vue';
import VnConfirm from 'src/components/ui/VnConfirm.vue';

const quasar = useQuasar();
const $props = defineProps({
    hasAccount: {
        type: Boolean,
        default: false,
        required: true,
    },
});
const { t } = useI18n();
const { hasAccount } = toRefs($props);
const { openConfirmationModal } = useVnConfirm();
const route = useRoute();

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';
    quasar.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';
    quasar.notify({
        message: t(`account.card.actions.${status}User.success`),
        type: 'positive',
    });
}
function setPassword() {
    quasar.dialog({
        component: CustomerChangePassword,
        componentProps: {
            id: entityId.value,
        },
    });
}
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,
    });
    quasar.notify({
        message: t('account.card.actions.sync.success'),
        type: 'positive',
    });
}
</script>
<template>
    <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-ripple clickable @click="setPassword">
        <QItemSection>{{ t('account.card.actions.setPassword') }}</QItemSection>
    </QItem>
    <QItem
        v-if="!account.hasAccount"
        v-ripple
        clickable
        @click="
            openConfirmationModal(
                t('account.card.actions.enableAccount.title'),
                t('account.card.actions.enableAccount.subtitle'),
                () => updateStatusAccount(true)
            )
        "
    >
        <QItemSection>{{ t('account.card.actions.enableAccount.name') }}</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 />
    <QItem
        @click="
            openConfirmationModal(
                t('account.card.actions.delete.title'),
                t('account.card.actions.delete.subTitle'),
                removeAccount
            )
        "
        v-ripple
        clickable
    >
        <QItemSection avatar>
            <QIcon name="delete" />
        </QItemSection>
        <QItemSection>{{ t('account.card.actions.delete.name') }}</QItemSection>
    </QItem>
</template>
