<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="姓名" prop="areaName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="采样时间" prop="samplingTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.samplingTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择采样时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="核酸分类" prop="sort">
        <el-select v-model="queryParams.sort" placeholder="请选择分类" clearable size="small">
          <el-option v-for="(i,index) in templateData.rnaTypeSort" :key="index" :label="i.label"
                     :value="i.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="检测时间" prop="resultTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.resultTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择检测时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="核酸结果" prop="rnaResult">
        <el-select v-model="queryParams.rnaResult" placeholder="请选择核酸结果" clearable size="small">
          <el-option v-for="(i,index) in templateData.rnaResultData" :key="index" :label="i.label"
                     :value="i.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="测定值" prop="ctValue">
        <el-input
          v-model="queryParams.ctValue"
          placeholder="请输入CT检测值"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="抗原结果" prop="antigenResult">
        <el-select v-model="queryParams.antigenResult" placeholder="请选择抗原结果" clearable size="small">
          <el-option v-for="(i,index) in templateData.rnaResultData" :key="index" :label="i.label"
                     :value="i.id"/>
        </el-select>
      </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"
          v-hasPermi="['yiqing:rnsResult:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['yiqing:rnsResult:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['yiqing:rnsResult:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['yiqing:rnsResult:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="handleImport"
        >导入
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rnsResultList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="采样时间" align="center" prop="samplingTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.samplingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="userName"/>
      <el-table-column label="证件类型" align="center" prop="otherAttest">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.card_type" :value="scope.row.otherAttest"/>
        </template>
      </el-table-column>
      <el-table-column label="证件号" align="center" prop="attestNo"/>
      <el-table-column label="核酸结果" align="center" prop="rnaResult"/>
      <el-table-column label="测定值" align="center" prop="ctValue"/>
      <el-table-column label="抗原结果" align="center" prop="antigenResult"/>
      <el-table-column label="核酸分类" align="center" prop="sort"/>
      <el-table-column label="检测时间" align="center" prop="resultTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.resultTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="ORF1ab基因（SS）" align="center" prop="excelT1" width="180"/>
      <el-table-column label="N基因（SS）" align="center" prop="excelT2" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['yiqing:rnsResult:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['yiqing:rnsResult: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="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="采样时间" prop="samplingTime">
          <el-date-picker clearable size="small"
                          v-model="form.samplingTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择采样时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="检测时间" prop="resultTime">
          <el-date-picker clearable size="small"
                          v-model="form.resultTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择检测时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="核酸结果" prop="rnaResult">
          <el-select v-model="form.rnaResult" placeholder="请选择核酸结果">
            <el-option v-for="(i,index) in templateData.rnaResultData" :key="index" :label="i.label"
                       :value="i.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="测定值" prop="ctValue">
          <el-input v-model="form.ctValue" placeholder="请输入CT检测值"/>
        </el-form-item>
        <el-form-item label="ORF1ab基因（SS）" prop="excelT1">
          <el-input v-model="form.excelT1" placeholder="请输入CT检测值"/>
        </el-form-item>
        <el-form-item label="N基因（SS）"  prop="excelT2">
          <el-input v-model="form.excelT2" placeholder="请输入CT检测值"/>
        </el-form-item>
        <el-form-item label="抗原结果" prop="antigenResult">
          <el-select v-model="form.antigenResult" placeholder="请选择抗原结果">
            <el-option v-for="(i,index) in templateData.rnaResultData" :key="index" :label="i.label"
                       :value="i.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="核酸分类" prop="sort">
          <el-select v-model="form.sort" placeholder="请选择分类">
            <el-option v-for="(i,index) in templateData.rnaTypeSort" :key="index" :label="i.label"
                       :value="i.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" 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>
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                   @click="importTemplate">下载模板
          </el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="cancelTemplate">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import {
    addRnsResult,
    delRnsResult,
    exportRnsResult,
    getRnsResult,
    importTemplate,
    listRnsResult,
    updateRnsResult
  } from '@/api/yiqing/rnsResult'
  import { getToken } from '@/utils/auth'

  export default {
    name: 'RnsResult',
    dicts: ['card_type'],
    data() {
      return {
        templateData: {
          rnaResultData: [
            {
              'id': '阴性',
              'label': '阴性'
            }, {
              'id': '阳性',
              'label': '阳性'
            }, {
              'id': '应测未测',
              'label': '应测未测'
            }
          ],
          rnaTypeSort: [
            {
              id: '日常核酸',
              label: '日常核酸'
            }, {
              id: '管控第 7 天核酸',
              label: '管控第 7 天核酸'
            }, {
              id: '管控第 14 天核酸',
              label: '管控第 14 天核酸'
            }, {
              id: '管控第 5 天核酸',
              label: '管控第 5 天核酸'
            }, {
              id: '管控第 21 天核酸',
              label: '管控第 21 天核酸'
            }, {
              id: '日常抗原',
              label: '日常抗原'
            }, {
              id: '其它',
              label: '其它'
            }
          ],
          zzStatus: [
            {
              id: '1',
              label: '转正'
            }, {
              id: '2',
              label: '空'
            }
          ]
        },
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 核酸检测记录表格数据
        rnsResultList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        upload: {
          // 是否显示弹出层（用户导入）
          open: false,
          // 弹出层标题（用户导入）
          title: '',
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          // 设置上传的请求头部
          headers: { Authorization: 'Bearer ' + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + '/yiqing/rnsResult/importData'
        },
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          userName: null,
          samplingTime: null,
          userDataId: null,
          sort: null,
          resultTime: null,
          rnaResult: null,
          ctValue: null,
          excelT1: null,
          excelT2: null,
          antigenResult: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      }
    },
    created() {
      this.getList()
    },
    methods: {
      /** 查询核酸检测记录列表 */
      getList() {
        this.loading = true
        listRnsResult(this.queryParams).then(response => {
          this.rnsResultList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          samplingTime: null,
          userDataId: null,
          sort: null,
          userName: null,
          resultTime: null,
          rnaResult: null,
          ctValue: null,
          excelT1: null,
          excelT2: null,
          antigenResult: null,
          isDelete: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams.userName = null
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = '添加核酸检测记录'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const id = row.id || this.ids
        getRnsResult(id).then(response => {
          this.form = response.data
          this.open = true
          this.title = '修改核酸检测记录'
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateRnsResult(this.form).then(() => {
                this.$modal.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              addRnsResult(this.form).then(() => {
                this.$modal.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        this.$modal.confirm('是否确认删除核酸检测记录编号为"' + ids + '"的数据项？').then(function() {
          return delRnsResult(ids)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        }).catch(() => {
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams
        this.$modal.confirm('是否确认导出所有核酸检测记录数据项？').then(() => {
          this.exportLoading = true
          return exportRnsResult(queryParams)
        }).then(response => {
          this.$download.name(response.msg)
          this.exportLoading = false
        }).catch(() => {
        })
      },
      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(response.msg, '导入结果', { dangerouslyUseHTMLString: true })
        this.cancel()
        this.getList()
      },

      importTemplate() {
        importTemplate().then(response => {
          this.$download.name(response.msg)
        })
      },

      submitFileForm() {
        this.$refs.upload.submit()
      },

      cancelTemplate() {
        this.upload.open = false
        this.form.moneyType = '3'
      }
    }
  }
</script>
