<template>
  <div class="height_100">
    <el-container>
      <el-aside width="200px">Aside</el-aside>
      <el-container>
        <el-header style="display:flex;justify-content:space-between">
          <div style="display:flex">
            <el-tooltip class="item" effect="dark" :content="(showLeft ? '隐藏' : '显示') + '左侧菜单'" placement="top">
              <i :class="showLeft ? 'el-icon-s-fold' : 'el-icon-s-unfold'"
                style="font-size: 25px;position: relative; top: 5px; color: #2f2f2f; cursor: pointer"
                @click="showLeft = !showLeft"></i>
            </el-tooltip>
            <mySearch :SearchFields="searchFiled" @search="search" :searchValue="searchOption"></mySearch>
            <el-popover placement="bottom" title="关键词高亮" width="320" trigger="click">
              <!-- <patent-keywords-highlight :project-id="projectId" /> -->
              <el-button slot="reference" size="small" type="primary" class="margin-right_10 margin-left_10"
                :disabled="!$permission('/workspace/common/highlight')">
                高亮<i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
            </el-popover>
          </div>
          <div>
            <el-button-group class="margin-left_10">
              <el-tooltip v-for="item in viewList" :key="item.value" class="item" effect="dark" :content="item.title"
                placement="top">
                <el-button @click="handleChangeView(item)" size="small"
                  :type="viewSelected === item.value ? 'primary' : ''">
                  <i :class="item.btn" style="font-size: 12px !important;"></i>
                </el-button>
              </el-tooltip>
            </el-button-group>
            <el-dropdown trigger="click" split-button type="primary" size="small">
              <span @click="handleField"
                v-disabled="!($permission('/workspace/common/customField') && $r(projectId, [1, 2]))">自定义栏位管理</span>
              <el-dropdown-menu slot="dropdown" class="text-align_center">
                <el-dropdown-item @click.native="handleFile">报告文档</el-dropdown-item>
                <el-dropdown-item @click.native="handleQuestion" v-if="$permission('/workspace/event')">事
                  件</el-dropdown-item>
                <el-dropdown-item @click.native="handleFieldManage">显示栏位管理</el-dropdown-item>
                <el-dropdown-item @click.native="exportPDF">导出PDF首页</el-dropdown-item>
                <el-dropdown-item @click.native="handleBatch"
                  :disabled="!($permission('/workspace/folder/batchUploadSpecification') && $r(projectId, [1, 2]))">批量上传说明书</el-dropdown-item>
                <el-dropdown-item>
                  <el-dropdown trigger="hover" placement="right-start">
                    <span>
                      导出专利<i class="el-icon-arrow-right el-icon--right"></i>
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="handleExport(false)">当前全部</el-dropdown-item>
                      <el-dropdown-item @click.native="handleExport(true)">当前所选</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-dropdown-item>
                <el-dropdown-item>
                  <el-dropdown trigger="hover" placement="right-start">
                    <span>
                      {{ familyObj[queryParams.family] }}<i class="el-icon-arrow-right el-icon--right"></i>
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item v-for="(item, key) in familyObj" :key="key" @click.native="onChangeFamily(key)">{{
                        item }}</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-dropdown-item>
                <el-dropdown-item :disabled="!$permission('/workspace/folder/merge')">
                  <el-dropdown trigger="hover" placement="right-start">
                    <span>
                      合并<i class="el-icon-arrow-right el-icon--right"></i>
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="handleMerge2"
                        :disabled="!($permission('/workspace/folder/merge/inventorMerge') && $r(projectId, [1, 2]))">发明人</el-dropdown-item>
                      <el-dropdown-item @click.native="handleMerge"
                        :disabled="!($permission('/workspace/folder/merge/applicationMerge') && $r(projectId, [1, 2]))">申请人/权利人</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-dropdown-item>
                <el-dropdown-item v-if="$permission('/workspace/createReport')">
                  <el-dropdown trigger="hover" placement="right-start">
                    <span>
                      创建报告<i class="el-icon-arrow-right el-icon--right"></i>
                    </span>
                    <el-dropdown-menu slot="dropdown" style="margin-top:0px">
                      <!-- 遍历按钮 -->
                      <el-dropdown-item @click.native="handleAnalyses(3)"
                        v-if="$permission('/workspace/createReport/FTO')">FTO调查</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-dropdown-item>
                <el-dropdown-item @click.native="handleAnalyse('custom')"
                  :disabled="!($permission('/workspace/folder/analyticSystem/chartAnalysis') && $r(projectId, [1, 2, 4]))">图表分析</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>

        </el-header>
        <el-main v-loading="loading">
          <div>
            <component ref="patentViewList" :is="viewSelected" @download="handleDownload" @change-read="handleChangeRead"
              :projectId="projectId" :selected.sync="queryParams.selected" :patentNoList.sync="patentNoList"
              :column="customList" @index-setting="handleIndexSetting" @on-sort="handleSort" :queryParams="queryParams"
              :tableData="tableData"></component>
          </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-container>
    </el-container>

    <Field-Drawer ref="FieldDrawer"></Field-Drawer>
    <field ref="field" type="patent" @getFieldList="getFieldList"></field>
  </div>
