<template>
    <div>
        <div class="headers">
            <div class="headers-left">
                <div class="imgs">
                    <img v-if="enterpriseLogo" :src="enterpriseLogo" alt="企业LOGO" class="enterprise-logo-img" />
                </div>
                <div class="img-right">
                    <div class="name-title">微育科技</div>
                    <div class="right-name">数智科创 · 通达未来 </div>
                </div>
            </div>
            <div class="headers-right">
                <div class="rights">
                  

                        {{ titleName }}
                   

                </div>
            </div>
        </div>
        
        <div class="contents">
            <div class="content-left">
                
                <div class="content-lefts">
                    <div class="leftss">

                        

                        <div class="leftss-header">
                            <div>发明专利编辑</div>
                            <div @click="btnEdint"> <el-button size="small"
                                    style="background-color: #5165f7;color:white;">保存提交</el-button>
                                    <el-button size="small"
                                    @click="closeguan"
                                    style="width: 82px;">取消</el-button>
                            </div>
                        </div>
                        <div class="table-area">
                            <div class="table-header">
                                <div class="names">
                                    <span class="times">上传凭证｜<span class="namess">上传《产权证书》后可对其进行数据解析，解析结果自动填充</span> </span>
                                </div>
                             
                                
                            </div>
                           <div class="tab-area">
                            <el-table :data="tableData2" border >
                                <el-table-column prop="date" label="产权证书" align="center">
                                    <template #default>
                                        <el-upload
                                    :http-request="(options) => handleUpload(options, 0)"
                                    list-type="picture-card"
                                    :file-list="fileListMap[0]"
                                    :on-preview="handlePictureCardPreview"
                                    :on-remove="(file, fileList) => handleRemove(file, fileList, 0)">
                                    <i class="el-icon-plus"></i>
                                    <div class="shangchuang">上传材料</div>
                                    </el-upload>
                                    <el-dialog :visible.sync="dialogVisible">
                                    <img width="100%" :src="dialogImageUrl" alt="">
                                    </el-dialog>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="name" label="缴费证明（最近一次）" align="center">
                                    <el-upload
                                    :http-request="(options) => handleUpload(options, 1)"
                                    list-type="picture-card"
                                    :file-list="fileListMap[1]"
                                    :on-preview="handlePictureCardPreview"
                                    :on-remove="(file, fileList) => handleRemove(file, fileList, 1)">
                                    <i class="el-icon-plus"></i>
                                    <div class="shangchuang">上传材料</div>
                                    </el-upload>
                                    <el-dialog :visible.sync="dialogVisible">
                                    <img width="100%" :src="dialogImageUrl" alt="">
                                    </el-dialog>
                                </el-table-column>
                                <el-table-column prop="name" label="变更证明（若有请上传）" align="center">
                                    <el-upload
                                    :http-request="(options) => handleUpload(options, 2)"
                                    list-type="picture-card"
                                    :file-list="fileListMap[2]"
                                    :on-preview="handlePictureCardPreview"
                                    :on-remove="(file, fileList) => handleRemove(file, fileList, 2)">
                                    <i class="el-icon-plus"></i>
                                    <div class="shangchuang">上传材料</div>
                                    </el-upload>
                                    <el-dialog :visible.sync="dialogVisible">
                                    <img width="100%" :src="dialogImageUrl" alt="">
                                    </el-dialog>
                                </el-table-column>
                            </el-table>
                           </div>
                        </div>
                        
                        <div class="table-area" style="margin-top: 30px;">
                            <div class="table-header">
                                <div>
                                    <span class="times"> 产权信息｜ </span>
                                </div>
                                <div style="display: flex;">
                                    <div class="youxiaos">
                                        <el-tooltip class="item" effect="dark"
                                            content="注意！设置“无效”，设置无效后改内容仅保留展但不做应用，默认有效" placement="top-start">
                                            <span> <!-- 单个根元素 -->
                                                <i class="el-icon-info" style="margin-right: 5px;"></i>
                                                <span class="youxiao">有效性设置</span>
                                            </span>
                                        </el-tooltip>

                                    </div>
                                    <div class="yuan">
                                        <div class="yuan-left" v-for="(item, index) in tables" :key="index" :class="{
                                            'yuanright': yuanright === index,
                                            'left-radius': yuanright === 0 && index === 0,
                                            'right-radius': yuanright === 1 && index === 1
                                        }" @click="btnYuan(index)">
                                            {{ item.name }}
                                        </div>


                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="content-table">
                       <!-- 动态渲染字段 -->
                       <template v-for="(field, index) in fieldConfig.fields">
                         <!-- 普通单行字段 -->
                         <div v-if="!field.split && field.type !== 'select' && field.condition !== 'transferor'" 
                              :key="`field-${index}`"
                              class="content-areas" 
                              :style="{ 'position': 'relative', 'top': index > 0 ? '5px' : '0' }">
                           <div class="contents-left">
                             <span style="margin-left: 20px;">{{ field.label }}</span>
                           </div>
                           <div class="contents-right" :style="{ 'border-top': index === 0 ? '1px solid #DCE0FD' : 'none' }">
                             <span style="margin-left: 20px;">
                               <el-input v-if="field.type === 'input' || field.type === 'text'" 
                                        v-model="formData[field.key]" 
                                        placeholder="请输入内容"
                                        style="width: 100%;"></el-input>
                               <el-date-picker v-else-if="field.type === 'date'"
                                              style="width: 100%;"
                                              v-model="formData[field.key]"
                                              placeholder="选择日期时间">
                               </el-date-picker>
                             </span>
                           </div>
                         </div>
                         
                         <!-- 获得方式下拉字段 -->
                         <div v-else-if="field.key === 'acquisitionMethod'" 
                              :key="`acquisition-${index}`"
                              class="content-areas" 
                              :style="{ 'position': 'relative', 'top': index > 0 ? '5px' : '0' }">
                           <div class="contents-left">
                             <span style="margin-left: 20px;">{{ field.label }}</span>
                           </div>
                           <div class="contents-right" :style="{ 'border-top': index === 0 ? '1px solid #DCE0FD' : 'none' }">
                             <span style="margin-left: 20px;">
                               <el-select v-model="formData.acquisitionMethod" 
                                         placeholder="请选择" 
                                        >
                                 <el-option
                                   v-for="item in acquisitionMethodOptions"
                                   :key="item.value"
                                   :label="item.label"
                                   :value="item.value">
                                 </el-option>
                               </el-select>
                             </span>
                           </div>
                         </div>
                         
                         <!-- 转让方字段（条件显示） -->
                         <div v-else-if="field.condition === 'transferor' && showTransferor" 
                              :key="`transferor-${index}`"
                              class="content-areas" 
                              :style="{ 'position': 'relative', 'top': '5px' }">
                           <div class="contents-left">
                             <span style="margin-left: 20px;">{{ field.label }}</span>
                           </div>
                           <div class="contents-right">
                             <span style="margin-left: 20px;">
                               <el-input v-model="formData.transferor" 
                                        placeholder="请输入转让方机构名称"></el-input>
                             </span>
                           </div>
                         </div>
                         
                         <!-- 三列分割字段（授权公告号+专利号） -->
                         <div v-else-if="field.split && field.split[0] === 'patentNumber'" 
                              :key="`patent-${index}`"
                              class="content-areas content-areas2">
                           <div class="contents-left">
                             <span class="table-name">{{ field.label }}</span>
                           </div>
                           <div class="contents-right">
                             <div class="rightss rightss1">
                               <span class="table-name">
                                 <el-input v-model="formData[field.key]" 
                                          placeholder="请输入" 
                                          style="width: 90%;"></el-input>
                               </span>
                             </div>
                             <div class="rightss rightss2">
                               <span class="table-name">{{ field.split[1] }}</span>
                             </div>
                             <div class="rightss rightss3">
                               <span class="table-name">
                                 <el-input v-model="formData.patentNumber" 
                                          placeholder="请输入" 
                                          style="width: 90%;"></el-input>
                               </span>
                             </div>
                           </div>
                         </div>
                         
                         
                         <!-- 三列分割字段（下年度缴费日期+年费缴纳情况） -->
                         <div v-else-if="field.split && field.split[0] === 'annualFeeStatus'" 
                              :key="`annual-${index}`"
                              class="content-areas content-areas2" 
                              style="top: 7px;">
                           <div class="contents-left" style="border-top: none;">
                             <span class="table-name">{{ field.label }}</span>
                           </div>
                           <div class="contents-right" style="border: none;">
                             <div class="rightss rightss1">
                               <span class="table-name">
                                 <el-date-picker
                                   style="width: 88%;"
                                   v-model="formData.nextPaymentDate"
                                   placeholder="选择日期时间">
                                 </el-date-picker>
                               </span>
                             </div>
                             <div class="rightss rightss2" style="border-top: none;">
                               <span class="table-name">{{ field.split[1] }}</span>
                             </div>
                             <div class="rightss rightss3">
                               <span class="table-name">
                                 <el-select v-model="formData.annualFeeStatus" 
                                          placeholder="请选择" 
                                          style="width: 88%;">
                                   <el-option
                                     v-for="item in annualFeeOptions"
                                     :key="item.value"
                                     :label="item.label"
                                     :value="item.value">
                                   </el-option>
                                 </el-select>
                               </span>
                             </div>
                           </div>
                         </div>
                       </template>
                    </div>
                    
                </div>
             
            </div>
            <div class="content-right">

                <div class="leftss-header right-header">
                    <div class="header-area">
                        <div class="area-name" v-for="(item,index) in listDate" 
                        :key="index"
                        @click="btnNmae(index)"
                        :class="{'active':active ==index}"
                        >
                            {{item.name }}
                        </div>
                      
                    </div>
                    <div> <el-button size="small" style="color:#5165f7;">
                           解析中</el-button>
                    </div>
                </div>
                <div class="right-area">
                    <div class="preview-images" v-if="currentFileList && currentFileList.length > 0">
                        <div class="image-item" v-for="(file, index) in currentFileList" :key="file.uid || index">
                            <img :src="file.url" alt="上传的图片" @click="handlePictureCardPreview(file)" style="max-width: 100%; cursor: pointer;" />
                        </div>
                    </div>
                    <div class="no-image-tip" v-else>
                        <span>暂无上传图片，请在左侧上传材料</span>
                    </div>
                </div>

            </div>
        </div>

        <!-- 保存成功对话框 -->
        <el-dialog
            :visible.sync="saveSuccessDialog"
            width="450px"
            :show-close="false"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            class="save-success-dialog">
            <div class="success-dialog-content">
                <div class="success-icon-wrapper">
                   <img src="@/assets/succes.png" alt="">
                </div>
                <div class="success-title">提交保存成功</div>
                <div class="success-message">
                    <p>提交内容未审核通过时资源资料库不展示该内容</p>
                    <p>审核状态请前往审核中心查看</p>
                </div>
                <div class="success-footer">
                    <el-button type="primary" @click="closeSuccessDialog" style="background-color: #5165f7;width: 100px;">知道了</el-button>
                </div>
            </div>
        </el-dialog>

        <!-- 保存失败对话框 -->
        <el-dialog
            :visible.sync="saveFailDialog"
            width="450px"
            :show-close="false"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            class="save-fail-dialog">
            <div class="fail-dialog-content">
                <div class="fail-icon-wrapper">
                   <img src="@/assets/err.png" alt="">
                </div>
                <div class="fail-title">提交失败</div>
                <div class="fail-message">
                    <p>{{ errorMessage || '提交失败原因描述' }}</p>
                </div>
                <div class="fail-footer">
                    <el-button type="primary" @click="closeFailDialog" style="background-color: #5165f7;width: 100px;">知道了</el-button>
                </div>
            </div>
        </el-dialog>

    </div>
