<script setup>
import { useI18n } from 'vue-i18n';
import FormModel from 'components/FormModel.vue';
import VnRow from 'components/ui/VnRow.vue';
import VnSubToolbar from 'src/components/ui/VnSubToolbar.vue';
import VnInput from 'src/components/common/VnInput.vue';
import axios from 'axios';
import useNotify from 'src/composables/useNotify.js';

const { t } = useI18n();
const { notify } = useNotify();

const onSynchronizeAll = async () => {
    try {
        notify(t('Synchronizing in the background'), 'positive');
        await axios.patch(`Accounts/syncAll`);
    } catch (error) {
        console.error('Error synchronizing all accounts', error);
    }
};

const onSynchronizeRoles = async () => {
    try {
        await axios.patch(`RoleInherits/sync`);
        notify(t('Roles synchronized!'), 'positive');
    } catch (error) {
        console.error('Error synchronizing roles', error);
    }
};
</script>

<template>
    <QPage>
        <VnSubToolbar />
        <FormModel
            :url="`AccountConfigs/${1}`"
            :url-update="`AccountConfigs/${1}`"
            model="AccountAccounts"
            auto-load
        >
            <template #moreActions>
                <QBtn
                    class="q-ml-none"
                    color="primary"
                    :label="t('accounts.syncAll')"
                    @click="onSynchronizeAll()"
                />
                <QBtn
                    color="primary"
                    :label="t('accounts.syncRoles')"
                    @click="onSynchronizeRoles()"
                />
            </template>
            <template #form="{ data }">
                <div class="q-gutter-y-sm">
                    <VnInput :label="t('accounts.homedir')" v-model="data.homedir" />
                    <VnInput :label="t('accounts.shell')" v-model="data.shell" />
                    <VnInput
                        :label="t('accounts.idBase')"
                        v-model="data.idBase"
                        type="number"
                        min="0"
                    />
                    <VnRow>
                        <VnInput
                            :label="t('accounts.min')"
                            v-model="data.min"
                            type="number"
                            min="0"
                        />
                        <VnInput
                            :label="t('accounts.max')"
                            v-model="data.max"
                            type="number"
                            min="0"
                        />
                    </VnRow>
                    <VnRow>
                        <VnInput
                            :label="t('accounts.warn')"
                            v-model="data.warn"
                            type="number"
                            min="0"
                        />
                        <VnInput
                            :label="t('accounts.inact')"
                            v-model="data.inact"
                            type="number"
                            min="0"
                        />
                    </VnRow>
                </div>
            </template>
        </FormModel>
    </QPage>
</template>

<i18n>
es:
    Roles synchronized!: ¡Roles sincronizados!
    Synchronizing in the background: Sincronizando en segundo plano
</i18n>