</template>

<script>
import { downLoad2, getTreeDataList, getTreeLastChildren, getPatentCountry } from "@/utils"
import FieldDrawer from '@/views/components/drawer/Field.vue'
import PatentAbstractListView from './components/views/Abstract'
import PatentTableListView from './components/views/Table.vue'
import PatentPictureListView from './components/views/Picture'
// import PatentPDFListView from './components/views/PDF.vue'
import field from '@/views/components/dialog/fields.vue';
export default {
  components: {
    FieldDrawer,
    PatentAbstractListView,
    PatentTableListView,
    PatentPictureListView,
    field
  },
  props: {},
  data() {
    return {
      //是否隐藏左侧
      showLeft: false,
      //检索字段
      searchFiled: [],
      //检索条件
      searchOption: {
        name: localStorage.searchContent,
      },
      //专利数据库ID
      projectId: 0,
      //视图方式
      viewList: [
        {
          value: 'patent-table-list-view',
          title: '表格视图',
          btn: 'el-icon-tickets',
        },
        {
          value: 'patent-abstract-list-view',
          title: '摘要视图',
          btn: 'el-icon-news',
        },
        {
          value: 'patent-picture-list-view',
          title: '图片视图',
          btn: 'el-icon-picture-outline',
        },
        {
          value: 'PatentPDFListView',
          title: 'PDF视图',
          btn: 'iconfont icon-pdf',
        }
      ],
      //选择的视图
      viewSelected: "patent-table-list-view",
      //显示栏位
      customList: [],
      //同族
      familyObj: {
        0: '不区分同族',
        1: '简单同族',
        2: 'INPADOC同族',
        3: '扩展同族',
      },
      //页大小
      sizeArr: [10, 20, 30, 40, 50, 100],
      //分页
      queryParams: {
        current: 1,
        size: 10
      },
      //总数
      total: 0,
      //排序
      sort: [],
      //选择的专利集合
      patentNoList: [],
      //加载
      loading: false,
      // 数据
      tableData: [],
    };
  },
  watch: {},
  computed: {
    // 专题库id
    id() {
      return this.$route.query.id
    },
  },
  created() { },
  async mounted() {
    this.customList = await this.$commonJS.getCustomField('patent')
    await this.getColumn()
    // 获取专利信息
    this.getList()
  },
  methods: {
    //获取table栏位及分组字段、检索字段
    async getColumn() {
      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 = params
      // 调用查询接口
      this.queryParams.current = 1
      this.getList()
    },
    //切换视图
    async handleChangeView(item) {
      this.viewSelected = item.value
      this.queryParams.current = 1
      // if (this.viewSelected == 'PatentPDFListView') {
      //   return false
      // }
      if (this.viewSelected === 'patent-picture-list-view') {
        this.$set(this.queryParams, 'size', 20)
      } else {
        this.queryParams.size = 10
      }
      this.getList()
    },
    //获取专利信息
    getList(size) {
      if (size) {
        this.queryParams.size=size
      }
      let params = {
        ...this.queryParams,//分页信息
        projectId: this.id,
        searchQuery: this.$commonJS.objectToString(this.searchOption),//检索条件
        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();
    },

    //自定义栏位管理
    handleField() {
      this.$refs.FieldDrawer.open(5)
    },
    //获取自定义栏位
    getFieldList(data) {
      this.customList = data
    },
    //显示栏位管理
    async handleFieldManage() {
      this.$refs.field.open(this.customList)
    },
    //批量上传说明书
    handleBatch() {
      this.$refs.batchInstructionDialog.open()
    },
    //报告文档
    handleFile() {
      this.$refs.projectFileDrawer.open(this.projectId, 1)
    },
    // 事件按钮
    handleQuestion() {
      let a = this.$router.resolve({
        path: '/questionEvent',
        query: {
          projectId: this.projectId
        }
      })
      window.open(a.href, '_blank');
    },
    //PDF首页导出
    exportPDF() {
      let params = JSON.parse(JSON.stringify(this.queryParams))
      params.tree.map(tree => {
        let field = params.field.filter(item => item.key === tree.key)
        if (field.length === 0) {
          params.field.push(tree)
        }
      })
      params.tree = undefined
      this.$api.pdfFirstPage(params).then(response => {
        if (response.code == 200) {
          const router = this.$router.resolve({
            path: '/taskList',
            query: {
              projectId: this.projectId,
            }
          })
          window.open(router.href, '_blank')
        }
      })
    },
    //导出专利
    async handleExport(selectId) {
      this.exportLoading = true
      const key = await this.saveSelectPatent(selectId)
      if (!key) {
        return false
      }
      this.exportLoading = false
      this.$router.push({
        path: '/workspace/folder/export/patent',
        query: {
          id: this.projectId,
          key: key
        }
      })
    },
    //切换同族
    onChangeFamily(key) {
      this.queryParams.current = 1
      this.queryParams.family = parseInt(key)
      this.getList()
    },
    //合并申请人
    handleMerge() {
      this.$refs.patentApplicantMergeDrawer.open(this.projectId)
    },
    //合并发明人
    handleMerge2() {
      this.$refs.patentInventorMergeDrawer.open(this.projectId)
    },
    //创建FTO报告
    handleAnalyses(id) {
      this.$api.getProject({ id: this.projectId }).then(response => {
        var form = {
          type: id,
          projectId: this.projectId,
          departmentId: response.data[0].departmentId,
          personId: response.data[0].personnelId,
          clientId: response.data[0].clientId,
          proTec: response.data[0].technicalTheme,
          scenarioList: response.data[0].scenarioList
        }
        this.$refs.ReportForm.open(form)
      })
    },
    //图表分析
    async handleAnalyse(path) {
      this.chartLoading = true
      const key = await this.saveSelectPatent(false)
      this.chartLoading = false
      const router = this.$router.resolve({
        path: '/analyse/' + path,
        query: {
          id: this.projectId,
          key: key
        }
      })
      window.open(router.href, '_blank');
    },
    //下载PDF
    async handleDownload(patent) {
      const { data } = await this.$api.getPatentInstructionList({ current: 1, size: 9999, patentNo: patent.patentNo })
      if (data.records.length !== 0) {
        downLoad2(data.records[0].url)
      } else {
        this.$alert('专利暂未上传说明书文档', '请求错误', {
          confirmButtonText: '确定',
          type: 'error',
          callback: action => { }
        });
      }
    },
    //切换已读未读
    handleChangeRead(ids, status) {
      let params = {
        patentIds: ids,
        projectId: this.projectId,
        status: status,
        folder: this.queryParams.folder
      }
      this.fastEditLoading = true
      this.$api.updatePatentReadStatus(params).then(response => {
        this.$refs.patentQueryFilter.getPatentReadTotal()
        this.fastEditLoading = false
        this.getList()
      }).catch(error => {
        this.fastEditLoading = false
      })
    },
    //自定义栏位以及标签设置值
    handleIndexSetting(patent, field) {
      if (field) {
        this.$refs.patentIndexSetting.open(this.queryParams.projectId, patent, field)
      }
    },
  },
};
</script>
<style lang="scss" scoped></style>