<template>
    <el-drawer v-model="open" :before-close="handleClose" :title="$t('file.download')" size="50%">
        <el-row>
            <el-col :span="22" :offset="1">
                <el-form
                    ref="fileForm"
                    label-position="top"
                    :model="addForm"
                    label-width="100px"
                    :rules="rules"
                    v-loading="loading"
                >
                    <el-form-item :label="$t('file.downloadUrl')" prop="url">
                        <el-input v-model="addForm.url" @input="getFileName" />
                    </el-form-item>
                    <el-form-item :label="$t('file.path')" prop="path">
                        <el-input v-model="addForm.path" disabled>
                            <template #append><FileList :path="addForm.path" @choose="getPath"></FileList></template>
                        </el-input>
                    </el-form-item>
                    <el-form-item :label="$t('file.name')" prop="name">
                        <el-input v-model="addForm.name"></el-input>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="handleClose()" :disabled="loading">{{ $t('commons.button.cancel') }}</el-button>
                <el-button type="primary" @click="submit(fileForm)" :disabled="loading">
                    {{ $t('commons.button.confirm') }}
                </el-button>
            </span>
        </template>
    </el-drawer>
</template>

<script lang="ts" setup>
import { WgetFile } from '@/api/modules/files';
import { Rules } from '@/global/form-rules';
import i18n from '@/lang';
import { ElMessage, FormInstance, FormRules } from 'element-plus';
import { reactive, ref } from 'vue';
import FileList from '@/components/file-list/index.vue';

interface WgetProps {
    path: string;
}

const fileForm = ref<FormInstance>();
const loading = ref(false);
let open = ref(false);
let submitData = ref(false);

const rules = reactive<FormRules>({
    name: [Rules.requiredInput],
    path: [Rules.requiredInput],
    url: [Rules.requiredInput],
});

const addForm = reactive({
    url: '',
    path: '',
    name: '',
});

const em = defineEmits(['close']);

const handleClose = () => {
    if (fileForm.value) {
        fileForm.value.resetFields();
    }
    open.value = false;
    em('close', submitData);
};

const getPath = (path: string) => {
    addForm.path = path;
};

const submit = async (formEl: FormInstance | undefined) => {
    if (!formEl) return;
    await formEl.validate((valid) => {
        if (!valid) {
            return;
        }
        loading.value = true;
        WgetFile(addForm)
            .then(() => {
                ElMessage.success(i18n.global.t('file.downloadStart'));
                submitData.value = true;
                handleClose();
            })
            .finally(() => {
                loading.value = false;
            });
    });
};

const getFileName = (url: string) => {
    const paths = url.split('/');
    addForm.name = paths[paths.length - 1];
};

const acceptParams = (props: WgetProps) => {
    addForm.path = props.path;
    open.value = true;
};

defineExpose({ acceptParams });
</script>
