<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" ref="myDropdown" trigger="click" :hide-on-click="false" split-button type="primary" size="small" @click="handleFieldManage"
                @command="handleCommand($event)">
                <span>显示栏位管理</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-item v-if="projectType == 2 && assProjectId" command="5">从专题库导入</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" v-loading="loading" style="padding: 0 !important;">
            <div class="patent-list-container">
              <div class="height_100"> 
                <component :is='components' :tableData="tableData" :column="fieldList" :projectId="projectId"
                  @on-sort="handleSort" :queryParams="queryParams" @clickPatentNo="handleClick"
                  @ifHaveChild="handleIfHaveChild" :refresh='refresh' @index-setting="handleIndexSetting" :patentNoList.sync="patentNoList" @addSelect="getChoosePatentNo"></component>
              </div>
            </div>
          </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-footer class="pagination" style="display:flex;justify-content:space-between;align-items:center">
          <div>
            <div style="display:flex;align-items:center" v-if="taskType == 0">
              <span v-if="selectedTotal > 0">
                已勾选 <b>{{ selectedTotal }}</b> 条
              </span>
              <el-popover placement="bottom" title="" width="220" trigger="click">
                <el-main class="patent-fast-edit-popover" v-loading="selectNumberLoading">
                  <div class="btn" @click="handleCancelSelectNumber">取消选择</div>
                  <div class="btn" @click="handleSelectNumber(0)">本页选择</div>
                  <div class="btn" @click="handleSelectNumber(1)">全部选择</div>
                  <el-divider></el-divider>
                  <div class="select-number">
                    <span>从</span>
                    <el-input size="mini" v-model="queryParams.startNumber" @change="change1"></el-input>
                    <span>到</span>
                    <el-input size="mini" v-model="queryParams.endNumber" @change="change2"></el-input>
                    <el-button type="text" size="" @click="handleSelectNumber(2)">确定</el-button>
                  </div>
                </el-main>
                <el-button type="info" size="small" class="margin-left_10" slot="reference"> 选择专利<i
                    class="el-icon-arrow-down el-icon--right"></i></el-button>
              </el-popover>

              <div v-show="selectedTotal>0" class="margin-left_10">
                <el-dropdown size="medium">
                  <el-button type="primary" size="small">
                    批量操作<i class="el-icon-arrow-down el-icon--right"></i>
                  </el-button>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="handleIndex">批量标引</el-dropdown-item>
                    <el-dropdown-item @click.native="deletePatents" v-if="!taskId">删除</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </div>
            </div>
          </div>
         
          <div>
            <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="changeSize" :total="total">
            </el-pagination>
          </div>

        </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>

     <!-- 批量那个标引 -->
     <PatentBatchIndexVue ref="PatentBatchIndexVue" @submit="submitIndex(true)" :taskId="taskId" :projectId="projectId"></PatentBatchIndexVue>
    <!-- 单个栏位标引 -->
    <patent-index-setting @submit="submitIndex(false)" :taskId="taskId" :projectId="projectId" ref="patentIndexSetting" />
  </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'

import { fastSelectPatent } from '@/views/project/patentCollection/components/mixins/fastSelectPatent'
import PatentBatchIndexVue from '@/views/project/patentCollection/components/dialog/PatentBatchIndex.vue'
import PatentIndexSetting from "@/views/project/patentCollection/components/dialog/PatentIndexSetting"
export default {
  mixins: [fastSelectPatent],
  props: {
    taskId: {
      default: null
    },
    projectId: {
      default: 0
    },
    assProjectId:{

    },
    projectName:{

    },
    projectType:{

    },
    taskType: {
      default: 0
    },
    reportType: {
      default: ''
    },
    isResult: {//1处理2查看3确认结果
      default: ''
    },
    handlerType: {//1内部人员0外部人员（只有外部人员做协同才会传递）
      default: ''
    },
  },
  components: {
    PatentTableListView,
    field,
    customFields,
    searchPatent,

    PatentBatchIndexVue,
    PatentIndexSetting
  },
  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: [],
      loading:false,
    }
  },
  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.queryParams.current = 1
      this.handleCancelSelectNumber()
      this.getList()
      this.$refs.customFields.close()
    },
    // 树类型是否包括子分类
    handleIfHaveChild(val) {
      this.ifHaveChild = val
    },
    // 统计组件传的值
    handleCustomFields(val) {
      this.searchPatentCheck  = val
      this.$nextTick(() => {
        this.$refs.searchPatent.search2()
      })
    },
    // 处理任务
    handleClick(row, location) {
      var search = {
        searchOption: {
          searchQuery: this.searchStr || '',//检索条件
          customFields: this.customFields || [],
          orderDTOList: this.sort,//排序信息
          moreConditions:true
        },
        location: location
      }
      this.$s.setSession('search', search)
      // if (this.taskType) {//FTO标引任务
      this.$router.push(
        {
          path: '/patentDetails/' + row.patentNo,
          query: {
            projectId: this.projectId,
            projectName: this.projectName,
            projectType: this.projectType,
            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',
            children:'children'
          })
        }
      })
    },
