<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="姓名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="日期">
        <el-date-picker style="width: 300px" v-model="dateRange" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="部门" prop="orgname">
        <el-input v-model="queryParams.orgname" placeholder="请输入部门" clearable @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="warning" icon="el-icon-upload" size="mini" @click="handleImport">导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="clockRecordList" stripe>
      <el-table-column label="序号" type="index" width="55" align="center" />
      <el-table-column label="设备工号" align="center" prop="deviceNum" />
      <el-table-column label="员工工号" align="center" prop="teacherCode" />
      <el-table-column label="姓名" align="center" prop="userName" />
      <el-table-column label="部门" align="center" prop="orgname" />
      <el-table-column label="考勤日期" align="center" prop="clockDate" />
      <el-table-column label="打卡时间" min-width="200px" align="center" prop="clockTime" />
      <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-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:clockRecord:edit']">删除
          </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="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 + '?updateSupport=' + upload.updateSupport" :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">
          <!-- <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport"/>
            是否更新已经存在的用户数据
          </div> -->
          <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="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSchoolAtt, deleteRow } from "@/api/smartSchool/schoolManage/clockmanage.js"
import { getToken } from "@/utils/auth";

export default {
  name: 'SchoolManageClockRecord',
  components: {},
  props: {},
  data() {
    return {
      // 日期范围
      dateRange: [],
      // 模糊搜索
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        orgname: undefined,
      },
      // 加载遮罩
      loading: true,
      // 记录条数
      total: 0,
      // 记录列表
      clockRecordList: [],
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/schoolAtt/importData"
      },
    };
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取列表
    getList() {
      this.loading = true;
      listSchoolAtt(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.clockRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      })
    },
    // 删除记录
    handleDelete(row) {
      this.$modal.confirm('是否确认删除').then(() => {
        return deleteRow({
          deviceNum: row.deviceNum,
          teacherCode: row.teacherCode,
          clockDate: row.clockDate
        }).then((res) => {
          if (res.code == 200) {
            this.$modal.msgSuccess("删除成功");
          }
        });
      }).then(() => {
        this.getList();
      }).catch(() => {
      });
    },
    // 模糊搜索
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/schoolAtt/export', {
        ...this.queryParams
      }, `schoolAtt_${Date.now()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "记录导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('/schoolAtt/importTemplate', {}, `schoolAtt_template_${Date.now()}.xlsx`)
    },
    // 文件上传中处理
    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("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },


};
</script>
<style scoped></style>
