<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="登记时间" prop="transferTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.normalTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择登记时间">
        </el-date-picker>
      </el-form-item>
      <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="normalStatus">
        <el-select v-model="queryParams.normalStatus"
                   placeholder="请选择状态">
          <el-option v-for="i in templateData.zzStatus" :label="i.label" :value="i.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="属地/社区" prop="areaName">
        <el-input
          v-model="queryParams.areaName"
          placeholder="请输入属地/社区"
          clearable
          size="small"
          @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"
          v-hasPermi="['yiqing:yquser: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:yquser: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:yquser: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:yquser:export']"-->
      <!--        >导出-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-form :inline="true" style="margin-top: 20px">
      <el-form-item label="状态" prop="normalStatus">
        <el-select v-model="queryParams.normalStatus1"
                   placeholder="状态" clearable size="small">
          <el-option v-for="i in templateData.zzStatus" :label="i.label" :value="i.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="派单时间">
        <el-date-picker clearable size="small"
                        v-model="queryParams.orderImportTime"
                        type="date"
                        :picker-options="pickerOptions"
                        value-format="yyyy-MM-dd"
                        placeholder="派单时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="登记后 ? 天">
        <el-input-number
          v-model="queryParams.registerGapDay"
          placeholder="天数"
          size="small"
        />
      </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>
        <el-button type="warning" icon="el-icon-download" size="mini" @click="orderImport">派单导出</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="yquserList" border>
      <el-table-column type="expand">
        <template slot-scope="scope">
          <div style="padding: 30px;font-size: 14px">
            <el-descriptions column="4">
              <template v-for="(item,index) in scope.row.rnsResults">
                <el-descriptions-item :label="'派单0'+(index+1)+'日期'">{{item.samplingTime}}</el-descriptions-item>
                <el-descriptions-item :label="'派单0'+(index+1)+'核酸结果'">{{item.rnaResult}}</el-descriptions-item>
              </template>
            </el-descriptions>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="编号" align="center" prop="userNo"/>
      <el-table-column label="登记时间" align="center" prop="normalTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.normalTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="属地/社区" align="center" prop="areaName"/>
      <el-table-column label="居委" align="center" prop="houseNo"/>
      <el-table-column label="状态" align="center" prop="normalStatus">
        <template slot-scope="scope">
          <div v-for="i in templateData.zzStatus"
               v-if="scope.row.normalStatus === i.id +''">
            <span>{{i.label}}</span>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="联系人" align="center" prop="belongName"/>
      <el-table-column label="联系电话" align="center" prop="belongPhone"/>
      <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="phone"/>
      <el-table-column label="居住地址" align="center" prop="address" width="200px"/>
      <el-table-column label="最新派单时间" align="center" prop="sevenSendOrders"/>
      <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:yquser:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['yiqing:yquser: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="userNo">
          <el-input v-model="form.userNo" placeholder="请输入编号"/>
        </el-form-item>
        <el-form-item label="异常类型" prop="exceptionRegisterType">
          <el-select v-model="form.exceptionRegisterType" placeholder="请选择异常登记类型">
            <el-option v-for="i in templateData.excRegType" :label="i.label" :value="i.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="异常登记时间" prop="exceptionRegisterTime">
          <el-date-picker clearable size="small"
                          v-model="form.exceptionRegisterTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择异常登记时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="当前隔离方式" prop="transferType">
          <el-select v-model="form.transferType"
                     placeholder="请选择当前隔离方式">
            <el-option v-for="i in templateData.transferData" :label="i.label" :value="i.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="解除管控时间" prop="transferTime">
          <el-date-picker clearable size="small"
                          v-model="form.transferTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择解除管控时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最新状态">
          <el-radio-group v-model="form.normalStatus">
            <el-radio v-for="i in templateData.zzStatus" :label="i.label" :value="i.id"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="出隔离时间" prop="normalTime">
          <el-date-picker clearable size="small"
                          v-model="form.normalTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择出隔离时间  ">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="证件类型" prop="otherAttest">
          <el-select v-model="form.otherAttest" placeholder="请选择证件类型">
            <el-option
              v-for="dict in dict.type.card_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="证件号" prop="attestNo">
          <el-input v-model="form.attestNo" placeholder="请输入证件号"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话"/>
        </el-form-item>
        <el-form-item label="关联地址" prop="otherAttest">
          <el-select v-model="form.addrId" placeholder="请选择关联地址" @change="changeIso">
            <el-option
              v-for="dict in addrList"
              :key="dict.addrId"
              :label="dict.addrName"
              :value="dict.addrId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="居住地址" prop="address">
          <el-input v-model="form.address" disabled placeholder="居住地址"/>
        </el-form-item>
        <el-form-item label="居委" prop="houseNo">
          <el-input v-model="form.houseNo" disabled placeholder="居委"/>
        </el-form-item>
        <el-form-item label="属地/社区" prop="areaName">
          <el-input v-model="form.areaName" disabled 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="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 {
    addYquser,
    delYquser,
    exportYquser,
    getYquser,
    importTemplate,
    listYquser,
    ordersImport,
    updateYquser
  } from '@/api/yiqing/yquser'

  import { listAddr } from '@/api/yiqing/addr'

  import { getToken } from '@/utils/auth'

  export default {
    name: 'Yquser',
    dicts: ['card_type'],
    data() {
      return {
        pickerOptions: {
          disabledDate(time) {
            const date = new Date()
            return time.getTime() < Date.now() - 8.64e7     //禁止选取当天之后的日期（不包括静止当天）
            //或者是：
            //return time.getTime() > Date.now();
            //return time.getTime() > Date.now()- 8.64e7;     //禁止选取当天之后的日期（包括禁止当天）
          }
        },
        templateData: {
          rnaResultData: [
            {
              'id': '阴性',
              'label': '阴性'
            }, {
              'id': '阳性',
              'label': '阳性'
            }, {
              'id': '应测未测',
              'label': '应测未测'
            }
          ],
          transferData: [
            {
              id: '1',
              label: '居家隔离'
            }, {
              id: '2',
              label: '阳性集中隔离'
            }, {
              id: '3',
              label: '密接集中隔离'
            }, {
              id: '4',
              label: '解除隔离'
            }, {
              id: '5',
              label: '失联'
            }, {
              id: '6',
              label: '其他'
            }
          ],
          zzStatus: [
            {
              id: '1',
              label: '阳性'
            }, {
              id: '2',
              label: '密接'
            }, {
              id: '3',
              label: '次密接'
            }, {
              id: '4',
              label: '复阳'
            }, {
              id: '6',
              label: '阳性治愈'
            }, {
              id: '7',
              label: '失联'
            }, {
              id: '8',
              label: '死亡'
            }, {
              id: '9',
              label: '其它'
            }
          ]
        },
        // 遮罩层
        loading: false,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 阳性及密接人员表格数据
        yquserList: [],
        addrList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        upload: {
          // 是否显示弹出层（用户导入）
          open: false,
          // 弹出层标题（用户导入）
          title: '',
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          // 设置上传的请求头部
          headers: { Authorization: 'Bearer ' + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + '/yiqing/yquser/importData'
        },
        // 查询参数
        queryParams: {
          listType: 1,
          pageNum: 1,
          pageSize: 10,
          otherAttest: null,
          attestNo: null,
          areaName: null,
          rnaResult: null,
          exceptionRegisterType: null,
          exceptionRegisterTime: null,
          transferType: null,
          transferTime: null,
          normalStatus: null,
          normalStatus1: null,
          normalTime: null,
          orderTime: null,
          orderImportTime: null,
          registerGapDay: 7
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      }
    },
    created() {
      this.getList()
      this.getAddrList()
    },
    methods: {
      /** 查询阳性及密接人员列表 */
      getList() {
        this.loading = true
        listYquser(this.queryParams).then(response => {
          this.yquserList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      /** 查询阳性及密接人员列表 */
      getAddrList() {
        listAddr().then(response => {
          this.addrList = response.data
        })
      },
      changeIso() {
        this.addrList.forEach(a => {
          if (a.addrId === this.form.addrId) {
            this.form.address = a.addrDetail
            this.form.areaName = a.addrName
            this.form.houseNo = a.houseNo
            this.form.belongName = a.belongName
            this.form.belongPhone = a.belongPhone
          }
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          dataId: null,
          userNo: null,
          userName: null,
          otherAttest: null,
          attestNo: null,
          phone: null,
          address: null,
          houseNo: null,
          areaName: null,
          sevenSendOrders: null,
          fourteenSendOrders: null,
          exceptionRegisterType: null,
          exceptionRegisterTime: null,
          transferType: null,
          transferTime: null,
          normalStatus: null,
          normalTime: '',
          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.normalTime = null
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.dataId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = '添加阳性及密接人员'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const dataId = row.dataId || this.ids
        getYquser(dataId).then(response => {
          this.form = response.data
          this.open = true
          this.title = '修改阳性及密接人员'
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.dataId != null) {
              updateYquser(this.form).then(() => {
                this.$modal.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              addYquser(this.form).then(() => {
                this.$modal.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const dataIds = row.dataId || this.ids
        this.$modal.confirm('是否确认删除阳性及密接人员编号为"' + dataIds + '"的数据项？').then(function() {
          return delYquser(dataIds)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        }).catch(() => {
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams
        this.$modal.confirm('是否确认导出所有阳性及密接人员数据项？').then(() => {
          this.exportLoading = true
          return exportYquser(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'
      },
      orderImport() {
        const queryParams = this.queryParams
        if (null === queryParams.orderImportTime || undefined === queryParams.orderImportTime) {
          this.$alert('派单发起时间不能为空')
          return
        }
        this.$modal.confirm('是否确认派单导出，登记间隔 ' + queryParams.registerGapDay + ' 天的数据？').then(() => {
          this.exportLoading = true
          return ordersImport(queryParams)
        }).then(response => {
          this.$download.name(response.msg)
          this.exportLoading = false
        }).catch(() => {
        })
      }
    }
  }
</script>
