<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="用户编号" prop="userNo">
        <el-input
          v-model="queryParams.userNo"
          placeholder="请输入用户编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="当天时间" prop="todayTime">
        <el-input
          v-model="queryParams.todayTime"
          placeholder="请输入当天时间"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="星期" prop="week">
        <el-select v-model="queryParams.week" placeholder="请选择星期" clearable size="small">
          <el-option
            v-for="dict in dict.type.week_num"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="班次类型" prop="shiftId">-->
<!--        <el-select v-model="queryParams.shiftId" placeholder="请选择班次类型" clearable size="small">-->
<!--          <el-option label="请选择字典生成" value="" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="上班打卡时间" prop="dutyClockTime">-->
<!--        <el-date-picker clearable size="small"-->
<!--          v-model="queryParams.dutyClockTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="选择上班打卡时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="下班打卡时间" prop="offDutyClockTime">-->
<!--        <el-date-picker clearable size="small"-->
<!--          v-model="queryParams.offDutyClockTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="选择下班打卡时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item label="考勤类型" prop="clockingInType">
        <el-select v-model="queryParams.clockingInType" placeholder="请选择缺勤类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.clocking_in_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="迟到时长" prop="belateTimeNum">-->
<!--        <el-input-->
<!--          v-model="queryParams.belateTimeNum"-->
<!--          placeholder="请输入迟到时长"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @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="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['clockingin:clockingin:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['clockingin:clockingin:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['clockingin:clockingin:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['clockingin:clockingin:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="clockinginList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户编号" align="center" prop="userNo" />
      <el-table-column label="用户名称" align="center" prop="userName" />
      <el-table-column label="当天时间" align="center" prop="todayTime" />
      <el-table-column label="星期" align="center" prop="week">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.week_num" :value="scope.row.week"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="班次类型" align="center" prop="shiftId" />-->
      <el-table-column label="上班打卡时间" align="center" prop="dutyClockTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dutyClockTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下班打卡时间" align="center" prop="offDutyClockTime" width="180">
        <template slot-scope="scope">
          <span v-if="scope.row.offDutyClockTime !== undefined">{{ parseTime(scope.row.offDutyClockTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          <span v-else>暂未打卡</span>
        </template>
      </el-table-column>
      <el-table-column label="考勤类型" align="center" prop="clockingInType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.clocking_in_type" :value="scope.row.clockingInType"/>
        </template>
      </el-table-column>
      <el-table-column label="迟到时长" align="center" prop="belateTimeNum" />
      <el-table-column label="说明" align="center" prop="remark" />
      <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-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['clockingin:clockingin:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['clockingin:clockingin:remove']"
          >删除</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="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" >
        <el-form-item label="用户编号" prop="userNo">
          <el-input v-model="form.userNo" placeholder="请输入用户编号" />
        </el-form-item>
        <el-form-item label="用户名称" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户名称" />
        </el-form-item>
        <el-form-item label="当天时间" prop="todayTime">
          <el-input v-model="form.todayTime" placeholder="请输入当天时间" />
        </el-form-item>
        <el-form-item label="星期" prop="week">
          <el-select v-model="form.week" placeholder="请选择星期">
            <el-option
              v-for="dict in dict.type.week_num"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="班次类型" prop="shiftId">-->
<!--          <el-select v-model="form.shiftId" placeholder="请选择班次类型">-->
<!--            <el-option label="请选择字典生成" value="" />-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item label="上班打卡时间" prop="dutyClockTime">
          <el-date-picker clearable size="small"
            v-model="form.dutyClockTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择上班打卡时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="下班打卡时间" prop="offDutyClockTime">
          <el-date-picker clearable size="small"
            v-model="form.offDutyClockTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择下班打卡时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="缺勤类型" prop="clockingInType">
          <el-select v-model="form.clockingInType" placeholder="请选择缺勤类型">
            <el-option
              v-for="dict in dict.type.clocking_in_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="迟到时长" prop="belateTimeNum">
          <el-input v-model="form.belateTimeNum" placeholder="请输入迟到时长" />
        </el-form-item>
        <el-form-item label="说明" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入说明" />
        </el-form-item>
        <el-form-item label="是否删除" prop="isDelete">
          <el-select v-model="form.isDelete" placeholder="请选择是否删除">
            <el-option
              v-for="dict in dict.type.is_delete"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-divider content-position="center">考勤班次信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddHsPnsShift">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteHsPnsShift">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="hsPnsShiftList" :row-class-name="rowHsPnsShiftIndex" @selection-change="handleHsPnsShiftSelectionChange" ref="hsPnsShift">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="班次名称" prop="shiftName">
            <template slot-scope="scope">
              <el-input v-model="scope.row.shiftName" placeholder="请输入班次名称" />
            </template>
          </el-table-column>
          <el-table-column label="班次类型" prop="shiftType">
            <template slot-scope="scope">
              <el-input v-model="scope.row.shiftType" placeholder="请输入班次类型" />
            </template>
          </el-table-column>
          <el-table-column label="上班时间" prop="dutyTime">
            <template slot-scope="scope">
              <el-input v-model="scope.row.dutyTime" placeholder="请输入上班时间" />
            </template>
          </el-table-column>
          <el-table-column label="下班时间" prop="offDutyTime">
            <template slot-scope="scope">
              <el-input v-model="scope.row.offDutyTime" placeholder="请输入下班时间" />
            </template>
          </el-table-column>
          <el-table-column label="备注(说明)" prop="remark">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注(说明)" />
            </template>
          </el-table-column>
          <el-table-column label="是否删除" prop="isDelete">
            <template slot-scope="scope">
              <el-input v-model="scope.row.isDelete" placeholder="请输入是否删除" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listClockingin, getClockingin, delClockingin, addClockingin, updateClockingin, exportClockingin } from "@/api/clockingin/clockingin";

export default {
  name: "Clockingin",
  dicts: ['week_num', 'clocking_in_type', 'is_delete'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedHsPnsShift: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 每日考勤信息表格数据
      clockinginList: [],
      // 考勤班次表格数据
      hsPnsShiftList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userNo: null,
        userName: null,
        todayTime: null,
        week: null,
        shiftId: null,
        dutyClockTime: null,
        offDutyClockTime: null,
        clockingInType: null,
        belateTimeNum: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询每日考勤信息列表 */
    getList() {
      this.loading = true;
      listClockingin(this.queryParams).then(response => {
        this.clockinginList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        clockingInId: null,
        userNo: null,
        userName: null,
        todayTime: null,
        week: null,
        shiftId: null,
        dutyClockTime: null,
        offDutyClockTime: null,
        clockingInType: null,
        belateTimeNum: null,
        remark: null,
        isDelete: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.hsPnsShiftList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.clockingInId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加每日考勤信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const clockingInId = row.clockingInId || this.ids
      getClockingin(clockingInId).then(response => {
        this.form = response.data;
        this.hsPnsShiftList = response.data.hsPnsShiftList;
        this.open = true;
        this.title = "修改每日考勤信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.hsPnsShiftList = this.hsPnsShiftList;
          if (this.form.clockingInId != null) {
            updateClockingin(this.form).then(() => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClockingin(this.form).then(() => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const clockingInIds = row.clockingInId || this.ids;
      this.$modal.confirm('是否确认删除每日考勤信息编号为"' + clockingInIds + '"的数据项？').then(function() {
        return delClockingin(clockingInIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 考勤班次序号 */
    rowHsPnsShiftIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 考勤班次添加按钮操作 */
    handleAddHsPnsShift() {
      let obj = {};
      obj.shiftName = "";
      obj.shiftType = "";
      obj.dutyTime = "";
      obj.offDutyTime = "";
      obj.remark = "";
      obj.isDelete = "";
      this.hsPnsShiftList.push(obj);
    },
    /** 考勤班次删除按钮操作 */
    handleDeleteHsPnsShift() {
      if (this.checkedHsPnsShift.length == 0) {
        this.$modal.msgError("请先选择要删除的考勤班次数据");
      } else {
        const hsPnsShiftList = this.hsPnsShiftList;
        const checkedHsPnsShift = this.checkedHsPnsShift;
        this.hsPnsShiftList = hsPnsShiftList.filter(function(item) {
          return checkedHsPnsShift.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleHsPnsShiftSelectionChange(selection) {
      this.checkedHsPnsShift = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有每日考勤信息数据项？').then(() => {
        this.exportLoading = true;
        return exportClockingin(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
