<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-col :span="8">
                    <FileOrImageDisplay label="身份证国徽面" :file="form.fileList2[0]" :image-url="form.url2" />
                </el-col>
            </el-row>

            <el-row :gutter="24">
                <el-col :span="8">
                    <DetailItem label="人员职位" :value="personnelPosition.label" />
                </el-col>
                <el-col :span="8">
                    <DetailItem label="人员姓名" :value="form.personnelName" />
                </el-col>
                <el-col :span="8">
                    <DetailItem label="身份证号码" :value="form.personnelIdCard" />
                </el-col>
            </el-row>
            
            <el-row :gutter="24">
                <el-col :span="8">
                    <DetailItem label="入职时间" :value="form.employmentDate" />
                </el-col>
                <el-col :span="8">
                    <DetailItem label="从业年限" :value="form.employmentYears" suffix="年" />
                </el-col>
            </el-row>

            <el-row :gutter="24">
                <el-col :span="8">
                    <FileOrImageDisplay label="学历证书" :file="form.fileList3[0]" :image-url="form.url3" />
                </el-col>
            </el-row>
            <el-row :gutter="24">
                <el-col :span="8">
                    <DetailItem label="毕业院校" :value="form.graduateSchool" />
                </el-col>
                <el-col :span="8">
                    <DetailItem label="毕业专业" :value="form.graduationMajor" />
                </el-col>
                <el-col :span="8">
                    <DetailItem label="学历" :value="form.qualification" />
                </el-col>
            </el-row>
            
            <el-row :gutter="24">
                <el-col :span="8">
                    <DetailItem label="毕业时间" :value="form.graduationDate" />
                </el-col>
                <el-col :span="8">
                    <DetailItem label="联系方式" :value="form.personnelPhone" />
                </el-col>
            </el-row>
            
            <el-row :gutter="24">
                <el-col :span="8">
                    <FileOrImageDisplay label="劳动合同" :file="form.fileList4[0]" :image-url="form.url4" />
                </el-col>
            </el-row>
            
        </div>
    </div>
</template>

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

// 文件类型常量
const FILE_TYPES = {
    FACE_ID_CARD: 'face_id_card_portrait',
    NATIONAL_EMBLEM: 'national_emblem_id_card',
    ACADEMIC_CERT: 'academic_certificate',
    LABOR_CONTRACT: 'labor_contract'
};

// 文件配置映射
const FILE_CONFIGS = [
    { key: 'fileList', urlKey: 'url', type: FILE_TYPES.FACE_ID_CARD },
    { key: 'fileList2', urlKey: 'url2', type: FILE_TYPES.NATIONAL_EMBLEM },
    { key: 'fileList3', urlKey: 'url3', type: FILE_TYPES.ACADEMIC_CERT },
    { key: 'fileList4', urlKey: 'url4', type: FILE_TYPES.LABOR_CONTRACT }
];

export default {
    name: 'BasicInfoPersonnelDetail',
    components: {
        FileOrImageDisplay,
        DetailItem
    },
    props: {
        detailData: {
            type: Object,
            default: () => ({})
        },
        personnelPosition: {
            type: Object,
            default: () => ({})
        }
    },
    data() {
        return {
            form: {
                personnelPosition: '',
                personnelName: '',
                employmentDate: '',
                employmentYears: '',
                graduateSchool: '',
                graduationMajor: '',
                qualification: '',
                graduationDate: '',
                personnelPhone: '',
                personnelIdCard: '',
                url: null,
                url2: null,
                url3: null,
                url4: null,
                fileList: [],
                fileList2: [],
                fileList3: [],
                fileList4: [],
            }
        }
    },
    watch: {
        detailData: {
            handler(newVal) {
                if (Object.keys(newVal).length > 0) {
                    this.setDetailData();
                }
            },
            deep: true,
            immediate: true
        }
    },
    methods: {
        setDetailData() {
            const { enterprisePersonnel = {}, resourceFileVoList = [] } = this.detailData;

            // 处理文件数据
            const fileData = this.processFileData(resourceFileVoList);

            this.form = {
                personnelPosition: enterprisePersonnel.personnelPosition || '',
                personnelName: enterprisePersonnel.personnelName || '',
                personnelIdCard: enterprisePersonnel.personnelIdCard || '',
                employmentDate: enterprisePersonnel.employmentDate || null,
                employmentYears: enterprisePersonnel.employmentYears || '',
                graduateSchool: enterprisePersonnel.graduateSchool || '',
                graduationMajor: enterprisePersonnel.graduationMajor || '',
                qualification: enterprisePersonnel.qualification || '',
                graduationDate: enterprisePersonnel.graduationDate || null,
                personnelPhone: enterprisePersonnel.personnelPhone || '',
                ...fileData
            }
        },

        processFileData(fileList) {
            const result = {};

            FILE_CONFIGS.forEach(config => {
                const files = this.getFileList(fileList, config.type);
                result[config.key] = files;
                result[config.urlKey] = files[0]?.fileType === '1' ? files[0]?.lsFilePath : null;
            });

            return result;
        },

        getFileList(fileList, businessType) {
            return fileList
                .filter(item => item.businessType === businessType)
                .map(item => ({
                    name: item.fileName,
                    filePath: item.filePath,
                    lsFilePath: item.lsFilePath,
                    fileType: item.fileType,
                    id: item.sourceId
                }));
        }
    }
}
</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;
        }
    }
}
</style>