
import moment from 'moment'
export default{
    props:{
        dataObj:{
            type:Object,
            default:()=>{
                return {
                    baseMessage:{},
                    technicalCase:{},
                    compareLiterature:[],
                    compareRecord:[],
                    searchRecord:[]
                }
            }
        },
        reportData:{
            type:Object,
            default:()=>{
                return {
                    baseMessage:{},
                    technicalCase:{},
                    compareLiterature:[],
                    compareRecord:[],
                    searchRecord:[]
                }
            }
        },
        editMessage:{
            type:Array,
            default:()=>{
                return []
            }
        },
        editModel:{
            type:Boolean,
            default:false
        },
        reLoadBasicMessage:{
            type:Boolean,
            default:false
        },
    },
    data() {
        return {
            nowEdit:{

            },
            hasCopy:{

            },
            dbTypes:{
                CN:'中国数据库',
                'WD':'全球数据库'
            }
        }
    },
    computed:{
        userinfo(){
            return this.$s.getObj('userinfo')
        },
    },
    watch:{
        reLoadBasicMessage(val){
            if(val){
                this.reLoad()
            }
        }
    },
    methods: {
        reLoad(){
            for(let key in this.hasCopy){
                if(key.indexOf('baseMessage') !=-1){
                    this.hasCopy[key] = false
                }
            }
            this.reportData.baseMessage = {}
        },
        setValue(data){
            var str = ''
            for(var i = 0;i<data.length;i++){
                var str2 = data[i].value
                if(str2){
                    str2 = str2.trim()
                    var char = str2.slice(-1)
                    var sign = this.isPunctuation(char)
                    if(sign){
                        str2 = str2.substring(0,str2.length-1)
                    }
                    str+=str2
                    if(i == data.length - 1){
                        str+='。'
                    }else{
                        str+='，'
                    }
                }
            }
            return str
        },
        //判断是不是标点符号
        isPunctuation(char) {  
            // 定义一个更全面的标点符号正则表达式，基于 Unicode 范围  
            const punctuationRegex = /[\p{P}\p{So}]/u;  
            // 测试字符是否匹配正则表达式  
            return punctuationRegex.test(char);  
        },
        //获取最后一次检索记录的检索时间
        getSearchTime(){
            if(!this.dataObj.searchRecord || this.dataObj.searchRecord.length == 0){
                return ''
            }
            var data = JSON.parse(JSON.stringify(this.dataObj.searchRecord))
            var obj = data.sort((a, b) => (a.retrieveTime?new Date(a.retrieveTime):0) - (b.retrieveTime?new Date(b.retrieveTime):0))[data.length-1]
            if(obj){
                if(obj.retrieveTime){
                    var date = moment(obj.retrieveTime).format('YYYY-MM-DD')
                    return date
                }
                return obj.retrieveTime
            }
            return ''
        },
        saveMessage2(check,value,field,property){
            var data = this.reportData[field]
            if(property){
                data = this.reportData[field][property]
            }
            if(typeof data == 'object'){
                if(check){
                    data.push(value)
                }else{
                    var index = data.indexOf(value)
                    if(index!=-1){
                        data.splice(index,1)
                    }
                }
            }else{
                if(check){
                    this.$set(data,'',value)
                }else{
                    this.$set(data,'','')  
                }
            }
            // this.$set(this.reportData[field],property,value)
        },
        getColumnData(field,property,value){
            var key = field + '-' + property
            if(!this.editModel && !this.hasCopy[key]){
                if(value){
                    var value1 = value
                    if(typeof value == 'string'){
                        value1 = value.replace(/\n/g,'<br>')
                    }
                    else if(typeof value == 'function'){
                        value1 = value()
                    }
                    if(property){
                        this.$set(this.reportData[field],property,value1)
                    }else{
                        this.$set(this.reportData,field,value1)
                    }
                }else{
                    if(property){
                        this.$set(this.reportData[field],property,'')
                    }else{
                        this.$set(this.reportData,field,'')
                    }
                }
                
                this.hasCopy[key] = true
            }
            if(property){
                return this.reportData[field][property];
            }else{
                if(field == 'compareLiterature' || field == 'searchRecord'){
                    if(this.reportData[field].length==0){
                        this.reportData[field].push({})
                    }
                }
                return this.reportData[field]
            }
            
            
            
        },
        //分组
        groupBy(arr,key){
            return arr.reduce((acc,obj)=>{
                const groupKey = obj[key]
                if(!acc[groupKey]){
                    acc[groupKey] = [];
                }
                acc[groupKey].push(obj)
                return acc
            },{})
        },
        //模板数据格式统一的公用
        getCompareRecordData2(type,field,property){
            if(!type){
                var data = this.reportData[field]
                if(property){
                    data = data[property]
                }
                return data
            }
            var key = field + '-' + property
            if(!this.editModel && !this.hasCopy[key]){
                if(this.dataObj.compareLiterature && this.dataObj.compareLiterature.length>0){
                    var data = []
                    var that = this
                    var compareRecord = {}
                    if(this.dataObj.compareRecord && this.dataObj.compareRecord.length>0){
                        compareRecord = this.groupBy(this.dataObj.compareRecord,'patentNo')
                    }
                    this.dataObj.compareLiterature.forEach(async (item,index)=>{
                        item.patentNo = item.literatureNo
                        var compareRecord1 = {
                            index:index+1,
                            compareFile:`对比文件${index+1}：${item.patentNo}-${item.name}：`,
                            analysis:'',
                            compareRecord:[]
                        }
                        
                        var num = 1
                        if(item.noveltyCompareRecordVOS && item.noveltyCompareRecordVOS.length>0){
                            // var patent = await that.getPatentMessage(item.patentNo)
                            var patent = item.patentMessage
                            var abstractStr = ''
                            if(patent.abstractStr){
                                var patentObj = patent.abstractStr.find(item => {
                                    return item.if_origin
                                })
                                if (patentObj) {
                                    abstractStr = patentObj.text_content
                                }
                            }
                            
                            item.noveltyCompareRecordVOS.forEach(i=>{
                                var obj = {
                                    index:num,
                                    compareText:abstractStr?`[摘要原文]${abstractStr}`:'',
                                    compareDescription:i.markNoteText
                                }
                                compareRecord1.compareRecord.push(obj)
                                num+=1
                            })
                        }
                        var compareRecordData = compareRecord[item.patentNo]||[]
                        if(compareRecordData.length>0){
                            compareRecordData.forEach((i,ind)=>{
                                if(ind == 0){
                                    if(i.generalRemark){
                                        compareRecord1.analysis = i.generalRemark
                                    }
                                }
                                var obj = {
                                    index:num,
                                    compareText:`[${i.markSelectField}]${i.markSelectText}`,
                                    compareDescription:""
                                }
                                var str = i.markNoteText
                                if(i.technicalFeature){
                                    str = `揭示了“${i.technicalFeature}”${str}`
                                }
                                obj.compareDescription = str
                                compareRecord1.compareRecord.push(obj)
                                num+=1
                            })
                        }
                        data.push(compareRecord1)
                    })
                    if(type == 'string'){
                        var str = ''
                        data.forEach(item=>{
                            str = `${str}${item.compareFile}<br>`
                            if(item.compareRecord.length>0){
                                item.compareRecord.forEach(i=>{
                                    str = `${str}  ${i.compareText}（${i.compareDescription}）<br>`
                                })
                            }
                            if(item.analysis){
                                str = `${str}  ${item.analysis}<br>`
                            }
                        })
                        this.setColumnData(str,field,property)
                    }else{
                        this.setColumnData(data,field,property)
                    }
                    
                }else{
                    if(type == 'string'){
                        this.setColumnData('',field,property)
                    }else{
                        this.setColumnData([
                            {
                                index:1,
                                compareFile:"",
                                analysis:'',
                                compareRecord:[]
                            }
                        ],field,property)
                    }
                    
                }
                this.hasCopy[key] = true
            }
            return this.getCompareRecordData2(null,field,property)
        },
        //装载数据
        setColumnData(data,field,property){
            if(property){
                this.$set(this.reportData[field],property,data)
            }else{
                this.$set(this.reportData,field,data)
            }
        },
        //获取对比记录所需要的数据
        getCompareRecordData(column,props=[]){
            var key = 'compareRecord'
            if(!this.editModel && !this.hasCopy[key]){

                if(this.dataObj.compareLiterature && this.dataObj.compareLiterature.length>0){
                    var data = []
                    var that = this
                    var compareRecord = {}
                    if(this.dataObj.compareRecord && this.dataObj.compareRecord.length>0){
                        compareRecord = this.groupBy(this.dataObj.compareRecord,'patentNo')
                    }
                    this.dataObj.compareLiterature.forEach(async (item,index)=>{
                        item.patentNo = item.literatureNo
                        var compareRecord1 = {
                            index:index+1,
                            patentNo:item.literatureNo,
                            data:compareRecord[item.patentNo]||[],
                            compareLiterature:item,
                            noveltyCompareRecordVOS:item.noveltyCompareRecordVOS,
                            patentMessage:item.patentMessage
                        }
                        // if(props.indexOf('patentMessage') !=-1){
                        //     var patent = await that.getPatentMessage(item.patentNo)
                        //     compareRecord1.patentMessage = patent
                        // }
                        var obj = {
                            index:index+1
                        }
                        column.forEach(co=>{
                            var field = co.key
                            obj[field] = that.$commonJS.getColumnData(compareRecord1,co)
                            
                        })
                        data.push(obj)
                        
                    })
                    this.$set(this.reportData,'compareRecord',data)
                }else{
                    this.$set(this.reportData,'compareRecord',[{index:1}])
                }
                
                // if(this.dataObj.compareRecord && this.dataObj.compareRecord.length>0){
                //     var data = []
                //     var that = this
                //     var compareRecord = this.groupBy(this.dataObj.compareRecord,'patentNo')
                //     var patents = Object.keys(compareRecord)
                //     patents.forEach(async (item,index) => {
                //         //获取专利信息
                //         var compareRecord1 = {
                //             patentNo:item,
                //             data:compareRecord[item]
                //         }
                //         if(props.indexOf('patentMessage') !=-1){
                //             var patent = await that.getPatentMessage(item)
                //             compareRecord1.patentMessage = patent
                //         }
                //         if(props.indexOf('noveltyRemark') !=-1){
                //             var obj = this.dataObj.compareLiterature.find(file=>{
                //                 return file.literatureNo == item
                //             })
                //             if(obj){
                //                 if(obj.noveltyCompareRecordVOS && obj.noveltyCompareRecordVOS.length>0){
                //                     compareRecord1.noveltyCompareRecordVOS = obj.noveltyCompareRecordVOS
                //                 }
                //             }
                //         }
                        
                //         var obj = {
                //             index:index+1
                //         }
                //         column.forEach(co=>{
                //             var field = co.key
                //             obj[field] = that.$commonJS.getColumnData(compareRecord1,co)
                            
                //         })
                //         data.push(obj)
                //     });
                //     this.$set(this.reportData,'compareRecord',data)
                // }else{
                //     this.$set(this.reportData,'compareRecord',[{index:1}])
                // }
                this.hasCopy[key] = true
            }
            // if(this.reportData.compareRecord.length == 0){
            //     return this.reportData.compareRecord.push({index:1})
            // }
            return this.reportData.compareRecord
        },
        //获取数字以及其他部分
        getNumberAndStr(data=''){
            if(typeof data == 'number'){
                data = data+''
            }
            var nums = data.match(/\d+/g)
            var num = 0
            if(nums && nums.length>0){
                num = nums[0]
            }
            var str = data.replace(/\d+/g,'') || ''
            return {
                num:num,
                str:str
            }
        },
        delCompareRecord(data,index){
            data.splice(index,1)
            for(var i = index;i<data.length;i++){
                var item = data[i]
                var obj = this.getNumberAndStr(item.index)
                var str = obj.str
                if(obj.num || obj.num == 0){
                    item.index =str + (Number(obj.num) - 1)
                }else{
                    item.index = str + (i + 1)
                }
                
            }
        },
        addCompareRecord(data,index){
            var obj1 = this.getNumberAndStr(data[index].index)
            var str1 = obj1.str
            data.splice(index+1,0,{index:str1 + (index+1)})
            for(var i = index+1;i<data.length;i++){
                var item = data[i]
                var obj = this.getNumberAndStr(item.index)
                var str = obj.str
                if(obj.num || obj.num == 0){
                    item.index =str + (Number(obj.num) + 1)
                }else{
                    item.index = str + (i + 1)
                }
            }
        },
        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
        },
        edit(field,property,index=''){
            this.nowEdit = {
                field:field,
                property:property,
                index:index,
            }
            try{
                var editValue = this.reportData
                if(typeof field == 'object'){
                    editValue = this.getDataByField(field)
                    editValue = this.getDataByField({property:property,index:index},editValue)
                }else{
                    editValue = this.getDataByField({field:field,property:property,index:index})
                }
                
                this.nowEdit.value = editValue 
            }catch{}
            var obj = this.editMessage.find(item=>{
                return item.field == field && item.property == property && item.index == index
            })
 
            if(obj){
                return
            }
            this.editMessage.push(
                {
                    ...this.nowEdit
                }
            )
        },
        getDataByField(field,currentData=this.reportData){
            var {field,property,index} = field
            var data = currentData
            if(field){
                data = data[field]
            }
            if(index === 0 || index){
                data = data[index]
            }
            if(property){
                data = data[property]
            }
            return data
        },
        isEdit(field,property,index=''){
            if(this.$_.isEqual(this.nowEdit.field,field) && this.nowEdit.property==property && this.nowEdit.index == index){
                return true
            }
            return false
        },
        clearEdit(){
            this.nowEdit = {}
        },
        saveMessage(value,fu){
            if(value == this.nowEdit.value){
                this.clearEdit()
                return
            }
            //执行保存
            var field = this.nowEdit.field
            var property = this.nowEdit.property
            var index = this.nowEdit.index

            var editValue = this.reportData
            if(typeof field == 'object'){
                editValue = this.getDataByField(field)
                if(index ===0 || index){
                    this.$set(editValue[index],property,value)
                }else{
                    this.$set(editValue,property,value)
                }
            }else{
                if(index ===0 || index){
                    this.$set(this.reportData[field][index],property,value)
                }else{
                    this.$set(this.reportData[field],property,value)
                }
            }
            
            
            if(fu && typeof fu == 'function'){
              fu(value)  
            }
            
            this.clearEdit()
        },

        // 图片变化的change事件
        handleChange(row,file, fileList) {
            // file.raw.url = file.url
            // row.push(file.raw)
            this.uploadFile(row,file)
        },
        // 文件列表移除文件
        delImage(row,file,index) {
            row.splice(index, 1)
            return
            // let index = this.reportData.signMessage.systemFileList.findIndex(item => {
            // if (file.guid) {
            //     return item.guid == file.guid
            // } else {
            //     return item.uid == file.uid
            // }
            // })
            // if (index != -1) {
            //     this.reportData.signMessage.systemFileList.splice(index, 1)
            // }
        },
        //上传附件
        uploadFile(data,file) {
            let formData = new FormData()
            formData.append('sourceId', this.$constants.sourceId)
            formData.append('files', file.raw)
            this.$api.uploadFile(formData).then(response => {
            if (response.code == 200) {
                var guid = response.data[0]
                data.push({url:guid})
            }
            })
        },
    },
}