<template>
    <div class="edit-add-detail">
        <div v-loading="detailLoading" class="form-content" element-loading-text="加载中...">
            <div class="page-form">
                <div class="main-content">
                    <!-- 步骤条 -->
                    <div class="step-container">
                        <el-steps
                            :active="currentStep"
                            align-center
                            finish-status="success"
                            process-status="finish"
                        >
                            <el-step title="1 等级变更申请"></el-step>
                            <el-step title="2 变更审核"></el-step>
                            <el-step title="3 完成"></el-step>
                        </el-steps>
                    </div>
                    <div class="content-area">
                        <el-form
                            ref="addEditFormRef"
                            :model="addEditForm"
                            :rules="addEditFormRules"
                            size="large"
                            label-position="left"
                            label-suffix="："
                        >
                            <el-collapse v-model="activeCollapse">
                                <el-collapse-item name="basic-info">
                                    <template #title>
                                        <div class="section-title">
                                            <el-icon><Document /></el-icon>
                                            <span>供应商分级分类</span>
                                        </div>
                                    </template>
                                    <div class="section-content">
                                        <el-descriptions :column="2" border>
                                            <el-descriptions-item>
                                                <template #label>
                                                    <span class="required-asterisk">*</span>
                                                    供应商分级
                                                </template>
                                                <el-form-item prop="gyqyName"> - </el-form-item>
                                            </el-descriptions-item>
                                            <el-descriptions-item>
                                                <template #label>
                                                    <span class="required-asterisk">*</span>
                                                    供应商分类
                                                </template>
                                                <el-form-item prop="gysdz"> - </el-form-item>
                                            </el-descriptions-item>
                                            <el-descriptions-item :span="2">
                                                <template #label>
                                                    <span class="required-asterisk">*</span>
                                                    变更原因
                                                </template>
                                                <el-form-item prop="gyslxdh"> - </el-form-item>
                                            </el-descriptions-item>
                                            <el-descriptions-item :span="2">
                                                <template #label> 附件 </template>
                                                <el-form-item prop="b">
                                                    <CustomUploader
                                                        type="button"
                                                        :fileList="fileList"
                                                        :disabled="true"
                                                        :previewAndDownload="false"
                                                        :typeRegularArr="typeRegularArr"
                                                        @change="handleFileChange"
                                                        @startUpload="handleStartUpload"
                                                    >
                                                    </CustomUploader>
                                                </el-form-item>
                                            </el-descriptions-item>
                                        </el-descriptions>
                                    </div>
                                </el-collapse-item>
                            </el-collapse>
                        </el-form>
                    </div>
                </div>
            </div>
        </div>

        <div class="footer">
            <el-button type="primary" @click="handleSubmit">同 意</el-button>
            <el-button type="danger" @click="handleReject">驳 回</el-button>
            <el-button @click="handleCancel"> 取 消</el-button>
        </div>

        <!-- 驳回弹窗 -->
        <RejectDialog
            ref="rejectDialogRef"
            v-model:isShowAdd="isShowRejectDialog"
            title="驳回理由"
            @rejectSuccess="handleRejectSuccess"
        ></RejectDialog>
    </div>
</template>

<script setup lang="ts">
import { Document, List, Plus, Upload, Delete, Download } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { type FormRules } from 'element-plus'
import CustomUploader from '@/components/CustomUploader/index.vue'
import RejectDialog from '@/components/RejectDialog/index.vue'

interface AddEditForm {
    [key: string]: string | number | undefined | null | any
}

interface FileItem {
    id?: number | string
    fileName: string
    ignored: boolean
    status?: string
    paused?: boolean
    _prevProgress?: number
    timeRemaining?: () => number
    currentSpeed?: number
    extensionName?: string
    fileId?: string
    [key: string]: any
}

interface ScoreItem {
    [key: string]: any
}

const route = useRoute()
const router = useRouter()

// 表单相关
const currentStep = ref(1)
const addEditFormRef = ref()
const detailLoading = ref(false)
const activeCollapse = ref(['basic-info'])

// 编辑相关
const isEdit = computed(() => route.name === 'edit')
const editId = computed(() => route.params.id as string)

// 表单数据
const detailForm = reactive<AddEditForm>({})
const addEditForm = reactive<AddEditForm>({
    gysfzj: '',
    gyssfzj: '',
    gyslxdh: '',
    fileList: []
})

// 表单验证规则
const addEditFormRules = reactive<FormRules>({
    gysfzj: [{ required: true, message: '请选择供应商分级', trigger: 'change' }],
    gyssfzj: [{ required: true, message: '请选择供应商分类', trigger: 'change' }],
    gyslxdh: [{ required: true, message: '请输入变更原因', trigger: 'blur' }]
})

//附件上传
const btnLoading = ref<Boolean>(false)
const readyToUpload = ref<Boolean>(false)
const fileList = ref<FileItem[]>([])
const typeRegularArr = [
    'mp4',
    'mov',
    'avi',
    'mkv',
    'wmv',
    'flv',
    'mpeg',
    'mpg',
    '3gp',
    'webm', // 视频格式
    'doc',
    'docx',
    'xls',
    'xlsx',
    'ppt',
    'pptx',
    'txt',
    'rtf',
    'pdf', // 文档格式
    'jpg',
    'png',
    'bmp',
    'gif' // 图像文件
]
const handleStartUpload = () => {
    btnLoading.value = true
}

const handleFileChange = (files: FileItem[]) => {
    fileList.value = files.map((item) => item as FileItem)
    btnLoading.value = !files.every((item: FileItem) => item.status === 'success')
}

// 取消
const handleCancel = () => {
    router.push({ name: 'SupplierLevelChange' })
}

// 提交
const handleSubmit = () => {
    addEditFormRef.value?.validate(async (valid: boolean, object: any) => {
        if (valid) {
            handleCancel()
        } else {
            ElMessage.warning(
                `有${Object.keys(object || {}).length}个字段未通过校验，请正确填写表单内容。`
            )
        }
    })
}

const isShowRejectDialog = ref<Boolean>(false)
const rejectDialogRef = ref<InstanceType<typeof RejectDialog> | null>(null)

const handleReject = () => {
    isShowRejectDialog.value = true
}

const handleRejectSuccess = (data: any) => {
    isShowRejectDialog.value = false
    console.log(isShowRejectDialog.value)
}

onMounted(() => {
    readyToUpload.value = true
    // 如果是编辑模式，获取详情数据
    if (isEdit.value) {
        // getDetail(editId.value)
    }
})
</script>

<style lang="scss" scoped>
.form-content {
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start !important;

    .page-form {
        overflow-y: hidden;
        overflow-y: auto;
        padding: 0 20px;

        /* 隐藏滚动条样式 */
        &::-webkit-scrollbar {
            width: 0;
            height: 0;
        }

        &::-webkit-scrollbar-track {
            background: transparent;
        }

        &::-webkit-scrollbar-thumb {
            background: transparent;
        }

        /* Firefox 隐藏滚动条 */
        scrollbar-width: none;
        -ms-overflow-style: none;
    }
}
.step-container {
    width: 100%;
    margin-bottom: 20px;
}
.table-actions {
    margin: 16px 0 16px 16px;
}

.content-area {
    /* 确保容器的高度计算正确 */
    box-sizing: border-box;

    :deep(.el-collapse) {
        /* 通过增加 1px 来补偿 border 导致的 1px 高度差 */
        margin-bottom: 1px;
    }
}
</style>
