<template>
  <!-- 任务专利列表页面 -->
  <div class="height_100">
    <div class="height_100 content" :style="{ 'flex-direction': (taskId ? 'row-reverse' : 'row') }">
      <div class="height_100  left" :style="{ width: showRight ? `calc(100% - 300px)` : '100%' }">
        <el-container>
          <el-header style="padding:0 10px !important;">
            <div style="display: flex;align-items: center;">
              <div v-if="taskId">
                <el-tooltip class="item" effect="dark" :content="(showRight ? '隐藏' : '显示') + '右侧自定义栏位'" placement="top">
                  <i :class="!showRight ? 'el-icon-s-unfold' : 'el-icon-s-fold'"
                    style="font-size: 25px; margin-left: 10px;color: #2f2f2f; cursor: pointer"
                    @click="showRight = !showRight"></i>
                </el-tooltip>
              </div>
              <!-- <mySearch style="width:500px" :SearchFields="searchFiled" @search="search" :searchValue="searchOption">
              </mySearch> -->
              <searchPatent ref="searchPatent" :searchFiled="searchFiled" :searchPatentCheck="searchPatentCheck"
                @searchValue="handleSearchValue"></searchPatent>
            </div>
            <el-button v-if="taskId" type="primary" size="small" @click="handleFieldManage">显示栏位管理</el-button>
            <div v-else style="display:flex;align-items:center">
              <el-dropdown id="step4" trigger="click" split-button type="primary" size="small" @click="handleFieldManage"
                @command="handleCommand($event)">
                <span v-disabled="false">显示栏位管理</span>
                <el-dropdown-menu slot="dropdown" class="text-align_center">
                  <el-dropdown-item command="1">任务分配</el-dropdown-item>
                  <el-dropdown-item>
                    <el-dropdown trigger="hover" placement="right-start" @command="handleCommand($event)">
                      <p>导入专利<i class="el-icon-arrow-right el-icon--right"></i></p>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item command="2">专利号导入</el-dropdown-item>
                        <el-dropdown-item command="3">Excel导入</el-dropdown-item>
                        <el-dropdown-item command="4">检索导入</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
              <div>
                <el-tooltip class="item" effect="dark" :content="(showRight ? '隐藏' : '显示') + '右侧自定义栏位'" placement="top">
                  <i :class="showRight ? 'el-icon-s-unfold' : 'el-icon-s-fold'"
                    style="font-size: 25px; margin-left: 10px;color: #2f2f2f; cursor: pointer"
                    @click="showRight = !showRight"></i>
                </el-tooltip>
              </div>
            </div>
          </el-header>
          <el-main class="height_100">
            <component :is='components' :tableData="tableData" :column="fieldList" :projectId="projectId"
              @on-sort="handleSort" :queryParams="queryParams" @clickPatentNo="handleClick"
              @ifHaveChild="handleIfHaveChild"></component>
          </el-main>
          <el-footer class="pagination">
            <el-pagination background layout="total, sizes, prev, pager, next, jumper"
              :current-page.sync="queryParams.current" :page-sizes="sizeArr" :page-size.sync="queryParams.size"
              @current-change="handleCurrentChange" @size-change="getList" :total="total">
            </el-pagination>
          </el-footer>
        </el-container>
      </div>
      <div class="height_100 right" v-show="showRight">
        <customFields ref="customFields" :projectId="projectId" :taskId="taskId" :searchOption="searchStr" :customFields="customFields"
          @customTab="handleCustomFields" :searchPatentCheck="searchPatentCheck"></customFields>
      </div>
    </div>

    <field ref="field" type="patent" :projectId="projectId" :taskId="taskId" @getFieldList="getFieldList"></field>
  </div>
</template>

