<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="employeeName">
        <el-input v-model="queryParams.employeeName" placeholder="员工姓名" clearable style="width: 250px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="部门" prop="deptId">
        <treeselect style="width: 250px" :default-expand-level="1" v-model="queryParams.deptId" :options="deptOptions"
          :normalizer="normalizer" placeholder="选择部门" />
      </el-form-item>
      <el-form-item label="手机号" prop="employeeMobile">
        <el-input v-model="queryParams.employeeMobile" placeholder="请输入手机号" clearable style="width: 250px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="开始日期" prop="attendanceStartDate">
        <el-date-picker
          v-model="queryParams.attendanceStartDate"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="选择日期"
          style="width: 250px">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束日期" prop="attendanceEndDate">
        <el-date-picker
          v-model="queryParams.attendanceEndDate"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="选择日期"
          style="width: 250px">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
          v-hasPermi="['attendance:infoDaily:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="typeList">
      <el-table-column label="序号" type="index" width="50"align="center"/>
      <el-table-column label="员工姓名" align="center" prop="employeeName" min-width="80" />
      <el-table-column label="部门" align="center" prop="deptName" min-width="100" />
      <el-table-column label="手机号" align="center" prop="employeeMobile" min-width="100" />
      <el-table-column label="日期" align="center" prop="attendanceDate" min-width="100" />
      <el-table-column label="上班时间" align="center" prop="workStartTime" min-width="100" />
      <el-table-column label="下班下班" align="center" prop="workEndTime" min-width="100" />
      <el-table-column label="迟到时间(min)" align="center" prop="lateMinute" min-width="100" />
      <el-table-column label="早退时间(min)" align="center" prop="earlyMinute" min-width="100" />
      <el-table-column label="总工作时间(min)" align="center" prop="totalWorkingMinute" min-width="100" />
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import { listInfoDaily, exportInfoDaily } from "@/api/personnel/attendance/info/index";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { listDept } from "@/api/system/dept";
export default {
  name: "SystemDictType",
  components: {
    Treeselect,
  },
  data() {
    return {
      postOptions: [],
      // 部门列表
      deptOptions: [], // 部门属性结构
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        employeeName: undefined,
        deptId: undefined,
        employeeMobile: undefined,
        attendanceStartDate: this.getNowDate(),
        attendanceEndDate: this.getNowDate(),
      },
    };
  },
  created() {
    this.getList();
    this.getListDept();
  },
  methods: {
    //获取当前日期(年月日)
    getNowDate() {
      const date = new Date()
      const year = date.getFullYear()
      const month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
      const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
      return `${year}-${month}-${day}`
    },
    /** 查询部门列表 */
    getListDept() {
      listDept().then(response => {
        this.deptOptions = this.handleTree(response.data, "id");
      });
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      listInfoDaily(this.queryParams).then(response => {
        this.typeList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = { ...this.queryParams };
      params.pageNo = undefined;
      params.pageSize = undefined;
      // 执行导出
      this.$modal.confirm('是否确认导出所有数据项?').then(() => {
        this.exportLoading = true;
        return exportInfoDaily(params);
      }).then(response => {
        this.$download.excel(response, '每日考勤信息.xls');
        this.exportLoading = false;
      }).catch(() => { });
    }
  }
};
</script>