export default{
    props: {
        conditions:{
          type:Object,
          default:()=>{
            return {}
          }
        },
        projectId:'',
         // 检索条件
        searchOption: {
          default: () => {
            return ''
          }
        },
        //号码检索
        numberQuery:{
          default: () => {
            return ''
          }
        },
        // 自定义栏位检索条件
        customFields: {
          default: () => {
            return []
          }
        },
        groupField:{
          default:''
        },
        refreshOption:{
          type:Boolean,
          default:false
        },
        field:{
          type:Array,
          default:()=>{
            return []
          }
        }
      },
    data() {
        return {
            activeNames:[],
        }
    },
    watch:{
      refreshOption(val){
        if(val){
          this.refreshOptions()
        }
      },
    },
    methods: {
        //刷新栏位
        async refreshField(){
           
        },
        // 条件改变时刷新已打开的折叠面板
        refreshOptions(sign) {
            for(let i = 0;i<this.field.length;i++){
                var index = this.activeNames.indexOf(i)
                if(index==-1){
                    continue
                }
                let row = this.field[i]
                row.optionsLength = 0
                this.getOptions(row)
            }
        },
        //点击折叠面板打开
        clickItem(item,index){
            var index1 = this.activeNames.indexOf(index)
            if(index1 == -1){
                return 
            }
            if(item.options && item.options.length>0){
                return
            }
            this.handleSearch(item)
        },
        //获取显示的名称
        getValueName(item,name){
          if(item.optionValue){
            var obj = item.optionValue.find(item=>{
              return item.value == name
            })
            if(obj){
              return obj.label
            }
          }
          return name
        },
        // 点击折叠面板中的搜索
        handleSearch(row) {
          row.optionsLength = 0
          this.getOptions(row)
        },
        //获取选项
        async getOptions(row,sign) {
          if(!sign){
            row.topN = 10
          }
          this.handleSelectCancel(row)
          if (row) {
            if (row.filedKind != -1 && (row.type == 'Array' || row.type == 'tree')) {
              if (row.filedKind == 0) {
                this.$set(row,'loading',true)
                const [num, option] = await Promise.allSettled([this.esCountAnalysis(row), this.queryCustomOption(row)])
                var nums = num.status == 'fulfilled' ? num.value.detailDTOS : []
                var options = option.status == 'fulfilled' ? option.value : []
                var obj = nums.find(item=>{
                  return item.name == '未选择'
                })
                if(obj && obj.number!=0){
                  options.push(
                    {
                      id: "未选择",
                      name: "未选择",
                      customFieldId:row.value
                    }
                  )
                }
                this.$set(row, 'options', options)
                this.$set(row,'loading',false)
                if (!sign) {
                 this.$set(row, 'check', []) 
                }
                
                this.$set(row, 'nums', nums)
              } else {
                var type = {
                  7: '2',//2产品
                  8: '1',//1产品类别
                  9: '3'// 3技术分类
                }
                const [num,option] = await Promise.allSettled([this.esCountAnalysis(row),this.queryProductCategory(row,type[row.filedKind])])
                var nums = num.status == 'fulfilled' ? num.value.detailDTOS : []
                var options = option.status == 'fulfilled' ? option.value : []
                this.$set(row, 'options', options)
                if (!sign) {
                 this.$set(row, 'check', []) 
                }
                this.$set(row, 'nums', nums)
              }
            } else {
              this.$set(row,'loading',true)
              const [num] = await Promise.allSettled([this.esCountAnalysis(row)])
              var nums = num.status == 'fulfilled' ? num.value.detailDTOS : []
              for(var i = 0;i<nums.length;i++){
                if(nums[i].name == '其他'){
                  nums.splice(i,1)
                  break;
                }
              }
              this.$set(row, 'options', nums)
              this.$set(row,'loading',false)
              if (!sign) {
                this.$set(row, 'check', []) 
              }
              if(nums.length>0){
                row.optionsLength = num.value.remainingNum
              }else{
                row.optionsLength = 0
              }
              if(row.optionsLength == 0){
                this.$set(row, 'isShowBtn', false)
              }else{
                this.$set(row, 'isShowBtn', true)
              }
            }
          }
        },
        async esCountAnalysis(row) {
          var countVOS = []
          if (row.filedKind == -1) {//专利本身栏位（专利著录）
            let value = (row.children && row.children.length>0) ? row.select : row.value
            countVOS = [
              {
                field: value,
                valueOne: row.valueOne,
                valueTwo: row.valueTwo,
                topN: row.topN,
                // fieldType: row.type
              }
            ]
          } else {//
            countVOS = [
              {
                field: 'field',
                fieldId: row.value,
                valueOne: row.valueOne,
                valueTwo: row.valueTwo,
                topN: 9999,//自定义栏位没有加载更多
                ifHaveChild: row.type == 'tree' ? this.ifHaveChild : false,
                fieldType: row.type
              },
            ]
          }
    
          let params = {
            condition: this.searchOption || '',//检索条件
            numberQuery:this.numberQuery,
            projectId: this.projectId,//项目id
            taskId: this.taskId,//任务id
            countVOS: countVOS,//栏位的聚合入参
            customFields: this.customFields,//自定义栏位查询
            groupField:this.groupField,
          }
          return await this.$api.esCountAnalysis(params).then(res => res.data)
        },
        // 查询自定义栏位选项数据
        async queryCustomOption(row) {
          let params = {
            customFieldId: row.value,
          }
          return await this.$api.queryCustomOption(params).then(response => response.data.data)
        },
        //获取产品或产品类别架构以及技术分类
        async queryProductCategory(item, type) {
          let params = {
            projectId: this.projectId,
            taskId: this.taskId,
            type: type,//类型：1产品类别，2产品，3技术分类，4自定义树
            typeId: item.value,//产品或类别id
          }
          return await this.$api.queryTreeNodeTree(params).then(response => response.data.data)
        },
        /**
         * 获取子组件选择的值
         * @param {*} value //选中的选项值
         * @param {*} row //选择选项所属栏位
         * @param {*} checked //是否选中
         * @param {*} name //树形属性选中的选项名称
         */
        checkData(value,row,checked,name) {
          if(checked){
            var obj = {
              field:{
                filedKind:row.filedKind,
                groupBy:row.groupBy,
                search:(row.children && row.children.length>0)?row.select:row.value,
                show:null
              },
              value:{
                search:value
              },
            }
            //获取检索栏位信息
            if(!this.conditions[obj.field.search]){
              if(row.children && row.children.length>0){
                var obj1 = row.children.find(item=>{
                  return item.value == row.select
                })
                if(obj1){
                  obj.field.show = obj1.label
                }else{
                  obj.field.show = row.name
                }
              }else{
                obj.field.show = row.name
              }
            }
            //获取检索值信息
            if(row.filedKind == -1){
              if(row.optionValue && row.optionValue.length>0){
                var obj2 = row.optionValue.find(item1=>{
                    return item1.value == value
                })
                if(obj2){
                  obj.value.show = obj2.label
                }else{
                  obj.value.show = value
                }
                
              }else{
                obj.value.show = value
              }
            }else{
              var obj2 = row.options.find(item1=>{
                return item1.id == value
              })
              if(obj2){
                obj.value.show = obj2.name
              }else{
                obj.value.show = value
              }
            }
    
            if(name){
              obj.name = name
            }
    
            if(this.conditions[obj.field.search]){
              this.conditions[obj.field.search].value.push(obj.value)
            }else{
              this.conditions[obj.field.search] = {
                field:obj.field,
                value:[obj.value],
                op:row.groupBy=='company'?'≡':'='
              }
            }
          }else {
            var searchField = (row.children && row.children.length>0)?row.select:row.value
            var condition = this.conditions[searchField]
            if(!condition){
              return
            }
            var values = condition.value
            var index = values.findIndex(item => {
              return item.search == value
            })
            if (index != -1) {
              values.splice(index,1)
            }
    
            if(values.length == 0){
              delete this.conditions[searchField]
            }
          }
        },
         // 全选
         handleSelectAll(val) {
          //勾选状态
          if (val.filedKind == -1) {//专利本身的全选
            val.check = val.options.map(item => {
              return item.name
            })
          } else {//其他
            val.check = val.options.map(item => {
              return item.id
            })
          }
    
          var field = {
            filedKind:val.filedKind,
            groupBy:val.groupBy,
            search:(val.children && val.children.length>0)?val.select:val.value,
            show:null
          }
          if(!this.conditions[field.search]){
            if(val.children && val.children.length>0){
              var obj1 = val.children.find(item=>{
                return item.value == val.select
              })
              if(obj1){
                field.show = obj1.label
              }else{
                field.show = val.name
              }
            }else{
              field.show = val.name
            }
            this.conditions[field.search] = {
              field:field,
              value:[],
              op:val.groupBy=='company'?'≡':'='
            }
          }
          
          var values = []
          for(var i = 0;i<val.check.length;i++){
            var value = val.check[i]
            var valueObj = {
              search:value
            }
            if(val.filedKind == -1){
              if(val.optionValue && val.optionValue.length>0){
                var obj2 = val.optionValue.find(item1=>{
                    return item1.value == value
                })
                if(obj2){
                  valueObj.show = obj2.label
                }else{
                  valueObj.show = value
                }
                
              }else{
                valueObj.show = value
              }
            }else{
              var obj2 = val.options.find(item1=>{
                return item1.id == value
              })
              if(obj2){
                valueObj.show = obj2.name
              }else{
                valueObj.show = value
              }
            }
            values.push(valueObj)
          }
          this.conditions[field.search].value = values
    
        },
        // 取消
        handleSelectCancel(row) {
          this.$set(row,'check',[])
          var searchField = (row.children && row.children.length>0)?row.select:row.value
          var condition = this.conditions[searchField]
          if(!condition){
            return
          }
          delete this.conditions[searchField]
        },
        // 加载更多
        handleLoading(val) {
          val.topN += 10
          this.getOptions(val,1)
        },
        //  专利IPC/CPC/UPC/LOC的选项变化
        selectChange(val, data) {
          this.getOptions(data)
        },
      },
}