<template>
  <div class="app-container">
    <el-form
      ref="queryForm"
      :model="queryParams"
      size="small"
      :inline="true"
      class="app-container--search"
    >
      <el-form-item label="考勤日期" prop="strDay">
        <el-date-picker
          type="date"
          v-model="queryParams.strDay"
          format="yyyy 年 MM 月 dd 日"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item label="员工编号" prop="sysUserName">
        <el-input
          v-model="queryParams.sysUserName"
          placeholder="请输入员工编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="打卡状态" prop="checkInStatus">
        <el-select
          v-model="queryParams.checkInStatus"
          placeholder="请选择打卡状态"
          clearable
        >
          <el-option
            v-for="dict in daka_check_in_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </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-item style="float: right">
        <el-button type="warning" @click="handleExportSet"
          >导出考勤数据</el-button
        >
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="resultList">
      <el-table-column label="序号" type="index" width="100"> </el-table-column>
      <el-table-column label="打卡日期" width="120" prop="checkInTime">
        <template slot-scope="scope">
          {{ formatDate(scope.row.checkInTime) }}
        </template>
      </el-table-column>
      <el-table-column label="员工编号" width="120" prop="sysUserName">
      </el-table-column>
      <el-table-column
        label="员工姓名"
        prop="nickName"
        width="120"
      ></el-table-column>
      <el-table-column
        label="考勤组"
        prop="attendanceGroupName"
      ></el-table-column>
      <el-table-column label="上班" width="200">
        <template slot-scope="scope">
          <span style="color: #909399" v-if="scope.row.checkInStatus == '0'">
            <!-- 上班未打卡 -->
            未打卡
          </span>
          <span
            style="color: #409eff"
            v-else-if="scope.row.clockInStatus == '1'"
          >
            <!--上班已打卡-->
            正常 {{ formatTime(scope.row.clockIn) }}</span
          >
          <span
            style="color: #f56c6c"
            v-else-if="scope.row.clockInStatus == '3'"
          >
            迟到 {{ formatTime(scope.row.clockIn) }}
          </span>
          <span v-else>
            {{ scope.row.clockInStatus }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="下班" width="200">
        <template slot-scope="scope">
          <span style="color: #909399" v-if="scope.row.clockOutStatus == '0'">
            <!-- 下班未打卡 -->
            未打卡
          </span>
          <span
            style="color: #409eff"
            v-else-if="scope.row.clockOutStatus == '2'"
          >
            <!--下班已打卡-->
            正常 {{ formatTime(scope.row.clockOut) }}</span
          >
          <span
            style="color: #f56c6c"
            v-else-if="scope.row.clockOutStatus == '4'"
          >
            早退 {{ formatTime(scope.row.clockOut) }}
          </span>
          <span
            style="color: #f56c6c"
            v-else-if="scope.row.clockOutStatus == '5'"
          >
            更新 {{ formatTime(scope.row.clockOut) }}
          </span>
          <span v-else>
            {{ scope.row.clockOutStatus }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="备注" prop="description"></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="导出考勤数据"
      :visible.sync="open"
      width="460px"
      append-to-body
    >
      <el-form
        :model="exportFormData"
        label-position="top"
        ref="exportFormRef"
        size="small"
        :rules="exportFormRules"
      >
        <el-form-item label="选择区域(区分打卡模版)" prop="modelType">
          <el-select
            v-model="exportFormData.modelType"
            clearable
            style="width: 100%"
          >
            <el-option
              v-for="dict in modelTypeDate"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="选择月份" prop="strMonth">
          <el-date-picker
            v-model="exportFormData.strMonth"
            type="month"
            format="yyyy 年 MM 月"
            value-format="yyyy-MM"
            placeholder="选择月"
            style="width: 100%"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="选择考勤组(支持多选)" prop="attendanceGroupIds">
          <el-select
            v-model="exportFormData.attendanceGroupIds"
            placeholder="请选择考勤组"
            clearable
            multiple
            style="width: 100%"
          >
            <el-option
              v-for="dict in attendanceGroups"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="open = false">取 消</el-button>
        <el-button
          type="primary"
          :disabled="exportDisabled"
          @click="handleExport"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { queryList } from "@/api/oa/attendance/history";
import { queryAttendanceGroup } from "@/api/oa/attendance/group";
import { formatTime, formatDate } from "@/utils/filters";

export default {
  name: "AttendanceHistory",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      resultList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        sysUserName: undefined,
        checkInStatus: undefined,
        strDay: undefined,
        flag: "all",
      },
      exportFormData: {
        modelType: undefined,
        strMonth: undefined,
        attendanceGroupIds: [],
      },
      modelTypeDate: [
        { label: "国内", value: "guonei" },
        { label: "日本", value: "riben" },
      ],
      daka_check_in_status: [
        { label: "未打卡", value: "0" },
        { label: "上班已打卡", value: "1" },
        { label: "下班已打卡", value: "2" },
        { label: "迟到打卡", value: "3" },
        { label: "早退打卡", value: "4" },
        { label: "更新打卡", value: "5" },
      ],
      attendanceGroups: [],
      exportDisabled: false,
      exportFormRules: {
        modelType: [
          { required: true, message: "请选择区域", trigger: "change" },
        ],
        strMonth: [
          { required: true, message: "请选择月份", trigger: "change" },
        ],
        attendanceGroupIds: [
          { required: true, message: "请选择考勤组", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.queryAttendanceGroup();
  },
  methods: {
    formatDate,
    formatTime,
    handleExportSet() {
      this.open = true;
      this.reset();
    },
    queryAttendanceGroup() {
      queryAttendanceGroup().then((response) => {
        const data = response.data;
        const newData = data.map((d) => {
          return { label: d.name, value: d.id };
        });
        this.attendanceGroups = newData;
      });
    },
    /** 查询结果列表 */
    getList() {
      this.loading = true;
      queryList(this.queryParams).then((response) => {
        this.resultList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.reset();
      this.open = false;
    },
    // 表单重置
    reset() {
      this.exportFormData = {
        modelType: undefined,
        strMonth: undefined,
        attendanceGroupIds: [],
      };
      this.$nextTick(() => {
        this.$refs.exportFormRef.resetFields();
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 导出按钮操作 */
    handleExport() {
      this.$refs.exportFormRef.validate((valid) => {
        if (valid) {
          this.download(
            "dk/record/exportNew",
            {
              ...this.exportFormData,
            },
            `考勤_${new Date().toLocaleString()}.xls`
          );
          this.open = false;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
