<template>
    <div>
        <my-card v-if="$scopedSlots.search" class="tw-mb-2.5">
            <a-form-model :model="queryForm" layout="horizontal">
                <a-row :gutter="16">
                    <slot name="search" :query="queryForm" />
                </a-row>
            </a-form-model>

            <div class="tw-text-right tw-mt-2">
                <a-space>
                    <a-button @click="reset">重置</a-button>
                    <a-button type="primary" @click="getData">查询</a-button>
                </a-space>
            </div>
        </my-card>

        <my-card>
            <a-space class="tw-mb-2">
                <a-button type="primary" v-if="newBtn" @click="addBtnClick">
                    {{ newBtn.text || '新增' }}
                </a-button>
                <slot name="operation" />
            </a-space>

            <a-table
                :data-source="data"
                :loading="loading"
                v-bind="$attrs"
                :rowKey="rowKey"
                :pagination="pagination"
                @change="pageChange"
                :row-selection="selected ? rowSelection : undefined"
            >
                <slot />
                <a-table-column title="操作" v-if="buttons">
                    <template #default="row">
                        <my-ac-btn :row="row" :buttons="buttons" />
                    </template>
                </a-table-column>
            </a-table>
        </my-card>

        <a-drawer
            placement="right"
            :visible="visible"
            :drawerStyle="drawerStyle"
            :bodyStyle="bodyStyle"
            destroyOnClose
            :width="600"
            @close="hidden"
            :maskClosable="false"
            :title="title"
        >
            <slot name="drawer" :hidden="hidden" :refresh="getData" />
        </a-drawer>
    </div>
</template>

<script>
import { request, METHOD } from '@/utils/requestUtil';

const initQuery = {
    pageSize: 10,
    pageNum: 1,
};

const defaultTitle = '新增';

export default {
    props: {
        url: String,
        addBtn: [Object, Boolean],
        buttons: Array,
        noPage: Boolean,
        formatData: Function,
        rowKey: [String, Function],
        selected: Array,
    },

    data() {
        const newBtn = this.addBtn ? (typeof this.addBtn === 'object' ? this.addBtn : {}) : this.addBtn;
        return {
            initQuery: {
                ...initQuery,
            },
            data: [],
            queryForm: {},
            loading: false,
            total: 0,
            visible: false,
            title: newBtn.title ?? defaultTitle,
            drawerStyle: {
                display: 'flex',
                flexDirection: 'column',
                overflowY: 'hidden',
            },
            bodyStyle: {
                flex: 1,
                overflow: 'hidden',
            },
        };
    },

    mounted() {
        this.getData();
    },

    computed: {
        newBtn() {
            return this.addBtn ? (typeof this.addBtn === 'object' ? this.addBtn : {}) : this.addBtn;
        },
        pagination() {
            return this.noPage
                ? false
                : {
                      current: this.initQuery.pageNum,
                      pageSize: this.initQuery.pageSize,
                      total: this.total,
                      showQuickJumper: true,
                  };
        },
        rowSelection() {
            return {
                onChange: (selectedRowKeys, selectedRows) => {
                    this.$emit('update:selected', [selectedRowKeys, selectedRows]);
                },
            };
        },
    },

    methods: {
        async getData() {
            this.loading = true;
            try {
                this.noPage ? await this.getDataNoPage() : await this.getDataWithPage();
            } catch (error) {
                // todo
            }
            this.loading = false;
        },

        async getDataWithPage() {
            const res = await request(this.url, METHOD.GET, { ...this.initQuery, ...this.queryForm });
            this.total = res.total;
            if (this.formatData) this.data = this.formatData(res);
            else this.data = res.records;
        },

        async getDataNoPage() {
            const res = await request(this.url, METHOD.GET, this.queryForm);
            if (this.formatData) this.data = this.formatData(res);
            else this.data = res;
        },

        pageChange(page) {
            this.initQuery.pageNum = page.current;
            this.getData();
        },

        hidden() {
            this.visible = false;
            this.title = this.addBtn?.title ?? defaultTitle;
        },

        show({ title } = {}) {
            this.visible = true;
            if (title) this.title = title;
        },

        reset() {
            this.queryForm = {};
            this.initQuery = { ...initQuery };
            this.getData();
        },

        addBtnClick() {
            const { click } = typeof this.addBtn === 'object' ? this.addBtn : {};
            click && click();
            this.visible = true;
        },

        getFormRef(ref) {
            console.log(ref);
        },
    },
};
</script>
