<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="140px">
      <el-form-item label="唯一注册号" prop="assemblyAccession">
        <el-input
          v-model="queryParams.assemblyAccession"
          placeholder="请输入唯一注册号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生物名称" prop="organismName">
        <el-input
          v-model="queryParams.organismName"
          placeholder="请输入生物名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="病源类型" prop="group">
        <el-input
          v-model="queryParams.group"
          placeholder="请输入病源类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="子类标签" prop="group1">
        <el-input
          v-model="queryParams.group1"
          placeholder="请输入子类标签"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="中文解释" prop="defined">
        <el-input
          v-model="queryParams.defined"
          placeholder="请输入中文解释"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="毒力基因" prop="dljy">
        <el-input
          v-model="queryParams.dljy"
          placeholder="请输入毒力基因"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="毒力基因解释" prop="dljyExpress">
        <el-input
          v-model="queryParams.dljyExpress"
          placeholder="请输入毒力基因解释"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="毒力基因解释来源" prop="dljySource">
        <el-input
          v-model="queryParams.dljySource"
          placeholder="请输入毒力基因解释来源"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="$refs.upload_file.click()"
          >导入</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="dataBaseList" >
        <el-table-column label="唯一注册号" align="center" prop="assemblyAccession" width="100" />
        <el-table-column label="生物名称" align="center" prop="organismName" width="150"/>
        <el-table-column label="病源类型" align="center" prop="group" />
        <el-table-column label="子类标签" align="center" prop="group1" />
        <el-table-column label="中文解释" align="center" prop="defined"  width="500" />
        <el-table-column label="解释的来源" align="center" prop="definedSource" width="200" />
        <el-table-column label="毒力基因" align="center" prop="dljy" />      
        <el-table-column label="毒力基因解释" align="center" prop="dljyExpress" width="100"  />      
        <el-table-column label="毒力基因解释来源" align="center" prop="dljySource" width="200"  />
      <!-- <el-table-column label="参考基因组类型" align="center" prop="refseqCategory" />
      <el-table-column label="分类号" align="center" prop="taxid" />
      <el-table-column label="种分类号" align="center" prop="speciesTaxid" />
      <el-table-column label="亚种名称" align="center" prop="infraspecificName"/>
      <el-table-column label="分类类型" align="center" prop="isolate" />
      <el-table-column label="拼装级别" align="center" prop="assemblyLevel" />
      <el-table-column label="基因组级别" align="center" prop="genomeRep" />
      <el-table-column label="seqRelDate" align="center" prop="seqRelDate" />     
      <el-table-column label="ASM编号" align="center" prop="asmName" />
      <el-table-column label="gbrs_paired_asm" align="center" prop="gbrsPairedAsm" />
      <el-table-column label="paired_asm_comp" align="center" prop="pairedAsmComp" />
      <el-table-column label="ftp_path" align="center" prop="ftpPath" />
      <el-table-column label="excluded_from_refseq" align="center" prop="excludedFromRefseq" />
      <el-table-column label="装配类型" align="center" prop="assemblyType" />
      <el-table-column label="基因组大小" align="center" prop="genomeSize" />
      <el-table-column label="基因组大小(无缺口)" align="center" prop="genomeSizeUngapped" />
      <el-table-column label="GC比例" align="center" prop="gcPercent" />
      <el-table-column label="replicon_count" align="center" prop="repliconCount" />
      <el-table-column label="scaffold_count" align="center" prop="scaffoldCount" />
      <el-table-column label="contig_count" align="center" prop="contigCount" />
      <el-table-column label="总基因数" align="center" prop="totalGeneCount" />
      <el-table-column label="翻译蛋白基因数" align="center" prop="proteinCodingGeneCount" />      
      <el-table-column label="非编码基因数" align="center" prop="nonCodingGeneCount" />      
      <el-table-column label="耐药基因" align="center" prop="nyjy" />      
      <el-table-column label="耐药基因解释" align="center" prop="nyjyExpress" />      
      <el-table-column label="耐药基因解释来源" align="center" prop="nyjySource" />
      <el-table-column label="点突变" align="center" prop="dtpnyjy" />      
      <el-table-column label="点突变解释" align="center" prop="dtpnyjyExpress" />      
      <el-table-column label="点突变解释来源" align="center" prop="dtpnyjySource" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['data:dataReportByt:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['data:dataBase:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改病原体管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="唯一注册号">{{ form.assemblyAccession }}</el-form-item>
        <el-form-item label="生物名称">{{ form.organismName }}</el-form-item>
        <el-form-item label="病源类型">{{ form.group }}</el-form-item>
        <el-form-item label="子类标签">{{ form.group1 }}</el-form-item>
        <el-form-item label="中文解释">{{ form.defined }}</el-form-item>
        <el-form-item label="解释的来源">{{ form.definedSource }}</el-form-item>
        <el-form-item label="参考基因组类型">{{ form.refseqCategory }}</el-form-item>
        <el-form-item label="分类号">{{ form.taxid }}</el-form-item>
        <el-form-item label="种分类号">{{ form.speciesTaxid }}</el-form-item>
        <el-form-item label="亚种名称">{{ form.infraspecificName }}</el-form-item>
        <el-form-item label="分类类型">{{ form.isolate }}</el-form-item>
        <el-form-item label="拼装级别">{{ form.assemblyLevel }}</el-form-item>
        <el-form-item label="基因组级别">{{ form.genomeRep }}</el-form-item>
        <el-form-item label="seq_rel_date">{{ form.seqRelDate }}</el-form-item>
        <el-form-item label="ASM编号">{{ form.asmName }}</el-form-item>
        <el-form-item label="gbrs_paired_asm">{{ form.gbrs_paired_asm }}</el-form-item>
        <el-form-item label="paired_asm_comp">{{ form.paired_asm_comp }}</el-form-item>
        <el-form-item label="ftp_path">{{ form.ftp_path }}</el-form-item>
        <el-form-item label="excluded_from_refseq">{{ form.excluded_from_refseq }}</el-form-item>        
        <el-form-item label="装配类型">{{ form.assemblyType }}</el-form-item>
        <el-form-item label="基因组大小">{{ form.genomeSize }}</el-form-item>
        <el-form-item label="基因组大小(无缺口)">{{ form.genomeSizeUngapped }}</el-form-item>
        <el-form-item label="GC比例">{{ form.gcPercent }}</el-form-item>
        <el-form-item label="replicon_count">{{ form.replicon_count }}</el-form-item>
        <el-form-item label="scaffold_count">{{ form.scaffold_count }}</el-form-item>
        <el-form-item label="contig_count">{{ form.contig_count }}</el-form-item>
        <el-form-item label="总基因数">{{ form.totalGeneCount }}</el-form-item>
        <el-form-item label="翻译蛋白基因数">{{ form.proteinCodingGeneCount }}</el-form-item>
        <el-form-item label="非编码基因数">{{ form.nonCodingGeneCount }}</el-form-item>
        <el-form-item label="毒力基因">{{ form.dljy }}</el-form-item>
        <el-form-item label="毒力基因解释">{{ form.dljyExpress }}</el-form-item>
        <el-form-item label="毒力基因解释来源">{{ form.dljySource }}</el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 上传文件  -->
     <input hidden type="file" ref="upload_file" accept=".xls,.xlsx" class="upload_file" @change="readExcel($event)" />

  </div>
</template>

<script>
import { listDataBase, getDataBase, delDataBase, addDataBase, updateDataBase,importDataBase } from "@/api/data/dataBase"
import * as XLSX from "xlsx"

export default {
  name: "DataBase",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 病原体管理表格数据
      dataBaseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type:2,
        organismName: null,
        group: null,
        group1: null,
        defined: null,
        dljy:null,
        dljyExpress:null,
        dljySource:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      upload_file: "",
      lists: [],
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 读取Excel文件
    readExcel(e) {
      console.log("读取Excel文件", e);
      console.log("读取Excel文件11", XLSX);
  // 读取表格文件
  let that = this;
  const files = e.target.files;
  if (files.length <= 0) {
    return false;
  } else if (!/\.(xls|xlsx)$/.test(files[0].name.toLowerCase())) {
    this.$message({
      message: "上传格式不正确，请上传xls或者xlsx格式",
      type: "warning",
    });
    return false;
  } else {
    this.uploadFile(files[0]);
    // 更新获取文件名
    // that.upload_file = files[0].name;
  }
  // const fileReader = new FileReader();
  // fileReader.onload = (ev) => {
  //   try {
  //     const data = ev.target.result;
  //     const workbook = XLSX.read(data, {
  //       type: "binary",
  //     });
  //     const wsname = workbook.SheetNames[0]; //取第一张表
  //     console.log(XLSX);
  //     console.log(wsname);
  //     console.log(workbook);
  //     const ws = XLSX.utils.sheet_to_json(workbook.Sheets[wsname]); //生成json表格内容
  //     that.lists = [];
  //     console.log(ws);
  //   } catch (e) {
  //     return false;
  //   }
  // };
  // fileReader.readAsBinaryString(files[0]);
},
uploadFile(file) {
  console.log("上传文件", file);
  // 创建FormData对象
const formData = new FormData(); // 创建FormData对象
formData.append('file', file); // 将文件添加到FormData对象中，
importDataBase(formData).then(response => {
  this.$modal.msgSuccess("导入成功")
  this.getList()
})
},
    /** 查询病原体管理列表 */
    getList() {
      this.loading = true
      listDataBase(this.queryParams).then(response => {
        this.dataBaseList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        assemblyAccession: null,
        refseqCategory: null,
        taxid: null,
        speciesTaxid: null,
        organismName: null,
        infraspecificName: null,
        isolate: null,
        assemblyLevel: null,
        genomeRep: null,
        seqRelDate: null,
        asmName: null,
        gbrsPairedAsm: null,
        pairedAsmComp: null,
        ftpPath: null,
        excludedFromRefseq: null,
        assemblyType: null,
        group: null,
        genomeSize: null,
        genomeSizeUngapped: null,
        gcPercent: null,
        repliconCount: null,
        scaffoldCount: null,
        contigCount: null,
        totalGeneCount: null,
        proteinCodingGeneCount: null,
        nonCodingGeneCount: null,
        nameCn: null,
        defined: null,
        seqSource: null,
        express: null,
        filePath: null,
        dtpnyjy: null,
        dtpnyjyExpress: null,
        nyjy: null,
        nyjyExpress: null,
        dljy: null,
        dljyExpress: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.assemblyAccession)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.assemblyAccession || this.ids
      this.$modal.confirm('是否确认删除病原体管理编号为"' + ids + '"的数据项？').then(function() {
        return delDataBase(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    handleUpdate(row) {
      this.reset()
      this.form = row
      this.open = true
      this.title = "查看毒力基因信息"
    },
  }
}
</script>
