<template>
    <el-dialog v-model="data.dialogVisible" :title="data.title" :before-close="handleClose" style="width: 650px; padding-bottom: 20px">
        <el-form ref="formDataRef" :model="ruleForm" :rules="rules" label-width="auto">
            <el-form-item label="司机姓名" prop="username">
                <el-input v-model="ruleForm.username" placeholder="请输入司机姓名" clearable></el-input>
            </el-form-item>
            <el-form-item label="司机手机号" prop="mobile">
                <el-input v-model="ruleForm.mobile" placeholder="请输入司机手机号" clearable></el-input>
            </el-form-item>
            <el-form-item label="驾驶证照片" prop="driver_license">
                <el-upload
                    :limit="2"
                    list-type="picture-card"
                    action="/api/common/upload"
                    :on-success="(response, file, fileList) => handleAvatarSuccess(response, file, fileList, 'driver_license')"
                    :on-preview="handlePreview"
                    :on-remove="(file, fileList) => handleRemove(file, fileList, 'driver_license')"
                    :before-upload="beforeAvatarUpload"
                    :file-list="ruleForm.driver_license"
                    :headers="importHeaders"
                    :on-exceed="() => handleExceed(2)"
                    @drop.native="(e) => handleDrop(e, 'driver_license', 2)"
                    @dragover.native.prevent
                    @dragenter.native="(e) => handleDragEnter(e)"
                    @dragleave.native="(e) => handleDragLeave(e)"
                    class="upload-drag-area"
                >
                    <el-icon><Plus /></el-icon>
                </el-upload>
            </el-form-item>
            <el-form-item label="身份证照片" prop="id_card">
                <el-upload
                    :limit="2"
                    list-type="picture-card"
                    action="/api/common/upload"
                    :on-success="(response, file, fileList) => handleAvatarSuccess(response, file, fileList, 'id_card')"
                    :on-preview="handlePreview"
                    :on-remove="(file, fileList) => handleRemove(file, fileList, 'id_card')"
                    :before-upload="beforeAvatarUpload"
                    :file-list="ruleForm.id_card"
                    :headers="importHeaders"
                    :on-exceed="() => handleExceed(2)"
                    @drop.native="(e) => handleDrop(e, 'id_card', 2)"
                    @dragover.native.prevent
                    @dragenter.native="(e) => handleDragEnter(e)"
                    @dragleave.native="(e) => handleDragLeave(e)"
                    class="upload-drag-area"
                >
                    <el-icon><Plus /></el-icon>
                </el-upload>
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button :loading="data.saveLoading" type="primary" @click="onClickSave">保存</el-button>
                <el-button @click="handleClose">取消</el-button>
            </span>
        </template>
        <el-dialog v-model="data.dialogVisibleImg">
            <img w-full :src="data.dialogImageUrl" alt="Preview Image" />
        </el-dialog>
    </el-dialog>
</template>

<script setup>
import { ref, reactive, onMounted, nextTick } from 'vue';
import { ElMessage } from 'element-plus';
import { Plus } from '@element-plus/icons-vue';
import { driverAdd, driverEdit } from '@/api/userManage.js';
import { useUserStore } from '../../store/user';

const userStore = useUserStore();
const importHeaders = reactive({ Authorization: userStore.$state.token });
const emits = defineEmits();

const formDataRef = ref();
const data = reactive({
    dialogVisible: false,
    title: '',
    saveLoading: false,
    dialogVisibleImg: false,
    dialogImageUrl: '',
    isEdit: false,
});

const ruleForm = reactive({
    id: null,
    username: '',
    mobile: '',
    driver_license: [], // 驾驶证照片
    id_card: [], // 身份证照片
});

const rules = reactive({
    username: [{ required: true, message: '请输入司机姓名', trigger: 'blur' }],
    mobile: [
        {
            required: true,
            validator(rule, value, callback) {
                if (!value) {
                    callback(new Error('请输入司机手机号'));
                } else if (!/^1[3-9]\d{9}$/.test(value)) {
                    callback(new Error('请输入正确的手机号'));
                } else {
                    callback();
                }
            },
            trigger: 'blur',
        },
    ],
    driver_license: [{ required: true, message: '请上传驾驶证照片', trigger: 'change' }],
    id_card: [{ required: true, message: '请上传身份证照片', trigger: 'change' }],
});

const handleAvatarSuccess = (res, file, fileList, type) => {

    if (ruleForm.hasOwnProperty(type)) {
        let imageUrl = null;
        
        if (res && res.data && res.data.src) {
            imageUrl = res.data.src;
        } else if (res && res.data && typeof res.data === 'string') {
            imageUrl = res.data;
        } else if (res && res.url) {
            imageUrl = res.url;
        } else if (res && typeof res === 'string') {
            imageUrl = res;
        }
        
        if (imageUrl) {
            // 直接更新 fileList
            file.url = imageUrl;
            ruleForm[type] = fileList;

        } else {
            console.error('无法解析图片URL:', res);
            ElMessage.error('上传失败：无法获取图片URL');
        }
    }
};

const handleRemove = (file, fileList, type) => {
    if (ruleForm.hasOwnProperty(type)) {
        ruleForm[type] = fileList;
    }
};

const beforeAvatarUpload = (file) => {
    const isImage = file.type.startsWith('image/');
    const isLt10M = file.size / 1024 / 1024 < 10;

    if (!isImage) {
        ElMessage.error('上传文件只能是图片格式!');
    }
    if (!isLt10M) {
        ElMessage.error('上传图片大小不能超过 10MB!');
    }
    return isImage && isLt10M;
};

const handleExceed = (number) => {
    ElMessage.warning(`最多只能上传${number}张图片！`);
};