</template>
<script>
import { show } from '@/api/homepage/corporateInformation'
import { getEnterpriseLogo } from '@/utils/auth'
import { images } from '@/api/public'
import{save} from '@/api/resource/achievement/knowledge'
export default {
    data() {
        return {
            enterpriseLogo: '', // 企业LOGO图片URL
            type: null, // 知识产权类型
            titleName: '发明专利编辑', // 标题名称
            typeNameMap: {
                1: '发明专利编辑',
                2: '植物新品种编辑',
                3: '国家新药编辑',
                4: '集成电路布图设计专有权编辑',
                5: '实用新型专利编辑',
                6: '软件著作权编辑',
                7: '外观设计专利编辑',
                8: '商标信息编辑'
            },
            // 类型到名称的映射（用于保存时的invention_name）
            typeToNameMap: {
                1: '发明专利',
                2: '植物新品种',
                3: '国家新药',
                4: '集成电路布图',
                5: '实用新型',
                6: '软件著作权',
                7: '外观设计',
                8: '商标信息'
            },
            active:0,
            listDate:[
                {
                    name:'产权证书'
                },
                {
                      name:'缴费证明'
                },
                {
                      name:'变更证明'
                }
            ],
            value1:"",
            yuanright: 0,
            tables: [
                {
                    name: '有效'
                },
                {
                    name: '无效'
                },
            ],
            shu: 0,
           
            lastActiveKey: 'headerActiveIndex',
            tableData: [
                {
                    date: "其中",
                    name: "",
                    address: "",
                    children: [
                        { date: "2024", name: "年度审计报告及财务情况说明书", address: "8000" },
                        { date: "2025", name: "年度审计报告及财务情况说明书", address: "7500" },
                      
                    ],
                },
                { date: "其他项目", name: "5000", address: "4000" },
            ],

            tableData2: [
       
            { date: "2025", name: "年度审计报告及财务情况说明书", address: "7500" },],
            // 上传相关
            fileListMap: {
                0: [], // 产权证书的文件列表
                1: [], // 缴费证明的文件列表
                2: []  // 变更证明的文件列表
            },
            dialogVisible: false, // 图片预览对话框
            dialogImageUrl: '', // 预览图片URL
            saveSuccessDialog: false, // 保存成功对话框显示状态
            saveFailDialog: false, // 保存失败对话框显示状态
            errorMessage: '', // 错误信息
            // 获得方式选项
            acquisitionMethodOptions: [
                { label: '自主研发', value: '自主研发', code: 1 },
                { label: '受让', value: '受让', code: 2 },
                { label: '受赠', value: '受赠', code: 3 },
                { label: '并购', value: '并购', code: 4 }
            ],
            // 年费缴纳情况选项
            annualFeeOptions: [
                { label: '已缴纳', value: '已缴纳', code: 1 },
                { label: '未缴纳', value: '未缴纳', code: 0 },
                { label: '待缴纳', value: '待缴纳', code: 2 }
            ],
            // 表单数据
            formData: {
                // 发明专利/实用新型/外观设计通用字段
                inventionName: '', // 发明名称/外观设计名称
                authorizationNumber: '', // 授权公告号
                patentNumber: '', // 专利号
                authorizationDate: '', // 授权公告日
                acquisitionMethod: '', // 获得方式
                transferor: '', // 转让方
                patentee: '', // 专利权人
                inventor: '', // 发明人/设计人
                address: '', // 地址
                nextPaymentDate: '', // 下年度缴费日期
                annualFeeStatus: '', // 年费缴纳情况
                // 软件著作权字段
                softwareName: '', // 软件名称
                registrationNumber: '', // 登记号
                completionDate: '', // 开发完成日期
                firstReleaseDate: '', // 首次发布日期
                copyrightOwner: '', // 著作权人
                // 集成电路布图字段
                layoutDesignName: '', // 布图设计名称
                rightHolder: '', // 权利人的姓名或者名称
                firstCommercialUseDate: '', // 首次商业利用的时间
                applicationDate: '', // 申请日
                // 国家新药字段
                drugName: '', // 药品名称
                certificateNumber: '', // 证书编号
                mainIngredients: '', // 主要成分
                holder: '', // 持有者
                // 植物新品种字段
                varietyName: '', // 品种名称
                varietyRightNumber: '', // 品种权号
                varietyRightHolder: '', // 品种权人
                genusOrSpecies: '', // 属或者种
                breeder: '', // 培育人
                authorizationDate2: '', // 授权日
                // 商标信息字段
                trademarkName: '', // 商标名称
                internationalClassification: '', // 国际分类
                applicationRegistrationNumber: '', // 申请/注册号
                registrationDate: '' // 注册日期
            }
        }
    },

    computed: {
        // 根据当前激活的tab返回对应的文件列表
        currentFileList() {
            return this.fileListMap[this.active] || []
        },
        // 根据知识产权类型返回字段配置
        fieldConfig() {
            const configs = {
                1: { // 发明专利
                    fields: [
                        { key: 'inventionName', label: '发明名称', type: 'text' },
                        { key: 'authorizationNumber', label: '授权公告号', type: 'text', split: ['patentNumber', '专利号'] },
                        { key: 'authorizationDate', label: '授权公告日', type: 'date' },
                        { key: 'acquisitionMethod', label: '获得方式', type: 'select' },
                        { key: 'transferor', label: '转让方', type: 'input', condition: 'transferor' },
                        { key: 'patentee', label: '专利权人', type: 'text' },
                        { key: 'inventor', label: '发明人', type: 'text' },
                        { key: 'address', label: '地址', type: 'text' },
                        { key: 'nextPaymentDate', label: '下年度年费缴纳日期', type: 'date', split: ['annualFeeStatus', '年费缴纳情况', 'select'] }
                    ]
                },
                5: { // 实用新型
                    fields: [
                        { key: 'inventionName', label: '实用新型名称', type: 'text' },
                        { key: 'authorizationNumber', label: '授权公告号', type: 'text', split: ['patentNumber', '专利号'] },
                        { key: 'authorizationDate', label: '授权公告日', type: 'date' },
                        { key: 'acquisitionMethod', label: '获得方式', type: 'select' },
                        { key: 'transferor', label: '转让方', type: 'input', condition: 'transferor' },
                        { key: 'patentee', label: '专利权人', type: 'text' },
                        { key: 'inventor', label: '发明人', type: 'text' },
                        { key: 'address', label: '地址', type: 'text' },
                        { key: 'nextPaymentDate', label: '下年度年费缴纳日期', type: 'date', split: ['annualFeeStatus', '年费缴纳情况', 'select'] }
                    ]
                },
                6: { // 软件著作权
                    fields: [
                        { key: 'softwareName', label: '软件名称', type: 'text' },
                        { key: 'registrationNumber', label: '登记号', type: 'text' },
                        { key: 'completionDate', label: '开发完成日期', type: 'date' },
                        { key: 'firstReleaseDate', label: '首次发布日期', type: 'date' },
                        { key: 'acquisitionMethod', label: '获得方式', type: 'select' },
                        { key: 'transferor', label: '转让方', type: 'input', condition: 'transferor' },
                        { key: 'copyrightOwner', label: '著作权人', type: 'text' }
                    ]
                },
                4: { // 集成电路布图
                    fields: [
                        { key: 'layoutDesignName', label: '布图设计名称', type: 'text' },
                        { key: 'rightHolder', label: '权利人的姓名或者名称', type: 'text' },
                        { key: 'firstCommercialUseDate', label: '首次商业利用的时间', type: 'date' },
                        { key: 'applicationDate', label: '申请日', type: 'date' },
                        { key: 'registrationNumber', label: '登记号', type: 'text' },
                        { key: 'acquisitionMethod', label: '获得方式', type: 'select' },
                        { key: 'transferor', label: '转让方', type: 'input', condition: 'transferor' },
                        { key: 'nextPaymentDate', label: '下年度年费缴纳日期', type: 'date', split: ['annualFeeStatus', '年费缴纳情况', 'select'] }
                    ]
                },
                7: { // 外观设计
                    fields: [
                        { key: 'inventionName', label: '外观设计名称', type: 'text' },
                        { key: 'authorizationNumber', label: '授权公告号', type: 'text', split: ['patentNumber', '专利号'] },
                        { key: 'authorizationDate', label: '授权公告日', type: 'date' },
                        { key: 'acquisitionMethod', label: '获得方式', type: 'select' },
                        { key: 'transferor', label: '转让方', type: 'input', condition: 'transferor' },
                        { key: 'patentee', label: '专利权人', type: 'text' },
                        { key: 'address', label: '地址', type: 'text' },
                        { key: 'inventor', label: '设计人', type: 'text' },
                        { key: 'nextPaymentDate', label: '下年度年费缴纳日期', type: 'date', split: ['annualFeeStatus', '年费缴纳情况', 'select'] }
                    ]
                },
                3: { // 国家新药
                    fields: [
                        { key: 'drugName', label: '药品名称', type: 'text' },
                        { key: 'certificateNumber', label: '证书编号', type: 'text' },
                        { key: 'mainIngredients', label: '主要成分', type: 'text' },
                        { key: 'holder', label: '持有者(可能包含多个)', type: 'text' },
                        { key: 'acquisitionMethod', label: '获得方式', type: 'select' },
                        { key: 'nextPaymentDate', label: '下年度年费缴纳日期', type: 'date', split: ['annualFeeStatus', '年费缴纳情况', 'select'] }
                    ]
                },
                2: { // 植物新品种
                    fields: [
                        { key: 'varietyName', label: '品种名称', type: 'text' },
                        { key: 'varietyRightNumber', label: '品种权号', type: 'text' },
                        { key: 'varietyRightHolder', label: '品种权人', type: 'text' },
                        { key: 'genusOrSpecies', label: '属或者种', type: 'text' },
                        { key: 'breeder', label: '培育人', type: 'text' },
                        { key: 'authorizationDate2', label: '授权日', type: 'date' },
                        { key: 'acquisitionMethod', label: '获得方式', type: 'select' },
                        { key: 'nextPaymentDate', label: '下年度年费缴纳日期', type: 'date', split: ['annualFeeStatus', '年费缴纳情况', 'select'] }
                    ]
                },
                8: { // 商标信息
                    fields: [
                        { key: 'trademarkName', label: '商标名称', type: 'text' },
                        { key: 'internationalClassification', label: '国际分类', type: 'text' },
                        { key: 'applicationRegistrationNumber', label: '申请/注册号', type: 'text' },
                        { key: 'registrationDate', label: '注册日期', type: 'date' },
                        { key: 'acquisitionMethod', label: '获得方式', type: 'select' },
                        { key: 'nextPaymentDate', label: '下年度年费缴纳日期', type: 'date', split: ['annualFeeStatus', '年费缴纳情况', 'select'] }
                    ]
                }
            }
            return configs[this.type] || configs[1] // 默认返回发明专利配置
        },
        // 是否显示转让方字段
        showTransferor() {
            const method = this.formData.acquisitionMethod
            return method === '受让' || method === '受赠' || method === '并购'
        }
    },

    created() {
        this.loadEnterpriseLogo()
        // 从路由参数获取类型
        this.type = this.$route.query.type ? parseInt(this.$route.query.type) : 1
        // 根据类型设置标题
        this.titleName = this.typeNameMap[this.type] || '发明专利编辑'
    },

    watch: {

    },

    methods: {
        // 从localStorage加载企业LOGO
        loadEnterpriseLogo() {
            const logo = getEnterpriseLogo()
            if (logo) {
                this.enterpriseLogo = logo
            }
        },
        // 获取企业LOGO（保留作为备用方法，如果localStorage没有则调用API）
        async fetchEnterpriseLogo() {
            // 先尝试从localStorage读取
            const logo = getEnterpriseLogo()
            if (logo) {
                this.enterpriseLogo = logo
                return
            }
            try {
                const res = await show({})
                if (res && res.code === 1 && res.data) {
                    // 数据结构可能是对象（键是数字字符串）或数组
                    let dataToProcess = res.data
                    
                    // 如果是数组，转换为对象格式以便统一处理
                    if (Array.isArray(res.data)) {
                        dataToProcess = {}
                        res.data.forEach((item, index) => {
                            const parentId = item.parent_id !== undefined ? item.parent_id : index
                            dataToProcess[String(parentId)] = item
                        })
                    }
                    
                    // 查找 parent_id: 0 的基本信息（图片数据）
                    if (dataToProcess['0'] && dataToProcess['0'].list && Array.isArray(dataToProcess['0'].list)) {
                        dataToProcess['0'].list.forEach(imgItem => {
                            if (imgItem.name === 'logo' && imgItem.value) {
                                // 如果 value 是完整 URL，直接使用
                                if (imgItem.value.startsWith('http://') || imgItem.value.startsWith('https://')) {
                                    this.enterpriseLogo = imgItem.value
                                } else if (imgItem.host) {
                                    // 如果有 host，组合完整的 URL
                                    const host = imgItem.host.endsWith('/') ? imgItem.host : imgItem.host + '/'
                                    const path = imgItem.value.startsWith('/') ? imgItem.value.slice(1) : imgItem.value
                                    this.enterpriseLogo = host + path
                                } else {
                                    this.enterpriseLogo = imgItem.value
                                }
                            }
                        })
                    }
                }
            } catch (error) {
                console.error('获取企业LOGO失败:', error)
                // 获取失败时不显示错误提示，保持默认样式
            }
        },
        //取消
        closeguan(){
            this.$router.go('-1')
        },
        btnNmae(e){
            this.active=e
        },
        //切换时效性
        btnYuan(e) {
            this.yuanright = e
        },
        // 保存提交
        async btnEdint() {
            try {
                // 构建保存数据
                const saveData = this.buildSaveData()
                
                // 调用保存接口
                const res = await save(saveData)
                
                if (res && res.code === 1) {
                    // 显示成功对话框
                    this.saveSuccessDialog = true
                } else {
                    // 显示失败对话框，优先使用 message 字段，其次使用 msg 字段
                    this.errorMessage = res?.message || res?.msg || '保存失败，请稍后重试'
                    this.saveFailDialog = true
                }
            } catch (error) {
                console.error('保存失败:', error)
                // 显示失败对话框，优先使用响应中的 message 字段
                const errorData = error?.response?.data || {}
                this.errorMessage = errorData.message || errorData.msg || error.message || '保存失败，请稍后重试'
                this.saveFailDialog = true
            }
        },
        // 关闭成功对话框
        closeSuccessDialog() {
            this.saveSuccessDialog = false
            // 返回上一页
            this.$router.go(-1)
        },
        // 关闭失败对话框
        closeFailDialog() {
            this.saveFailDialog = false
            this.errorMessage = ''
        },
        // 日期格式化函数：将日期转换为 YYYY-MM-DD 格式
        formatDate(date) {
            if (!date) return null
            // 如果是字符串，先转换为Date对象
            const dateObj = typeof date === 'string' ? new Date(date) : date
            // 检查是否为有效日期
            if (isNaN(dateObj.getTime())) return null
            // 格式化为 YYYY-MM-DD
            const year = dateObj.getFullYear()
            const month = String(dateObj.getMonth() + 1).padStart(2, '0')
            const day = String(dateObj.getDate()).padStart(2, '0')
            return `${year}-${month}-${day}`
        },
        // 构建保存数据
        buildSaveData() {
            // 获取获得方式代码
            const acquisitionMethodOption = this.acquisitionMethodOptions.find(
                item => item.value === this.formData.acquisitionMethod
            )
            const acquireMode = acquisitionMethodOption ? acquisitionMethodOption.code : null
            
            // 获取年费缴纳情况代码
            const annualFeeOption = this.annualFeeOptions.find(
                item => item.value === this.formData.annualFeeStatus
            )
            const feeStatus = annualFeeOption ? annualFeeOption.code : 0
            
            // 有效性状态：yuanright 0=有效(status=1), 1=无效(status=0)
            const status = this.yuanright === 0 ? 1 : 0
            
            // 获取文件路径（取第一个文件的path或url）
            const getFilePath = (fileList) => {
                if (fileList && fileList.length > 0) {
                    const file = fileList[0]
                    return file.path || file.url || ''
                }
                return ''
            }
            
            // 根据类型构建不同的数据
            const baseData = {
                type: this.type,
                acquire_mode: acquireMode,
                transferor: this.formData.transferor || null,
                status: status,
                fee_status: feeStatus,
                next_payment_date: this.formatDate(this.formData.nextPaymentDate),
                property_right_file: getFilePath(this.fileListMap[0]),
                payment_file: getFilePath(this.fileListMap[1]),
                change_file: getFilePath(this.fileListMap[2])
            }
            
            // 获取类型对应的名称（用于invention_name字段）
            const inventionName = this.typeToNameMap[this.type] || ''
            
            // 根据类型添加特定字段
            if (this.type === 1 || this.type === 5 || this.type === 7) {
                // 发明专利、实用新型、外观设计
                return {
                    ...baseData,
                    invention_name: inventionName, // 添加类型名称
                    name: this.formData.inventionName || '', // 具体的发明/设计名称
                    grant_notice_no: this.formData.authorizationNumber || '',
                    patent_no: this.formData.patentNumber || '',
                    grant_date: this.formatDate(this.formData.authorizationDate),
                    patentee: this.formData.patentee || '',
                    inventor: this.formData.inventor || '',
                    address: this.formData.address || ''
                }
            } else if (this.type === 6) {
                // 软件著作权
                return {
                    ...baseData,
                    invention_name: inventionName, // 添加类型名称
                    software_name: this.formData.softwareName || '',
                    patent_no: this.formData.registrationNumber || '', // 登记号使用patent_no字段
                    completion_date: this.formatDate(this.formData.completionDate),
                    first_release_date: this.formatDate(this.formData.firstReleaseDate),
                    copyright_owner: this.formData.copyrightOwner || ''
                }
            } else if (this.type === 4) {
                // 集成电路布图
                return {
                    ...baseData,
                    invention_name: inventionName, // 添加类型名称
                    layout_design_name: this.formData.layoutDesignName || '',
                    right_holder: this.formData.rightHolder || '',
                    first_commercial_use_date: this.formatDate(this.formData.firstCommercialUseDate),
                    application_date: this.formatDate(this.formData.applicationDate),
                    patent_no: this.formData.registrationNumber || '' // 登记号使用patent_no字段
                }
            } else if (this.type === 3) {
                // 国家新药
                return {
                    ...baseData,
                    invention_name: inventionName, // 添加类型名称
                    drug_name: this.formData.drugName || '',
                    certificate_number: this.formData.certificateNumber || '',
                    main_ingredients: this.formData.mainIngredients || '',
                    holder: this.formData.holder || ''
                }
            } else if (this.type === 2) {
                // 植物新品种
                return {
                    ...baseData,
                    invention_name: inventionName, // 添加类型名称
                    variety_name: this.formData.varietyName || '',
                    variety_right_number: this.formData.varietyRightNumber || '',
                    variety_right_holder: this.formData.varietyRightHolder || '',
                    genus_or_species: this.formData.genusOrSpecies || '',
                    breeder: this.formData.breeder || '',
                    authorization_date: this.formatDate(this.formData.authorizationDate2)
                }
            } else if (this.type === 8) {
                // 商标信息
                return {
                    ...baseData,
                    invention_name: inventionName, // 添加类型名称
                    trademark_name: this.formData.trademarkName || '',
                    international_classification: this.formData.internationalClassification || '',
                    application_registration_number: this.formData.applicationRegistrationNumber || '',
                    registration_date: this.formatDate(this.formData.registrationDate)
                }
            }
            
            return {
                ...baseData,
                invention_name: inventionName
            }
        },
        btnShang() {
            this.$router.push('/annual')
        },
        // 文件上传
        async handleUpload(options, tabIndex) {
            try {
                // 创建 FormData，参数名是 image
                const formData = new FormData()
                formData.append('image', options.file)
                
                // 调用上传接口
                const res = await images(formData)
                
                if (res && res.code === 1) {
                    // 根据实际接口返回的字段设置图片URL
                    let fileData = null
                    
                    // 按优先级尝试获取数据
                    if (res.data && res.data.data && res.data.data.data) {
                        // 三层嵌套：res.data.data.data
                        fileData = res.data.data.data
                    } else if (res.data && res.data.data) {
                        // 两层嵌套：res.data.data
                        fileData = res.data.data
                    } else if (res.data) {
                        // 一层嵌套：res.data
                        fileData = res.data
                    }
                    
                    let imageUrl = ''
                    let imageHost = ''
                    
                    // 优先使用 fullURL 字段
                    if (fileData && fileData.fullURL) {
                        imageUrl = fileData.fullURL
                        imageHost = fileData.host || ''
                    } else if (fileData && fileData.host && fileData.path) {
                        // 保存 host
                        imageHost = fileData.host
                        // 确保 host 以 / 结尾，path 不以 / 开头
                        const host = fileData.host.endsWith('/') ? fileData.host : fileData.host + '/'
                        const path = fileData.path.startsWith('/') ? fileData.path.slice(1) : fileData.path
                        imageUrl = host + path
                    } else if (fileData && fileData.path) {
                        // 如果只有 path，使用默认 host
                        const defaultHost = 'http://api.sg199.net/'
                        const path = fileData.path.startsWith('/') ? fileData.path.slice(1) : fileData.path
                        imageUrl = defaultHost + path
                        imageHost = defaultHost
                    } else if (fileData && fileData.url) {
                        imageUrl = fileData.url
                        imageHost = ''
                    } else {
                        // 降级处理
                        imageUrl = res.data?.url || res.url || ''
                    }
                    
                    if (!imageUrl) {
                        this.$message.error('无法获取图片URL')
                        return
                    }
                    
                    // 添加到对应tab的文件列表
                    const fileItem = {
                        uid: options.file.uid || Date.now() + Math.random(),
                        name: options.file.name || '上传的图片',
                        url: imageUrl,
                        host: imageHost,
                        path: fileData?.path || '',
                        status: 'success'
                    }
                    
                    // 使用 Vue.set 确保响应式更新
                    this.$set(this.fileListMap, tabIndex, [...this.fileListMap[tabIndex], fileItem])
                    
                    this.$message.success('文件上传成功')
                } else {
                    this.$message.error(res?.msg || '文件上传失败')
                    // 上传失败，移除文件
                    const currentList = this.fileListMap[tabIndex] || []
                    const index = currentList.findIndex(item => item.uid === options.file.uid)
                    if (index > -1) {
                        this.$set(this.fileListMap, tabIndex, currentList.filter((item, i) => i !== index))
                    }
                }
            } catch (error) {
                console.error('文件上传失败:', error)
                this.$message.error('文件上传失败，请稍后重试')
                
                // 上传失败，移除文件
                const currentList = this.fileListMap[tabIndex] || []
                const index = currentList.findIndex(item => item.uid === options.file.uid)
                if (index > -1) {
                    this.$set(this.fileListMap, tabIndex, currentList.filter((item, i) => i !== index))
                }
            }
        },
        // 图片预览
        handlePictureCardPreview(file) {
            this.dialogImageUrl = file.url || (file.raw ? URL.createObjectURL(file.raw) : '')
            this.dialogVisible = true
        },
        // 删除文件
        handleRemove(file, fileList, tabIndex) {
            // 更新对应tab的文件列表
            this.$set(this.fileListMap, tabIndex, fileList)
        }

    }
}
</script>
<style>
.el-tooltip__popper.is-dark {
    color: #F04B4B !important;
    background: #FCF1EB !important;
}

