<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="NCBI基因组序列编号" prop="assemblyAccession">
        <!-- <el-input
          v-model="queryParams.assemblyAccession"
          placeholder="请输入NCBI基因组序列编号"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
         <!-- 修改为支持远程搜索的选择器 -->
        <el-select
          v-model="queryParams.assemblyAccession"
          filterable
          remote
          reserve-keyword
          placeholder="请输入NCBI基因组序列编号"
          :remote-method="queryAssemblyAccession"
          :loading="assemblyAccessionLoading"
          style="width: 200px"
        >
          <el-option
            v-for="item in assemblyAccessionOptions"
            :key="item.id"
            :label="item.assemblyAccession"
            :value="item.assemblyAccession"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="病原体名称" prop="organismName">
        <!-- <el-input
          v-model="queryParams.organismName"
          placeholder="请输入病原体名称"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
              <!-- 修改为支持远程搜索的选择器 -->
        <el-select
          v-model="queryParams.organismName"
          filterable
          remote
          reserve-keyword
          placeholder="请输入病原体名称"
          :remote-method="queryOrganismName"
          :loading="organismNameLoading"
          style="width: 200px"
        >
          <el-option
            v-for="item in organismNameOptions"
            :key="item.id"
            :label="item.asmName"
            :value="item.asmName"
          >
          </el-option>
        </el-select>
      </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="nyjy">
        <el-input
          v-model="queryParams.nyjy"
          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="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <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="NCBI基因组序列编号" 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="nyjy" />
      <!-- <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="dljy" />
      <el-table-column label="毒力基因注释" align="center" prop="dljyExpress" />
      <el-table-column label="毒力基因注释来源" align="center" prop="dljySource" /> -->
      <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="NCBI基因组序列编号" prop="assemblyAccession">
           <!-- <el-input v-model="form.assemblyAccession" :disabled="disabledAssemblyAccession" placeholder="请输入NCBI基因组序列编号" /> -->
           <!-- 修改为支持远程搜索的选择器 -->
        <el-select
          v-model="form.assemblyAccession"
          filterable
          remote
          reserve-keyword
          placeholder="请输入NCBI基因组序列编号"
          :remote-method="queryAssemblyAccession"
          :loading="assemblyAccessionLoading"
          style="width: 100%"
        >
          <el-option
            v-for="item in assemblyAccessionOptions"
            :key="item.id"
            :label="item.assemblyAccession"
            :value="item.assemblyAccession"
          >
          </el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="病原体名称" prop="organismName">
           <!-- <el-input v-model="form.organismName" placeholder="请输入病原体名称" /> -->
                <!-- 修改为支持远程搜索的选择器 -->
        <el-select
          v-model="form.organismName"
          filterable
          remote
          reserve-keyword
          placeholder="请输入病原体名称"
          :remote-method="queryOrganismName"
          :loading="organismNameLoading"
          style="width: 100%"
        >
          <el-option
            v-for="item in organismNameOptions"
            :key="item.id"
            :label="item.asmName"
            :value="item.asmName"
          >
          </el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="病原类型" prop="group">
          <el-input v-model="form.group" placeholder="请输入病原类型" />
        </el-form-item>
        <el-form-item label="子类标签" prop="group1">
          <el-input v-model="form.group1" placeholder="请输入子类标签" />
        </el-form-item>
        <el-form-item label="中文注释" prop="defined">
          <el-input type="textarea" v-model="form.defined" placeholder="请输入中文注释" />
        </el-form-item>
        <el-form-item label="解释的来源" prop="definedSource">
          <el-input v-model="form.definedSource" placeholder="请输入解释的来源" />
        </el-form-item>
        <el-form-item label="参考基因组类型" prop="refseqCategory">
          <el-input v-model="form.refseqCategory" placeholder="请输入参考基因组类型" />
        </el-form-item>
        <el-form-item label="分类号" prop="taxid">
          <el-input v-model="form.taxid" placeholder="请输入分类号" />
        </el-form-item>
        <el-form-item label="种分类号" prop="speciesTaxid">
          <el-input v-model="form.speciesTaxid" placeholder="请输入种分类号" />
        </el-form-item>
        <el-form-item label="亚种名称" prop="infraspecificName">
          <el-input v-model="form.infraspecificName" placeholder="请输入亚种名称" />
        </el-form-item>
        <el-form-item label="分类类型" prop="isolate">
          <el-input v-model="form.isolate" placeholder="请输入分类类型" />
        </el-form-item>
        <el-form-item label="拼装级别" prop="assemblyLevel">
          <el-input v-model="form.assemblyLevel" placeholder="请输入拼装级别" />
        </el-form-item>
        <el-form-item label="基因组级别" prop="genomeRep">
          <el-input v-model="form.genomeRep" placeholder="请输入基因组级别" />
        </el-form-item>
        <el-form-item label="seq_rel_date" prop="seqRelDate">
          <el-input v-model="form.seqRelDate" placeholder="请输入seq_rel_date" />
        </el-form-item>
        <el-form-item label="ASM编号" prop="asmName">
          <el-input v-model="form.asmName" placeholder="请输入ASM编号" />
        </el-form-item>
        <el-form-item label="gbrs_paired_asm" prop="gbrs_paired_asm">
          <el-input v-model="form.gbrs_paired_asm" placeholder="请输入gbrs_paired_asm" />
        </el-form-item>
        <el-form-item label="paired_asm_comp" prop="paired_asm_comp">
          <el-input v-model="form.paired_asm_comp" placeholder="请输入paired_asm_comp" />
        </el-form-item>
        <el-form-item label="ftp_path" prop="ftp_path">
          <el-input v-model="form.ftp_path" placeholder="请输入ftp_path" />
        </el-form-item>
        <el-form-item label="excluded_from_refseq" prop="excluded_from_refseq">
          <el-input v-model="form.excluded_from_refseq" placeholder="请输入excluded_from_refseq" />
        </el-form-item>
        <el-form-item label="装配类型" prop="assemblyType">
          <el-input v-model="form.assemblyType" placeholder="请输入装配类型" />
        </el-form-item>
        <el-form-item label="基因组大小" prop="genomeSize">
          <el-input v-model="form.genomeSize" placeholder="请输入基因组大小" />
        </el-form-item>
        <el-form-item label="基因组大小(无缺口)" prop="genomeSizeUngapped">
          <el-input v-model="form.genomeSizeUngapped" placeholder="请输入基因组大小(无缺口)" />
        </el-form-item>
        <el-form-item label="GC比例" prop="gcPercent">
          <el-input v-model="form.gcPercent" placeholder="请输入GC比例" />
        </el-form-item>
        <el-form-item label="replicon_count" prop="replicon_count">
          <el-input v-model="form.replicon_count" placeholder="请输入replicon_count" />
        </el-form-item>
        <el-form-item label="scaffold_count" prop="scaffold_count">
          <el-input v-model="form.scaffold_count" placeholder="请输入scaffold_count" />
        </el-form-item>
        <el-form-item label="contig_count" prop="contig_count">
          <el-input v-model="form.contig_count" placeholder="请输入contig_count" />
        </el-form-item>
        <el-form-item label="总基因数" prop="totalGeneCount">
          <el-input v-model="form.totalGeneCount" placeholder="请输入总基因数" />
        </el-form-item>
        <el-form-item label="翻译蛋白基因数" prop="proteinCodingGeneCount">
          <el-input v-model="form.proteinCodingGeneCount" placeholder="请输入翻译蛋白基因数" />
        </el-form-item>
        <el-form-item label="非编码基因数" prop="nonCodingGeneCount">
          <el-input v-model="form.nonCodingGeneCount" placeholder="请输入非编码基因数" />
        </el-form-item>
        <el-form-item label="耐药药品" prop="nyjy">
          <el-input type="textarea" v-model="form.nyjy" placeholder="请输入耐药药品" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <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,getAssemblyAccessionList } 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:3,
        assemblyAccession: null,
        organismName: null,
        group: null,
        group1: null,
        defined: null,
        nyjy:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      upload_file: "",
      lists: [],
      disabledAssemblyAccession:false,
      assemblyAccessionOptions: [],
      assemblyAccessionLoading: false,
      organismNameOptions: [],
      organismNameLoading: false,

    }
  },
  created() {
    this.getList()
  },
  methods: {
         // 查询NCBI基因组序列编号
    queryAssemblyAccession(query) {
      if (query) {
        this.assemblyAccessionLoading = true
        getAssemblyAccessionList(query).then(response => {
          this.assemblyAccessionOptions = response.data
          this.assemblyAccessionLoading = false
        })
      } else {
        this.assemblyAccessionOptions = []
      }
    },
    // 查询病原体名称
    queryOrganismName(query) {
      if (query) {
        this.organismNameLoading = true
        getAssemblyAccessionList(query).then(response => {
          this.organismNameOptions = response.data
          this.organismNameLoading = false
        })
      } else {
        this.organismNameOptions = []
      }
    },
    // 读取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,
        nyjy: 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(() => {})
    },
     /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.disabledAssemblyAccession = false
      this.title = "添加病原体信息"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.assemblyAccession
      this.disabledAssemblyAccession = true;
      getDataBase(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改病原体信息"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.assemblyAccession != null) {
            updateDataBase(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addDataBase(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
  }
}
</script>
