<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" v-if="!sliderValue">
        <el-button
          icon="el-icon-upload2"
          plain
          size="mini"
          type="info"
          @click="handleImport"
        >导入
        </el-button>
      </el-col>
      <el-col :span="1.5" v-if="resourceList.total>0">
        <el-button
          icon="el-icon-zoom-in"
          plain
          size="mini"
          type="primary"
          @click="findAll"
        >批量查重
        </el-button>
      </el-col>
    </el-row>
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" append-to-body width="400px">
      <el-upload
        ref="upload"
        :action="upload.url"
        :auto-upload="false"
        :disabled="upload.isUploading"
        :headers="upload.headers"
        :limit="1"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        accept=".xlsx, .xls"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
    <span v-if="sliderFlag">
      <br>
      <el-slider v-model="sliderValue" :show-tooltip="false" :max="resourceList.list.length" min="0" />
      <br>
    </span>
    <el-table v-if="resourceList.total>0" :data="resourceList.list" border>
      <el-table-column label="编号" prop="id" align="center" />
      <el-table-column label="题名" prop="title" align="center" />
      <el-table-column label="题名译名" prop="otherTitle" align="center" />
      <el-table-column label="PISSN" prop="pissn" align="center" />
      <el-table-column label="EISSN" prop="eissn" align="center" />
      <el-table-column label="PISBN" prop="pisbn" align="center" />
      <el-table-column label="EISBN" prop="eisbn" align="center" />
      <el-table-column label="文献类型" prop="type" align="center" />
      <el-table-column label="载体" prop="carrier" align="center" />
      <el-table-column label="学科" prop="subject" align="center" />
      <el-table-column label="国家/地区" prop="country" align="center" />
      <el-table-column label="语种" prop="language" align="center" />
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.code === 0" style="color: #00afff">待开始</span>
          <span v-if="scope.row.code === 1" style="color: #00afff">查重中</span>
          <span v-if="scope.row.code === 2" style="color: #00afff">
            <el-button v-if="scope.row.resourceInfoList.length > 0"
                       type="text" style="color: #00afff;" @click="findRepeatedList(scope.row.resourceInfoList)">
              查重完成</el-button>
            <el-button v-else type="text" style="color: #00afff;">无重复</el-button>
          </span>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-show="resourceList.total>0"
      :total="resourceList.total"
      :page-size="10"
      layout="prev, pager, next, jumper"
      @current-change="selList"
    />
    <el-dialog title="重复列表" :visible.sync="repeatedListFlag" append-to-body width="80%">
      <el-table :data="repeatedList" border>
        <el-table-column label="编号" prop="id" align="center" />
        <el-table-column label="题名" prop="title" align="center" />
        <el-table-column label="题名译名" prop="otherTitle" align="center" />
        <el-table-column label="PISSN" prop="pissn" align="center" />
        <el-table-column label="EISSN" prop="eissn" align="center" />
        <el-table-column label="PISBN" prop="pisbn" align="center" />
        <el-table-column label="EISBN" prop="eisbn" align="center" />
        <el-table-column label="文献类型" prop="type" align="center" />
        <el-table-column label="载体" prop="carrier" align="center" />
        <el-table-column label="学科" prop="subject" align="center" />
        <el-table-column label="国家/地区" prop="country" align="center" />
        <el-table-column label="语种" prop="language" align="center" />
        <el-table-column label="相似状态" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.flag">重复</span>
            <span v-else>相似</span>
          </template>
        </el-table-column>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="repeatedListFlag = false">确 定</el-button>
        </div>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）,
// 例如：import 《组件名称》 from '《组件路径》,
import { getToken } from '../../utils/auth'
import { findAll, getList } from '../../api/resource'

export default {
  // import引入的组件需要注入到对象中才能使用"
  name: 'index',
  props: {},
  data() {
    // 这里存放数据"
    return {
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/resource/Resource/upload"
      },
      resourceList: [],
      pageNum: 1,
      repeatedListFlag: false,
      repeatedList: [],
      sliderFlag: false,
      sliderValue: 0
    }
  },
  // 计算属性 类似于data概念",
  computed: {},
  // 生命周期 - 创建完成（可以访问当前this实例）",
  created() {
    this.selList(1);
  },
  // 方法集合",
  methods: {
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "文件导入";
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", {dangerouslyUseHTMLString: true});
      this.selList(1);
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    // 查询列表
    selList(val) {
      getList(val).then(res => {
        this.resourceList = res.data
      })
    },
    findAll() {
      this.sliderFlag = true
      const list = this.resourceList.list
      for (let i = 0; i < list.length; i++) {
        this.resourceList.list[i].code = 1
        const req = {
          title: list[i].title,
          pissn: list[i].pissn,
          eissn: list[i].eissn,
          pisbn: list[i].pisbn,
          eisbn: list[i].eisbn
        }
        console.log(req)
        findAll(req).then(res => {
          this.resourceList.list[i].resourceInfoList = res.data
          this.resourceList.list[i].code = 2
          this.sliderValue += 1
          if (this.sliderValue === this.resourceList.list.length){
            this.sliderFlag = false
            this.sliderValue = 0
          }
        })
      }
    },
    // 查询重复列表
    findRepeatedList(list) {
      this.repeatedList = list
      this.repeatedListFlag = true
    }
  }
}
</script>
<style scoped>

</style>
