// 查重 dl
<template>
  <div>
    <dialog-edit :visible.sync="cnkiData.dialogVisible"
                 :editModel="editModel"
                 append-to-body
                 :form={}
                 width="1480px"
                 :title="$t('knowledgeBaseIndex.cnik')"
                 :autoSave="false"
                 @onSaved="onSaved">
        <template>
          <!-- es 的查询 -->
          <yf-es-search
            v-show="isShow"
            @callbackFunc="updata"
            v-bind="yfEsSearchData"
            ref="yfEsSearch"
            :shouldBefore="true"
            :beforeLoad="beforeLoad"
            :paramPostDaata="paramPostDaata"></yf-es-search>
          <div class="titleLine">{{$t('knowledgeBaseIndex.cnkiContent')}}<span class="line"></span></div>
          <div style="overflow: hidden;">
            <el-form label-width="100px" :disabled="true">
              <el-col :span="8">
                <el-form-item :label="$t('knowledgeBaseIndex.fileType')">
                  <el-input v-model="cnkiData.fromData.fileTypeStr"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item :label="$t('knowledgeBaseIndex.fileName')">
                  <el-input v-model="cnkiData.fromData.fileNameStr"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item :label="$t('knowledgeBaseIndex.problemType')">
                  <el-input v-model="cnkiData.fromData.problemType"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item :label="$t('knowledgeBaseIndex.problemPoint')">
                  <el-input v-model="cnkiData.fromData.problemPoint"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item :label="$t('knowledgeBaseIndex.solution')">
                  <el-input v-model="cnkiData.fromData.solution"></el-input>
                </el-form-item>
              </el-col>
            </el-form>
          </div>
          <div>
            <div class="titleLine">{{$t('knowledgeBaseIndex.cnkiResult')}}<span class="line"></span></div>
            <el-table
              header-cell-class-name="common-table-header"
              :data="tableData"
              border
              style="width: 100%">
              <el-table-column :label="$t('knowledgeBaseIndex.choice')" width="50" v-if="cnkiData.showSave">
                <template slot-scope="scope">
                    <el-radio class="radio" v-model="radioCheck" :label="scope.$index+''" style="padding-left:7px;" @change="handleTable(scope.row)">&nbsp;</el-radio>
                </template>
              </el-table-column>
              <el-table-column prop="solutionCode" :label="$t('knowledgeBaseIndex.solutionCode')" width="150"></el-table-column>
              <el-table-column prop="fileType" :label="$t('knowledgeBaseIndex.fileType')" width="100" :formatter="fFileType"></el-table-column>
              <el-table-column prop="fileName" :label="$t('knowledgeBaseIndex.fileName')" width="200"></el-table-column>
              <el-table-column prop="problemType" :label="$t('knowledgeBaseIndex.problemType')" width="100"></el-table-column>
              <el-table-column prop="problemPoint" :label="$t('knowledgeBaseIndex.problemPoint')" width="200"></el-table-column>
              <el-table-column prop="solution" :label="$t('knowledgeBaseIndex.solution')" width="300"></el-table-column>
              <el-table-column prop="applicableProjectName" :label="$t('knowledgeBaseIndex.production')"></el-table-column>
              <el-table-column prop="Score" :label="$t('knowledgeBaseIndex.kHz')" width="140" align="right"></el-table-column>
            </el-table>
            <div style="text-align: right;margin-top:10px;">
              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="yfEsSearchData.pageConfig.pageIndex"
                :page-sizes="[3, 5, 10, 15, 20]"
                :page-size="yfEsSearchData.pageConfig.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :prev-text="$t('retrievalIndex.prevText')"
                :next-text="$t('retrievalIndex.nextText')"
                :total="yfEsSearchData.pageConfig.totalCount">
              </el-pagination>
            </div>
          </div>
        </template>
        <!-- 解决弹窗抖动问题 -->
        <div style="height:1px"></div>
    </dialog-edit>
  </div>
</template>

