<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">
                <slot />
            </a-table>
        </div>

        <!-- 新增 -->
        <a-drawer
            :title="addBtn.title || '新建'"
            placement="right"
            :visible="addVisible"
            @close="addDrawerClose"
            v-if="addBtn"
            :maskClosable="addBtn.maskClosable"
        >
            <slot name="add" />
        </a-drawer>
    </div>
</template>

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

const data = [
    {
        key: '1',
        name: 'John Brown',
        age: 32,
        address: 'New York No. 1 Lake Park',
        tags: ['nice', 'developer'],
    },
    {
        key: '2',
        name: 'Jim Green',
        age: 42,
        address: 'London No. 1 Lake Park',
        tags: ['loser'],
    },
    {
        key: '3',
        name: 'Joe Black',
        age: 32,
        address: 'Sidney No. 1 Lake Park',
        tags: ['cool', 'teacher'],
    },
];

export default {
    props: {
        url: String,
        buttons: {
            type: Array,
            default: () => [],
        },
        addBtn: {
            type: Object,
        },
    },
    data() {
        return {
            data: [],
            queryForm: {},
            loading: false,
            addVisible: false,
        };
    },
    mounted() {
        this.getData();
    },

    methods: {
        async getData() {
            console.log('查询');
            this.loading = true;
            setTimeout(() => {
                this.data = data;
                this.loading = false;
            }, 1000);
            const res = await request(this.url, METHOD.GET);
            console.log(res);
        },
        add() {
            this.addVisible = true;
        },
        addDrawerClose() {
            this.addVisible = false;
        },
    },
};
</script>

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