<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="content-area">
                        <el-form
                            ref="addEditFormRef"
                            :model="addEditForm"
                            :rules="addEditFormRules"
                            label-position="left"
                            label-suffix="："
                            size="large"
                        >
                            <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">
                                                    {{ detailForm.gyqyName || '-' }}
                                                </el-form-item>
                                            </el-descriptions-item>
                                            <el-descriptions-item>
                                                <template #label>
                                                    <span class="required-asterisk">*</span>
                                                    签署时间
                                                </template>
                                                <el-form-item prop="tctcrq">
                                                    <el-date-picker
                                                        v-model="addEditForm.tctcrq"
                                                        placeholder="请选择"
                                                        style="width: 100%"
                                                        type="date"
                                                        value-format="YYYY-MM-DD"
                                                    />
                                                </el-form-item>
                                            </el-descriptions-item>
                                            <el-descriptions-item :span="2">
                                                <template #label>
                                                    <span class="required-asterisk">*</span>
                                                    详细原因
                                                </template>
                                                <el-form-item prop="detailedReason">
                                                    <el-input
                                                        v-model="addEditForm.detailedReason"
                                                        :autosize="{ minRows: 4, maxRows: 6 }"
                                                        clearable
                                                        placeholder="请输入"
                                                        type="textarea"
                                                    />
                                                </el-form-item>
                                            </el-descriptions-item>
                                            <el-descriptions-item :span="2">
                                                <template #label> 附件</template>
                                                <el-form-item prop="b">
                                                    <CustomUploader
                                                        :fileList="addEditForm.files"
                                                        :previewAndDownload="false"
                                                        :typeRegularArr="typeRegularArr"
                                                        type="button"
                                                        @change="handleFileChange"
                                                        @startUpload="handleStartUpload"
                                                    >
                                                    </CustomUploader>
                                                </el-form-item>
                                            </el-descriptions-item>
                                        </el-descriptions>
                                        <div
                                            style="
                                                display: flex;
                                                align-items: center;
                                                justify-content: space-between;
                                            "
                                        >
                                            <div
                                                style="
                                                    box-sizing: border-box;
                                                    font-size: 12px;
                                                    padding: 10px;
                                                    text-align: left;
                                                    font-weight: bold;
                                                "
                                            >
                                                近三年度销售额(万元)
                                            </div>
                                            <div class="base-more">
                                                <span>查看更多</span>
                                                <el-icon><DArrowRight /></el-icon>
                                            </div>
                                        </div>
                                        <el-table :data="orderSales" border stripe>
                                            <el-table-column
                                                type="index"
                                                label="序号"
                                                width="80px"
                                                align="center"
                                            />
                                            <el-table-column
                                                prop="year"
                                                label="年份"
                                                align="center"
                                            />
                                            <el-table-column
                                                prop="annualTransAmount"
                                                label="销售额(万元)"
                                                align="center"
                                            />
                                        </el-table>
                                    </div>
                                </el-collapse-item>
                                <el-collapse-item name="access-evaluate">
                                    <template #title>
                                        <div class="section-title">
                                            <el-icon><Document /></el-icon>
                                            <span>历史评分项</span>
                                        </div>
                                    </template>
                                    <div class="section-content">
                                        <el-table :data="hisToryList" border stripe row-key="id">
                                            <el-table-column
                                                type="index"
                                                label="序号"
                                                width="80"
                                                align="center"
                                            />
                                            <el-table-column
                                                prop="assessmentYear"
                                                label="评价年度"
                                                align="center"
                                            />
                                            <el-table-column
                                                prop="assessmentType"
                                                label="周期类型"
                                                align="center"
                                            >
                                                <template v-slot="{ row }">
                                                    <div>
                                                        {{
                                                            dictionaryEscape(
                                                                'khpj-khlx',
                                                                row.assessmentType
                                                            )
                                                        }}
                                                    </div>
                                                </template>
                                            </el-table-column>
                                            <el-table-column
                                                prop="assessmentQuater"
                                                label="评价时间"
                                                align="center"
                                            >
                                                <template v-slot="{ row }">
                                                    <div v-if="row.assessmentQuater">
                                                        <span>{{ row.assessmentYear }}年</span>
                                                        <span>{{ row.assessmentQuater }}季度</span>
                                                    </div>
                                                    <div v-else-if="row.assessmentMonth">
                                                        <span>{{ row.assessmentYear }}年</span>
                                                        <span>{{ row.assessmentMonth }}月</span>
                                                    </div>
                                                    <div v-else>{{ row.assessmentYear }}年</div>
                                                </template>
                                            </el-table-column>
                                            <el-table-column
                                                prop="operationScore"
                                                label="评价得分"
                                                align="center"
                                            />
                                            <el-table-column
                                                prop="level"
                                                label="评价等级"
                                                align="center"
                                            >
                                                <template v-slot="{ row }">
                                                    <div>
                                                        {{ dictionaryEscape('fzdj', row.level) }}
                                                    </div>
                                                </template>
                                            </el-table-column>
                                        </el-table>
                                    </div>
                                </el-collapse-item>
                            </el-collapse>
                        </el-form>
                    </div>
                </div>
            </div>
        </div>

        <!-- 底部按钮 -->
        <div class="footer">
            <el-button type="primary" :loading="submitLoading" @click="handleSubmit"
                >确定</el-button
            >
            <el-button @click="handleCancel">取消</el-button>
        </div>
    </div>