//查询专利自定义字段对应的值
getPatentFields(){
      var arr = this.fieldList.filter(item=>{
        return item.ifPersonal && !item.ifHidden
      })
      if(arr.length == 0){
        return false
      }
      if(this.tableData.length == 0){
        return false
      }
      var patent = this.tableData[0]
      var customFields = patent.customFields
      if(customFields){
        for(var i = 0;i<customFields.length;i++){
          var index = arr.find(item=>{
            return item.value == customFields[i].value
          })
          if(index != -1){
            arr.splice(index,1)
          }
        }
      }
      var patentNos = this.tableData.map(item=>{
        return item.patentNo
      })
      var params = {
        projectId:this.projectId,
        // taskId:this.taskId,
        patentNos:patentNos,
        innerFields:arr.map(item=>{
          return {
            fieldType:item.type,
            fieldId:item.value
          }
        })
      }
      this.$api.getPatentFields(params).then(response=>{
        if(response.code == 200){
          var data = response.data.data
          for(var i = 0;i<data.length;i++){
            var index = patentNos.findIndex(item=>{
              return item == data[i].patentNo
            })
            if(index!=-1){
              this.$set(this.tableData[index],'customFields',data[i].innerClassFields)
            }
          }
        }
      })
    },

    //获取专利列表
    getList() {
      let params = {
        ...this.queryParams,//分页信息
        projectId: this.projectId,
        taskId: this.taskId,
        searchQuery: this.searchStr || '',//检索条件
        customFields: this.customFields || [],
        orderDTOList: this.sort,//排序信息
      }
      this.$store.commit("SET_PATENT_RECORDS", []);
      this.loading = true
      this.$api.QueryPatent(params).then(res => {
        if (res.code == 200) {
          this.tableData = res.data.data
          this.total = res.data.total

          this.$set(this.queryParams, 'startNumber', this.endNumber > 0 ? this.startNumber : 1)
          this.$set(this.queryParams, 'endNumber', this.endNumber > 0 ? this.endNumber : this.total)
          this.$store.commit("SET_PATENT_RECORDS", res.data.data);
          if (this.quickSelect) {
            this.commonSwitch()
          }
          this.isRefresh()
          this.getPatentFields()
          this.loading = false
        }
      }).catch(error => {
        this.tableData = []
        this.total = 0
        this.loading = false
      })
    },
    //排序
    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();
    },
    //切换页大小
    changeSize(val){
      this.queryParams.size = val
      this.queryParams.current = 1
      this.getList()
    },
    //显示栏位管理
    handleFieldManage() {
      this.$refs.field.open(this.fieldList)
    },
    // 显示管理旁下拉菜单
    async handleCommand(event) {
      if(event != undefined){
        this.$refs.myDropdown.hide()
      }
      if(!(await this.$permission.projectPermission(this.projectId))){
        this.$showPermissionDialog()
        return false
      }
      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;
        case '5':
          const router = this.$router.resolve({
            path: '/project/patentCollection',
            query: {
              id: this.assProjectId,//专利数据库id
              projectName:this.projectName,//专利数据库名称
              projectType:2,//专利数据库类型
              toProjectId:this.projectId,
              toProjectName:this.projectName
            }
          })
          window.open(router.href, '_self');
          break;
        default:
          break;
      }
    },
    //专利导入
    patentImport(type) {
      var form = {
        importToId: this.projectId,
        importToType: 1,
        type: type,
      }
      if(type == 4){
        form.reportType = this.reportType
      }
      this.$commonJS.toImportParent(form, type)
    },
    // 跳转分配任务页面
    handleAllocation() {
      this.$router.push(
        {
          path: '/allocationTask',
          query: {
            projectId: this.projectId,
            searchOption: this.searchStr,//输入框检索条件
            customFields: JSON.stringify(this.customFields),//右侧勾选自定义栏位的条件
          }
        }
      )
    },
    //获取显示自定义栏位
    getFieldList(data) {
      this.fieldList = []
        this.$nextTick(()=>{
            this.fieldList = data
            this.getPatentFields()
        })
    },

    //自定义栏位以及标签设置值
    async handleIndexSetting(patent, field) {
      if(this.taskType != 0){
        return
      }
      let obj = {
        isAdd: [patent.patentNo],
        isDelete: [],
        startNumber: 1,
        endNumber: 0,
        searchQuery: this.searchStr,
        customFields: this.customFields,
        orderDTOList:this.sort,
      }
      if (field) {
        this.$refs.patentIndexSetting.open(obj,patent, field)
      }
    },
    submitIndex(value){
      if(value){
        this.handleCancelSelectNumber()
      }
      
      this.getList()
      this.$refs.customFields.refreshOptions(1)
    },
  },
}
</script>

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

  .right {
    min-width: 300px;
  }

  .left {
    width: 100%;
  }
}
</style>
<style lang="scss" scoped>
.patent-fast-edit-popover {
  padding: 0 !important;

  .btn {
    color: #000;
    line-height: 30px;
    border-radius: 5px;
    padding-left: 10px;
    text-align: left;
    font-size: 14px;
    cursor: pointer;

    &:hover {
      background: #adadad;
      color: #fff;
    }
  }

  .disabled {
    cursor: not-allowed !important;
  }

  .bottom {
    text-align: right;
    color: #1e9fff;
    line-height: 40px;
    padding-left: 10px;
    font-size: 18px;
  }

  .el-divider--horizontal {
    margin: 10px 0 !important;
  }

  .select-number {
    .el-input {
      width: 70px;
    }

    span {
      padding: 0 3px;
    }
  }
}
</style>