<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="登记类型" prop="registerType">
        <el-select v-model="queryParams.registerType" placeholder="请选择登记类型" clearable size="small">
          <el-option v-for="(i,index) in templateData.registerTypeData" :key="index" :label="i.label" :value="i.id"/>
        </el-select>
      </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.sortData" :key="index" :label="i.label" :value="i.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option v-for="(i,index) in templateData.statusData" :key="index" :label="i.label"
                     :value="i.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="隔离地点" prop="isolatedPlace">
        <el-select v-model="form.isolatedPlace" placeholder="请选择隔离地点">
          <el-option v-for="i in addrList" :key="i.id" :label="i.addrName"
                     :value="i.id"/>
        </el-select>
      </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.rnaAndAntigenResultData" :key="index" :label="i.label"
                     :value="i.id"/>
        </el-select>
      </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.rnaAndAntigenResultData" :key="index" :label="i.label"
                     :value="i.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="检测值" prop="estimatedValue">
        <el-input
          v-model="queryParams.estimatedValue"
          placeholder="请输入检测值"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="派单时间" prop="sendOrdersEnd">
        <el-date-picker clearable size="small"
                        v-model="queryParams.sendOrdersEnd"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择派单时间">
        </el-date-picker>
      </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:register: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:register: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:register: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:register: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>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="dayImport(0)"
        >7天派单导出
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="dayImport(1)"
        >14天派单导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="registerList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="派单时间" align="center" prop="sendOrdersEnd" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sendOrdersEnd, '{y}-{m}-{d}  {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分类" align="center" prop="sort"/>
      <el-table-column label="编号" align="center" prop="userNo"/>
      <el-table-column label="姓名" align="center" prop="userName"/>
      <el-table-column label="证件号" align="center" prop="attestNo"/>
      <el-table-column label="登记类型" align="center" prop="registerType"/>
      <el-table-column label="登记时间" align="center" prop="createTime"/>
      <el-table-column label="状态" align="center" prop="status"/>
      <el-table-column label="隔离地点" align="center" prop="isolatedPlace"/>
      <el-table-column label="核酸结果" align="center" prop="rnaResult"/>
      <el-table-column label="抗原结果" align="center" prop="antigenResult"/>
      <el-table-column label="检测值" align="center" prop="estimatedValue"/>
      <el-table-column label="登记说明" align="center" prop="registerExplain"/>
      <el-table-column label="属地联系人" align="center" prop="belongName"/>
      <el-table-column label="联系电话" align="center" prop="belongPhone"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <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:register:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['yiqing:register: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="registerType">
          <el-select v-model="form.registerType" placeholder="请选择登记类型">
            <el-option v-for="(i,index) in templateData.registerTypeData" :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.sortData" :key="index" :label="i.label" :value="i.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option v-for="(i,index) in templateData.statusData" :key="index" :label="i.label" :value="i.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="隔离地点" prop="isolatedPlace">
          <el-select v-model="form.isolatedPlace" placeholder="请选择隔离地点" @change="changeIso">
            <el-option v-for="i in addrList" :key="i.id" :label="i.addrName"
                       :value="i.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="核酸结果" prop="rnaResult">
          <el-select v-model="form.rnaResult" placeholder="请选择核酸结果">
            <el-option v-for="(i,index) in templateData.rnaAndAntigenResultData" :key="index" :label="i.label"
                       :value="i.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="抗原结果" prop="antigenResult">
          <el-select v-model="form.antigenResult" placeholder="请选择抗原结果">
            <el-option v-for="(i,index) in templateData.rnaAndAntigenResultData" :key="index" :label="i.label"
                       :value="i.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="检测值" prop="estimatedValue">
          <el-input v-model="form.estimatedValue" placeholder="请输入检测值"/>
        </el-form-item>
        <el-form-item label="登记说明" prop="registerExplain">
          <el-input v-model="form.registerExplain" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="姓名" prop="belongName">
          <el-input v-model="form.belongName" disabled placeholder="姓名"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="belongPhone">
          <el-input v-model="form.belongPhone" disabled placeholder="联系电话"/>
        </el-form-item>
        <el-form-item label="登记时间" prop="createTime">
          <el-date-picker clearable size="small"
                          v-model="form.createTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择登记时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" 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 {
    addRegister,
    delRegister,
    exportRegister,
    fourteenImport,
    getRegister,
    importTemplate,
    listRegister,
    sevenImport,
    updateRegister
  } from '@/api/yiqing/register'

  import { getToken } from '@/utils/auth'
  import { listAddr } from '@/api/yiqing/addr'

  export default {
    name: 'Register',
    data() {
      return {
        templateData: {
          registerTypeData: [
            {
              'id': '异常登记',
              'label': '异常登记'
            }, {
              'id': '入舱转运',
              'label': '入舱转运'
            }, {
              'id': '在舱管控',
              'label': '在舱管控'
            }, {
              'id': '第7天核酸',
              'label': '第7天核酸'
            }, {
              'id': '第14天核酸',
              'label': '第14天核酸'
            }, {
              'id': '常规核酸',
              'label': '常规核酸'
            }, {
              'id': '出院/舱',
              'label': '出院/舱'
            }, {
              'id': '居家管控',
              'label': '居家管控'
            }, {
              'id': '解除隔离',
              'label': '解除隔离'
            }, {
              'id': '移除异常',
              'label': '移除异常'
            }],
          sortData: [
            {
              'id': '正常',
              'label': '正常'
            }, {
              'id': '密接/次密接',
              'label': '密接/次密接'
            }, {
              'id': '阳性',
              'label': '阳性'
            }, {
              'id': '解除密接',
              'label': '解除密接'
            }, {
              'id': '阳性治愈',
              'label': '阳性治愈'
            }, {
              'id': '死亡',
              'label': '死亡'
            }, {
              'id': '其他',
              'label': '其他'
            }, {
              'id': '复阳/感染',
              'label': '复阳/感染'
            }
          ],
          statusData: [
            {
              'id': '居家隔离',
              'label': '居家隔离'
            }, {
              'id': '居家待处理',
              'label': '居家待处理'
            }, {
              'id': '集中隔离',
              'label': '集中隔离'
            }, {
              'id': '失联',
              'label': '失联'
            }, {
              'id': '其他',
              'label': '其他'
            }
          ],
          rnaAndAntigenResultData: [
            {
              'id': '阴性',
              'label': '阴性'
            }, {
              'id': '阳性',
              'label': '阳性'
            }, {
              'id': '未测试',
              'label': '未测试'
            }
          ]
        },
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 疫情登记信息表格数据
        registerList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        upload: {
          // 是否显示弹出层（用户导入）
          open: false,
          // 弹出层标题（用户导入）
          title: '',
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          // 设置上传的请求头部
          headers: { Authorization: 'Bearer ' + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + '/yiqing/register/importData'
        },
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          userDataId: null,
          registerType: null,
          sort: null,
          status: null,
          isolatedPlace: null,
          rnaResult: null,
          antigenResult: null,
          estimatedValue: null,
          registerExplain: null,
          belongName: null,
          belongPhone: null,
          sendOrdersEnd: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        addrList: {}
      }
    },
    created() {
      this.getList()
      this.getListAddr()
    },
    methods: {
      dayImport(type) {
        const queryParams = this.queryParams
        if (type === 0) {
          this.$modal.confirm('是否确认导出7天派单数据？').then(() => {
            this.exportLoading = true
            return sevenImport(queryParams)
          }).then(response => {
            this.$download.name(response.msg)
            this.exportLoading = false
          }).catch(() => {
            this.exportLoading = false
          })
        } else {
          this.$modal.confirm('是否确认导出14天派单数据？').then(() => {
            this.exportLoading = true
            return fourteenImport(queryParams)
          }).then(response => {
            this.$download.name(response.msg)
            this.exportLoading = false
          }).catch(() => {
            this.exportLoading = false
          })
        }
        this.exportLoading = false
      },
      changeIso() {
        this.addrList.forEach(a => {
          if (a.id === this.form.isolatedPlace) {
            this.form.belongName = a.belongName
            this.form.belongPhone = a.belongPhone
          }
        })
      },
      /** 查询所有疫情地理信息列表 */
      getListAddr() {
        listAddr().then(response => {
          this.addrList = response.data
        })
      },
      /** 查询疫情登记信息列表 */
      getList() {
        this.loading = true
        listRegister(this.queryParams).then(response => {
          this.registerList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          userDataId: null,
          registerType: null,
          sort: null,
          status: null,
          isolatedPlace: null,
          rnaResult: null,
          antigenResult: null,
          estimatedValue: null,
          registerExplain: null,
          belongName: null,
          belongPhone: null,
          sendOrdersEnd: 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.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
        getRegister(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) {
              updateRegister(this.form).then(() => {
                this.$modal.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              addRegister(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 delRegister(ids)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        }).catch(() => {
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams
        this.$modal.confirm('是否确认导出所有疫情登记信息数据项？').then(() => {
          this.exportLoading = true
          return exportRegister(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.loading = false
        this.$alert(response.msg, '导入结果', { dangerouslyUseHTMLString: true })
        this.cancel()
        this.getList()
      },

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

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

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