<template>
  <!-- 选择专利弹窗 -->
  <div>
    <el-dialog title="选择专利" :visible.sync="dialogVisible" width="800px" :before-close="handleClose">
      <el-container>
        <el-header style="display: flex;align-items: center;">
          <slot name="head">
            <div id="step1">
              <!-- <mySearch style="width: 500px" :SearchFields="searchFiled" @search="search" :searchValue="searchOption">
              </mySearch> -->
              <searchPatent ref="searchPatent" :searchFiled="searchFiled"
              @searchValue="search"></searchPatent>
            </div>
          </slot>
          
        </el-header>
        <el-main style="height:calc(100vh - 300px)">
          <Table :column="column" :tableData="tableData" :queryParams="queryParams" :refresh="refresh" v-loading="loading"
            :patentNoList.sync="patentNoList" :projectId="projectId" @on-sort="handleSort"></Table>
        </el-main>
        <el-footer>
          <div style="display: flex;justify-content: space-between;margin-bottom: 10px;">
            <div>
              <el-button type="primary" @click="selectPage">本页选择</el-button>
            </div>
            <div>
              <el-pagination background layout="total, sizes, prev, pager, next, jumper"
                :current-page.sync="queryParams.current" :page-size.sync="queryParams.size"
                @current-change="handleCurrentChange" @size-change="getList" :total="total">
              </el-pagination>
            </div>

          </div>
        </el-footer>
      </el-container>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="handleSure">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Table from '@/views/project/patentCollection/components/views/Table.vue'
