<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="perName">
        <el-input
          v-model="queryParams.perName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="cardNumber">
        <el-select
          v-model="queryParams.cardNumber"
          placeholder="请选择卡号"
          clearable
        >
          <el-option
            v-for="dict in cardNoList"
            :key="dict.value"
            :label="dict.name"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="" prop="departId">
        <el-select
          v-model="queryParams.departId"
          placeholder="请选择部门"
          clearable
        >
          <el-option
            v-for="dict in DepartmentList"
            :key="dict.value"
            :label="dict.name"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="" prop="positionId">
        <el-select v-model="queryParams.positionId" placeholder="请选择职务" clearable>
          <el-option
            v-for="dict in InfoPositionList"
            :key="dict.value"
            :label="dict.name"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="jobTypeId">
        <el-select v-model="queryParams.jobTypeId" placeholder="请选择工种" clearable>
          <el-option
            v-for="dict in WorkTypeList"
            :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="结束日期"
          :picker-options="pickerOptions"
        ></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-table v-loading="loading" :data="perHisList">
      <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="stationName" />
      <el-table-column label="分站位置" align="center" prop="location" />
      <el-table-column label="所属区域" align="center" prop="areaName" />
      <el-table-column label="时间" align="center" prop="time">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分站距离（m）" align="center" prop="distance" />
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { perHistory } from "@/api/tyler/perHis";
import {
  baseInfoDepartment,
  baseInfoArea,
  baseInfoStation,
  baseInfoWorkType,
  baseInfoPosition,
  baseInfoCardNumber,
} from "@/api/tyler/common";

export default {
  name: "perHis",
  data() {
    return {
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,

      // 考勤管理数据表格数据
      perHisList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        perName: null,
        cardNumber: null,
        departId: null,
        positionId: null,
        jobTypeId: null,
      },
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // common
      //卡号列表
      cardNoList: [],
      //部门列表
      DepartmentList: [],
      //分站列表
      StationList: [],
      //区域列表
      AreaList: [],
      //职务列表
      InfoPositionList: [],
      //工种列表
      WorkTypeList: [],
      pickerOptions: {
        disabledDate: (time) => {
          const oneDay = 24 * 3600 * 1000;
          const maxRange = 7 * oneDay;
          if (this.pickerState.minDate) {
            const minTime = new Date(this.pickerState.minDate).getTime();
            return (
              time.getTime() > Date.now() ||
              time.getTime() < minTime - maxRange ||
              time.getTime() > minTime + maxRange
            );
          }
          return time.getTime() > Date.now();
        },
        onPick: ({ maxDate, minDate }) => {
          this.pickerState = { minDate, maxDate };
        },
      },
      pickerState: {},
    };
  },
  created() {
    // 设置默认日期范围为最近7天
    const end = new Date();
    const start = new Date();
    start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
    //转化为yyyy-MM-dd格式
    const formatDate = (date) => {
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, "0");
      const day = String(date.getDate()).padStart(2, "0");
      return `${year}-${month}-${day}`;
    };
    this.dateRange = [formatDate(start), formatDate(end)];
    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;
      });
      //职务
      baseInfoPosition().then((res) => {
        this.InfoPositionList = res.data;
      });

      //工种
      baseInfoWorkType().then((res) => {
        this.WorkTypeList = res.data;
      });
    },
    /** 查询人员定位实时数据列表 */
    getList() {
      this.loading = true;
      perHistory(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.perHisList = response.rows;
          this.leaderList = response.remark;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        perName: null,
        departId: null,
        addressId: null,
        areaId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "business/per/export",
        {
          ...this.queryParams,
        },
        `per_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.now-leader {
  display: flex;
  justify-content: space-between;
}
</style>
