<template>
    <div class="height_100 OADefense">
      <conversationRecords v-show="showMenu" name="OA答辩" :show_add="false" :conversation="currentConversation" :confessionSessionList="confessionSessionList" @addConversation="addConversation" @changeConversation="changeConversation" @updateConversation="queryConfessionSession" ></conversationRecords>
      
      <myView style="height: 100%;width: 100%;" position="row">
       <!-- 左侧文件展示 -->
       <div slot="left" class="left customize_file" v-show="showLeft">
            <div class="head">
                <div class="head_left">
                    <div class="margin-right_10">
                        <i v-show="showMenu" class="el-icon-s-fold" @click="showMenu = !showMenu"></i>
                        <i v-show="!showMenu" class="el-icon-s-unfold" @click="showMenu = !showMenu"></i>
                    </div>
                    <div style="font-weight:600">OA答辩</div>
                    <el-button size="mini" class="margin-left_10" type="primary" @click="addConversation">添加新OA答辩</el-button>
                    <el-button v-if="notice_file.guid && !loading" class="margin-left_10" type="primary" size="mini" @click="regenerate">重新生成</el-button>
                </div>
                <div class="head_right">
                    <el-select v-model="current_guid" size="mini" placeholder="请选择">
                        <el-option v-if="notice_file.guid" :label="`审查意见通知书-${notice_file.originalName || notice_file.name}`" :value="notice_file.guid" @click.native="handleOptionClick(notice_file,1)"></el-option>
                        <el-option v-if="app_file.guid" :label="`申请文件-${app_file.appNo}`" :value="app_file.guid" @click.native="handleOptionClick(app_file,2)"></el-option>
                        <el-option v-if="modify_file.guid" :label="`修改对照页`" :value="modify_file.guid" @click.native="handleOptionClick(modify_file,4)"></el-option>
                        <template v-if="contrast_file && contrast_file.length>0">
                            <el-option
                                v-for="(item,index) in contrast_file"
                                :key="index"
                                :label="`D${item.index}-${item.publicNo}`"
                                :value="item.guid"
                                @click.native="handleOptionClick(item,3)">
                            </el-option>
                        </template>
                        
                    </el-select>
                </div>
                
            </div>
            <div>
            <hr>
            </div>
            <!-- 输入 -->
            <template v-if="add">
                <div >
                    <!-- <p>审查意见书：</p>
                    <div>
                        <el-upload
                            ref="upload_file"
                            class="upload-demo"
                            drag
                            action="#"
                            :auto-upload="false"
                            :limit="1"
                            :file-list="notice_file.guid?[notice_file]:[]"
                            :on-change="onChange"
                            style="width: 100%"
                        >
                            <i class="el-icon-upload"></i>
                            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                        </el-upload>
                    </div>
                    <div style="padding: 10px 0;">
                        <div><b>相关文件(申请文件和对比文件)：（<span style="font-size:18px">非必填</span>）</b></div>
                        <div style="font-size:16px;color:red">
                            注：
                            <br>
                            1.如果上传了相关文件则优先使用上传的文件，如果不上传则系统会自动获取文件
                            <br>
                            2.上传的申请文件要包含本申请号码，对比文件包含对比文件序号或者名称
                        </div>
                    </div>
                    <div>
                        <myUpload :file-list="patent_fileUrls" @on-change="onchangeFile" @on-remove="onRemove" style="height: 180px;" :autoUpload="true"></myUpload>
                    </div> -->
                    <div style="height:calc(100vh - 210px);overflow-y: auto;overflow-x:hidden">
                        <div style="display:flex;align-items: center;justify-content:space-between">
                            <p class="file_title"><i class="el-icon-star-on"></i>本次需要答复的审查意见通知书</p>
                            <div>
                                <el-button size="mini" class="margin-right_10" type="primary" @click="batch_upload">批量上传</el-button>
                            </div>
                        </div>
                        
                        <div class="upload_file">
                            <div v-if="notice_file.guid" class="file_show">
                                <myTooltip :content="notice_file.name ? notice_file.name : notice_file.originalName">
                                    <div class="noWrap file_name">
                                        <i class="el-icon-document" style="margin-right: 10px"></i>
                                        <span class="name">{{notice_file.name ? notice_file.name : notice_file.originalName}}</span>
                                    </div>
                                </myTooltip>
                                <div class="type">{{ notice_file.guid ? "已上传" : "待上传" }}</div>
                                <div class="icon" @click="notice_file = {}">
                                    <i class="el-icon-close"></i>
                                </div>
                            </div>
                            <el-upload
                                v-else
                                ref="upload_file"
                                class="upload-demo"
                                drag
                                action="#"
                                :auto-upload="false"
                                :limit="1"
                                :on-change="(file)=>onChange(file,'notice_file')"
                                style="width: 100%"
                            >
                                <i class="el-icon-upload"></i>
                                <div class="el-upload__text">
                                    <div><em>点击上传</em></div>
                                    <div>或将文件拖到此处上传</div>
                                </div>
                            </el-upload>
                        </div>
                        <!-- 选择最接近本申请的现有技术 -->
                        <p>
                            <span class="file_title">最接近本申请的现有技术：</span>
                            <span>
                                <el-select size="small" v-model="changeClaim.near_index" placeholder="请选择">
                                    <el-option
                                        v-for="item in 15"
                                        :key="item"
                                        :label="'D' + item"
                                        :value="item">
                                    </el-option>
                                </el-select>

                            </span>
                        </p>
                        <p>
                            <span class="file_title">是否进行权要修改：</span>
                            <span>
                                <el-radio-group v-model="changeClaim.changeClaim">
                                    <el-radio :label="1">是</el-radio>
                                    <el-radio :label="2">否</el-radio>
                                    <!-- <el-radio :label="3">AI推荐</el-radio> -->
                                </el-radio-group>   
                            </span>
                        </p>
                        <div v-if="changeClaim.changeClaim == 1">
                            <p class="file_title">修改后完整的权利要求</p>
                            <div>
                                <el-input type="textarea" v-model="changeClaim.claim" placeholder="请输入修改后完整的权利要求"></el-input>
                            </div>
                        </div>
                        <div style="margin:25px 0;">
                            <el-divider content-position="left">相关文件（选填）</el-divider>
                        </div>
                        
                        <p class="file_title">本申请文件</p>
                        <div class="upload_file">
                            <div v-if="relevant_file.app_file.guid" class="file_show">
                                <myTooltip :content="relevant_file.app_file.name ? relevant_file.app_file.name : relevant_file.app_file.originalName">
                                    <div class="noWrap file_name">
                                        <i class="el-icon-document" style="margin-right: 10px"></i>
                                        <span class="name">{{relevant_file.app_file.name ? relevant_file.app_file.name : relevant_file.app_file.originalName}}</span>
                                    </div>
                                </myTooltip>
                                <div class="type">{{ relevant_file.app_file.guid ? "已上传" : "待上传" }}</div>
                                <div class="icon" @click="relevant_file.app_file = {}">
                                    <i class="el-icon-close"></i>
                                </div>
                            </div>
                            <el-upload
                                v-else
                                ref="upload_file"
                                class="upload-demo"
                                drag
                                action="#"
                                :auto-upload="false"
                                :limit="1"
                                :on-change="(file)=>onChange(file,'relevant_file.app_file')"
                                style="width: 100%"
                            >
                                <i class="el-icon-upload"></i>
                                <div class="el-upload__text">
                                    <div><em>点击上传</em></div>
                                    <div>或将文件拖到此处上传</div>
                                </div>
                            </el-upload>
                        </div>
                        <p class="file_title">上一次OA的修改对照页（若有，要包含权利要求和说明书全文内容）</p>
                        <div class="upload_file">
                            <div v-if="relevant_file.modify_file.guid" class="file_show">
                                <myTooltip :content="relevant_file.modify_file.name ? relevant_file.modify_file.name : relevant_file.modify_file.originalName">
                                    <div class="noWrap file_name">
                                        <i class="el-icon-document" style="margin-right: 10px"></i>
                                        <span class="name">{{relevant_file.modify_file.name ? relevant_file.modify_file.name : relevant_file.modify_file.originalName}}</span>
                                    </div>
                                </myTooltip>
                                <div class="type">{{ relevant_file.modify_file.guid ? "已上传" : "待上传" }}</div>
                                <div class="icon" @click="relevant_file.modify_file = {}">
                                    <i class="el-icon-close"></i>
                                </div>
                            </div>
                            <el-upload
                                v-else
                                ref="upload_file"
                                class="upload-demo"
                                drag
                                action="#"
                                :auto-upload="false"
                                :limit="1"
                                :on-change="(file)=>onChange(file,'relevant_file.modify_file')"
                                style="width: 100%"
                            >
                                <i class="el-icon-upload"></i>
                                <div class="el-upload__text">
                                    <div><em>点击上传</em></div>
                                    <div>或将文件拖到此处上传</div>
                                </div>
                            </el-upload>
                        </div>
                        <template v-if="relevant_file.contrast_file">
                            <div>
                                <div v-for="contrast_file in relevant_file.contrast_file" :key="'D'+contrast_file.order">
                                    <p class="file_title">对比文件D{{ contrast_file.order }}</p>
                                    <div class="upload_file">
                                        <div v-if="contrast_file.file.guid" class="file_show">
                                            <myTooltip :content="contrast_file.file.name ? contrast_file.file.name : contrast_file.file.originalName">
                                                <div class="noWrap file_name">
                                                    <i class="el-icon-document" style="margin-right: 10px"></i>
                                                    <span class="name">{{contrast_file.file.name ? contrast_file.file.name : contrast_file.file.originalName}}</span>
                                                </div>
                                            </myTooltip>
                                            <div class="type">{{ contrast_file.file.guid ? "已上传" : "待上传" }}</div>
                                            <div class="icon" @click="contrast_file.file = {}">
                                                <i class="el-icon-close"></i>
                                            </div>
                                        </div>
                                        <el-upload
                                            v-else
                                            ref="upload_file"
                                            class="upload-demo"
                                            drag
                                            action="#"
                                            :auto-upload="false"
                                            :limit="1"
                                            :on-change="(file)=>onChange(file,'file',contrast_file)"
                                            style="width: 100%"
                                        >
                                            <i class="el-icon-upload"></i>
                                            <div class="el-upload__text">
                                                <div><em>点击上传</em></div>
                                                <div>或将文件拖到此处上传</div>
                                            </div>
                                        </el-upload>
                                    </div>
                                </div>
                                <div class="addContrastFile">
                                    <el-button size="medium" @click="add_contrast_file">添加 + </el-button>
                                </div>

                            </div>
                        </template>
                    </div>
                    <hr >
                    <div class="run" >
                        <div></div>
                        <el-button type="primary" size="small" @click="run"><div class="btn_content"><i class="el-icon-caret-right"></i>运行</div></el-button>
                    </div>
                </div>
            </template>
            <template v-else-if="show">
                <vabOnlyOffice class="myIframe" :option='option'></vabOnlyOffice>
            </template>
            <hr>
        </div>
        <!-- 中间的线 -->
        <!-- <div class="line" v-show="showLeft"></div> -->
        <!-- 右侧发明点和对话框 -->
        <div slot="right" class="right" :style="!showLeft?'width:calc(100% - 0px) !important':''">
            <div>
                <div class="head">
                    <div class="head_left">
                        <div class="margin-right_10">
                            <i v-show="showLeft" title="收起左侧内容" class="el-icon-s-fold" @click="showLeft = !showLeft"></i>
                            <i v-show="!showLeft" title="展开左侧内容" class="el-icon-s-unfold" @click="showLeft = !showLeft"></i>
                        </div>
                        <div class="style_starIcon__t_QZN">
                            <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                                <path d="M3.66699 1.33366C3.66699 0.965469 3.36852 0.666992 3.00033 0.666992C2.63214 0.666992 2.33366 0.965469 2.33366 1.33366V2.33366H1.33366C0.965469 2.33366 0.666992 2.63214 0.666992 3.00033C0.666992 3.36852 0.965469 3.66699 1.33366 3.66699H2.33366V4.66699C2.33366 5.03518 2.63214 5.33366 3.00033 5.33366C3.36852 5.33366 3.66699 5.03518 3.66699 4.66699V3.66699H4.66699C5.03518 3.66699 5.33366 3.36852 5.33366 3.00033C5.33366 2.63214 5.03518 2.33366 4.66699 2.33366H3.66699V1.33366Z" fill="#444CE7"/>
                                <path d="M3.66699 11.3337C3.66699 10.9655 3.36852 10.667 3.00033 10.667C2.63214 10.667 2.33366 10.9655 2.33366 11.3337V12.3337H1.33366C0.965469 12.3337 0.666992 12.6321 0.666992 13.0003C0.666992 13.3685 0.965469 13.667 1.33366 13.667H2.33366V14.667C2.33366 15.0352 2.63214 15.3337 3.00033 15.3337C3.36852 15.3337 3.66699 15.0352 3.66699 14.667V13.667H4.66699C5.03518 13.667 5.33366 13.3685 5.33366 13.0003C5.33366 12.6321 5.03518 12.3337 4.66699 12.3337H3.66699V11.3337Z" fill="#444CE7"/>
                                <path d="M9.28922 1.76101C9.1902 1.50354 8.94284 1.33366 8.66699 1.33366C8.39114 1.33366 8.14378 1.50354 8.04476 1.76101L6.88864 4.76691C6.68837 5.28761 6.62544 5.43766 6.53936 5.55872C6.45299 5.68019 6.34686 5.78632 6.22539 5.87269C6.10432 5.95878 5.95428 6.02171 5.43358 6.22198L2.42767 7.37809C2.17021 7.47712 2.00033 7.72448 2.00033 8.00033C2.00033 8.27617 2.17021 8.52353 2.42767 8.62256L5.43358 9.77867C5.95428 9.97894 6.10432 10.0419 6.22539 10.128C6.34686 10.2143 6.45299 10.3205 6.53936 10.4419C6.62544 10.563 6.68837 10.713 6.88864 11.2337L8.04476 14.2396C8.14379 14.4971 8.39114 14.667 8.66699 14.667C8.94284 14.667 9.1902 14.4971 9.28922 14.2396L10.4453 11.2337C10.6456 10.713 10.7085 10.563 10.7946 10.4419C10.881 10.3205 10.9871 10.2143 11.1086 10.128C11.2297 10.0419 11.3797 9.97894 11.9004 9.77867L14.9063 8.62256C15.1638 8.52353 15.3337 8.27617 15.3337 8.00033C15.3337 7.72448 15.1638 7.47712 14.9063 7.37809L11.9004 6.22198C11.3797 6.02171 11.2297 5.95878 11.1086 5.87269C10.9871 5.78632 10.881 5.68019 10.7946 5.55872C10.7085 5.43766 10.6456 5.28761 10.4453 4.76691L9.28922 1.76101Z" fill="#444CE7"/>
                            </svg>
                        </div>
                        <div class="title">AI 智能书写 <span v-if="loading"><i class="el-icon-loading margin-left_10"></i></span></div>
                    </div>
                    <div class="head_right">
                        <!-- <el-button v-if="!loading && result && result.code != 1001 && !result_file.show" type="primary" size="mini" @click="copy">复制</el-button> -->
                        <el-button v-if="result_file.show" type="primary" size="mini" :loading="btnLoading" @click="download_file">下载</el-button>
                    </div>
                </div>
            </div>
            <div v-if="!result && loading" class="loading-container">
                <div v-if="loading_text">
                    {{ loading_text }} <i class="el-icon-loading margin-left_10"></i>
                </div>
                <div v-else class="loading-text">
                    <span>思</span>
                    <span>考</span>
                    <span>中</span>
                    <span>.</span>
                    <span>.</span>
                    <span>.</span>
                </div>
            </div>
            <div class="result" ref="AIMessage" v-else>
                <template v-if="result_file.show">
                    <vabOnlyOffice class="myIframe" :option='result_file.option'></vabOnlyOffice>
                </template>
                <!-- 缺少信息 -->
                <template v-else-if="result && result.code == 1001">
                    <div>
                      <div style="font-size:16px" v-html="result.data.replace('\n','<br>')"></div>  
                      <div>
                        <el-link type="primary" @click="openErrorTip">点击上传</el-link>
                      </div>
                    </div>
                    
                </template>
                <!-- 请求超时 -->
                <template v-else-if="result && result.code == 1002">
                    <div>
                      <div style="font-size:16px">
                        {{ result.data }}，请<el-link type="primary" @click="getResult">重试</el-link>
                      </div>
                    </div>
                    
                </template>
                <!-- 修改后的权利要求输出 -->
                <template v-else-if="result && result.code == 201">
                    <div>
                      <p><b>AI建议修改后的权利要求：</b><span style="color:red;font-size:12px">可以手动修改</span></p>
                      <el-input type="textarea" :rows="30" v-model="changeClaim.claim"></el-input>
                      <div class="result_btn">
                        <el-button type="primary" size="small" @click="reLoadSubmit">确认并继续</el-button>
                      </div>
                    </div>
                </template>
                <!-- 区别技术特征 -->
                <template v-else-if="result && result.code == 202">
                    <div v-for="(item,index) in main_claim_reason" :key="index">
                      <p><b>权利要求{{ item.after_claimOrder || item.claim_order[0] }}的答辩点：</b><span style="color:red;font-size:12px">可以手动修改</span></p>
                      <el-input type="textarea" :rows="10" v-model="item.difference_feature"></el-input>
                    </div>
                    <div class="result_btn">
                        <el-button type="primary" size="small" @click="reLoadSubmit">确认并继续</el-button>
                      </div>
                </template>
                <!-- 结果 -->
                <template v-else-if="result">
                    <div class="answer_warp">
                        <div v-for="(item,index) in formateAIAnswer(this.result)" :key="index">
                                <div v-if="item.type == 'text'" id='answer'>
                                    <div v-if="item.content && item.content != 'null'" v-html="renderMarkdown(item.content)"></div>
                                </div>
                            </div>
                    </div>
                </template>
                <div class="jiazai" v-if="result && loading">
                   <span style="color:red;">{{loading_text}}</span> <i class="el-icon-loading margin-left_10"></i>
                </div>
            </div>
        </div>
      </myView>
        <!-- 错误提示以及上传文件 -->
        <errorTipDialog ref="errorTip" @submit="reLoadSubmit"></errorTipDialog>
        <!-- 批量上传文件 -->
        <batchFileDialog ref="batchFile" :sign="1" @get_file_obj="get_file_obj"></batchFileDialog>
    </div>