</template>

<script lang="ts" setup>
import moment from 'moment'
import { Document } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox, type FormRules } from 'element-plus'
import { dataDetail } from '@/api/supplierMonitor/SupplyManagement/SupplierQualified.ts'
import { dictionaryEscape, getDictionaryByCode } from '@/utils/dictionary'

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
}

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

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

// 表单数据
const orderSales = ref<any[]>([])
const hisToryList = ref<any[]>([])
const detailForm = reactive<any>({})
const addEditForm = reactive<any>({
    tctcrq: moment().format('YYYY-MM-DD'),
    detailedReason: '',
    files: []
})

// 表单验证规则
const addEditFormRules = reactive<FormRules>({
    tctcrq: [{ required: true, message: '请选择签署时间', trigger: 'change' }],
    detailedReason: [{ required: true, message: '请输入详细原因', trigger: 'blur' }]
})

const getDetail = async (id: string) => {
    // 获取详情逻辑
    const { ...rest } = (await dataDetail(id)) as any
    Object.assign(detailForm, {
        ...rest
    })
}

//附件上传
const btnLoading = ref<Boolean>(false)
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[]) => {
    addEditForm.files = files.map((item) => item as FileItem)
    btnLoading.value = !files.every((item: FileItem) => item.status === 'success')
}

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

// 提交
const submitLoading = ref(false)
const handleSubmit = () => {
    addEditFormRef.value?.validate(async (valid: boolean, object: any) => {
        if (valid) {
            try {
                submitLoading.value = true
                let params = JSON.parse(JSON.stringify({ ...addEditForm }))
                // const { state } = await signAgreement(params)
                // if (state) {
                //     ElMessage.success('申请成功')
                //     handleCancel()
                // }
            } finally {
                submitLoading.value = false
            }
        } else {
            ElMessage.warning(
                `有${Object.keys(object || {}).length}个字段未通过校验，请正确填写表单内容。`
            )
        }
    })
}

onMounted(() => {
    // 如果是编辑模式，获取详情数据
    if (route.query.id && typeof route.query.id === 'string') {
        getDetail(route.query.id)
    }
})
</script>

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

    .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;
    }
}

.base-more {
    display: flex;
    align-items: center;
    margin-right: 10px;
    cursor: pointer;
    color: #1a8cff;
}
</style>
