<template>
    <div class="height_100">
      
      <div style="display:flex;justify-content:flex-end;padding:10px">
          <!-- <el-button type="primary" size="small" @click="getMessage">根据内部卷号获取信息</el-button> -->
          <el-button type="primary" size="small" @click="saveEdit">保存修改</el-button>
          <el-button type="primary" size="small" @click="reporting">生成报告</el-button>
      </div>
      <div class="reportTemplate" style="height:calc(100% - 60px);overflow-y:auto" v-loading="loading">
          <component v-if="!loading && component"  :is='component'  :dataObj="dataObj" :reportData="reportData" :editMessage="editMessage" :editModel="editModel" :reLoadBasicMessage="reLoadBasicMessage"></component>    
      </div>

      <getMessageByNumber ref="getMessageByNumber" @save="getReportMessage(1)"></getMessageByNumber>
    </div>
</template>
  
<script>
import mixins1 from './mixins/index'
import mixins2 from './components/mixins'
import getMessageByNumber from '../dialog/getMessageByNumber.vue';
export default {
    components: {
        getMessageByNumber,
    },
    mixins:[mixins1],
    props: {
        reportTemplateId:{},
        projectId:{},
        reportFileId:{},
        editModel:{
            type:Boolean,
            default:false
        }
    },
    data() {
      return {
          component:'',
          loading:false,
          dataObj:{
            baseMessage:{
                customCaseNumber:'123',
                caseName:'你好'
            },
            technicalCase:{},
            compareLiterature:[],
            compareRecord:[],
            searchRecords:[]
          },
          reportData:{
            baseMessage:{},
            technicalCase:{},
            compareLiterature:[],
            compareRecord:[],
            searchRecord:[]
          },
          editMessage:[],
          reLoadBasicMessage:false,
          path:''
      };
    },
    watch: {
        reportTemplateId(){
            this.init()
        },
        editModel(){
            this.getData()
        },
    },
    computed: {
     },
    created() {},
    async mounted() {
        await this.init()
    },
    deactivated(){
        if(this.component){
            this.getCss(this.path)
        }
        
    },
    methods: {
        async init(){
            await this.getData()
            // this.loading = true
            // this.$nextTick(()=>{
            //     this.loading = false
            // })
            await this.getReportTemplate()
        },
        //获取报告所需信息
        async getData(){
            if(this.editModel && this.reportFileId){
                var params = {
                    id:this.reportFileId,
                    size:10,
                    current:1
                }
                this.$api.queryNoveltyReport(params).then(response=>{
                    if(response.code == 200){
                        var data = response.data.data
                        if(data.length>0){
                            var obj = data[0].dataSource
                            try{
                                this.reportData = JSON.parse(obj)
                            }catch{

                            }
                            
                        }
                    }
                })
                return
            }
            const [ReportMessage,TechnicalCase,ContrastFile,ContrastRecord,SearchRecord] = await Promise.allSettled([this.getReportMessage(),this.getTechnicalCase(),this.getContrastFile(), this.getContrastRecord(),this.getSearchRecord()])
            this.dataObj.baseMessage = ReportMessage.status =='fulfilled'?(ReportMessage.value[0]||{}):{}
            this.dataObj.technicalCase = TechnicalCase.status =='fulfilled'?(TechnicalCase.value||{}):{}
            this.dataObj.compareLiterature = ContrastFile.status =='fulfilled'?(ContrastFile.value||[]):[]
            this.dataObj.compareRecord = ContrastRecord.status =='fulfilled'?(ContrastRecord.value||[]):[]
            this.dataObj.searchRecord = SearchRecord.status =='fulfilled'?(SearchRecord.value||[]):[]
            for(const item of this.dataObj.compareLiterature){
                var patent = await this.getPatentMessage(item.literatureNo)
                item.patentMessage = patent
            }
            // this.getReportMessage()
            // this.getTechnicalCase()
            // this.getContrastFile()
            // this.getContrastRecord()
            // this.getSearchRecord()
        },
        //获取专利信息
        async getPatentMessage(patentNo){
            var params = {
                patentNo: patentNo
              }
              var api = 'selectPatentDetail'
              var patent = {}
              await this.$api[api](params).then(response => {
                if (response.code == 200) {
                  patent = response.data
                }
              }).catch(error => {
              })
              return patent
        },
        //获取报告基本信息
        async getReportMessage(type){
            var params = {
                projectId:this.projectId
            }
            if(type !=1){
                return await this.$api.queryNoveltyProject(params).then(response=> response.data.data)
            }
            
            this.$api.queryNoveltyProject(params).then(response=>{
                if(response.code == 200){
                    this.dataObj.baseMessage = response.data.data[0] || {}
                    this.reLoadBasicMessage = true
                    this.$nextTick(()=>{
                        this.reLoadBasicMessage = false
                    })
                }
            })
        },
        //获取技术方案信息
        async getTechnicalCase(){
            var params = {
                projectId:this.projectId
            }
            return await this.$api.queryTechnicalCase(params).then(response=> response.data)
            this.$api.queryTechnicalCase(params).then(response=>{
                if(response.code == 200){
                    this.dataObj.technicalCase = response.data || {}
                }
            }).catch(error=>{
            })
        },
        //获取对比文件
        async getContrastFile(){
            var params = {
                projectId:this.projectId
            }
            return await this.$api.queryComparePatent(params).then(response=> response.data.data)
        },
        //获取对比记录
        async getContrastRecord(){
            var params = {
                projectId:this.projectId
            }
            return await this.$api.queryNoveltyCompareRecord(params).then(response=> response.data)
        },
        //获取检索记录
        getSearchRecord(){
            var params = {
                projectId:this.projectId
            }
            return this.$api.queryNoveltySearchRecord(params).then(response=> response.data.data)
        },
        //获取报告模板
        async getReportTemplate(){
            this.loading  =true
            var params = {
                templateId:this.reportTemplateId
            }
            await this.$api.queryNoveltyTemplate(params).then(response=>{
                if(response.code = 200){
                    this.loading = false
                    var path=response.data.data[0].configMessage
                    this.path = path
                    this.getReportTemplateByPath(path)
                }
            }).catch(error=>{
                this.$message.error('未获取到模板信息')
                this.loading = false
            })
        },
        //根据报告名称获取显示的html
        async getReportTemplateByPath(path){
            this.loading = true
            var component1 = await this.getComponents(path)
            
            component1.mixins = [mixins2]
           
            if(!component1.template){
                this.loading = false
                return
            }
             this.component = component1
            this.loading = false
        },
        getMessage(){
            this.$refs.getMessageByNumber.open()
            
        },
        //保存修改的信息
        async saveEdit(type){
            var message = this.$message({
                message: '信息保存中...',
                type: 'warning',
                duration:0
            });
            var params = {
                id:this.reportFileId,
                projectId:this.projectId,
                templateId:this.reportTemplateId,
                dataSource:JSON.stringify(this.reportData)
            }
            await this.$api.editNoveltyReport(params).then(response=>{
                if(response.code == 200){
                    message.close()
                    this.$message.success('信息保存成功')
                    if(!this.reportFileId){
                        this.$router.replace(
                            {
                                path:'/exportReport',
                                query:{
                                    projectId:this.projectId,
                                    reportTemplateId:this.reportTemplateId,
                                    reportFileId:response.data,
                                    editModel:true
                                }
                            }
                        )
                    }
                }
            }).catch(error=>{
                message.close()
                this.$message.error('信息保存失败')
            })
        },
        //生成报告
        async reporting(){
            await this.saveEdit()
            if(!this.reportFileId){
                return
            }
            var message = this.$message({
                message: '报告生成中...',
                type: 'warning',
                duration:0
            });
            var params = {
                id:this.reportFileId
            }
            this.$api.exportNoveltyReport(params).then(response=>{
                if(response.code == 200){
                    message.close()
                    this.$message.success('报告生成成功')
                    const router = this.$router.resolve({
                        path: '/noveltySearchDetails',
                        query: {
                            projectId:this.projectId,
                            firstComponent:'reportFile'
                        }
                    })
                    window.open(router.href,"_self")
                }
            }).catch(error=>{
                message.close()
                this.$message.error('报告生成失败')
            })
        },
    },
  };
  </script>
  <style lang="scss">
      
  </style>
  <style lang="scss" scoped>
  .reportTemplate:empty{
    text-align: center;
    line-height: 100px;
  }
  .reportTemplate:empty::before{
    content: '模板不存在';
    font-style: normal;
    white-space: nowrap;
    color: #cacdd4;
  }
  </style>