<template>
    <a-modal v-model="visible" title="导入" @ok="handleOk" destroyOnClose>
        <a-form-model layout="vertical" :model="form" :rules="rules" ref="form">
            <a-form-model-item label="组织" prop="orgId">
                <UrlSelect
                    v-model="form.orgId"
                    url="/api/v1/users/organizations"
                    labelFiled="orgName"
                    valueFiled="orgId"
                />
            </a-form-model-item>
            <a-form-model-item label="上传文件" prop="file">
                <Upload v-model="form.file" showUploadList :number="1" />
                <template #extra>
                    <a-button type="link" @click="getTemplate"> 下载模板 </a-button>
                </template>
            </a-form-model-item>
        </a-form-model>
    </a-modal>
</template>
<script>
import { getTemplateApi, uploadFileRouteApi } from '@/api';
import UrlSelect from '@/components/MySelect/url_select.vue';
import { downloadFile } from '@/utils';
import Upload from './uploadList.vue';
export default {
    components: { UrlSelect, Upload },
    data() {
        return {
            visible: false,
            form: {},
            rules: {
                orgId: [{ required: true }],
                file: [{ required: true }],
            },
        };
    },
    props: {
        afterSubmit: Function,
    },
    methods: {
        showModal() {
            this.form = {};
            console.log(this.form);
            this.visible = true;
        },
        async handleOk(e) {
            await this.$refs.form.validate();
            const { file, ...rest } = this.form;
            await uploadFileRouteApi({ ...rest }, file[0].oFile);
            this.afterSubmit && this.afterSubmit();
            this.$message.info('操作成功!');
            this.closeModal();
        },
        closeModal() {
            this.visible = false;
        },
        async getTemplate() {
            const resp = await getTemplateApi();
            downloadFile(resp, 'xlsx');
        },
    },
};
</script>