</template>
  
<script>
import vabOnlyOffice from '@/components/VabOnlyOffice/index.vue'
import  mixin  from './mixins/index1.js';
import conversationRecords from '../components/conversationRecords.vue';
import {renderMarkdown} from '@/utils/markdown'
import { downLoad2 } from '@/utils'
import { scrollToBottom } from '../components/mixins/scrollToBottom'
import errorTipDialog from './dialog/errorTip.vue'
import batchFileDialog from './dialog/batchFile.vue';
export default {
    components: {
      conversationRecords,
      vabOnlyOffice,
      errorTipDialog,
      batchFileDialog
    },
    props: {},
    mixins:[mixin,scrollToBottom],
    data() {
      return {
        showMenu:false,//是否显示左侧菜单
        showLeft:true,//展示左侧内容
        currentConversation:{},//当前会话
        confessionSessionList:[],//会话记录
        chat_id:null,
        btnLoading:false,//按钮加载
        loading:false,//生成中
        loading_text:'',
        result:'',//OA答辩结果文本
        show:false,//显示左侧文件
        option:{//左侧文件配置
            url: '',
            isEdit: false,
            fileType: '',
            title: '',
            lang: 'zh-CN',
            isPrint: true,
            user: {
                id: this.$s.getObj('userinfo').id,
                name: this.$s.getObj('userinfo').name
            }
        },
        changeClaim:{
            near_index:1,
            changeClaim:1,
            claim:""
        },
        main_claim_reason:[],//区别技术特征
        confession:{},//文件信息
        reLoadSearch:false,//重新生成OA答辩
        add:true,//添加审查通知书
        notice_file:{},//审查通知书文件
        relevant_file:{
            app_file:{},
            modify_file:{},
            contrast_file:[
                {
                    order:1,
                    file:{}
                },
                {
                    order:2,
                    file:{}
                },
                {
                    order:3,
                    file:{}
                },
            ]
        },
        patent_fileUrls:[],//专利文件地址
        result_file:{},//结果文件
        app_file:{},//申请文件
        modify_file:{},//修改对照页文件
        contrast_file:[],//对比文件
        current_guid:'',//左侧当前显示的文件
      };
    },
    watch: {
        result(){
            this.scrollToBottom()
        }
    },
    computed: {},
    created() {
        this.getInit()
    },
    mounted() {},
    methods: {
        renderMarkdown,
        //初始化
        async getInit(){
            this.queryConfessionSession()
        },
        add_contrast_file(){
            let order1 = this.relevant_file.contrast_file[this.relevant_file.contrast_file.length-1].order
            this.relevant_file.contrast_file.push(
                {
                    order:order1 + 1,
                    file:{}
                }
            )
        },
        //批量上传
        batch_upload(){
            this.$refs.batchFile.open()
        },
        get_file_obj(data){
            if(data.notice_file){
                this.$set(this,'notice_file',data.notice_file)
            }
            if(data.app_file){
                this.$set(this.relevant_file,'app_file',data.app_file)
            }
            if(data.modify_file){
                this.$set(this.relevant_file,'modify_file',data.modify_file)
            }
            if(data.contrast_file && data.contrast_file.length>0){
                let contrast_file = this.relevant_file.contrast_file
                for(let i = 0;i<data.contrast_file.length;i++){
                    let item = data.contrast_file[i]
                    let order = item.order
                    let obj = contrast_file.find(file=>{
                        return file.order == order
                    })
                    if(obj){
                        obj.file = item.file
                    }else{
                        let order1 = contrast_file[contrast_file.length-1].order
                        if(order > order1 + 1){
                            for(let y = order1 + 1;y < order;y++){
                                contrast_file.push(
                                    {
                                        order:y,
                                        file:{}
                                    }
                                )
                            }
                        }
                        contrast_file.push(item)
                    }
                }
            }
        },
        //再次提交
        reLoadSubmit(data){
            if(data){
              this.get_file_obj(data)  
            }
            let content = this.currentConversation.content
            var params = {
                confessionSessionId:this.currentConversation.id,
            }
            if(!content){
                params.content = JSON.stringify(
                    {
                        query:{
                            // patent_fileUrls:this.patent_fileUrls,
                            relevant_file:this.relevant_file,
                            claimChange:this.claimChange,
                            main_claim_reason:this.main_claim_reason
                        }
                    }
                )
            }else{
                try{
                    let data1 = JSON.parse(content)
                    if(data1.data && data1.data.code == 1001){
                        data1.data = {}
                    }
                    // if(data1.query && data1.query.patent_fileUrls){
                    //     data1.query.patent_fileUrls = this.patent_fileUrls
                    // }else{
                    //     data1.query = {
                    //         patent_fileUrls:this.patent_fileUrls
                    //     }
                    // }
                    if(data1.query && data1.query.relevant_file){
                        data1.query.relevant_file = this.relevant_file
                    }else{
                        data1.query = {
                            relevant_file:this.relevant_file,
                            claimChange:this.claimChange,
                            main_claim_reason:this.main_claim_reason
                        }
                    }
                    params.content = JSON.stringify(data1)
                    
                }catch(e){
                    params.content = JSON.stringify(
                        {
                            query:{
                                // patent_fileUrls:this.patent_fileUrls,
                                relevant_file:this.relevant_file,
                                claimChange:this.claimChange,
                                main_claim_reason:this.main_claim_reason
                            }
                        }
                    ) 
                }
            }

            this.$api.updateConfessionSession(params).then(response=>{
                if(response.code == 200){
                    this.reLoadSearch = true
                    this.queryConfessionSession()
                    this.getResult()
                }
            }).catch(error=>{
                
            })
        },
        //查询技术交底书会话记录
        async queryConfessionSession(onlyQuery,request,result){
            var params = {
                type:2
            }
            await this.$api.queryConfessionSession(params).then(response=>{
            if(response.code == 200){
                this.confessionSessionList = response.data || []
                if(!onlyQuery && this.confessionSessionList.length>0){
                    this.add = false
                    let obj = null
                    if(this.currentConversation.id){
                        obj = this.confessionSessionList.find(item=>{
                            return item.id == this.currentConversation.id
                        })
                    }else{
                        obj = this.confessionSessionList[0]
                    }
                    if(obj){
                        this.changeConversation(obj,true)
                    }
                    
                }else if(this.confessionSessionList.length==0){
                    this.addConversation()
                }
                if(request){
                    this.getResult()
                }
                if(result){
                    if(!this.confessionSessionList || this.confessionSessionList.length==0){
                        return
                    }
                    let obj = this.confessionSessionList.find(item=>{
                        return item.id == this.currentConversation.id
                    })
                    if(!obj){
                        obj = this.confessionSessionList[0]
                    }
                    this.getResultContent(obj)
                }
            }
            })
      },
      //获取生成的技术交底书理解结果
        async getConversionDetails(){
            var params = {
                confessionSessionId:this.currentConversation.id
            }
            await this.$api.discoveryResultDetail(params).then(response=>{
                if(response.code == 200){
                    let data = response.data || {}
                    this.currentConversation = {
                        ...this.currentConversation,
                        ...data
                    }
                }
            }).catch(error=>{
            })
        },
      run(){
        this.$set(this,'result_file',{})
        var params = {
            fileGuid:this.notice_file.guid,
            content:JSON.stringify(
                {
                    query:{
                        // patent_fileUrls:this.patent_fileUrls,
                        relevant_file:this.relevant_file,
                        claimChange:this.claimChange,
                        main_claim_reason:this.main_claim_reason
                    }
                }
            ),
            type:2
        }
        this.$api.addConfessionSession(params).then(response=>{
            if(response.code == 200){
                //查询技术交底书会话记录
                this.currentConversation = {
                    id:response.data
                }
                this.queryConfessionSession(false,true)
            }
        }).catch(error=>{
            message.close()
        })
      },
      //结果生成过程中切换、添加、刷新提示
      async loading_change(){
        var isNext = true
        if(this.loading){
            await this.$confirm('结果正在生成中，如果执行当前操作会取消当前的结果生成, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.cancelRun()
            }).catch(() => {
                isNext = false
            });
        }
        return isNext
        
      },
      //添加会话
      async addConversation(){
        let isNext = await this.loading_change()
        if(!isNext){
            return
        }
        this.$set(this,'currentConversation',{})
        this.$set(this,'confession',{})
        this.$set(this,'add',true)
        this.$set(this,'notice_file',{})
        // this.$set(this,'patent_fileUrls',[])
        this.set_relevant_file()
        this.$set(this,'changeClaim',{
            near_index:1,
            changeClaim:1,
            claim:''
        })
        this.$set(this,'main_claim_reason',[])
        this.$set(this,'app_file',{})
        this.$set(this,'app_file',{})
        this.$set(this,'contrast_file',[])
        this.$set(this,'result_file',{})
        this.$set(this,'current_guid','')
        this.$set(this,'result',null)
        this.$set(this,'loading_text','')
        this.$set(this,'loading',false)
      },
      //设置相关文件初始化
      set_relevant_file(){
        this.$set(this,'relevant_file',
            {
                app_file:{},
                modify_file:{},
                contrast_file:[
                    {
                        order:1,
                        file:{}
                    },
                    {
                        order:2,
                        file:{}
                    },
                    {
                        order:3,
                        file:{}
                    },
                ]
            }
        )
      },
      //切换左侧展示的文件
        handleOptionClick(obj,type){
            switch(type){
                case 1:
                    this.confession = obj
                    this.getOption()
                case 2:
                case 3:
                case 4:
                    if(type == 2){
                        obj.originalName = `申请文件-${obj.appNo}`
                    }else if(type == 3){
                        obj.originalName = `D${obj.index}-${obj.publicNo}`
                    }else if(type == 4){
                        obj.originalName = "修改对照页"
                    }
                   
                    if(obj.type){
                        this.confession = obj
                        this.getOption()
                    }else{
                        this.getFileByGuid(obj)
                    }
                    break;
            }
        },
        //获取文件信息
        async getFileByGuid(obj){
            if(!obj.guid){
                this.$message.warning('当前文件不存在')
                return
            }
            var fileIds = [obj.guid]
            await this.$api.getFileData(fileIds).then(response=>{
                if(response && response.length>0){
                    var data = response[0]
                    // obj.originalName = data.originalName
                    obj.type = data.type
                    this.confession = obj
                    this.getOption()
                }
            })
        },
      //切换会话
      async changeConversation(obj,sign){
        if(!sign){
            let isNext = await this.loading_change()
            if(!isNext){
                return
            } 
        }
        this.add = false
        if(obj.id == this.currentConversation.id){
            this.currentConversation = obj
            if(sign && !this.reLoadSearch){
                //获取当前会话id
                this.chat_id = obj.conversationId
                // this.$set(this,'current_guid',this.notice_file.guid)
                //装载审查意见通知书并展示
                this.confession = obj.systemFile || {}
                this.$set(this,'notice_file',JSON.parse(JSON.stringify(this.confession)))
                this.$set(this,'current_guid',this.notice_file.guid)
                this.getOption()
                this.reLoadSearch = false
            }
            if(this.reLoadSearch){
                this.reLoadSearch = false
            }
          return
        }
        //装载当前会话信息
        this.currentConversation = obj
        //获取当前会话id
        this.chat_id = obj.conversationId

        //装载审查意见通知书并展示
        this.confession = obj.systemFile || {}
        this.$set(this,'notice_file',JSON.parse(JSON.stringify(this.confession)))
        this.$set(this,'current_guid',this.notice_file.guid)
        this.getOption()
        await this.getConversionDetails()
        //装载结果
        this.getResultContent(obj)
      },
      getResultContent(obj){
        this.result_file = {
            file_message:obj.assoSystemFile || {},
            show:false,
            option:{
                url: '',
                isEdit: false,
                fileType: '',
                title: '',
                lang: 'zh-CN',
                isPrint: true,
                user: {
                    id: this.$s.getObj('userinfo').id,
                    name: this.$s.getObj('userinfo').name
                }
            }
        }
        this.getResultOption()
        this.$set(this,'result',null)
        // this.$set(this,'patent_fileUrls',[])
        this.set_relevant_file()
        this.contrast_file = []
        this.app_file = {}
        let content = obj.content
        if(!content){
            // this.$set(this,'patent_fileUrls',[])
            this.set_relevant_file()
          return
        }
        try{
          let data = JSON.parse(content)
        //   if(data.query && data.query.patent_fileUrls){
        //     this.$set(this,'patent_fileUrls',data.query.patent_fileUrls)
        //   }
          if(data.query && data.query.relevant_file){
            this.$set(this,'relevant_file',data.query.relevant_file)
          }
          if(data.query && data.query.claimChange){
            this.$set(this,'claimChange',data.query.claimChange)
          }
          if(data.query && data.query.main_claim_reason){
            this.$set(this,'main_claim_reason',data.query.main_claim_reason)
          }
          if(data.answer){
            this.$set(this,'result',data.answer)
          }
          if(data.relevant_file){
            this.app_file = data.relevant_file.app_file || {}
            this.modify_file = data.relevant_file.modify_file || {}
            this.contrast_file = data.relevant_file.contrast_file || []
          }
          if(data.data){
            let answer_data = data.data
            if(answer_data.code == 200){
                this.app_file = answer_data.data.app_file || {}
                this.modify_file = answer_data.data.modify_file || {}
                this.contrast_file = answer_data.data.contrast_file || []
                if(!this.result_file.show){
                    let num = answer_data.data.num
                    let claim_change = answer_data.data.claim_change
                    let defense_opinion = answer_data.data.defense_opinion || []
                    let reason = answer_data.data.reason
                    let html = `<div style="line-height: 1.8em;"><div style="text-align: center;"><b>意见陈述书</b></div><div style="text-indent: 2em;margin: 0;">尊敬的审查员：</div><div style="text-indent: 2em;margin: 0;">首先，感谢审查员为审查本申请而付出的艰辛劳动！</div><div style="text-indent: 2em;margin: 0;">本意见陈述书是对国家知识产权局发出的第${num}次审查意见通知书所作的答复。</div><div style="text-indent: 2em;margin: 0;">审查意见通知书中指出：${ reason }</div><div style="text-indent: 2em;margin: 0;">申请人在仔细阅读、认真研究审查员的审查意见之后，在原权利要求书的基础上对权利要求进行了修改，并陈述意见如下：</div>`
                    if(claim_change){
                        html += `<div style="text-indent: 2em;margin: 0;"><b>一、修改说明</b></div><div style="text-indent: 2em;margin: 0;">${claim_change}</div>`
                    }
                    html += `<div style="text-indent: 2em;margin: 0;">`
                    defense_opinion.forEach(item => {
                        html = html + `<p>${item}</p>`
                    });
                    html = html + `</div><div style="text-indent: 2em;margin: 0;">综上所述，申请人认为，经过上述对原申请文件的陈述，已克服审查员在审查意见通知书中所指出的所有缺陷，希望审查员能以此为基础继续对本发明进行审查，如果审查员认为该申请仍有不符合专利法及其实施细则规定之处，恳请再给予一次陈述意见/修改的机会。</div><div style="text-indent: 2em;margin: 0;">感谢审查员的辛勤工作。</div></div>`
                    this.$set(this,'result',html)
                }
            }else if(answer_data.code == 1001){
                this.result = answer_data
                this.$refs.errorTip.open(answer_data)
            }else if(answer_data.code == 1002){
                this.result = answer_data
            }else if(answer_data.code == 201){
                this.result = answer_data
                this.$set(this.changeClaim,'claim',answer_data.data)
            }else if(answer_data.code == 202){
                this.result = answer_data
                this.$set(this,'main_claim_reason',answer_data.data)
            }
            
          }
          
        }catch(e){
  
        }
      },
      openErrorTip(){
        this.$refs.errorTip.open(this.result)
      },
      setData(row,field,value){
        var fieldArray = field.split('.')
        let current = row || this;  
        // 遍历路径中的每一部分，直到最后一个属性的父对象  
        for (let i = 0; i < fieldArray.length - 1; i++) {  
            // 确保当前对象有下一个属性，否则创建一个空对象（或你需要的任何默认值）  
            if (!current.hasOwnProperty(fieldArray[i])) {  
                current[fieldArray[i]] = {};  
            }  
            current = current[fieldArray[i]];  
        }  
        
        // 给最后一个属性赋值  
        current[fieldArray[fieldArray.length - 1]] = value;  
      },
      //上传文件
      onChange(file,field,row){
        let formData = new FormData()
        formData.append('sourceId',this.$constants.sourceId)
        formData.append('files',file.raw)
        
        var notice_file = {
            name:file.raw.name
        }
        var message = this.$message({
            message: '文件上传中...',
            type: 'warning',
            duration:0
        });
        this.$api.uploadFile(formData).then(response=>{
            if(response.code == 200){
                let guid = response.data[0]
                notice_file.guid = guid
                // this.$set(this,field,notice_file)
                this.setData(row,field,notice_file)
                message.close()
                this.$message.success('文件上传成功')
            }
        })
      },
        getOption(data,model='view'){
            let option_data = data || this.confession
            if(!option_data.guid){
                this.$message.warning('文件不存在')
                return 
            }
            this.show = false
            this.option.name = option_data.originalName
            this.option.id = option_data.guid
            this.option.title = option_data.originalName
            this.option.url = this.$c.url+ (this.$c.env=='production'?'/api':'')+'/fileManager/downloadFile?fileId=' + option_data.guid
            this.option.fileType = option_data.type
            this.option.key = option_data.guid
            
            this.option.model = model
            this.$nextTick(()=>{
                this.show = true
            })
        },
        getResultOption(model='edit'){
            let option_data = this.result_file.file_message
            if(!option_data.guid){
                // this.$message.warning('文件不存在')
                return 
            }
            let option = this.result_file.option
            // this.result_file.show = false
            this.$set(this.result_file,'show',false)
            option.name = option_data.originalName
            option.id = option_data.guid
            option.title = option_data.originalName
            option.url = this.$c.url+ (this.$c.env=='production'?'/api':'')+'/fileManager/downloadFile?fileId=' + option_data.guid
            option.fileType = option_data.type
            option.key = option_data.guid
            
            option.model = model
            this.$nextTick(()=>{
                this.$set(this.result_file,'show',true)
            })
        },
        //下载文件
        async download_file(){
            if(this.currentConversation.assoSystemFile && this.currentConversation.assoSystemFile.guid){
                downLoad2(this.currentConversation.assoSystemFile.guid,this.currentConversation.assoSystemFile.originalName)
                return 
            }
            let confessionSessionId = this.currentConversation.id
            let filename = this.currentConversation.conversationName
            this.btnLoading = true
            let html = ''
            let answerDomList = document.querySelectorAll('#answer')
    
            if(answerDomList.length == 0){
                this.btnLoading = false
                return
            }
            let answerDom = answerDomList[answerDomList.length - 1]
            html = answerDom.innerHTML
            let guid = await this.$commonJS.exportToWordByHtml(html,filename,true)
            if(guid){
                this.saveResultFile(confessionSessionId,guid)
            }
            this.btnLoading = false
        },
        //保存下载之后的文件
        saveResultFile(confessionSessionId,guid){
            var params = {
                confessionSessionId:confessionSessionId,
                guid:guid
            }
            this.$api.addConfessionSessionFile(params).then(response=>{
                if(response.code == 200){
                    this.queryConfessionSession()
                }
            })
        },
        //重新生成
        regenerate(){
            this.run()
        },
    },
};
</script>
<style lang="scss">
.OADefense{
    .file_title{
        font-weight: bold;
        i{
            color: red !important;
        }
    }
    .file_show{
        display: flex;
        align-items: center;
        justify-content: space-between;
        line-height: 100px;
        .file_name{
            width: calc(100% - 90px);
        }
        .type {
          min-width:75px;
          display: block;
          color: #57a5f7;
        }
        .icon {
          display: none;
        }
    }
    .file_show:hover {
        background: #f4f4f4;
        .name {
          color: #57a5f7;
          cursor: pointer;
        }
        .type {
          min-width:75px;
          display: none;
        }
        .icon {
          display: block;
          cursor: pointer;
        }
      }
      .customize_file{
        .upload-demo{
            .el-upload{
            width: 100% !important;  
            }
        
            .el-upload-dragger {
                width: 100% !important;
                height:100px !important;
                display: flex;
                align-items: center;
                .el-icon-upload{
                    font-size: 40px !important;
                    margin: 0 25px !important;
                }
            }
        }
      }
    
}
    
