<template>
    <div class="basic-info-detail">
        <div class="basic-info-title">
            <img src="@/assets/enterpriseLibrary/basic-info.png" alt="其他信息">
            <span>其他信息</span>
        </div>

        <div class="detail-content">
            <el-row :gutter="24">
                <el-col :span="8">
                    <!-- 职称证 -->
                    <FileOrImageDisplay label="职称证" :file="form.fileList[0]" :image-url="form.url" />
                </el-col>
            </el-row>

            <el-row :gutter="24">
                <el-col :span="8">
                    <!-- 职称名称 -->
                    <DetailItem label="职称名称" :value="form.titleName" />
                </el-col>
                <el-col :span="8">
                    <!-- 专业名称 -->
                    <DetailItem label="专业名称" :value="form.professionalName" />
                </el-col>
                <el-col :span="8">
                    <!-- 证书编号 -->
                    <DetailItem label="证书编号" :value="form.certificateCode" />
                </el-col>
            </el-row>

            <el-row :gutter="24">
                <el-col :span="24">
                    <!-- 人员简介 -->
                    <DetailItem label="人员简介" :value="form.personnelIntroduction" />
                </el-col>
            </el-row>
        </div>
    </div>
</template>

<script>
import FileOrImageDisplay from '@/views/common/FileOrImageDisplay.vue';
import DetailItem from '@/views/common/DetailItem.vue';

// 常量定义
const CERTIFICATE_TYPE = 'professional_title_certificate';
const CERTIFICATE_KEYS = ['titleName', 'certificateCode', 'professionalName'];

export default {
    name: 'OtherInfoPersonnelDetail',
    components: {
        FileOrImageDisplay,
        DetailItem
    },
    props: {
        detailData: {
            type: Object,
            default: () => ({})
        }
    },
    data() {
        return {
            form: {
                titleName: '',
                professionalName: '',
                certificateCode: '',
                personnelIntroduction: '',
                url: null,
                fileList: []
            }
        }
    },
    watch: {
        detailData: {
            handler(newVal) {
                if (Object.keys(newVal).length > 0) {
                    this.setDetailData();
                }
            },
            deep: true,
            immediate: true
        }
    },
    methods: {
        setDetailData() {
            const fileList = this.getFileList(CERTIFICATE_TYPE);
            const certificateData = this.getCertificateData(CERTIFICATE_TYPE);

            this.form = {
                titleName: certificateData.titleName || '',
                professionalName: certificateData.professionalName || '',
                certificateCode: certificateData.certificateCode || '',
                personnelIntroduction: this.detailData.enterprisePersonnel?.personnelIntroduction || '',
                url: fileList[0]?.fileType === '1' ? fileList[0]?.lsFilePath : null,
                fileList: fileList
            }
        },

        getFileList(businessType) {
            const certificateDetail = this.findCertificateDetail(businessType);

            if (certificateDetail?.fileVoList) {
                return certificateDetail.fileVoList.map(item => ({
                    name: item.fileName,
                    filePath: item.filePath,
                    lsFilePath: item.lsFilePath,
                    fileType: item.fileType,
                    id: item.sourceId
                }));
            }

            return [];
        },

        getCertificateData(businessType) {
            const certificate = this.findCertificateDetail(businessType)?.certificate;

            if (!certificate) {
                return {};
            }

            const result = {};
            CERTIFICATE_KEYS.forEach(key => {
                result[key] = certificate[key] != null ? certificate[key] : '';
            });

            return result;
        },

        findCertificateDetail(businessType) {
            return this.detailData?.certificateDetailList?.find(
                item => item.certificate?.certificateType === businessType
            );
        }
    }
}
</script>

<style scoped lang="scss">
.basic-info-detail {
    .basic-info-title {
        display: flex;
        align-items: center;
        margin: 10px 0;

        span {
            margin: 0 5px;
            font-size: 20px;
        }
    }

    .detail-content {
        .detail-item {
            margin-bottom: 16px;

            .item-label {
                color: #424242;
                font-size: 18px;
                font-weight: 500;
                margin-bottom: 8px;
            }

            .item-value {
                color: #424242;
                font-size: 16px;
                line-height: 1.5;

                .license-image {
                    width: 350px;
                    height: 220px;
                    display: block;
                    margin: 0 auto;
                    border-radius: 8px;
                    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
                    object-fit: cover;
                    cursor: pointer;
                    transition: transform 0.3s ease;

                    &:hover {
                        transform: scale(1.02);
                    }
                }
            }
        }
    }
}
</style>