<template>
    <admin-page-filters>
        <a-row :gutter="[16, 16]">
            <a-col :xs="24" :sm="24" :md="12" :lg="10" :xl="10">
                <a-space>
                    <slot name="actionButtons"></slot>
                    <a-button
                        v-if="
                            table.selectedRowKeys.length > 0 &&
                            (permsArray.includes('currencies_delete') ||
                                permsArray.includes('admin'))
                        "
                        type="primary"
                        @click="showSelectedDeleteConfirm"
                        danger
                    >
                        <template #icon><DeleteOutlined /></template>
                        {{ $t("common.delete") }}
                    </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="12" :xl="10">
                        <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('currency.name'),
                                ])
                            "
                        />
                    </a-col>
                </a-row>
            </a-col>
        </a-row>
    </admin-page-filters>

    <admin-page-table-content>
        <AddEdit
            :addEditType="addEditType"
            :visible="addEditVisible"
            :url="addEditUrl"
            @addEditSuccess="addEditSuccess"
            @closed="onCloseAddEdit"
            :formData="formData"
            :data="viewData"
            :pageTitle="pageTitle"
            :successMessage="successMessage"
        />
        <a-row>
            <a-col :span="24">
                <div class="table-responsive">
                    <a-table
                        :row-selection="{
                            selectedRowKeys: table.selectedRowKeys,
                            onChange: onRowSelectChange,
                            getCheckboxProps: (record) => ({
                                disabled:
                                    ((panelType == 'admin' &&
                                        (permsArray.includes('currencies_delete') ||
                                            permsArray.includes('admin'))) ||
                                        panelType == 'superadmin') &&
                                    appSetting.x_currency_id != record.xid
                                        ? false
                                        : true,
                                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 === 'action'">
                                <a-button
                                    v-if="
                                        (panelType == 'admin' &&
                                            (permsArray.includes('currencies_edit') ||
                                                permsArray.includes('admin'))) ||
                                        panelType == 'superadmin'
                                    "
                                    type="primary"
                                    @click="editItem(record)"
                                    style="margin-left: 4px"
                                >
                                    <template #icon><EditOutlined /></template>
                                </a-button>
                                <a-button
                                    v-if="
                                        ((panelType == 'admin' &&
                                            (permsArray.includes('currencies_delete') ||
                                                permsArray.includes('admin'))) ||
                                            panelType == 'superadmin') &&
                                        appSetting.x_currency_id != record.xid
                                    "
                                    type="primary"
                                    @click="showDeleteConfirm(record.xid)"
                                    style="margin-left: 4px"
                                >
                                    <template #icon><DeleteOutlined /></template>
                                </a-button>
                            </template>
                        </template>
                    </a-table>
                </div>
            </a-col>
        </a-row>
    </admin-page-table-content>
</template>

<script setup>
import { onMounted } from "vue";
import { EditOutlined, DeleteOutlined } from "@ant-design/icons-vue";
import fields from "./fields";
import AddEdit from "./AddEdit.vue";

const props = defineProps({
    panelType: {
        type: String,
        default: "admin",
    },
});

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

const { permsArray, appSetting } = common();
const { url, addEditUrl, initData, columns, filterableColumns } = fields(props.panelType);

// 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();

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

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

    table.filterableColumns = filterableColumns;

    fetch({
        page: 1,
    });

    crudUrl.value = addEditUrl;
    langKey.value = "currency";
    crudInitData.value = { ...initData };
    formData.value = { ...initData };
};

// Expose methods to parent component
defineExpose({
    addItem,
});
</script>