<script>
import field from '@/views/components/dialog/fields.vue';
import PatentTableListView from '@/views/project/patentCollection/components/views/Table.vue'
import customFields from './customFields/index.vue';
import searchPatent from './searchPatent.vue'
export default {
  props: {
    taskId: {
      default: null
    },
    projectId: {
      default: 0
    },
    taskType: {
      default: 0
    },
    reportType: {
      default: ''
    },
    isResult: {//1处理2查看3确认结果
      default: ''
    },
    handlerType: {//1内部人员0外部人员（只有外部人员做协同才会传递）
      default: ''
    },
  },
  components: {
    PatentTableListView,
    field,
    customFields,
    searchPatent,
  },
  data() {
    return {
      //显示组件
      components: 'PatentTableListView',
      //显示左側
      showRight: false,
      //检索字段
      searchFiled: [],
      //检索条件
      // searchOption: {},
      //专利数据库数据总数
      total: 0,
      //分页信息
      queryParams: {
        size: 10,
        current: 1,
      },
      //页大小
      sizeArr: [10, 20, 30, 40, 50, 100],
      //排序
      sort: [],
      //专利列表
      tableData: [],
      // 获取显示自定义栏位
      fieldList: [],
      // 统计勾选的值（专利著录（专利本身））
      searchPatentCheck: [],
      // 分类选择是否关联子类
      ifHaveChild: false,
      // 存放专利本身检索条件
      searchStr: '',
      // 存放自定义栏位的检索条件
      customFields: [],
    }
  },
  computed: {},
  async mounted() {
    this.fieldList = await this.$commonJS.getCustomField('patent', { projectId: this.projectId, taskId: this.taskId })
    this.getList()
    await this.getColumn()
  },
  methods: {
    // 处理后的条件
    handleSearchValue({ searchStr, customFields }) {
      this.searchStr = searchStr
      this.customFields = customFields
      this.$refs.customFields.close()
      this.getList()
    },
    // 树类型是否包括子分类
    handleIfHaveChild(val) {
      this.ifHaveChild = val
    },
    // 统计组件传的值
    handleCustomFields(val) {
      this.searchPatentCheck = []
      val.forEach(item => {
        if (item.check && item.check.length > 0) {
          if (item.filedKind == -1) {
            this.searchPatentCheck.push(item)
          } else {
            var checkName = []
            if (item.type == 'Array' || item.type == 'tree' || item.type == 'DateTime') {
              checkName = item.options.filter(item2 => item.check.includes(item2.id || item2.name)).map(item3 => item3.name); 
            }
            var obj = {
              value: item.value,
              name: item.name,
              filedKind: item.filedKind,
              check: item.check,
              checkName: checkName,//自定义栏位多选
              type:item.type,
              ifHaveChild: this.ifHaveChild
            }
            // return
            this.searchPatentCheck.push(obj)
          }
        }
      })
      this.$nextTick(() => {
        this.$refs.searchPatent.search2()
      })
    },
    // 处理任务
    handleClick(row, location) {
      var search = {
        searchOption: this.searchStr,
        location: location
      }
      this.$s.setSession('search', search)
      // if (this.taskType) {//FTO标引任务
      this.$router.push(
        {
          path: '/patentDetails/' + row.patentNo,
          query: {
            projectId: this.projectId,
            taskId: this.taskId,
            isResult: this.isResult,
            taskType: this.taskType,
            reportType: this.reportType,
            handlerType: this.handlerType || '',
          }
        }
      )
      // } else {//FTO对比任务协同任务
      //   this.$router.push(
      //     {
      //       path: '/tortIndex',
      //       query: {
      //         projectId: this.projectId,
      //         taskId: this.taskId,
      //         reportType: this.reportType,
      //         patentNo: row.patentNo,
      //         isResult:this.isResult,
      //         handlerType:this.handlerType || '',
      //       }
      //     }
      //   )
      // }

    },
    //获取检索字段
    async getColumn() {
      let params = [{
        tableName: 'patent',
        projectId: this.projectId,
      }]
      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',
          })
        }
      })
    },


    //获取专利列表
    getList() {
      let params = {
        ...this.queryParams,//分页信息
        projectId: this.projectId,
        taskId: this.taskId,
        searchQuery: this.searchStr || '',//检索条件
        customFields: this.customFields || [],
        orderDTOList: this.sort,//排序信息
      }
      this.$api.QueryPatent(params).then(res => {
        if (res.code == 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      }).catch(error => {
        this.tableData = []
        this.total = 0
      })
    },
    //排序
    handleSort({ column, prop, order }) {
      //如需要多个字段排序，则不需要清空
      var params = {
        sort: this.sort,
        column,
        prop,
        order,
      }
      this.sort = this.$commonJS.getSortData(params)
      this.queryParams.current = 1
      this.getList()
    },
    //分页
    handleCurrentChange(val) {
      this.queryParams.current = val;
      this.getList();
    },
    //显示栏位管理
    handleFieldManage() {
      this.$refs.field.open(this.fieldList)
    },
    // 显示管理旁下拉菜单
    handleCommand(event) {
      switch (event) {
        case '1':
          this.handleAllocation()
          break;
        case '2'://专利号
          this.patentImport(2)
          break;
        case '3'://excel
          this.patentImport(1)
          break;
        case '4'://检索导入
          this.patentImport(4)
          break;

        default:
          break;
      }
    },
    //专利导入
    patentImport(type) {
      var form = {
        importToId: this.projectId,
        importToType: 1,
        type: type,
      }
      this.$commonJS.toImportParent(form, type)
    },
    // 跳转分配任务页面
    handleAllocation() {
      this.$router.push(
        {
          path: '/allocationTask',
          query: {
            projectId: this.projectId,
            searchOption: JSON.stringify(this.searchStr),//输入框检索条件
            customFields: JSON.stringify(this.customFields),//右侧勾选自定义栏位的条件
          }
        }
      )
    },
    //获取显示自定义栏位
    getFieldList(data) {
      this.fieldList = data
    },
  },
}
</script>

<style lang="scss" scoped>
.content {
  display: flex;
  max-width: 100%;

  .right {
    min-width: 300px;
  }

  .left {
    width: 100%;
  }
}
</style>