.el-tooltip__popper .popper__arrow {
    border-top-color: #FCF1EB !important;
    /* 上方箭头 */
}

.el-tooltip__popper .popper__arrow::after {
    border-top-color: #FCF1EB !important;
}

/* 其他方向的箭头（按需添加） */
.el-tooltip__popper[x-placement^='bottom'] .popper__arrow {
    border-bottom-color: #FCF1EB !important;
}

.el-tooltip__popper[x-placement^='left'] .popper__arrow {
    border-left-color: #FCF1EB !important;
}

.el-tooltip__popper[x-placement^='right'] .popper__arrow {
    border-right-color: #FCF1EB !important;
}
</style>
<style scoped>
.area-name{
margin-left: 30px;
cursor: pointer;
}
.active{
   
    border-bottom: 3px solid #5165f7;
    height: 45px;
}
.header-area{
    display: flex;
    position: relative;
    left: -30px;
}
.rightss3{
    border-right: none;
    border-left: none;
}
.table-name{
    margin-left: 20px;
}
.rightss2{
    background: #EDEFFE;
}
.rightss1{
    border-left: none !important;
    border-right: none !important;
   
}
.rightss{
    height: 100%;
    border: 1px solid #DCE0FD;
    width: 100%;
    border-top: none;

}
.content-areas2{
    position: relative;
    top: 1px;

}
.contents-right{
    flex: 1;
    border: 1px solid #DCE0FD;
    line-height: 50px;
    font-size: 14px;
    color: #363636;
    display: flex
}
.contents-left{
    width: 30%;
    border: 1px solid #DCE0FD;
    background: #EDEFFE;;
    line-height: 50px;
    font-size: 14px;
    color: #363636;
    
}
.content-areas{
    display: flex;
    height: 50px;
    flex-wrap: wrap;
}
.tab-area{
    margin-top: 24px;
}
.namess{
    color: #5B5E72;
    font-size: 13px;
    margin-left: 12px;
}
.names{
    font-size: 14px;
    color: #363636;
}
:deep .el-tooltip__popper.is-dark {
    color: #F04B4B !important;
}