import searchPatent from '@/views/report/components/patentList/components/searchPatent.vue';
export default {
  components: {
    Table,
    searchPatent
  },
  props: {
    customFields: {
      default: () => {
        return []
      }
    },
    oldSearchOption: {
      default: () => {
        return ''
      }
    },
    sign:'',
    compareResult:{
      default:''
    }
  },
  data() {
    return {
      projectId: '',
      // 控制弹窗的打开关闭
      dialogVisible: false,
      //检索字段
      searchFiled: [],
      //检索条件
      searchOption: '',
      // 分页信息
      queryParams: {
        current: 1,
        size: 10,
      },
      total: 0,
      // 表格信息
      loading: false,
      // 表格数据
      tableData: [],
      checkList: [],
      // 表格栏位
      column: [
        {
          createType: 1,
          ifHidden: false,
          ifSearch: false,
          ifShow: true,
          name: "专利号",
          order: 0,
          type: "String",
          value: "patentNo",
        },
        {
          createType: 1,
          ifHidden: false,
          ifSearch: false,
          ifShow: true,
          name: "标题",
          order: 9,
          type: "Object",
          value: "title",
        },
        {
          createType: 1,
          ifHidden: false,
          ifSearch: false,
          ifShow: true,
          name: "专利状态",
          order: 19,
          type: "String",
          value: "SS",
        },
      ],
      // 选中的数据
      patentNoList: [],
      refresh: true,
      //排序
      sort: [],
      patentNos:[]
    }
  },
  watch:{
    compareResult(){
      this.getPatentList()
    }
  },
  async mounted() {
    
    await this.getColumn()
  },
  methods: {
    // 获取信息
    async getPatentList() {
      let params = {
        ...this.queryParams,
          projectId: this.projectId,
          compareResult: this.compareResult
        }
      await this.$api.getComparedPatent(params).then(response => {
        if (response.code == 200) {
            this.total = response.data.total
            this.tableData = response.data.data
        }
      }).catch(error => {
        this.total = 0
        this.tableData = []
      })
    },
    // 本页选择
    selectPage() {
      this.patentNoList = this.tableData.map(item => item.patentNo)
      this.refreshTable()
    },
    //刷新
    refreshTable() {
      this.refresh = false
      this.$nextTick(() => {
        this.refresh = true
      })
    },
    //获取table栏位及分组字段、检索字段
    async getColumn() {
      let params = [{
        tableName: 'patent',
        projectId: 0,
      }]
      this.searchFiled = []
      await this.$api.getQueryFields(params).then(res => {
        if (res.code == 200) {
          let conditionDTOList = JSON.parse(JSON.stringify(res.data.data[0].conditionDTOList))
          let field = conditionDTOList.filter(item => {
            return item.group == 'nos'
          })
          let custom = conditionDTOList.filter(item => {
            return item.group == 'customField'
          })
          let customField = custom.filter(item => {
            return item.type != 'tree' && item.type != 'Array'
          })
          let arr = field.concat(customField)
          // 搜索字段
          this.searchFiled = this.$commonJS.getField(arr, (u) => u.ifSearch == true, {
            label: 'name',
            value: 'field',
            type: 'type',
            group: 'group',
            fieldType: 'fieldType',
            groupBy: 'groupBy',
            children:'children'
          })
        }
      })
      // let params = ['patent']
      // await this.$api.getParamsCommon(params).then(res => {
      //   if (res.code == 200) {
      //     // let conditionDTOList = JSON.parse(JSON.stringify(res.data[0].conditionDTOList))
      //     // // 搜索字段
      //     // this.searchFiled = this.$commonJS.getField(conditionDTOList, (u) => u.ifSearch == true, {
      //     //   label: 'name',
      //     //   value: 'value',
      //     //   type: 'type',
      //     // })
      //   }
      // })
    },
    //检索
    // search(val) {
    //   let params = {}
    //   val.forEach(item => {
    //     if (item.type == 3) {
    //       params[item.value] = item.searchValue.map(itemValue => {
    //         return itemValue.value
    //       })
    //     } else {
    //       params[item.value] = item.searchValue.label
    //     }
    //   })
    //   // 返回字符串
    //   this.searchOption = this.$commonJS.ArrayToArray(val)
    //   // 调用查询接口
    //   this.queryParams.current = 1
    //   this.getList()
    // },
    search({ searchStr, customFields }) {
      this.searchOption = searchStr
      this.queryParams.current = 1
      this.getList()
    },
    // 打开弹窗
    open(projectId,patentNoList) {
      this.refresh = true
      this.projectId = projectId
      if (patentNoList) {
        this.patentNos = patentNoList
      }
      if (this.sign == 'teamwork') {
        this.getPatentList()
      } else {
        this.getList()
      }
      
      this.dialogVisible = true
    },
    // 弹窗确定
    handleSure() {
      this.$emit('checkPatentList', this.patentNoList)
      this.handleClose()
    },
    // 关闭弹窗
    handleClose() {
      this.patentNoList = []
      this.refresh = false
      this.dialogVisible = false
    },

    // 分页信息
    handleCurrentChange(val) {
      this.queryParams.current = val
      if (this.sign == 'teamwork') {
        this.getPatentList()
        return 
      }
      this.getList()
    },
    //排序
    handleSort({ column, prop, order }) {
      //如需要多个字段排序，则不需要清空
      var params = {
        sort: this.sort,
        column,
        prop,
        order,
      }
      this.sort = this.$commonJS.getSortData(params)
      this.queryParams.current = 1
      this.getList()
    },
    getList() {
      // var searchOption = {
      //   ...this.oldSearchOption,
      //   ...this.searchOption
      // }
      // var searchOption1 = this.searchOption
      // if(this.searchOption.constructor == Object){
      //   searchOption1 = this.$commonJS.objectToArray(this.searchOption)
      // }
      // if(this.oldSearchOption.constructor == Object){
      //   oldSearchOption = this.$commonJS.objectToArray(this.oldSearchOption)
      // }
      // let searchOption = [
      //   // ...oldSearchOption,//固有检索字段
      //   ...searchOption1
      // ]
      // var str = this.$commonJS.objectToString(searchOption)
      // var searchStr =  (this.oldSearchOption?(' AND ' + (this.oldSearchOption)):'')
      var searchStr =  this.searchOption + ((this.searchOption && this.oldSearchOption)?' AND ':'') + (this.oldSearchOption || '')
      var patentNoStr = ''
      this.patentNos.forEach((item, index) =>{
        if (index == 0) {
          patentNoStr = patentNoStr + `patentNo=${item}`
        } else {
          patentNoStr = patentNoStr + ` And patentNo=${item}`
        }
      })
      let params = {
        ...this.queryParams,//分页信息
        projectId: this.projectId,
        searchQuery: searchStr + ((patentNoStr)?(' Not '+`(${patentNoStr})`):''),//检索条件
        customFields: this.customFields,
        orderDTOList: this.sort,//排序信息
      }
      this.loading = true
      this.$api.QueryPatent(params).then(res => {
        if (res.code == 200) {
          this.tableData = res.data.data
          this.total = res.data.total
          this.loading = false
        }
      }).catch(error => {
        this.tableData = []
        this.total = 0
        this.loading = false
      })
    },
  },
}
</script>

<style lang="scss" scoped></style>