<template>
    <div>
        <div class="content">
            <!-- 基本信息区域 -->
            <div class="basic-info-area">
                <div class="info-title">基本信息</div>
                <div class="upload-section">
                    <!-- LOGO上传 -->
                    <div class="upload-item">
                        <el-upload
                            ref="logoUpload"
                            class="logo-uploader"
                            action="#"
                            :show-file-list="false"
                            :limit="1"
                            :before-upload="beforeLogoUpload"
                            :on-exceed="handleLogoExceed"
                            :http-request="handleLogoUpload">
                            <div class="upload-icon logo-icon" v-if="!logoUrl">
                                <span class="logo-text">W</span>
                            </div>
                            <img v-else :src="logoUrl" class="uploaded-image" />
                        </el-upload>
                        <div class="upload-info">
                            <div class="upload-label">LOGO</div>
                            <el-button type="text" class="re-upload-btn" @click="triggerLogoUpload">重新上传</el-button>
                        </div>
                    </div>
                    
                    <!-- 营业执照上传 -->
                    <div class="upload-item">
                        <el-upload
                            ref="licenseUpload"
                            class="license-uploader"
                            action="#"
                            :show-file-list="false"
                            :limit="1"
                            :before-upload="beforeLicenseUpload"
                            :on-exceed="handleLicenseExceed"
                            :http-request="handleLicenseUpload">
                            <div class="upload-icon license-icon" v-if="!licenseUrl">
                                <div class="license-grid"></div>
                            </div>
                            <img v-else :src="licenseUrl" class="uploaded-image" />
                        </el-upload>
                        <div class="upload-info">
                            <div class="upload-label">营业执照</div>
                            <el-button type="text" class="re-upload-btn" @click="triggerLicenseUpload">重新上传</el-button>
                        </div>
                    </div>
                </div>
            </div>
            <div class="heng"></div>
            <!-- 注册信息区域 -->
            <div class="registration-info-area">
               
            
                       <div class="content-areas" style="margin-top: -0px;">
                         <div class="contents-left">
                           <span style="margin-left: 20px;">企业名称</span>
                         </div>
                         <div class="contents-right">
                            <span style="margin-left: 20px;">
                                <el-input v-model="registrationData[0].value" placeholder="请输入内容"></el-input>
                            </span>
                         </div>
                         <div class="contents-left">
                           <span style="margin-left: 20px;">企业简称</span>
                         </div>
                      
                         <div class="contents-right">
                            <span style="margin-left: 20px;">
                                <el-input v-model="registrationData[1].value" placeholder="请输入内容"></el-input>
                            </span>
                         </div>
                       </div>
                       <div class="content-areas" style="margin-top: -0px;">
                         <div class="contents-left">
                           <span style="margin-left: 20px;">统一社会信用代码</span>
                         </div>
                         <div class="contents-right">
                            <span style="margin-left: 20px;">
                                <el-input v-model="registrationData[2].value" placeholder="请输入内容"></el-input>
                            </span>
                         </div>
                         <div class="contents-left">
                           <span style="margin-left: 20px;">企业类型</span>
                         </div>
                         <div class="contents-right">
                            <span style="margin-left: 20px;">
                                <el-input v-model="registrationData[3].value" placeholder="请输入内容"></el-input>
                            </span>
                         </div>
                       
                       </div>
                       <div class="content-areas" style="margin-top: -0px;">
                         <div class="contents-left">
                           <span style="margin-left: 20px;">注册资本</span>
                         </div>
                         <div class="contents-right">
                            <span style="margin-left: 20px;">
                                <el-input v-model="registrationData[4].value" placeholder="请输入内容"></el-input>
                            </span>
                         </div>
                         <div class="contents-left">
                           <span style="margin-left: 20px;">实缴资本</span>
                         </div>
                         <div class="contents-right">
                            <span style="margin-left: 20px;">
                                <el-input v-model="registrationData[5].value" placeholder="请输入内容"></el-input>
                            </span>
                         </div>
                       
                       </div>
                       <div class="content-areas" style="margin-top: -0px;">
                         <div class="contents-left">
                           <span style="margin-left: 20px;">成立日期</span>
                         </div>
                         <div class="contents-right">
                            <span style="margin-left: 20px;">
                                <el-input v-model="registrationData[6].value" placeholder="请输入内容"></el-input>
                            </span>
                         </div>
                         <div class="contents-left">
                           <span style="margin-left: 20px;">核准日期</span>
                         </div>
                         <div class="contents-right">
                            <span style="margin-left: 20px;">
                                <el-input v-model="registrationData[7].value" placeholder="请输入内容"></el-input>
                            </span>
                         </div>
                       
                       </div>
                       <div class="content-areas" style="margin-top: -0px;">
                         <div class="contents-left">
                           <span style="margin-left: 20px;">营业期限</span>
                         </div>
                         <div class="contents-right">
                            <span style="margin-left: 20px;">
                                <el-input v-model="registrationData[8].value" placeholder="请输入内容"></el-input>
                            </span>
                         </div>
                         <div class="contents-left">
                           <span style="margin-left: 20px;">所属区域</span>
                         </div>
                         <div class="contents-right">
                            <span style="margin-left: 20px;">
                                <el-input v-model="registrationData[9].value" placeholder="请输入内容"></el-input>
                            </span>
                         </div>
                       
                       </div>
                       <div class="content-areas" style="margin-top: -0px;">
                         <div class="contents-left">
                           <span style="margin-left: 20px;">登记机关</span>
                         </div>
                         <div class="contents-right">
                            <span style="margin-left: 20px;">
                                <el-input v-model="registrationData[10].value" placeholder="请输入内容"></el-input>
                            </span>
                         </div>
                         <div class="contents-left">
                           <span style="margin-left: 20px;">工商注册号</span>
                         </div>
                         <div class="contents-right">
                            <span style="margin-left: 20px;">
                                <el-input v-model="registrationData[11].value" placeholder="请输入内容"></el-input>
                            </span>
                         </div>
                       
                       </div>
                       <div class="content-areas" style="margin-top: -0px;">
                         <div class="contents-left">
                           <span style="margin-left: 20px;">所得税征收方式</span>
                         </div>
                         <div class="contents-right">
                            <span style="margin-left: 20px;">
                                <el-input v-model="registrationData[12].value" placeholder="请输入内容"></el-input>
                            </span>
                         </div>
                         <div class="contents-left">
                           <span style="margin-left: 20px;">所属国家高新区</span>
                         </div>
                         <div class="contents-right">
                            <span style="margin-left: 20px;">
                                <el-input v-model="registrationData[13].value" placeholder="请输入内容"></el-input>
                            </span>
                         </div>
                       
                       </div>
                       <div class="content-areas" style="margin-top: -0px;">
                         <div class="contents-left">
                           <span style="margin-left: 20px;">所属其他园区</span>
                         </div>
                         <div class="contents-right">
                            <span style="margin-left: 20px;">
                                <el-input v-model="registrationData[14].value" placeholder="请输入内容"></el-input>
                            </span>
                         </div>
                         <div class="contents-left">
                           <span style="margin-left: 20px;">所属国家经济开发区</span>
                         </div>
                         <div class="contents-right">
                            <span style="margin-left: 20px;">
                                <el-input v-model="registrationData[15].value" placeholder="请输入内容"></el-input>
                            </span>
                         </div>
                       
                       </div>
                       <div class="content-areas" style="margin-top: -0px;">
                         <div class="contents-left">
                           <span style="margin-left: 20px;">所属行业</span>
                         </div>
                         <div class="contents-right" style="border-bottom: 1px solid #DCE0FD">
                            <span style="margin-left: 20px;width: 100%;">
                                <el-input v-model="registrationData[16].value" placeholder="请输入内容"></el-input>
                            </span>
                         </div>
                        
                         </div>
                         <div class="content-areas" style="margin-top: -0px;">
                         <div class="contents-left">
                           <span style="margin-left: 20px;">通讯地址</span>
                         </div>
                         <div class="contents-right" style="border-bottom: 1px solid #DCE0FD">
                            <span style="margin-left: 20px;">
                                <el-input v-model="registrationData[17].value" placeholder="请输入内容"></el-input>
                            </span>
                         </div>
                        
                         </div>
                         <div class="content-areas" style="margin-top: -0px;">
                         <div class="contents-left">
                           <span style="margin-left: 20px;width: 100%;">经营范围</span>
                         </div>
                         <div class="contents-right" style="border-bottom: 1px solid #DCE0FD">
                            <span style="margin-left: 20px;">
                                <el-input v-model="registrationData[18].value" placeholder="请输入内容"></el-input>
                            </span>
                         </div>
                        
                         </div>
                         <div class="content-areas" style="margin-top: -0px;">
                         <div class="contents-left">
                           <span style="margin-left: 20px;width: 100%;">企业简介</span>
                         </div>
                         <div class="contents-right" style="border-bottom: 1px solid #DCE0FD">
                            <span style="margin-left: 20px;">
                                <el-input v-model="registrationData[19].value" placeholder="请输入内容"></el-input>
                            </span>
                         </div>
                       </div>
            </div>
            <div class="heng"></div>
            <!-- 法定代表人信息区域 -->
            <div class="legal-representative-area">
                <div class="info-title" style="color: #6C6C6C;">法定代表人信息</div>
                <div style="padding-bottom: 18px;">
                    <el-table :data="legalRepresentativeData" :header-cell-style="{ background: '#EDEFFE', color: '#363636' }" border style="width: 100%">
                        <el-table-column label="姓名" align="center">
                            <template slot-scope="scope">
                                <el-input 
                                    v-model="scope.row.name" 
                                    size="small" 
                                    class="table-input"
                                    @input="handleLegalInput('name', scope.$index, $event)">
                                </el-input>
                            </template>
                        </el-table-column>
                        <el-table-column label="电话号码" align="center">
                            <template slot-scope="scope">
                                <el-input 
                                    v-model="scope.row.phone" 
                                    size="small" 
                                    class="table-input" 
                                    placeholder="请输入…"
                                    @input="handleLegalInput('phone', scope.$index, $event)">
                                </el-input>
                            </template>
                        </el-table-column>
                        <el-table-column label="手机号码" align="center">
                            <template slot-scope="scope">
                                <el-input 
                                    v-model="scope.row.mobile" 
                                    size="small" 
                                    class="table-input"
                                    @input="handleLegalInput('mobile', scope.$index, $event)">
                                </el-input>
                            </template>
                        </el-table-column>
                        <el-table-column label="邮箱" align="center">
                            <template slot-scope="scope">
                                <el-input 
                                    v-model="scope.row.email" 
                                    size="small" 
                                    class="table-input" 
                                    placeholder="请输入…"
                                    @input="handleLegalInput('email', scope.$index, $event)">
                                </el-input>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </div>
            <div class="heng"></div>
            <!-- 企业联系人信息 -->
            <div class="legal-representative-area">
                <div class="info-title" style="color: #6C6C6C;">企业联系人信息</div>
                <div style="padding-bottom: 18px;">
                    <el-table :data="contactData" :header-cell-style="{ background: '#EDEFFE', color: '#363636' }" border style="width: 100%">
                        <el-table-column label="姓名" align="center">
                            <template slot-scope="scope">
                                <el-input 
                                    v-model="scope.row.name" 
                                    size="small" 
                                    class="table-input"
                                    @input="handleContactInput('name', scope.$index, $event)">
                                </el-input>
                            </template>
                        </el-table-column>
                        <el-table-column label="电话号码" align="center">
                            <template slot-scope="scope">
                                <el-input 
                                    v-model="scope.row.phone" 
                                    size="small" 
                                    class="table-input" 
                                    placeholder="请输入…"
                                    @input="handleContactInput('phone', scope.$index, $event)">
                                </el-input>
                            </template>
                        </el-table-column>
                        <el-table-column label="手机号码" align="center">
                            <template slot-scope="scope">
                                <el-input 
                                    v-model="scope.row.mobile" 
                                    size="small" 
                                    class="table-input"
                                    @input="handleContactInput('mobile', scope.$index, $event)">
                                </el-input>
                            </template>
                        </el-table-column>
                        <el-table-column label="邮箱" align="center">
                            <template slot-scope="scope">
                                <el-input 
                                    v-model="scope.row.email" 
                                    size="small" 
                                    class="table-input" 
                                    placeholder="请输入…"
                                    @input="handleContactInput('email', scope.$index, $event)">
                                </el-input>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </div>
            <div class="heng"></div>
            <!-- 企业上市情况 -->
            <div class="legal-representative-area">
                <div class="info-title" style="color: #6C6C6C;">企业上市情况</div>
                <div style="padding-bottom: 18px;">
                    <el-table :data="listingData" :header-cell-style="{ background: '#EDEFFE', color: '#363636' }" border style="width: 100%">
                        <el-table-column label="是否上市" align="center">
                            <template slot-scope="scope">
                                <el-select v-model="scope.row.isListed" size="small" class="table-select">
                                    <el-option label="已上市" value="yes"></el-option>
                                    <el-option label="未上市" value="no"></el-option>
                                </el-select>
                            </template>
                        </el-table-column>
                        <el-table-column label="上市类型" align="center">
                            <template slot-scope="scope">
                                <el-select v-model="scope.row.listingType" size="small" class="table-select" placeholder="请选择">
                                    <el-option label="新三板" value="新三板"></el-option>
                                    <el-option label="深交所中小板" value="深交所中小板"></el-option>
                                    <el-option label="深交所主板" value="深交所主板"></el-option>
                                    <el-option label="深交所创业板" value="深交所创业板"></el-option>
                                    <el-option label="上交所" value="上交所"></el-option>
                                    <el-option label="上交所科创板" value="上交所科创板"></el-option>
                                    <el-option label="北交所" value="北交所"></el-option>
                                    <el-option label="四板" value="四板"></el-option>
                                    <el-option label="纳斯达克" value="纳斯达克"></el-option>
                                    <el-option label="纽约交易所" value="纽约交易所"></el-option>
                                    <el-option label="伦敦" value="伦敦"></el-option>
                                    <el-option label="东京" value="东京"></el-option>
                                    <el-option label="新加坡" value="新加坡"></el-option>
                                    <el-option label="香港" value="香港"></el-option>
                                    <el-option label="其它" value="其它"></el-option>
                                </el-select>
                            </template>
                        </el-table-column>
                        <el-table-column label="上市时间" align="center">
                            <template slot-scope="scope">
                                <el-input v-model="scope.row.listingTime" size="small" class="table-input"></el-input>
                            </template>
                        </el-table-column>
                        <el-table-column label="股票代码" align="center">
                            <template slot-scope="scope">
                                <el-input v-model="scope.row.stockCode" size="small" class="table-input" placeholder="请输入…"></el-input>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </div>
            <div class="heng"></div>
            <div class="table-areas" >
                <div class="btn-areas" style="display: flex;justify-content: space-between;">
                    <div class="btn-areas-title" style="color: #6C6C6C;position: relative;top: 5px;">风险投资</div>
                    <el-button type="primary" size="small" style="background-color: #5165f7;" @click="handleAdd">添加</el-button>
                </div>
             <div style="padding-bottom: 18px;">
                <el-table :data="tableData" :header-cell-style="{ background: '#EDEFFE', color: '#363636' }" border style="width: 100%">
                 
                  <el-table-column label="公民类型(个人股东)"  align="center">
                    <template slot-scope="scope">
                        <el-select v-model="scope.row.citizenType" size="small" class="table-select">
                            <el-option label="中国公民" value="chinese"></el-option>
                            <el-option label="外国公民" value="foreign"></el-option>
                        </el-select>
                    </template>
                  </el-table-column>
                  <el-table-column label="姓名"  align="center">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.name" size="small" class="table-input"></el-input>
                    </template>
                  </el-table-column>
                  <el-table-column label="身份证 (护照) 号" align="center">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.idNumber" size="small" class="table-input"></el-input>
                    </template>
                  </el-table-column>
                  <el-table-column label="投资额(万元)" align="center">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.investment" size="small" class="table-input"></el-input>
                    </template>
                  </el-table-column>
                  <el-table-column
                  align="center"
                    label="操作"
                    width="80">
                    <template slot-scope="scope">
                        <el-button type="text" size="small" style="color: #F56C6C;" @click="handleDelete(scope.$index)">删除</el-button>
                    </template>
                    </el-table-column>
                </el-table>
             </div>

              </div>
              
            <!-- 法人股东表格 -->
            <div class="table-areas" style="margin-top:0px;">
                <div class="btn-areas">
                
                    <el-button type="primary" size="small" style="background-color: #5165f7;" @click="handleAddLegal">添加</el-button>
                </div>
             <div style="padding-bottom: 18px;">
                <el-table :data="legalTableData" :header-cell-style="{ background: '#EDEFFE', color: '#363636' }" border style="width: 100%">
                 
                  <el-table-column label="法人类型(法人股东)"  align="center">
                    <template slot-scope="scope">
                        <el-select v-model="scope.row.legalType" size="small" class="table-select">
                            <el-option label="中国企业法人" value="chinese"></el-option>
                            <el-option label="外国企业法人" value="foreign"></el-option>
                        </el-select>
                    </template>
                  </el-table-column>
                  <el-table-column label="名称"  align="center">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.name" size="small" class="table-input"></el-input>
                    </template>
                  </el-table-column>
                  <el-table-column label="组织机构代码或统一社会信用代码" align="center">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.orgCode" size="small" class="table-input"></el-input>
                    </template>
                  </el-table-column>
                  <el-table-column label="投资额(万元)" align="center">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.investment" size="small" class="table-input"></el-input>
                    </template>
                  </el-table-column>
                  <el-table-column
                  align="center"
                    label="操作"
                    width="80">
                    <template slot-scope="scope">
                        <el-button type="text" size="small" style="color: #F56C6C;" @click="handleDeleteLegal(scope.$index)">删除</el-button>
                    </template>
                    </el-table-column>
                </el-table>
             </div>

              </div>
              <div class="heng"></div>
            <!-- 风险投资表格 -->
            <div class="table-areas" style="margin-top:0px;">
                <div class="btn-areas" style="display: flex;justify-content: space-between;">
                    <div class="btn-areas-title" style="color: #6C6C6C;position: relative;top: 5px;">风险投资</div>
                    <div><el-button type="primary" size="small" style="background-color: #5165f7;" @click="handleAddInvestment">添加</el-button></div>
                </div>
             <div style="padding-bottom: 18px;">
                <el-table :data="investmentTableData" :header-cell-style="{ background: '#EDEFFE', color: '#363636' }" border style="width: 100%">
                 
                  <el-table-column label="法人类型"  align="center">
                    <template slot-scope="scope">
                        <el-select v-model="scope.row.legalType" size="small" class="table-select">
                            <el-option label="中国企业法人" value="chinese"></el-option>
                            <el-option label="外国企业法人" value="foreign"></el-option>
                        </el-select>
                    </template>
                  </el-table-column>
                  <el-table-column label="名称"  align="center">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.name" size="small" class="table-input"></el-input>
                    </template>
                  </el-table-column>
                  <el-table-column label="组织机构代码或统一社会信用代码" align="center">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.orgCode" size="small" class="table-input"></el-input>
                    </template>
                  </el-table-column>
                  <el-table-column label="出资额(万元)" align="center">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.investment" size="small" class="table-input"></el-input>
                    </template>
                  </el-table-column>
                  <el-table-column
                  align="center"
                    label="操作"
                    width="80">
                    <template slot-scope="scope">
                        <el-button type="text" size="small" style="color: #F56C6C;" @click="handleDeleteInvestment(scope.$index)">删除</el-button>
                    </template>
                    </el-table-column>
                </el-table>
             </div>

              </div>
              <div class="heng"></div>
            <!-- 底部按钮区域 -->
            <div class="footer-buttons">
                <el-button type="primary" size="small" @click="handleSave" class="save-btn">保存</el-button>
                <el-button size="small" @click="handleCancel" class="cancel-btn">取消</el-button>
              </div>
        </div>
    </div>
