<template>
    <admin-page-header>
        <template #header>
            <a-page-header :title="$t(`menu.staff_members`)" class="p-0" />
        </template>
        <template #breadcrumb>
            <a-breadcrumb separator="-" style="font-size: 12px">
                <a-breadcrumb-item>
                    <router-link :to="{ name: 'admin.dashboard.index' }">
                        {{ $t(`menu.dashboard`) }}
                    </router-link>
                </a-breadcrumb-item>
                <a-breadcrumb-item>
                    {{ $t(`menu.staff_members`) }}
                </a-breadcrumb-item>
            </a-breadcrumb>
        </template>
    </admin-page-header>

    <a-row>
        <a-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <admin-page-filters>
                <a-row :gutter="[16, 16]">
                    <a-col :xs="24" :sm="24" :md="12" :lg="10" :xl="10">
                        <a-space wrap class="staff-action-buttons">
                            <template
                                v-if="
                                    permsArray.includes('staff_members_create') ||
                                    permsArray.includes('admin')
                                "
                            >
                                <a-button type="primary" @click="addItem" class="staff-btn staff-btn-add">
                                    <PlusOutlined />
                                    <span class="btn-text">{{ $t("staff_member.add") }}</span>
                                </a-button>
                                <a-button @click="showQuickAdd" class="staff-btn staff-btn-quick">
                                    <UsergroupAddOutlined />
                                    <span class="btn-text">{{ $t("staff_member.quick_add") }}</span>
                                </a-button>
                                <a-button @click="showImport" class="staff-btn staff-btn-import">
                                    <ImportOutlined />
                                    <span class="btn-text">{{ $t("staff_member.import") }}</span>
                                </a-button>
                            </template>
                            <a-button
                                v-if="
                                    table.selectedRowKeys.length > 0 &&
                                    (permsArray.includes('staff_members_delete') ||
                                        permsArray.includes('admin'))
                                "
                                type="primary"
                                @click="showSelectedDeleteConfirm"
                                danger
                                class="staff-btn"
                            >
                                <template #icon><DeleteOutlined /></template>
                                <span class="btn-text">{{ $t("common.delete") }}</span>
                            </a-button>
                        </a-space>
                    </a-col>
                    <a-col :xs="24" :sm="24" :md="12" :lg="14" :xl="14">
                        <a-row :gutter="[16, 16]" justify="end">
                            <a-col :xs="24" :sm="24" :md="16" :lg="8" :xl="8">
                                <a-input-group compact>
                                    <a-input-search
                                        style="width: 100%"
                                        v-model:value="table.searchString"
                                        show-search
                                        @change="onTableSearch"
                                        @search="onTableSearch"
                                        :loading="table.filterLoading"
                                        :placeholder="
                                            $t('common.placeholder_search_text', [
                                                $t('staff_member.name'),
                                            ])
                                        "
                                    />
                                </a-input-group>
                            </a-col>
                        </a-row>
                    </a-col>
                </a-row>
            </admin-page-filters>

            <admin-page-table-content>
                <a-row>
                    <a-col :span="24">
                        <a-tabs
                            v-model:activeKey="extraFilters.status"
                            @change="setUrlData"
                        >
                            <a-tab-pane key="all" :tab="$t('common.all')" />
                            <a-tab-pane key="active" :tab="$t('common.active')" />
                            <a-tab-pane key="inactive" :tab="$t('common.inactive')" />
                        </a-tabs>
                    </a-col>
                </a-row>

                <AddEdit
                    :addEditType="addEditType"
                    :visible="addEditVisible"
                    :url="crudAddEditUrl"
                    :formData="formData"
                    :data="viewData"
                    :pageTitle="pageTitle"
                    :successMessage="successMessage"
                    @addEditSuccess="addEditSuccess"
                    @closed="onCloseAddEdit"
                />

                <a-row>
                    <a-col :span="24">
                        <div class="table-responsive">
                            <a-table
                                :row-selection="{
                                    selectedRowKeys: table.selectedRowKeys,
                                    onChange: onRowSelectChange,
                                    getCheckboxProps: (record) => ({
                                        disabled: user.xid == record.xid ? true : false,
                                        name: record.xid,
                                    }),
                                }"
                                :columns="columns"
                                :row-key="(record) => record.xid"
                                :data-source="table.data"
                                :pagination="table.pagination"
                                :loading="table.loading"
                                @change="handleTableChange"
                                bordered
                                size="middle"
                            >
                                <template #bodyCell="{ column, record }">
                                    <template v-if="column.dataIndex === 'name'">
                                        <UserInfoCard
                                            :name="record.name"
                                            :role="record.role?.display_name"
                                            :image="record.profile_image_url"
                                            @click="viewItem(record)"
                                            :rounded="true"
                                        />
                                    </template>

                                    <template v-if="column.dataIndex === 'status'">
                                        <a-tag
                                            :color="
                                                record.status === 'active'
                                                    ? 'success'
                                                    : 'error'
                                            "
                                        >
                                            {{ $t(`common.${record.status}`) }}
                                        </a-tag>
                                    </template>
                                    <template v-if="column.dataIndex === 'created_at'">
                                        {{ formatDateTime(record.created_at) }}
                                    </template>
                                    <template v-if="column.dataIndex === 'action'">
                                        <a-space>
                                            <a-button
                                                v-if="
                                                    permsArray.includes(
                                                        'staff_members_view'
                                                    ) || permsArray.includes('admin')
                                                "
                                                type="primary"
                                                @click="viewItem(record)"
                                            >
                                                <template #icon><EyeOutlined /></template>
                                            </a-button>
                                            <a-button
                                                v-if="
                                                    permsArray.includes(
                                                        'staff_members_edit'
                                                    ) || permsArray.includes('admin')
                                                "
                                                type="primary"
                                                @click="editItem(record)"
                                            >
                                                <template #icon
                                                    ><EditOutlined
                                                /></template>
                                            </a-button>
                                            <a-button
                                                v-if="
                                                    (permsArray.includes(
                                                        'staff_members_delete'
                                                    ) ||
                                                        permsArray.includes('admin')) &&
                                                    user.xid != record.xid
                                                "
                                                type="primary"
                                                @click="showDeleteConfirm(record.xid)"
                                            >
                                                <template #icon
                                                    ><DeleteOutlined
                                                /></template>
                                            </a-button>
                                        </a-space>
                                    </template>
                                </template>
                            </a-table>
                        </div>
                    </a-col>
                </a-row>
                <UserView
                    :user="viewData"
                    :visible="detailsVisible"
                    @closed="onCloseDetails"
                />

                <QuickAdd
                    :visible="quickAddVisible"
                    @closed="onCloseQuickAdd"
                    @success="onQuickAddSuccess"
                />

                <ImportModal
                    :visible="importVisible"
                    @closed="onCloseImport"
                    @success="onImportSuccess"
                />
            </admin-page-table-content>
        </a-col>
    </a-row>
