<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="" prop="areaId">
        <el-select v-model="queryParams.areaId" placeholder="请选择区域" clearable>
          <el-option
            v-for="dict in AreaList"
            :key="dict.value"
            :label="dict.name"
            :value="dict.value"
          />
        </el-select>
        
      </el-form-item>
      <el-form-item label="" prop="areaType">
        <el-select v-model="queryParams.areaType" placeholder="请选择区域类型" clearable>
          <el-option
            v-for="dict in AreaTypeList"
            :key="dict.value"
            :label="dict.name"
            :value="dict.value"
          />
        </el-select>
        
      </el-form-item>
      <el-form-item label="进入日期">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-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-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
        <el-button
          type="primary"
          plain
          icon="el-icon-printer"
          size="mini"
          v-print="printInfoObj"
        >打印</el-button>
      </el-form-item>
    </el-form>

    

    <el-table  id="printArea" v-loading="loading" :data="areaList" @selection-change="handleSelectionChange">
      <el-table-column label="姓名" align="center" prop="perName" />
      <el-table-column label="人员卡号" align="center" prop="cardNumber" />
      <el-table-column label="部门" align="center" prop="departName" />
      <el-table-column label="区域名称" align="center" prop="areaName" />
      <el-table-column label="进入时间" align="center" prop="entryTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.entryTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="离开时间" align="center" prop="leaveTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.leaveTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="停留时间(h)" align="center" prop="residenceTime" />
      
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    
  </div>
</template>

<script>
  import {getArea, areaHisGet} from "@/api/tyler/areaHis";
  import { baseInfoDepartment, baseInfoArea, baseInfoStation, baseInfoWorkType, baseInfoPosition, baseInfoCardNumber } from "@/api/tyler/common";
  export default {
  name: "Area",
  data() {
    return {
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 进出区域的历史数据表格数据
      areaList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaId: null,
        areaType:null,
       
      },
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // common
      //卡号列表
      cardNoList: [],
      //部门列表
      DepartmentList: [],
      //分站列表
      StationList: [],
      //区域列表
      AreaList: [],
      //区域类型
      AreaTypeList: [],
      // 打印
      printInfoObj:{
        id:"printArea",
        popTitle:"进出区域的历史数据",
        preview:false,  // 是否开启预览
        beforeOpenCallback:()=>{
          console.log('开始打印之前的callback')
        }
      },
    };
  },
  created() {
    this.getCommon();
    this.getList();
  },
  methods: {
    // common
    getCommon(){
      //卡号
      baseInfoCardNumber().then(res=>{
        this.cardNoList = res.data;
      })
      //部门
      baseInfoDepartment().then(res=>{
        this.DepartmentList = res.data;
      })
      //分站
      baseInfoStation().then(res=>{
        this.StationList = res.data;
      })
      //区域
      baseInfoArea().then(res=>{
        this.AreaList = res.data;
      })
      //区域类型
      this.AreaTypeList = [
        {value:'1',name:'重点区域'},
        {value:'2',name:'井口'},
        {value:'3',name:'限制区域'},
      ]
    },
    /** 查询进出区域的历史数据列表 */
    getList() {
      this.loading = true;
      areaHisGet(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.areaList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        areaName: null,
        cardNumber: null,
        departName: null,
        entryTime: null,
        leaveTime: null,
        residenceTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      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
      getArea(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) {
            updateArea(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addArea(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除？').then(function() {
        return delArea(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/business/area/history/export', {
        ...this.queryParams
      }, `进出区域的历史数据_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style media="print" lang="scss">
@page {
  size: auto;
  margin: 3mm;
}
@media print {
  html {
    background-color: #ffffff;
    height: auto;
    margin: 0px;
  }
 
  body {
    border: solid 1px #ffffff;
    margin: 10mm 15mm 10mm 15mm;
  }
  #printArea table {
    table-layout: auto !important;
  }
 
  #printArea .el-table__header-wrapper .el-table__header {
    width: 100% !important;
    border: solid 1px #f2f2f2;
  }
  #printArea .el-table__body-wrapper .el-table__body {
    width: 100% !important;
  }
  #printArea #pagetable table {
    table-layout: fixed !important;
  }
}
</style>