</template>
<script>
import{show,save} from '@/api/homepage/corporateInformation'
import {images} from '@/api/public'
import { setEnterpriseLogo } from '@/utils/auth'
export default {
    data(){
        return{
            tableData: [{
                citizenType: 'chinese',
            name: '王小虎',
                idNumber: '',
                investment: ''
            }],
            citizenTypeOptions: [
                { label: '中国公民', value: 'chinese' },
                { label: '外国公民', value: 'foreign' }
            ],
            legalTableData: [{
                legalType: 'chinese',
                name: '顺为资本',
                orgCode: '91520115MAAK5AWNXT',
                investment: '10000.00'
            }],
            investmentTableData: [{
                legalType: 'foreign',
                name: '李尼采',
                orgCode: '91520115MAAK5AWNXT',
                investment: '10000.00'
            }],
            // 保存初始数据
            initialTableData: [],
            initialLegalTableData: [],
            initialInvestmentTableData: [],
            initialRegistrationData: [],
            // 图片上传
            logoUrl: '',
            licenseUrl: '',
            logoHost: '',
            licenseHost: '',
            // 注册信息
            registrationData: [
                { label: '企业名称', value: '贵州微育科技有限公司', type: 'input' },
                { label: '企业简称', value: '微育科技', type: 'input' },
                { label: '统一社会信用代码', value: '91520115MAAK5AWNXT', type: 'input' },
                { label: '企业类型', value: '有限责任公司(自然人投资或控股)', type: 'input' },
                { label: '注册资本', value: '1000万元', type: 'input' },
                { label: '实缴资本', value: '800万元', type: 'input' },
                { label: '成立日期', value: '2020-12-03', type: 'input' },
                { label: '核准日期', value: '2020-12-03', type: 'input' },
                { label: '营业期限', value: '2020-12-03 至无固定期限', type: 'input' },
                { label: '所属区域', value: '贵州省贵阳市观山湖区', type: 'input' },
                { label: '登记机关', value: '贵阳市观山湖区市场监督管理局', type: 'input' },
                { label: '工商注册号', value: '520115000634139', type: 'input' },
                { label: '所得税征收方式', value: '', type: 'input', placeholder: '请输入……' },
                { label: '所属国家高新区', value: '高新区', type: 'input' },
                { label: '所属其他园区', value: '高新区', type: 'input' },
                { label: '所属国家经济开发区', value: '高新区', type: 'input' },
                { label: '所属行业', value: '信息传输、软件和信息技术服务业>软件和信息技术服务业>其他信息技术服务业>其他未列明信息技术服务业(I6599)', type: 'input' },
                { label: '通讯地址', value: '', type: 'input' },
                { label: '经营范围', value: '', type: 'input' },
                { label: '企业简介', value: '', type: 'input' }
            ],
            // 法定代表人信息
            legalRepresentativeData: [{
                name: '徐蛟',
                phone: '',
                mobile: '13524876565',
                email: ''
            }],
            initialLegalRepresentativeData: [],
            // 企业联系人信息
            contactData: [{
                name: '',
                phone: '',
                mobile: '',
                email: ''
            }],
            initialContactData: [],
            // 企业上市情况
            listingData: [{
                isListed: 'no',
                listingType: '',
                listingTime: '',
                stockCode: ''
            }],
            initialListingData: []
        }
    },
    created() {
        // 保存初始数据（深拷贝）
        this.initialTableData = JSON.parse(JSON.stringify(this.tableData))
        this.initialLegalTableData = JSON.parse(JSON.stringify(this.legalTableData))
        this.initialInvestmentTableData = JSON.parse(JSON.stringify(this.investmentTableData))
        this.initialRegistrationData = JSON.parse(JSON.stringify(this.registrationData))
        this.initialLegalRepresentativeData = JSON.parse(JSON.stringify(this.legalRepresentativeData))
        this.initialContactData = JSON.parse(JSON.stringify(this.contactData))
        this.initialListingData = JSON.parse(JSON.stringify(this.listingData))
        // 获取并回显数据
        this.fetchData()
    },
    methods: {
        async fetchData() {
            try {
                const res = await show({})
                
                console.log('fetchData 返回的完整数据:', res)
                
                if (res && res.code === 1 && res.data) {
                    // 数据结构可能是对象（键是数字字符串）或数组
                    let dataToProcess = res.data
                    
                    // 如果是数组，转换为对象格式以便统一处理
                    if (Array.isArray(res.data)) {
                        console.log('res.data 是数组格式，转换为对象格式')
                        dataToProcess = {}
                        res.data.forEach((item, index) => {
                            // 从 item 中获取 parent_id，如果没有则使用索引
                            const parentId = item.parent_id !== undefined ? item.parent_id : index
                            dataToProcess[String(parentId)] = item
                        })
                        console.log('转换后的 dataToProcess:', dataToProcess)
                    }
                    
                    // 遍历所有数据，根据 parent_title 来匹配
                    Object.keys(dataToProcess).forEach(key => {
                        const item = dataToProcess[key]
                        const parentId = parseInt(key)
                        const parentTitle = item.parent_title || ''
                        
                        console.log(`处理 parent_id: ${parentId}, parent_title: ${parentTitle}`)
                        
                        // parent_id: 0 - 基本信息（图片数据）
                        if (parentId === 0 && item.list && Array.isArray(item.list)) {
                            console.log('处理基本信息（图片数据），list:', item.list)
                            item.list.forEach(imgItem => {
                                console.log('处理图片项:', imgItem.name, 'value:', imgItem.value)
                                if (imgItem.name === 'logo') {
                                    // 如果 value 是完整 URL，直接使用
                                    if (imgItem.value && (imgItem.value.startsWith('http://') || imgItem.value.startsWith('https://'))) {
                                        this.logoUrl = imgItem.value
                                        console.log('设置 logoUrl (完整URL):', this.logoUrl)
                                        // 更新 localStorage 中的 logo
                                        setEnterpriseLogo(this.logoUrl)
                                    } else {
                                        this.logoUrl = imgItem.value || ''
                                        // 如果有 host 字段，保存 host
                                        if (imgItem.host) {
                                            this.logoHost = imgItem.host
                                            // 如果有 host，组合完整的 URL 用于显示
                                            if (imgItem.host && imgItem.value) {
                                                const host = imgItem.host.endsWith('/') ? imgItem.host : imgItem.host + '/'
                                                const path = imgItem.value.startsWith('/') ? imgItem.value.slice(1) : imgItem.value
                                                this.logoUrl = host + path
                                                console.log('设置 logoUrl (host + path):', this.logoUrl)
                                                // 更新 localStorage 中的 logo
                                                setEnterpriseLogo(this.logoUrl)
                                            }
                                        } else if (this.logoUrl) {
                                            // 即使没有 host，如果有 logoUrl，也更新 localStorage
                                            setEnterpriseLogo(this.logoUrl)
                                        }
                                    }
                                } else if (imgItem.name === 'license') {
                                    // 如果 value 是完整 URL，直接使用
                                    if (imgItem.value && (imgItem.value.startsWith('http://') || imgItem.value.startsWith('https://'))) {
                                        this.licenseUrl = imgItem.value
                                        console.log('设置 licenseUrl (完整URL):', this.licenseUrl)
                                    } else {
                                        this.licenseUrl = imgItem.value || ''
                                        // 如果有 host 字段，保存 host
                                        if (imgItem.host) {
                                            this.licenseHost = imgItem.host
                                            // 如果有 host，组合完整的 URL 用于显示
                                            if (imgItem.host && imgItem.value) {
                                                const host = imgItem.host.endsWith('/') ? imgItem.host : imgItem.host + '/'
                                                const path = imgItem.value.startsWith('/') ? imgItem.value.slice(1) : imgItem.value
                                                this.licenseUrl = host + path
                                                console.log('设置 licenseUrl (host + path):', this.licenseUrl)
                                            }
                                        }
                                    }
                                }
                            })
                        }
                        
                        // parent_id: 1 - 注册信息
                        if (parentId === 1 && item.list && Array.isArray(item.list)) {
                            item.list.forEach(regItem => {
                            // 根据 name 或 title 匹配注册信息字段
                            const regIndex = this.registrationData.findIndex(reg => {
                                const regName = reg.label.toLowerCase().replace(/\s+/g, '_').replace(/[()（）]/g, '')
                                const itemName = (regItem.name || '').toLowerCase().replace(/\s+/g, '_').replace(/[()（）]/g, '')
                                const matchByName = regName === itemName
                                const matchByTitle = reg.label === regItem.title
                                return matchByName || matchByTitle
                            })
                                if (regIndex !== -1) {
                                    this.registrationData[regIndex].value = regItem.value || ''
                                }
                            })
                        }
                        
                        // parent_id: 2 - 法定代表人信息（根据 parent_title 匹配）
                        if (parentId === 2 && parentTitle === '法定代表人信息' && item.list && Array.isArray(item.list)) {
                            const legalData = {}
                            item.list.forEach(legalItem => {
                            // 支持新的命名方式（legal_前缀）和旧的命名方式（兼容）
                            if (legalItem.name === 'legal_name' || legalItem.name === 'name') {
                                legalData.name = legalItem.value || ''
                            } else if (legalItem.name === 'legal_phone' || legalItem.name === 'phone') {
                                legalData.phone = legalItem.value || ''
                            } else if (legalItem.name === 'legal_mobile' || legalItem.name === 'mobile') {
                                legalData.mobile = legalItem.value || ''
                            } else if (legalItem.name === 'legal_email' || legalItem.name === 'email') {
                                legalData.email = legalItem.value || ''
                            }
                        })
                        if (Object.keys(legalData).length > 0) {
                            if (this.legalRepresentativeData.length > 0) {
                                this.legalRepresentativeData[0] = { ...this.legalRepresentativeData[0], ...legalData }
                                } else {
                                    this.legalRepresentativeData.push(legalData)
                                }
                            }
                        }
                        
                        // parent_id: 3 - 企业联系人信息（根据 parent_title 匹配）
                        if (parentId === 3 && item.list && Array.isArray(item.list)) {
                            // 检查 parent_title 是否匹配，如果没有 parent_title 也尝试匹配（兼容旧数据）
                            const isContactInfo = parentTitle === '企业联系人信息' || parentTitle === '' || !parentTitle
                            
                            if (isContactInfo) {
                                console.log('处理企业联系人信息，parent_title:', parentTitle)
                                console.log('item.list:', item.list)
                                
                                const contactData = {}
                                item.list.forEach(contactItem => {
                                    console.log('处理 contactItem:', contactItem.name, 'value:', contactItem.value)
                                    
                                    // 支持新的命名方式（contact_前缀）和旧的命名方式（兼容）
                                    // 直接使用 value，不进行 || 判断，因为空字符串也是有效值
                                    if (contactItem.name === 'contact_name' || contactItem.name === 'name') {
                                        // 优先使用带前缀的值
                                        if (contactItem.name === 'contact_name') {
                                            contactData.name = contactItem.value !== undefined && contactItem.value !== null ? String(contactItem.value) : ''
                                        } else if (contactItem.name === 'name' && contactData.name === undefined) {
                                            contactData.name = contactItem.value !== undefined && contactItem.value !== null ? String(contactItem.value) : ''
                                        }
                                    } else if (contactItem.name === 'contact_phone' || contactItem.name === 'phone') {
                                        if (contactItem.name === 'contact_phone') {
                                            contactData.phone = contactItem.value !== undefined && contactItem.value !== null ? String(contactItem.value) : ''
                                        } else if (contactItem.name === 'phone' && contactData.phone === undefined) {
                                            contactData.phone = contactItem.value !== undefined && contactItem.value !== null ? String(contactItem.value) : ''
                                        }
                                    } else if (contactItem.name === 'contact_mobile' || contactItem.name === 'mobile') {
                                        if (contactItem.name === 'contact_mobile') {
                                            contactData.mobile = contactItem.value !== undefined && contactItem.value !== null ? String(contactItem.value) : ''
                                        } else if (contactItem.name === 'mobile' && contactData.mobile === undefined) {
                                            contactData.mobile = contactItem.value !== undefined && contactItem.value !== null ? String(contactItem.value) : ''
                                        }
                                    } else if (contactItem.name === 'contact_email' || contactItem.name === 'email') {
                                        if (contactItem.name === 'contact_email') {
                                            contactData.email = contactItem.value !== undefined && contactItem.value !== null ? String(contactItem.value) : ''
                                        } else if (contactItem.name === 'email' && contactData.email === undefined) {
                                            contactData.email = contactItem.value !== undefined && contactItem.value !== null ? String(contactItem.value) : ''
                                        }
                                    }
                                })
                                
                                console.log('解析后的 contactData:', contactData)
                                
                                // 只要有字段就更新（即使值为空，也要显示空值）
                                if (Object.keys(contactData).length > 0) {
                                    // 确保 contactData 数组至少有一个元素
                                    if (this.contactData.length === 0) {
                                        this.contactData.push({
                                            name: '',
                                            phone: '',
                                            mobile: '',
                                            email: ''
                                        })
                                    }
                                    // 使用 Vue.set 确保响应式更新
                                    this.$set(this.contactData, 0, { ...this.contactData[0], ...contactData })
                                    console.log('更新后的 this.contactData:', this.contactData)
                                }
                            }
                        }
                        
                        // parent_id: 4 - 企业上市情况（根据 parent_title 匹配）
                        if (parentId === 4 && parentTitle === '企业上市情况' && item.list && Array.isArray(item.list)) {
                            const listingData = {}
                            item.list.forEach(listingItem => {
                            if (listingItem.name === 'isListed') {
                                listingData.isListed = listingItem.value || ''
                            } else if (listingItem.name === 'listingType') {
                                listingData.listingType = listingItem.value || ''
                            } else if (listingItem.name === 'listingTime') {
                                listingData.listingTime = listingItem.value || ''
                            } else if (listingItem.name === 'stockCode') {
                                listingData.stockCode = listingItem.value || ''
                            }
                        })
                        if (Object.keys(listingData).length > 0) {
                            if (this.listingData.length > 0) {
                                this.listingData[0] = { ...this.listingData[0], ...listingData }
                                } else {
                                    this.listingData.push(listingData)
                                }
                            }
                        }
                        
                        // parent_id: 5 - 股权结构（根据 parent_title 和 name 来区分个人股东、法人股东）
                        if (parentId === 5 && parentTitle === '股权结构' && item.list && Array.isArray(item.list)) {
                            item.list.forEach(shareItem => {
                                // 根据 name 字段判断是个人股东(gudong)还是法人股东(faren)
                                if (shareItem.name === 'gudong' && shareItem.value) {
                                    // 个人股东数据
                                    try {
                                        let gudongData = shareItem.value
                                        if (typeof gudongData === 'string') {
                                            // 将单引号替换为双引号以便 JSON.parse
                                            gudongData = gudongData.replace(/'/g, '"')
                                            gudongData = JSON.parse(gudongData)
                                        }
                                        
                                        // gudongData 应该是数组，每个元素包含 a, b, c, d 字段
                                        if (Array.isArray(gudongData)) {
                                            this.tableData = gudongData.map(gudongItem => ({
                                                citizenType: gudongItem.a || '',
                                                name: gudongItem.b || '',
                                                idNumber: gudongItem.c || '',
                                                investment: gudongItem.d || ''
                                            }))
                                        }
                                    } catch (error) {
                                        console.error('解析个人股东数据失败:', error)
                                    }
                                } else if (shareItem.name === 'faren' && shareItem.value) {
                                    // 法人股东数据
                                    try {
                                        let farenData = shareItem.value
                                        if (typeof farenData === 'string') {
                                            // 将单引号替换为双引号以便 JSON.parse
                                            farenData = farenData.replace(/'/g, '"')
                                            farenData = JSON.parse(farenData)
                                        }
                                        
                                        // farenData 应该是数组，每个元素包含 a, b, c, d 字段
                                        if (Array.isArray(farenData)) {
                                            this.legalTableData = farenData.map(farenItem => ({
                                                legalType: farenItem.a || '',
                                                name: farenItem.b || '',
                                                orgCode: farenItem.c || '',
                                                investment: farenItem.d || ''
                                            }))
                                        }
                                    } catch (error) {
                                        console.error('解析法人股东数据失败:', error)
                                    }
                                }
                            })
                        }
                        
                        // parent_id: 6 - 风险投资（根据 parent_title 匹配）
                        if (parentId === 6 && parentTitle === '风险投资' && item.list) {
                            // list 可能是一个对象（不是数组）
                            let ventureList = item.list
                            if (!Array.isArray(ventureList)) {
                                // 如果是对象，转换为数组处理
                                ventureList = [ventureList]
                            }
                            
                            ventureList.forEach(ventureItem => {
                                // 查找 name 为 'venture_capital' 的项
                                if (ventureItem.name === 'venture_capital' && ventureItem.value) {
                                    try {
                                        // 解析 value 中的 JSON 字符串（可能是单引号格式）
                                        let ventureData = ventureItem.value
                                        // 如果是字符串，尝试解析
                                        if (typeof ventureData === 'string') {
                                            // 将单引号替换为双引号以便 JSON.parse
                                            ventureData = ventureData.replace(/'/g, '"')
                                            ventureData = JSON.parse(ventureData)
                                        }
                                        
                                        // ventureData 应该是数组，每个元素包含 a, b, c, d 字段
                                        if (Array.isArray(ventureData)) {
                                            this.investmentTableData = ventureData.map(ventureDataItem => ({
                                                legalType: ventureDataItem.a || '',
                                                name: ventureDataItem.b || '',
                                                orgCode: ventureDataItem.c || '',
                                                investment: ventureDataItem.d || ''
                                            }))
                                        }
                                    } catch (error) {
                                        console.error('解析风险投资数据失败:', error)
                                    }
                                }
                            })
                        }
                    })
                    
                    // 更新初始数据
                    this.initialTableData = JSON.parse(JSON.stringify(this.tableData))
                    this.initialLegalTableData = JSON.parse(JSON.stringify(this.legalTableData))
                    this.initialInvestmentTableData = JSON.parse(JSON.stringify(this.investmentTableData))
                    this.initialRegistrationData = JSON.parse(JSON.stringify(this.registrationData))
                    this.initialLegalRepresentativeData = JSON.parse(JSON.stringify(this.legalRepresentativeData))
                    this.initialContactData = JSON.parse(JSON.stringify(this.contactData))
                    this.initialListingData = JSON.parse(JSON.stringify(this.listingData))
                }
            } catch (error) {
                console.error('获取数据失败:', error)
                // 不显示错误提示，因为可能是首次创建，没有数据
            }
        },
        handleAdd() {
            this.tableData.push({
                citizenType: 'chinese',
                name: '',
                idNumber: '',
                investment: ''
            })
        },
        handleDelete(index) {
            this.tableData.splice(index, 1)
        },
        handleAddLegal() {
            this.legalTableData.push({
                legalType: 'chinese',
                name: '',
                orgCode: '',
                investment: ''
            })
        },
        handleDeleteLegal(index) {
            this.legalTableData.splice(index, 1)
        },
        handleAddInvestment() {
            this.investmentTableData.push({
                legalType: 'chinese',
                name: '',
                orgCode: '',
                investment: ''
            })
        },
        handleDeleteInvestment(index) {
            this.investmentTableData.splice(index, 1)
        },
        async handleSave() {
            try {
                // 按照图片中的结构组织数据：最外层是数组，每个元素包含 parent_id 和 list
                const saveData = []
                
                // 调试：打印当前数据
                console.log('保存前的 contactData:', this.contactData)
                
                // parent_id: 0 - 图片数据（logo 和 license）
                const imageList = []
                if (this.logoUrl) {
                    // 提取路径部分
                    let logoPath = this.logoUrl
                    try {
                        // 如果是完整 URL，提取路径部分
                        if (logoPath.startsWith('http://') || logoPath.startsWith('https://')) {
                            const urlObj = new URL(logoPath)
                            logoPath = urlObj.pathname
                        } else if (logoPath.includes('/uploads/')) {
                            // 如果包含 /uploads/，提取从 /uploads/ 开始的部分
                            const uploadsIndex = logoPath.indexOf('/uploads/')
                            logoPath = logoPath.substring(uploadsIndex)
                        }
                    } catch (e) {
                        // 如果解析失败，使用原始值
                        console.warn('解析 logo URL 失败，使用原始值:', e)
                    }
                    
                    // 如果有 host，将 host 和 path 组合成完整 URL
                    let logoValue = logoPath
                    if (this.logoHost) {
                        const host = this.logoHost.endsWith('/') ? this.logoHost : this.logoHost + '/'
                        const path = logoPath.startsWith('/') ? logoPath.slice(1) : logoPath
                        logoValue = host + path
                    }
                    
                    imageList.push({
                        name: 'logo',
                        title: '企业LOGO',
                        value: logoValue,
                        type: 'file',
                        sort: 0
                    })
                }
                if (this.licenseUrl) {
                    // 提取路径部分
                    let licensePath = this.licenseUrl
                    try {
                        // 如果是完整 URL，提取路径部分
                        if (licensePath.startsWith('http://') || licensePath.startsWith('https://')) {
                            const urlObj = new URL(licensePath)
                            licensePath = urlObj.pathname
                        } else if (licensePath.includes('/uploads/')) {
                            // 如果包含 /uploads/，提取从 /uploads/ 开始的部分
                            const uploadsIndex = licensePath.indexOf('/uploads/')
                            licensePath = licensePath.substring(uploadsIndex)
                        }
                    } catch (e) {
                        // 如果解析失败，使用原始值
                        console.warn('解析 license URL 失败，使用原始值:', e)
                    }
                    
                    // 如果有 host，将 host 和 path 组合成完整 URL
                    let licenseValue = licensePath
                    if (this.licenseHost) {
                        const host = this.licenseHost.endsWith('/') ? this.licenseHost : this.licenseHost + '/'
                        const path = licensePath.startsWith('/') ? licensePath.slice(1) : licensePath
                        licenseValue = host + path
                    }
                    
                    imageList.push({
                        name: 'license',
                        title: '企业营业执照',
                        value: licenseValue,
                        type: 'file',
                        sort: imageList.length
                    })
                }
                if (imageList.length > 0) {
                    saveData.push({
                        parent_id: 0,
                        list: imageList
                    })
                }
                
                // parent_id: 1 - 注册信息（企业名称等所有字段）
                const registrationList = this.registrationData.map((item, index) => ({
                    name: item.label.toLowerCase().replace(/\s+/g, '_'), // 转换为小写并用下划线替换空格
                    title: item.label,
                    value: item.value || '',
                    type: item.type === 'input' ? 'text' : item.type,
                    sort: index
                }))
                if (registrationList.length > 0) {
                    saveData.push({
                        parent_id: 1,
                        list: registrationList
                    })
                }
                
                // parent_id: 2 - 法定代表人信息
                // 使用前缀 legal_ 确保 name 唯一
                // 确保 legalRepresentativeData 至少有一个元素，且只取第一个元素
                if (!this.legalRepresentativeData || this.legalRepresentativeData.length === 0) {
                    this.legalRepresentativeData = [{
                        name: '',
                        phone: '',
                        mobile: '',
                        email: ''
                    }]
                }
                
                // 直接读取第一个元素，不使用深拷贝，确保获取最新值
                console.log('保存前的 legalRepresentativeData (原始):', this.legalRepresentativeData)
                const legalItem = this.legalRepresentativeData[0] || {
                    name: '',
                    phone: '',
                    mobile: '',
                    email: ''
                }
                
                console.log('使用的 legalItem:', legalItem)
                console.log('legalItem.name:', legalItem.name)
                console.log('legalItem.phone:', legalItem.phone)
                console.log('legalItem.mobile:', legalItem.mobile)
                console.log('legalItem.email:', legalItem.email)
                
                // 使用与联系人信息相同的数据结构：每个字段一条数据
                const legalRepresentativeList = [
                    {
                        name: 'legal_name',
                        title: '姓名',
                        value: (legalItem.name !== undefined && legalItem.name !== null) ? String(legalItem.name) : '',
                        type: 'text',
                        sort: 0
                    },
                    {
                        name: 'legal_phone',
                        title: '电话号码',
                        value: (legalItem.phone !== undefined && legalItem.phone !== null) ? String(legalItem.phone) : '',
                        type: 'text',
                        sort: 1
                    },
                    {
                        name: 'legal_mobile',
                        title: '手机号码',
                        value: (legalItem.mobile !== undefined && legalItem.mobile !== null) ? String(legalItem.mobile) : '',
                        type: 'text',
                        sort: 2
                    },
                    {
                        name: 'legal_email',
                        title: '邮箱',
                        value: (legalItem.email !== undefined && legalItem.email !== null) ? String(legalItem.email) : '',
                        type: 'text',
                        sort: 3
                    }
                ]
                
                console.log('最终生成的 legalRepresentativeList:', JSON.stringify(legalRepresentativeList, null, 2))
                
                if (legalRepresentativeList.length > 0) {
                    saveData.push({
                        parent_id: 2,
                        list: legalRepresentativeList
                    })
                }
                
                // parent_id: 3 - 企业联系人信息
                // 使用前缀 contact_ 确保 name 唯一
                // 确保 contactData 至少有一个元素，且只取第一个元素（与法定代表人信息保持一致）
                if (!this.contactData || this.contactData.length === 0) {
                    this.contactData = [{
                        name: '',
                        phone: '',
                        mobile: '',
                        email: ''
                    }]
                }
                
                // 确保 contactData 数组存在且有数据
                // 直接读取响应式数据，不使用深拷贝，确保获取最新值
                console.log('保存前的 contactData (原始):', this.contactData)
                console.log('contactData 类型:', typeof this.contactData)
                console.log('contactData 是否为数组:', Array.isArray(this.contactData))
                console.log('contactData.length:', this.contactData ? this.contactData.length : 'undefined')
                
                // 直接读取第一个元素，不使用深拷贝
                let contactItem = null
                if (this.contactData && Array.isArray(this.contactData) && this.contactData.length > 0) {
                    contactItem = this.contactData[0]
                    console.log('直接读取 contactData[0]:', contactItem)
                    console.log('contactData[0] 的类型:', typeof contactItem)
                } else {
                    contactItem = {
                        name: '',
                        phone: '',
                        mobile: '',
                        email: ''
                    }
                    console.log('contactData 为空，使用默认值')
                }
                
                // 调试：打印每个字段的原始值
                console.log('contactItem 对象:', contactItem)
                console.log('contactItem.name 原始值:', contactItem ? contactItem.name : 'contactItem is null')
                console.log('contactItem.phone 原始值:', contactItem ? contactItem.phone : 'contactItem is null')
                console.log('contactItem.mobile 原始值:', contactItem ? contactItem.mobile : 'contactItem is null')
                console.log('contactItem.email 原始值:', contactItem ? contactItem.email : 'contactItem is null')
                
                // 尝试使用 Vue 的响应式数据
                const vm = this
                console.log('通过 vm.contactData 读取:', vm.contactData)
                if (vm.contactData && vm.contactData.length > 0) {
                    console.log('vm.contactData[0]:', vm.contactData[0])
                    // 如果直接读取为空，尝试使用 vm.$data
                    if (!contactItem.name && !contactItem.phone && !contactItem.mobile && !contactItem.email) {
                        console.warn('直接读取为空，尝试使用 vm.$data.contactData')
                        if (vm.$data.contactData && vm.$data.contactData.length > 0) {
                            contactItem = vm.$data.contactData[0]
                            console.log('使用 vm.$data.contactData[0]:', contactItem)
                        }
                    }
                }
                
                // 使用与法定代表人信息相同的数据结构：每个字段一条数据
                // 确保值不为空字符串时才使用，否则使用空字符串
                const getName = () => {
                    const val = contactItem ? contactItem.name : ''
                    console.log('getName() 返回值:', val, '类型:', typeof val)
                    return val ? String(val).trim() : ''
                }
                const getPhone = () => {
                    const val = contactItem ? contactItem.phone : ''
                    console.log('getPhone() 返回值:', val, '类型:', typeof val)
                    return val ? String(val).trim() : ''
                }
                const getMobile = () => {
                    const val = contactItem ? contactItem.mobile : ''
                    console.log('getMobile() 返回值:', val, '类型:', typeof val)
                    return val ? String(val).trim() : ''
                }
                const getEmail = () => {
                    const val = contactItem ? contactItem.email : ''
                    console.log('getEmail() 返回值:', val, '类型:', typeof val)
                    return val ? String(val).trim() : ''
                }
                
                const contactList = [
                    {
                        name: 'contact_name',
                        title: '姓名',
                        value: getName(),
                        type: 'text',
                        sort: 0
                    },
                    {
                        name: 'contact_phone',
                        title: '电话号码',
                        value: getPhone(),
                        type: 'text',
                        sort: 1
                    },
                    {
                        name: 'contact_mobile',
                        title: '手机号码',
                        value: getMobile(),
                        type: 'text',
                        sort: 2
                    },
                    {
                        name: 'contact_email',
                        title: '邮箱',
                        value: getEmail(),
                        type: 'text',
                        sort: 3
                    }
                ]
                
                console.log('最终生成的 contactList:', JSON.stringify(contactList, null, 2))
                
                // 调试：打印生成的 contactList
                console.log('企业联系人信息 contactList:', JSON.stringify(contactList, null, 2))
                
                // 只要有数据就保存，即使值为空也要传递
                if (contactList.length > 0) {
                    saveData.push({
                        parent_id: 3,
                        list: contactList
                    })
                }
                
                // parent_id: 4 - 企业上市情况
                const listingList = this.listingData.flatMap((item, index) => [
                    {
                        name: 'isListed',
                        title: '是否上市',
                        value: item.isListed || '',
                        type: 'text',
                        sort: 0
                    },
                    {
                        name: 'listingType',
                        title: '上市类型',
                        value: item.listingType || '',
                        type: 'text',
                        sort: 1
                    },
                    {
                        name: 'listingTime',
                        title: '上市时间',
                        value: item.listingTime || '',
                        type: 'text',
                        sort: 2
                    },
                    {
                        name: 'stockCode',
                        title: '股票代码',
                        value: item.stockCode || '',
                        type: 'text',
                        sort: 3
                    }
                ])
                if (listingList.length > 0) {
                    saveData.push({
                        parent_id: 4,
                        list: listingList
                    })
                }
                
                // parent_id: 5 - 个人股东和法人股东
                // list 是一个数组，包含 gudong（个人股东）和 faren（法人股东）两个对象
                const parent5List = []
                
                // 个人股东 (gudong)
                if (this.tableData.length > 0) {
                    // 将表格数据转换为数组，每个对象包含所有字段
                    const gudongData = this.tableData.map(item => ({
                        a: item.citizenType || '',
                        b: item.name || '',
                        c: item.idNumber || '',
                        d: item.investment || ''
                    }))
                    
                    // 转换为单引号格式的字符串
                    const gudongValue = JSON.stringify(gudongData).replace(/"/g, "'")
                    
                    parent5List.push({
                        name: 'gudong',
                        title: '',
                        value: gudongValue,
                        type: 'list',
                        sort: 0
                    })
                }
                
                // 法人股东 (faren)
                if (this.legalTableData.length > 0) {
                    // 将表格数据转换为数组，每个对象包含所有字段
                    const farenData = this.legalTableData.map(item => ({
                        a: item.legalType || '',
                        b: item.name || '',
                        c: item.orgCode || '',
                        d: item.investment || ''
                    }))
                    
                    // 转换为单引号格式的字符串
                    const farenValue = JSON.stringify(farenData).replace(/"/g, "'")
                    
                    parent5List.push({
                        name: 'faren',
                        title: '',
                        value: farenValue,
                        type: 'list',
                        sort: 0
                    })
                }
                
                if (parent5List.length > 0) {
                    saveData.push({
                        parent_id: 5,
                        list: parent5List
                    })
                }
                
                // parent_id: 6 - 风险投资
                // 所有风险投资数据放在一个对象中，name 为 "venture_capital"，所有行数据放在 value 里面
                if (this.investmentTableData.length > 0) {
                    // 将表格数据转换为数组，每个对象包含所有字段
                    const ventureCapitalData = this.investmentTableData.map(item => ({
                        a: item.legalType || '',
                        b: item.name || '',
                        c: item.orgCode || '',
                        d: item.investment || ''
                    }))
                    
                    // 转换为单引号格式的字符串
                    const ventureCapitalValue = JSON.stringify(ventureCapitalData).replace(/"/g, "'")
                    
                    const ventureCapitalList = {
                        name: 'venture_capital',
                        title: '',
                        value: ventureCapitalValue,
                        type: 'list',
                        sort: 0
                    }
                    saveData.push({
                        parent_id: 6,
                        list: ventureCapitalList
                    })
                }
                
                const res = await save(saveData)
                
                if (res.code === 1) {
                    this.$message.success('保存成功')
                    
                    // 如果保存了 logo，更新 localStorage 中的 logo，使所有需要显示 logo 的地方都能自动更新
                    if (this.logoUrl) {
                        setEnterpriseLogo(this.logoUrl)
                        // 触发 storage 事件，通知其他组件更新（同源页面）
                        window.dispatchEvent(new StorageEvent('storage', {
                            key: 'enterpriseLogo',
                            newValue: this.logoUrl,
                            storageArea: localStorage
                        }))
                    }
                    
                    // 保存成功后调用 show 接口，重新获取并渲染数据
                    try {
                        await this.fetchData()
                        // 重新获取数据后，再次更新 localStorage 中的 logo（确保使用最新的 URL）
                        if (this.logoUrl) {
                            setEnterpriseLogo(this.logoUrl)
                        }
                    } catch (error) {
                        console.error('重新获取数据失败:', error)
                        // 即使重新获取失败，也更新初始数据
                        this.initialTableData = JSON.parse(JSON.stringify(this.tableData))
                        this.initialLegalTableData = JSON.parse(JSON.stringify(this.legalTableData))
                        this.initialInvestmentTableData = JSON.parse(JSON.stringify(this.investmentTableData))
                        this.initialRegistrationData = JSON.parse(JSON.stringify(this.registrationData))
                        this.initialLegalRepresentativeData = JSON.parse(JSON.stringify(this.legalRepresentativeData))
                        this.initialContactData = JSON.parse(JSON.stringify(this.contactData))
                        this.initialListingData = JSON.parse(JSON.stringify(this.listingData))
                    }
                } else {
                    this.$message.error(res.data?.msg || '保存失败')
                }
            } catch (error) {
                console.error('保存失败:', error)
                this.$message.error('保存失败，请稍后重试')
            }
        },
        handleCancel() {
            // 重置所有表格数据到初始状态
            this.tableData = JSON.parse(JSON.stringify(this.initialTableData))
            this.legalTableData = JSON.parse(JSON.stringify(this.initialLegalTableData))
            this.investmentTableData = JSON.parse(JSON.stringify(this.initialInvestmentTableData))
            this.registrationData = JSON.parse(JSON.stringify(this.initialRegistrationData))
            this.legalRepresentativeData = JSON.parse(JSON.stringify(this.initialLegalRepresentativeData))
            this.contactData = JSON.parse(JSON.stringify(this.initialContactData))
            this.listingData = JSON.parse(JSON.stringify(this.initialListingData))
            this.$message.info('已取消，数据已重置')
        },
        // 处理企业联系人信息输入，确保 Vue 能正确追踪变化
        handleContactInput(field, index, value) {
            // 使用 Vue.set 确保响应式更新
            if (this.contactData && this.contactData[index]) {
                this.$set(this.contactData[index], field, value)
                console.log(`企业联系人信息更新: ${field} = ${value}`, this.contactData[index])
            }
        },
        // 处理法定代表人信息输入，确保 Vue 能正确追踪变化
        handleLegalInput(field, index, value) {
            // 使用 Vue.set 确保响应式更新
            if (this.legalRepresentativeData && this.legalRepresentativeData[index]) {
                this.$set(this.legalRepresentativeData[index], field, value)
                console.log(`法定代表人信息更新: ${field} = ${value}`, this.legalRepresentativeData[index])
            }
        },
        // LOGO上传相关方法
        beforeLogoUpload(file) {
            const isImage = file.type.indexOf('image/') === 0
            const isLt2M = file.size / 1024 / 1024 < 2

            if (!isImage) {
                this.$message.error('只能上传图片文件!')
                return false
            }
            if (!isLt2M) {
                this.$message.error('图片大小不能超过 2MB!')
                return false
            }
            return true
        },
        async handleLogoUpload(options) {
            // 释放旧的 blob URL，避免内存泄漏
            if (this.logoUrl && this.logoUrl.startsWith('blob:')) {
                URL.revokeObjectURL(this.logoUrl)
            }
            try {
                // 创建 FormData，参数名是 image
                const formData = new FormData()
                formData.append('image', options.file)
                
                // 调用上传接口
                const res = await images(formData)
                
                if (res && res.code === 1) {
                    // 根据实际接口返回的字段设置图片URL，使用 host + path
                    const fileData = res.data?.data || res.data
                    if (fileData && fileData.host && fileData.path) {
                        // 保存 host
                        this.logoHost = fileData.host
                        // 确保 host 以 / 结尾，path 不以 / 开头
                        const host = fileData.host.endsWith('/') ? fileData.host : fileData.host + '/'
                        const path = fileData.path.startsWith('/') ? fileData.path.slice(1) : fileData.path
                        this.logoUrl = host + path
                    } else {
                        this.logoUrl = res.data?.url || res.data || res.url
                    }
                    // 上传成功后立即更新 localStorage 中的 logo，使所有需要显示 logo 的地方都能自动更新
                    if (this.logoUrl) {
                        setEnterpriseLogo(this.logoUrl)
                        // 触发 storage 事件，通知其他组件更新（同源页面）
                        window.dispatchEvent(new StorageEvent('storage', {
                            key: 'enterpriseLogo',
                            newValue: this.logoUrl,
                            storageArea: localStorage
                        }))
                    }
                    this.$message.success('LOGO上传成功')
                } else {
                    this.$message.error(res?.msg || 'LOGO上传失败')
                }
            } catch (error) {
                console.error('LOGO上传失败:', error)
                this.$message.error('LOGO上传失败，请稍后重试')
            }
        },
        handleLogoExceed(files, fileList) {
            // 当超过限制时，清除旧文件并上传新文件
            this.$refs.logoUpload.clearFiles()
            const file = files[files.length - 1]
            // 验证文件
            if (this.beforeLogoUpload(file)) {
                this.handleLogoUpload({ file })
            }
        },
        triggerLogoUpload() {
            // 触发LOGO上传，先清除已上传的文件
            this.$refs.logoUpload.clearFiles()
            const input = this.$refs.logoUpload.$el.querySelector('input[type="file"]')
            if (input) {
                input.click()
            }
        },
        // 营业执照上传相关方法
        beforeLicenseUpload(file) {
            const isImage = file.type.indexOf('image/') === 0
            const isLt2M = file.size / 1024 / 1024 < 2

            if (!isImage) {
                this.$message.error('只能上传图片文件!')
                return false
            }
            if (!isLt2M) {
                this.$message.error('图片大小不能超过 2MB!')
                return false
            }
            return true
        },
        async handleLicenseUpload(options) {
            // 释放旧的 blob URL，避免内存泄漏
            if (this.licenseUrl && this.licenseUrl.startsWith('blob:')) {
                URL.revokeObjectURL(this.licenseUrl)
            }
            try {
                // 创建 FormData，参数名是 image
                const formData = new FormData()
                formData.append('image', options.file)
                
                // 调用上传接口
                const res = await images(formData)
                
                if (res && res.code === 1) {
                    // 根据实际接口返回的字段设置图片URL，使用 host + path
                    const fileData = res.data?.data || res.data
                    if (fileData && fileData.host && fileData.path) {
                        // 保存 host
                        this.licenseHost = fileData.host
                        // 确保 host 以 / 结尾，path 不以 / 开头
                        const host = fileData.host.endsWith('/') ? fileData.host : fileData.host + '/'
                        const path = fileData.path.startsWith('/') ? fileData.path.slice(1) : fileData.path
                        this.licenseUrl = host + path
                    } else {
                        this.licenseUrl = res.data?.url || res.data || res.url
                    }
                    this.$message.success('营业执照上传成功')
                } else {
                    this.$message.error(res?.msg || '营业执照上传失败')
                }
            } catch (error) {
                console.error('营业执照上传失败:', error)
                this.$message.error('营业执照上传失败，请稍后重试')
            }
        },
        handleLicenseExceed(files, fileList) {
            // 当超过限制时，清除旧文件并上传新文件
            this.$refs.licenseUpload.clearFiles()
            const file = files[files.length - 1]
            // 验证文件
            if (this.beforeLicenseUpload(file)) {
                this.handleLicenseUpload({ file })
            }
        },
        triggerLicenseUpload() {
            // 触发营业执照上传，先清除已上传的文件
            this.$refs.licenseUpload.clearFiles()
            const input = this.$refs.licenseUpload.$el.querySelector('input[type="file"]')
            if (input) {
                input.click()
            }
        }
    }
}
</script>
<style scoped>
.heng{
    width: 100%;
    height: 16px;
    background: #f8f8f8;
}
.btn-areas{
    display: flex;
    justify-content: end;
    padding-bottom: 12px;
}
.table-areas {
  width: 96%;
  margin: 0 auto;
  padding-top: 18px;
}
.content{
  background: white;
  width: 100%;
  margin-top: 12px;
}

.table-select {
    width: 100%;
}

.table-input {
    width: 100%;
}

::v-deep .table-select .el-input__inner {
    border: 1px solid #DCDFE6 !important;
}

::v-deep .table-input .el-input__inner {
    border: 1px solid #DCDFE6 !important;
}

.footer-buttons {
    margin: 0 auto;
    padding: 20px 0;
    display: flex;
    justify-content: center;
    gap: 12px;
    box-shadow: 0px 0px 4px 1px #3333;
}

.save-btn {
    background-color: #5165f7;
    border-color: #5165f7;
    color: white;
    padding: 10px 30px;
}

.cancel-btn {
    background: white;
    border: 1px solid #DCDFE6;
    color: #606266;
    padding: 10px 30px;
}

/* 基本信息区域样式 */
.basic-info-area {
    width: 96%;
    margin: 0 auto;
    padding: 18px 0;
}

.info-title {
    font-size: 16px;
    font-weight: 500;
    color: #363636;
    margin-bottom: 20px;
}

.upload-section {
    display: flex;
    gap: 40px;
}

.upload-item {
    display: flex;
    align-items: center;
    gap: 12px;
}

.upload-icon {
    width: 80px;
    height: 80px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.logo-icon {
    background-color: #5165f7;
}

.logo-text {
    color: white;
    font-size: 36px;
    font-weight: bold;
    font-family: Arial, sans-serif;
}

.license-icon {
    background-color: #F5E6D3;
}

.license-grid {
    width: 100%;
    height: 100%;
    background-image: 
        linear-gradient(to right, rgba(200, 200, 200, 0.3) 1px, transparent 1px),
        linear-gradient(to bottom, rgba(200, 200, 200, 0.3) 1px, transparent 1px);
    background-size: 20px 20px;
}

.uploaded-image {
    width: 80px;
    height: 80px;
    border-radius: 8px;
    object-fit: cover;
}

.upload-info {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.upload-label {
    font-size: 14px;
    color: #363636;
    font-weight: 500;
}

.re-upload-btn {
    color: #5165f7;
    padding: 0;
    font-size: 14px;
}

.re-upload-btn:hover {
    color: #5165f7;
}

::v-deep .logo-uploader .el-upload,
::v-deep .license-uploader .el-upload {
    border: none;
    width: 80px;
    height: 80px;
}

::v-deep .logo-uploader .el-upload:hover,
::v-deep .license-uploader .el-upload:hover {
    opacity: 0.8;
}

/* 注册信息区域样式 */
.registration-info-area {
    width: 96%;
    margin: 0 auto;
    padding: 18px 0;
}

/* 法定代表人信息区域样式 */
.legal-representative-area {
    width: 96%;
    margin: 0 auto;
    padding: 18px 0;
}

.registration-table {
    border: 1px solid #DCE0FD;
}

.content-areas {
    display: flex;
    height: 50px;
    flex-wrap: wrap;
}

.content-areas:first-child .contents-left,
.content-areas:first-child .contents-right {
    border-top: 1px solid #DCE0FD;
}

.contents-left {
    width: 30%;
    border-bottom: 1px solid #DCE0FD;
    border-right: 1px solid #DCE0FD;
    background: #EDEFFE;
    line-height: 50px;
    font-size: 14px;
    color: #363636;
    padding-left: 16px;
}

.content-areas:last-child .contents-left,
.content-areas:last-child .contents-right {
    border-bottom: none;
}
.contents-left{
    width: 12%;
    border: 1px solid #DCE0FD;
    background: #EDEFFE;;
    line-height: 50px;
    font-size: 14px;
    color: #363636;
    
}
.contents-right{
    flex: 1;
    border: 1px solid #DCE0FD;
    line-height: 50px;
    font-size: 14px;
    color: #363636;
    display: flex
}
.content-areas{
    display: flex;
    height: 50px;
    flex-wrap: wrap;
    margin-top: 12px;
}
.contents-right {
    flex: 1;
    border-bottom: 1px solid #DCE0FD;
    line-height: 50px;
    font-size: 14px;
    color: #363636;
    display: flex;
    align-items: center;
}

.registration-input {
    width: 100%;
    height: 100%;
}

.registration-select {
    width: 100%;
}

::v-deep .registration-input .el-input__wrapper {
    box-shadow: none !important;
    padding-left: 7px !important;
    background: none !important;
    height: 100%;
}

::v-deep .registration-input .el-input__inner {
    padding-left: 7px !important;
    border: none !important;
    background: none !important;
    height: 100%;
    line-height: 50px;
}

::v-deep .registration-select .el-input__wrapper {
    box-shadow: none !important;
    padding-left: 7px !important;
    background: none !important;
    height: 100%;
}

::v-deep .registration-select .el-input__inner {
    padding-left: 7px !important;
    border: none !important;
    background: none !important;
    height: 100%;
    line-height: 50px;
}
</style>