// 处理拖拽进入
const handleDragEnter = (e) => {
    e.preventDefault();
    e.stopPropagation();
    const target = e.currentTarget;
    if (target) {
        target.classList.add('drag-over');
    }
};

// 处理拖拽离开
const handleDragLeave = (e) => {
    e.preventDefault();
    e.stopPropagation();
    const target = e.currentTarget;
    if (target) {
        target.classList.remove('drag-over');
    }
};

// 处理拖拽上传
const handleDrop = (e, type, limit = 2) => {
    e.preventDefault();
    e.stopPropagation();
    // 移除拖拽样式
    const target = e.currentTarget;
    if (target) {
        target.classList.remove('drag-over');
    }
    const files = Array.from(e.dataTransfer.files);
    
    // 过滤出图片文件
    const imageFiles = files.filter(file => file.type.startsWith('image/'));
    
    if (imageFiles.length === 0) {
        ElMessage.error('请拖拽图片文件！');
        return;
    }
    
    // 检查限制：只处理不超过限制的文件
    const currentCount = ruleForm[type].length;
    const remainingSlots = limit - currentCount;
    
    if (remainingSlots <= 0) {
        ElMessage.warning(`最多只能上传${limit}张图片！`);
        return;
    }
    
    // 只处理前 remainingSlots 个文件
    const filesToUpload = imageFiles.slice(0, remainingSlots);
    
    // 处理每个文件
    filesToUpload.forEach((file, index) => {
        // 验证文件
        if (!beforeAvatarUpload(file)) {
            return;
        }
        
        // 创建 FormData 并上传
        const formData = new FormData();
        formData.append('file', file);
        
        // 使用 fetch 上传文件
        fetch('/api/common/upload', {
            method: 'POST',
            headers: {
                'Authorization': importHeaders.Authorization
            },
            body: formData
        })
        .then(response => response.json())
        .then(res => {
            // 创建文件对象
            const fileObj = {
                uid: Date.now() + Math.random() + index,
                name: file.name,
                status: 'success',
                url: ''
            };
            
            // 获取当前文件列表并添加新文件
            const currentFileList = [...ruleForm[type]];
            currentFileList.push(fileObj);
            
            // 调用成功处理函数
            handleAvatarSuccess(res, fileObj, currentFileList, type);
        })
        .catch(error => {
            console.error('上传失败:', error);
            ElMessage.error('上传失败，请稍后重试');
        });
    });
    
    // 如果拖拽的文件超过限制，提示用户
    if (imageFiles.length > remainingSlots) {
        ElMessage.warning(`只能上传${remainingSlots}张图片，已忽略多余的${imageFiles.length - remainingSlots}张`);
    }
};

const handlePreview = (file) => {
    data.dialogImageUrl = file.url;
    data.dialogVisibleImg = true;
};

const handleClose = () => {
    formDataRef.value?.resetFields();
    ruleForm.id = null;
    ruleForm.username = '';
    ruleForm.mobile = '';
    ruleForm.driver_license = [];
    ruleForm.id_card = [];
    data.dialogVisible = false;
    data.isEdit = false;
};

const onClickSave = () => {
    formDataRef.value.validate((valid) => {
        if (valid) {
            data.saveLoading = true;
            
            const params = {
                id: ruleForm.id,
                username: ruleForm.username,
                mobile: ruleForm.mobile,
                driverLicense: ruleForm.driver_license.length > 0 ? ruleForm.driver_license.map((item) => item.url).join(',') : '',
                idCard: ruleForm.id_card.length > 0 ? ruleForm.id_card.map((item) => item.url).join(',') : '',
            };
            

            const apiCall = data.isEdit ? driverEdit(params) : driverAdd(params);
            
            apiCall.then((res) => {
                data.saveLoading = false;
                if (res.code === 200) {
                    ElMessage.success(data.isEdit ? '编辑成功' : '新增成功');
                    handleClose();
                    emits('success');
                } else {
                    ElMessage.error(res.msg || '操作失败');
                }
            }).catch((error) => {
                data.saveLoading = false;
                console.error('保存失败:', error);
                ElMessage.error('保存失败，请稍后重试');
            });
        }
    });
};

const onShowDialog = (row = null) => {
    data.dialogVisible = true;
    data.isEdit = !!row;
    data.title = row ? '编辑司机' : '新增司机';
    
    if (row) {
        ruleForm.id = row.id;
        ruleForm.username = row.username || '';
        ruleForm.mobile = row.mobile || '';
        ruleForm.driver_license = row.driver_license ? row.driver_license.split(',').map(url => ({ url: url.trim() })) : [];
        ruleForm.id_card = row.id_card ? row.id_card.split(',').map(url => ({ url: url.trim() })) : [];
    }
};

defineExpose({ onShowDialog });
</script>

<style scoped>
.dialog-footer {
    display: flex;
    justify-content: flex-end;
}

/* 拖拽上传样式 */
:deep(.upload-drag-area) {
    position: relative;
}

:deep(.upload-drag-area .el-upload--picture-card) {
    border: 2px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: all 0.3s;
}

:deep(.upload-drag-area .el-upload--picture-card:hover) {
    border-color: #409eff;
    background-color: #f5f7fa;
}

:deep(.upload-drag-area .el-upload-list--picture-card) {
    display: inline-block;
    vertical-align: top;
}

/* 拖拽时的视觉反馈 */
:deep(.upload-drag-area.drag-over .el-upload--picture-card) {
    border-color: #409eff;
    background-color: #ecf5ff;
    border-style: solid;
}

:deep(.upload-drag-area.drag-over .el-upload--picture-card .el-icon) {
    color: #409eff;
}
</style>