.right-area {
    width: 90%;
    margin: 0 auto;
    height: 80vh;
    background: #f8f8f8;
    overflow-y: auto;
    padding: 20px;
    box-sizing: border-box;
}

.preview-images {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.image-item {
    width: 200px;
    height: 200px;
    border: 1px solid #ddd;
    border-radius: 8px;
    overflow: hidden;
    cursor: pointer;
    transition: all 0.3s;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    background: white;
}

.image-item:hover {
    transform: translateY(-4px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    border-color: #5165f7;
}

.image-item img {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.no-image-tip {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 200px;
    color: #999;
    font-size: 14px;
}

.right-header {
    width: 90%;
    margin: 0 auto;
}

.ths {
    text-align: center;
    background: #DCE0FD !important;
    color: #363636;
}

.tou {
    height: 30px;
    line-height: 30px;
    color: #606266;
    text-align: center;
}

.financial-table {
    width: 100%;
    border-collapse: collapse;
    font-family: Arial, sans-serif;
    font-size: 14px;
}

.financial-table th,
.financial-table td {
    border: 1px solid #ddd;
    padding: 8px 7px;
    text-align: center;
}

.financial-table th {
    background-color: #f5f5f5;
    font-weight: bold;
}

.financial-table .nested-label {
   
    text-align: center;
    vertical-align: middle;
    font-weight: bold;
}

.table-right {
    flex: 1;
}

.table-left {
    flex: 1;
}

.content-table {
   
    width: 90%;
    margin: 0 auto;
    margin-top: 14px;
}

/* 默认情况（有效）左边圆角 */
.left-radius {
    border-bottom-left-radius: 16px !important;
    border-top-left-radius: 16px !important;
}

/* 无效情况右边圆角 */
.right-radius {
    border-bottom-right-radius: 16px !important;
    border-top-right-radius: 16px !important;
}

.yuan-left {
    flex: 1;
    text-align: center;
    line-height: 30px;
    background: #f8f8f8;
    /* border-bottom-left-radius: 16px;
    border-top-left-radius: 16px; */
    cursor: pointer;
}

.yuanright {
    flex: 1;
    background: #5165f7;
    color: white;

    /* border-bottom-right-radius: 16px;
    border-top-right-radius: 16px;  */
    text-align: center;
    line-height: 30px;
    cursor: pointer;

}

.youxiaos {
    position: relative;
    top: 6px;
    left: -12px;
    cursor: pointer;
}

/* 修改.yuan的样式，移除外层圆角 */
.yuan {
    width: 100px;
    height: 30px;
    background: white;
    display: flex;
    font-size: 14px;
    overflow: hidden;
    /* 关键：确保内部div的圆角效果不被截断 */
}

/* 默认状态下两个按钮的外侧圆角 */
.yuan-left:first-child {
    border-top-left-radius: 16px;
    border-bottom-left-radius: 16px;
}

.yuan-left:last-child {
    border-top-right-radius: 16px;
    border-bottom-right-radius: 16px;
}

/* 选中状态样式 */
.yuanright {
    background: #5165f7;
    color: white;
}

/* 选中"有效"时的样式 */
.yuan-left.left-radius {
    border-top-right-radius: 0 !important;
    border-bottom-right-radius: 0 !important;
}

/* 选中"无效"时的样式 */
.yuan-left.right-radius {
    border-top-left-radius: 0 !important;
    border-bottom-left-radius: 0 !important;
}


.times {
    position: relative;
    top: 3px;
}

.youxiao {
    color: #5B5D76;
    font-size: 13px;
}

.table-header {
    display: flex;
    justify-content: space-between;
    margin-top: 14px;
}

.table-area {
    margin-top: 12px;
    font-size: 15px;
}

.leftss-header {
    display: flex;
    height: 60px;
    line-height: 60px;
    border-bottom: 1px solid #3333;
    font-weight: bold;
    justify-content: space-between;
}

.leftss {
    width: 90%;
    margin: 0 auto;
}

.content-lefts {
    background: white;
    padding-bottom: 30px;
    width: 100%;
    height: 100%;
    overflow-y: auto;
    box-sizing: border-box;
}

.content-left {
    flex: 1;
    height: 100%;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-height: 0;
}

.content-right {
    width: 600px;
    height: 100%;
    background: white;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-height: 0;
}

.contents {
    width: 100%;
    height: calc(100vh - 90px);
    background: #f8f8f8;
    display: flex;
    margin-top: 6px;
    overflow: hidden;
}

.shu {
    width: 3px;
    height: 18px;
    background: #5165f7;
    position: relative;
    display: inline-block;
    top: 7px;
}

::v-deep .el-table thead {
    background-color: #DCE0FD !important;
    color: #363636 !important;
    font-weight: bold;
}

::v-deep .el-table__header th {
    background-color: #DCE0FD !important;
    color: #363636 !important;
    font-weight: bold;
}

.rights {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    top: -6px;
}

.name-title {
    position: relative;
    top: -6px;
    font-weight: bold;
}

.right-name {
    font-size: 12px;
}

.img-right {
    font-size: 14px;
    position: relative;
    top: 35px;
    height: 40px;
    margin-left: 12px;
}

.imgs {
    width: 42px;
    height: 40px;
    background: #5165f7;
    border-radius: 6px;
    position: relative;
    top: 26px;
    margin-left: 24px;
    overflow: hidden;
}
.enterprise-logo-img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    border-radius: 6px;
}

.headers-right {
    flex: 1;
    background: white;
}

.headers-left {
    width: 222px;
    height: 100%;
    display: flex;
    background: white;
}

.headers {
    width: 100%;
    height: 90px;
    display: flex;
    border-bottom: 1px solid #EDEFFE;
}



.patent-container {
      max-width: 1000px;
      margin: 0 auto;
      background: white;
      border-radius: 8px;
      box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
      overflow: hidden;
    }
    .patent-header {
      background: linear-gradient(135deg, #6a5acd, #8a7dff);
      color: white;
      padding: 20px 30px;
      text-align: center;
    }
    .patent-header h1 {
      font-size: 24px;
      margin-bottom: 10px;
      font-weight: 600;
    }
    .patent-header p {
      opacity: 0.9;
      font-size: 14px;
    }
    .search-box {
      padding: 15px 30px;
      background-color: #f8f9fa;
      border-bottom: 1px solid #eaeaea;
    }
    .search-box input {
      width: 100%;
      padding: 10px 15px;
      border: 1px solid #ddd;
      border-radius: 4px;
      font-size: 14px;
      transition: border-color 0.3s;
    }
    .search-box input:focus {
      outline: none;
      border-color: #8a7dff;
      box-shadow: 0 0 0 2px rgba(138, 125, 255, 0.2);
    }
    .patent-table {
      width: 100%;
      border-collapse: collapse;
    }
    .patent-table th {
      background-color: #f0e6ff;
      color: #5a4fcf;
      padding: 12px 15px;
      text-align: left;
      font-weight: 600;
      border-bottom: 1px solid #ddd;
    }
    .patent-table td {
      padding: 12px 15px;
      border-bottom: 1px solid #f0f0f0;
      transition: background-color 0.2s;
    }
    .patent-table tr:hover td {
      background-color: #f9f7ff;
    }
    .patent-table tr:last-child td {
      border-bottom: none;
    }
    .status-badge {
      display: inline-block;
      padding: 4px 8px;
      border-radius: 4px;
      font-size: 12px;
      font-weight: 500;
    }
    .status-paid {
      background-color: #e8f5e9;
      color: #2e7d32;
    }
    .patent-footer {
      padding: 15px 30px;
      text-align: center;
      color: #777;
      font-size: 14px;
      border-top: 1px solid #eaeaea;
      background-color: #fafafa;
    }


    .shu{
  width: 10px;
    height: 20px;
    background: #5165f7;
    position: relative;
    top: -24px;
    left: 40%;
}
.active{
    color: #5165f7 !important;
}
.rights-names{
    margin-left: 32px;
    cursor: pointer;
}
.rights{
    width: 96%;
    height: 100%;
    margin: 0 auto;
    line-height: 94px;
    display: flex;
    position: relative;
    left: -27px;
}
.name-title{
    position: relative;
    top: -6px;
    font-weight: bold;
}
.right-name{
    font-size: 12px;
}
.img-right{
    font-size: 14px;
    position: relative;
    top: 40px;
    height: 40px;
    margin-left: 12px;
}
.imgs{
    width: 45px;
    height: 46px;
    background: #5165f7;
    border-radius: 6px;
    position: relative;
    top: 26px;
    margin-left: 24px;
    overflow: hidden;
}
.headers-right{
    flex: 1;
    background: white;
  
}
.headers-left{
    width: 222px;
    height: 100%;
    display: flex;
    background: white;
    border-right: 1px solid #EDEFFE;
}
.headers{
  width: 100%;
    height: 90px;
    display: flex
;
    border-bottom: 1px solid #EDEFFE;
}

/* 保存成功对话框样式 */
.save-success-dialog ::v-deep .el-dialog {
    border-radius: 8px;
    padding: 0;
}
.save-success-dialog ::v-deep .el-dialog__body {
    padding: 0;
}
.success-dialog-content {
    padding: 40px 30px 30px;
    text-align: center;
}
.success-icon-wrapper {
    position: relative;
    display: inline-block;
    margin-bottom: 20px;
}
.success-icon-wrapper img {
    width: 64px;
    height: 64px;
}
.success-title {
    font-size: 20px;
    font-weight: bold;
    color: #333;
    margin-bottom: 20px;
}
.success-message {
    font-size: 14px;
    color: #666;
    line-height: 24px;
    margin-bottom: 30px;
    text-align: center;
    padding: 0 20px;
}
.success-message p {
    margin: 8px 0;
}
.success-footer {
    text-align: center;
}

/* 保存失败对话框样式 */
.save-fail-dialog ::v-deep .el-dialog {
    border-radius: 8px;
    padding: 0;
}
.save-fail-dialog ::v-deep .el-dialog__body {
    padding: 0;
}
.fail-dialog-content {
    padding: 40px 30px 30px;
    text-align: center;
}
.fail-icon-wrapper {
    position: relative;
    display: inline-block;
    margin-bottom: 20px;
}
.fail-icon-wrapper img {
    width: 64px;
    height: 64px;
}
.fail-title {
    font-size: 20px;
    font-weight: bold;
    color: #333;
    margin-bottom: 20px;
}
.fail-message {
    font-size: 14px;
    color: #666;
    line-height: 24px;
    margin-bottom: 30px;
    text-align: center;
    padding: 0 20px;
}
.fail-message p {
    margin: 8px 0;
}
.fail-footer {
    text-align: center;
}
</style>