</style>
<style lang="scss" scoped>
.OADefense{
    position: relative;
    display: flex;
    align-items: stretch;
    height: calc(100% - 20px);
    width:calc(100% - 20px);
    padding: 10px;
    .left{
        width:50%;
        height:calc(100% - 0px);
        padding-right: 10px;
        display: flex;
        flex-direction: column;
        .head{
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding-bottom: 10px;
            .head_left{
                display: flex;
                align-items: center;
            }
            i{
                font-size: 20px;
            }
        }
        .addContrastFile{
            margin-top: 10px;
            display: flex;
            justify-content: center;
            width: 100%;
        }
        .run{
            display: flex;
            justify-content: space-between;
            .btn_content{
                font-size: 14px;
                display: flex;
                align-items: center;
            i{
                margin-right: 5px;
                font-size: 16px;
            }
            }
        }
    }
    .line{
        width:2px;
        height: 100%;
        background: #e6e6e6;
    }
    .right{
        width:calc(100% - 50% - 2px);
        min-width: 450px;
        height:100%;
        padding-left: 10px;
        display: flex;
        flex-direction: column;
        .result_btn{
            margin-top:10px;
            display:flex;
            justify-content:center;
        }
        .head{
            display: flex;
            align-items: center;
            justify-content: space-between;
            .head_left,.head_right{
                display: flex;
                align-items: center;
            }
        }
        .style_starIcon__t_QZN{
            width: 16px;
            height: 16px;
        }
        .title{
            margin-left: 20px;
            font-weight: 600;
        }
        .result{
            margin-top: 20px;
            height:calc(100% - 20px - 30px);
            overflow: auto;
            
            &:empty::after{
                content: "无结果";
                color:#c2c5c9;
                text-align: center;
            }
            .jiazai{
                display: flex;
                align-items: center;
                height: 20px;
            }
        }
        .loading-container{
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            .loading-text {
                font-size: 24px;
                font-family: Arial;
                display: flex;
                gap: 4px;
            }
    
            .loading-text span {
                display: inline-block;
                animation: bounce 1.2s infinite ease-in-out;
                color: black;
            }
    
            .loading-text span:nth-child(2) { animation-delay: 0.1s; }
            .loading-text span:nth-child(3) { animation-delay: 0.2s; }
            .loading-text span:nth-child(4) { animation-delay: 0.3s; }
            .loading-text span:nth-child(5) { animation-delay: 0.4s; }
    
            @keyframes bounce {
                0%, 40%, 100% { 
                    transform: translateY(0);
                }
                20% {
                    transform: translateY(-15px);
                }
            }
        }
    }
}
</style>