<template>
  <div class="div_main">
    <div class="div_search">
      <el-form :inline="true"
               :model="formData"
               ref="ref_searchform"
               :label-width="'110px'">
        <div>

          <el-form-item label="操作时间"
                        label-width="110px"
                        prop="startTime">
            <el-date-picker v-model="timeRange"
                            size="small"
                            type="datetimerange"
                            @change="handleTimeChange"
                            format="yyyy-MM-dd HH:mm"
                            value-format="yyyy-MM-dd HH:mm"
                            range-separator="至"
                            start-placeholder="开始时间"
                            end-placeholder="结束时间">
            </el-date-picker>

          </el-form-item>

          <div>
            <el-form-item label="姓名"
                          prop="name">
              <el-input v-model="formData.name"
                        size="small"
                        class="list_item_width_1"
                        clearable
                        placeholder="请输入姓名">
              </el-input>

            </el-form-item>

            <el-form-item label="手机"
                          prop="mobile">
              <el-input v-model="formData.mobile"
                        size="small"
                        class="list_item_width_1"
                        clearable
                        placeholder="请输入手机号">
              </el-input>

            </el-form-item>

            <el-button style="margin-left:30px"
                       size="small"
                       class="diy-button--search"
                       @click="handleSearch">查询</el-button>
            <el-button style="margin-left:10px"
                       size="small"
                       class="diy-button--reset"
                       @click="resetSearch">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>
    <div class="div_table">
      <div class="div_btn">

        <el-button style="float:left;margin-left:10px"
                   class="diy-button--reset"
                   size="small"
                   @click="handleExport">导出</el-button>
      </div>

      <el-table class="table"
                ref="ref_table"
                :data="tableData"
                border
                :height="tableHeight"
                v-loading="tableLoading"
                :header-cell-style="{background:'#2195FE',color:'#FFFFFF'}"
                style="width: 100%">
        <el-table-column label="序号"
                         header-align="center"
                         align="center"
                         type="index"
                         width="50"></el-table-column>

        <el-table-column prop="name"
                         header-align="center"
                         align="center"
                         label="姓名"
                         min-width="100">
        </el-table-column>
        <el-table-column prop="mobile"
                         header-align="center"
                         align="center"
                         label="手机号"
                         min-width="110">
        </el-table-column>

        <el-table-column prop="natTime"
                         header-align="center"
                         align="center"
                         label="操作事项"
                         min-width="230">
        </el-table-column>
        <el-table-column prop="natAddress"
                         header-align="center"
                         align="center"
                         show-overflow-tooltip
                         label="操作时间"
                         min-width="150">
        </el-table-column>

        <!-- <el-table-column label="操作"
                         fixed="right"
                         width="140"
                         header-align="center"
                         align="center"
                         class="operate">
          <template slot-scope="scope">

            <el-button type="text"
                       class="div-table-button--detail"
                       size="small"
                       @click="handleDetail(scope.row)">查看</el-button>

       
          

          </template>
        </el-table-column> -->
      </el-table>
      <div>
        <el-pagination @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       :current-page.sync="pageNo"
                       :page-sizes="[20, 50, 100, 200]"
                       :page-size="pageSize"
                       layout="sizes, prev, pager, next, total"
                       :total="total">
        </el-pagination>
      </div>
    </div>

  </div>
</template>

<script>



import { requestPost } from "@/js/dai/request";
import { mapGetters } from 'vuex'
import { Loading } from 'element-ui' // 引入Loading服务

