<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 = {}">重置</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="addBtn.title || '新建'"
            placement="right"
            :visible="addVisible"
            @close="addDrawerClose"
            v-if="addBtn"
            :maskClosable="!!addBtn.maskClosable"
            :closable="false"
            :drawerStyle="drawerStyle"
            :bodyStyle="bodyStyle"
            :width="addBtn.width"
            destroyOnClose
        >
            <div class="tw-overflow-y-hidden">
                <div class="tw-overflow-y-auto tw-h-full">
                    <slot name="add" />
                </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() {
        return {
            data: [],
            queryForm: {},
            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 {
                const res = await request(this.url, METHOD.GET);
                if (this.formatData) this.data = this.formatData(res);
                else this.data = res;
            } catch (error) {
                // todo
            }
            this.loading = false;
        },
        add() {
            this.addVisible = true;
        },
        addDrawerClose() {
            this.addVisible = false;
        },
        async submit() {
            this.submitLoading = true;
            try {
                await this.addBtn?.onOk();
            } catch (error) {
                // todo
            }
            this.submitLoading = false;
            this.addVisible = false;
        },
        showAdd() {
            this.addVisible = true;
        },
        refresh() {
            this.getData();
        },
    },
};
</script>

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