<script>
import { DialogEdit, yfEsSearch } from 'newepoch-components'
import * as format from '@/filters/index.js'
export default {
  components: { DialogEdit, yfEsSearch },
  props: {
    cnkiData: {
      type: Object,
      default () {
        return {
          dialogVisible: false,
          fromData: {}, // 表单数据
          showSave: true
        }
      }
    }
  },
  data () {
    return {
      isShow: false,
      dialogVisible: false,
      editModel: 'E',
      radioCheck: '', // 表格单选的值
      tableData: [], // 表格数据
      yfEsSearchData: {
        // configUrl: 'http://es.cdyfsz.com',
        // module: 'ckb_knowledge_base',
        filterKeys: [{ name: 'field_reuse_group', modle: 'must' }],
        sortKeys: '',
        pageConfig: {
          pageIndex: 1,
          pageSize: 3,
          totalCount: 0
        },
        recommendKey: { name: 'field_reuse_group', modle: 'must' },
        autoSearch: false,
        defauleKeys: [
          { name: 'voltooide_status', type: 'term', value: '0' },
          { name: 'data_status', type: 'term', value: '01' },
          { name: 'del_status', type: 'term', value: '0' }
        ]
      },
      rowData: '' // 单选的数据
    }
  },
  watch: {
    'cnkiData.dialogVisible': function (val) {
      if (val) { this.setSearch() }
    }
  },
  created () {
    if (!this.cnkiData.showSave) {
      this.editModel = 'V'
    }
  },
  mounted () {
    this.envSetFun()
  },
  methods: {
    // 设置关键词环境变量
    envSetFun () {
      this.$set(this.yfEsSearchData, 'configUrl', process.env.VUE_APP_CONFIG_URL)
      this.$set(this.yfEsSearchData, 'module', process.env.VUE_APP_KNOWLEDGE_BASE)
    },
    // 初始参数配置
    paramPostDaata (data) {
      const filenames = this.cnkiData.fromData.fileName
      data.query.bool.should = filenames.map(it => {
        return { match_phrase: { 'file_id': it } }
      })
      data.query.bool.filter = [
        { term: { 'voltooide_status': '0' } },
        { term: { 'data_status': '01' } },
        { term: { 'del_status': '0' } },
        { term: { 'file_type': this.cnkiData.fromData.fileType } },
        { term: { 'problem_type.keyword': this.cnkiData.fromData.problemType } }
      ]

      data.query.bool.must = [
        { match: { 'field_reuse_group': `${this.cnkiData.fromData.problemPoint}${this.cnkiData.fromData.solution}` } }
      ]
      return data
    },
    setSearch () {
      this.$nextTick(() => {
        this.$refs['yfEsSearch'].esValue = this.cnkiData.fromData.problemPoint
        this.$refs['yfEsSearch'].loadData()
      })
    },
    // 关键字返回数据
    updata (data) {
      this.yfEsSearchData.pageConfig.totalCount = data.total
      data.datas.forEach(item => {
        item.Score = Math.floor(item.Score * 100) / 100
      })
      this.tableData = data.datas
    },
    // 第一次查询 数据加载
    beforeLoad (load) {
      this.yfEsSearchData.pageConfig.pageIndex = 1
      load()
    },
    // 保存事件
    onSaved () {
      if (this.rowData === '') {
        this.$message.warning('必须选择一条查重结果!')
      } else {
        this.$emit('dleFromData', this.cnkiData.fromData.analysisDataIdex) // 返回索引，删除指定的表单值

        this.cnkiData.dialogVisible = false
        this.$emit('cnkiSave', this.rowData)
      }
    },
    // 分页 每页
    handleSizeChange (val) {
      this.yfEsSearchData.pageConfig.pageSize = val
      this.radioCheck = ''
      this.rowData = ''
      this.$refs.yfEsSearch.loadData()
    },
    // 分页 当前页
    handleCurrentChange (val) {
      this.yfEsSearchData.pageConfig.pageIndex = val
      this.radioCheck = ''
      this.rowData = ''
      this.$refs.yfEsSearch.loadData()
    },
    handleTable (row) {
      this.rowData = row
    },
    fFileType (row) {
      return format.formatDict(row.fileType, 'HG_FILE_TYPE')
    }
  }
}
</script>