let loading // 加载动画
export default {
  data () {
    return {
      loading: false,
      total: 0,
      pageSize: 20,
      pageNo: 0,
      tableLoading: false,


      agencyId: '',


      timeRange: [],
      formData: {

        name: '',//姓名
        mobile: '',//手机号
        idCard: '',//身份证号码
        startTime: '',//检测开始时间yyyy-MM-dd HH:mm
        endTime: '',//检测结束时间yyyy-MM-dd HH:mm


      },

      tableData: [],



    }
  },
  components: {

  },
  async created () {

  },
  async mounted () {
    //获取场所类型


    const { user } = this.$store.state
    this.agencyId = user.agencyId


    await this.loadTable()
  },

  activated () {
    this.$refs['ref_table'].doLayout()
  },

  methods: {

    async handleSearch () {
      this.isResiUser = this.formData.orgType === 'current'

      await this.loadTable()

      this.$nextTick(() => {
        this.$refs.ref_table.doLayout() // 解决表格错位
      })

    },

    async loadTable () {
      this.tableLoading = true

      const url = "/epmetuser/icNat/natlist"
      // const url = "http://yapi.elinkservice.cn/mock/245/epmetuser/icNat/natlist"
      let params = {
        pageSize: this.pageSize,
        pageNo: this.pageNo,
        ...this.formData
      }

      const { data, code, msg } = await requestPost(url, params)

      if (code === 0) {
        this.total = data.total
        this.tableData = data.list

        this.tableData.forEach(item => {

          item.isResiUserShow = item.isResiUser === '0' ? '否' : '是'
          if (item.natResult === '1') {
            item.natResultShow = '阳性'
          } else if (item.natResult === '0') {
            item.natResultShow = '阴性'
          } else {
            item.natResultShow = ''
          }

        });




      } else {
        this.$message.error(msg)
      }
      this.tableLoading = false
    },

    handleTimeChange (time) {
      if (time) {
        this.formData.startTime = time[0]
        this.formData.endTime = time[1]

      } else {
        this.formData.startTime = ''
        this.formData.endTime = ''

      }

    },


    //重置搜索条件
    resetSearch () {
      this.formData = {
        orgType: 'current',//当前组织:current 根组织：all
        name: '',//姓名
        mobile: '',//手机号
        idCard: '',//身份证号码
        startTime: '',//检测开始时间yyyy-MM-dd HH:mm
        endTime: '',//检测结束时间yyyy-MM-dd HH:mm
      }
      this.timeRange = []

      this.pageNo = 0
      // this.loadTable()
    },

    handleSizeChange (val) {
      this.pageSize = val
      this.pageNo = 1
      this.loadTable()
    },
    handleCurrentChange (val) {
      this.pageNo = val
      this.loadTable()
    },

    //导出表格
    async handleExport () {
      let title = '核酸检测信息'

      const url = "/epmetuser/icNat/export"

      app.ajax.exportFilePost(
        url,
        this.formData,
        (data, rspMsg) => {
          this.download(data, title + '.xlsx')
        },
        (rspMsg, data) => {
          this.$message.error(rspMsg);
        }
      );

    },

    // 下载文件
    download (data, fileName) {
      if (!data) {
        return
      }

      var csvData = new Blob([data])

      if (window.navigator && window.navigator.msSaveOrOpenBlob) {
        window.navigator.msSaveOrOpenBlob(csvData, fileName);
      }
      // for Non-IE (chrome, firefox etc.)
      else {
        var a = document.createElement('a');
        document.body.appendChild(a);
        a.style = 'display: none';
        var url = window.URL.createObjectURL(csvData);
        a.href = url;
        a.download = fileName;
        a.click();
        a.remove();
        window.URL.revokeObjectURL(url);
      }

    },



    // 开启加载动画
    startLoading () {
      loading = Loading.service({
        lock: true, // 是否锁定
        text: '正在加载……', // 加载中需要显示的文字
        background: 'rgba(0,0,0,.7)' // 背景颜色
      })
    },
    // 结束加载动画
    endLoading () {
      // clearTimeout(timer);
      if (loading) {
        loading.close()
      }
    }
  },
  computed: {
    tableHeight () {

      return this.$store.state.inIframe ? this.clientHeight - 420 + this.iframeHeight : this.clientHeight - 420


    },

    ...mapGetters(['clientHeight', 'iframeHeight'])
  },
  watch: {

  },
  props: {

  }
}
</script>
<style lang="scss" scoped >
@import "@/assets/scss/modules/management/epidemic.scss";
</style>