</template>

<script setup>
import { onMounted, ref } from "vue";
import {
    EyeOutlined,
    PlusOutlined,
    EditOutlined,
    DeleteOutlined,
    UsergroupAddOutlined,
    ImportOutlined,
} from "@ant-design/icons-vue";
import { useI18n } from "vue-i18n";
import fields from "./fields";
import AddEdit from "./AddEdit.vue";
import UserView from "./View.vue";
import QuickAdd from "./QuickAdd.vue";
import ImportModal from "./ImportModal.vue";
import UserInfoCard from "@/common/components/user/UserInfoCard.vue";

// Get composables from global window object
const crud = window.__composables.crud;
const common = window.__composables.common;

const { t } = useI18n();
const { permsArray, formatDateTime, user } = common();
const {
    url,
    addEditUrl,
    initData,
    columns,
    filterableColumns,
    hashableColumns,
} = fields();

// Call crud() and destructure all returns
const {
    table,
    addEditVisible,
    addEditType,
    addEditUrl: crudAddEditUrl,
    formData,
    viewData,
    pageTitle,
    successMessage,
    addItem,
    editItem,
    onCloseAddEdit,
    addEditSuccess,
    showDeleteConfirm,
    showSelectedDeleteConfirm,
    handleTableChange,
    onTableSearch,
    onRowSelectChange,
    tableUrl,
    crudUrl,
    langKey,
    initData: crudInitData,
    fetch,
    viewItem,
    detailsVisible,
    onCloseDetails,
    hashableColumns: crudhashableColumns,
} = crud();

const extraFilters = ref({
    status: "all",
});
const quickAddVisible = ref(false);
const importVisible = ref(false);

const showQuickAdd = () => {
    quickAddVisible.value = true;
};

const onCloseQuickAdd = () => {
    quickAddVisible.value = false;
};

const onQuickAddSuccess = () => {
    quickAddVisible.value = false;
    setUrlData();
};

const showImport = () => {
    importVisible.value = true;
};

const onCloseImport = () => {
    importVisible.value = false;
};

const onImportSuccess = () => {
    importVisible.value = false;
    setUrlData();
};

onMounted(() => {
    setUrlData();
});

const setUrlData = () => {
    tableUrl.value = {
        url,
        extraFilters,
    };

    table.filterableColumns = filterableColumns;

    fetch({
        page: 1,
    });

    crudUrl.value = addEditUrl;
    langKey.value = "staff_member";
    crudInitData.value = { ...initData };
    formData.value = { ...initData };
    crudhashableColumns.value = { ...hashableColumns };
};

</script>

<style scoped>
/* Staff action buttons container */
.staff-action-buttons {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.staff-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

/* Mobile Responsive Styles */
@media screen and (max-width: 768px) {
    .staff-action-buttons {
        gap: 6px;
    }

    .staff-btn {
        padding: 4px 10px;
        font-size: 13px;
    }

    .staff-btn .btn-text {
        display: none;
    }

    .staff-btn-add .btn-text {
        display: inline;
    }
}

@media screen and (max-width: 576px) {
    .staff-action-buttons {
        gap: 4px;
    }

    .staff-btn {
        padding: 4px 8px;
        font-size: 12px;
        min-width: 32px;
    }

    .staff-btn .btn-text {
        display: none;
    }

    /* Only show "Add" text on smallest screens for primary action */
    .staff-btn-add .btn-text {
        display: none;
    }
}

@media screen and (max-width: 480px) {
    .staff-btn {
        padding: 4px 6px;
    }
}
</style>
