<template>
    <div>
        <div :class="$style.card" v-if="$scopedSlots.search">
            <a-form-model layout="inline" :model="queryForm">
                <slot name="search" :query="queryForm" />
            </a-form-model>

            <div class="tw-text-right tw-mt-2">
                <a-space>
                    <a-button @click="queryForm = { ...initQuery }">重置</a-button>
                    <a-button type="primary" @click="getData">查询</a-button>
                </a-space>
            </div>
        </div>

        <div class="tw-mt-3" :class="$style.card">
            <a-space class="tw-mb-2.5">
                <a-button v-if="addBtn" type="primary" key="add" v-bind="addBtn.options" @click="add">
                    {{ addBtn.text || '新建' }}
                </a-button>
                <template v-for="btn of buttons">
                    <a-button :type="btn.primary" :key="btn.text">{{ btn.text }}</a-button>
                </template>
            </a-space>

            <a-table :data-source="data" :loading="loading" v-bind="$attrs" :pagination="!noPage">
                <slot />
            </a-table>
        </div>

        <!-- 新增 -->
        <a-drawer
            :title="title"
            placement="right"
            :visible="addVisible"
            @close="addDrawerClose"
            v-if="addBtn"
            :maskClosable="!!addBtn.maskClosable"
            :drawerStyle="drawerStyle"
            :bodyStyle="bodyStyle"
            :width="addBtn.width || 600"
            destroyOnClose
        >
            <div class="tw-overflow-y-hidden">
                <div class="tw-overflow-y-auto tw-h-full">
                    <slot name="drawer" />
                </div>
            </div>
            <a-divider />
            <a-space>
                <a-button @click="addVisible = false">取消</a-button>
                <a-button type="primary" @click="submit" :loading="submitLoading">确认</a-button>
            </a-space>
        </a-drawer>
    </div>
</template>

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

export default {
    props: {
        url: String,
        buttons: {
            type: Array,
            default: () => [],
        },
        addBtn: {
            type: Object,
        },
        formatData: Function,
        noPage: Boolean,
    },
    data() {
        this.initQuery = {
            pageNum: 1,
            pageSize: 10,
        };
        return {
            title: '新增',
            data: [],
            queryForm: {
                ...this.initQuery,
            },
            loading: false,
            addVisible: false,
            submitLoading: false,
            drawerStyle: {
                display: 'flex',
                flexDirection: 'column',
                overflowY: 'hidden',
            },
            bodyStyle: {
                flex: 1,
                overflow: 'hidden',
                display: 'flex',
                flexDirection: 'column',
            },
        };
    },
    watch: {
        addVisible(val) {
            if (!val && this.addBtn.onCancel) this.addBtn.onCancel();
        },
    },
    mounted() {
        console.log(this.addBtn);
        this.getData();
    },

    methods: {
        async getData() {
            this.loading = true;
            try {
                this.noPage ? await this.getDataNoPage() : await this.getDataWithPage();
            } catch (error) {
                // todo
            }
            this.loading = false;
        },
        async getDataNoPage() {
            const res = await request(this.url, METHOD.GET);
            if (this.formatData) this.data = this.formatData(res);
            else this.data = res;
        },

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

        add() {
            this.addVisible = true;
        },
        addDrawerClose() {
            this.addVisible = false;
        },
        async submit() {
            this.submitLoading = true;
            try {
                await this.addBtn?.onOk();
                this.addVisible = false;
                this.getData();
            } catch (error) {
                // todo
            }
            this.submitLoading = false;
        },
        show({ type, title } = {}) {
            if (type === 0) {
                this.title = '新增';
            }
            if (type === 1) {
                this.title = '编辑';
            }
            if (type === 2) {
                this.title = '查看';
            }
            if (title) {
                this.title = title;
            }
            this.addVisible = true;
        },
    },
};
</script>

<style module>
.card {
    @apply tw-bg-white tw-rounded-md tw-p-4;
}
</style>
