<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="150px">
      <el-form-item label="日常打卡奖励：" prop="dailyClockCredit">
        <el-input v-model="form.dailyClockCredit" placeholder="请输入日常打卡奖励" style="width: 25%;"/>
      </el-form-item>
      <el-form-item
        label="额外打卡奖励"
        prop="isExtraClock"
        label-width="120px"
      >
        <el-switch v-model="form.isExtraClock"> </el-switch>
      </el-form-item>
      <el-form-item label="额外打卡奖励积分：" prop="extraClockCredit" v-if="form.isExtraClock">
        <el-input v-model="form.extraClockCredit" placeholder="请输入额外打卡奖励积分" style="width: 25%;"/>
      </el-form-item>
      <el-form-item label="额外打卡时间段：" prop="timeRange" label-width="150px"  v-if="form.isExtraClock">
        <el-date-picker
          v-model="form.timeRange"
          type="datetimerange"
          value-format="yyyy-MM-dd HH:mm"
          format="yyyy-MM-dd HH:mm"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
    </el-form>
    <div style="margin-left: 70%" >
      <el-button type="primary" @click="submitForm">提 交</el-button>
    </div>
  </div>
</template>

<script>
import { listClock, getClock, delClock, addClock, updateClock } from "@/api/system/clockIntegral";

export default {
  name: "Clock",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 打卡奖励积分表格数据
      clockList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dailyClockCredit: null,
        isExtraClock: null,
        extraClockCredit: null,
        startTime: null,
        endTime: null,
        status: null,
        timeRange:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dailyClockCredit: [
          { required: true, message: "日常打卡奖励不能为空", trigger: "blur" }
        ],
        extraClockCredit: [
          { required: true, message: "额外打卡奖励积分不能为空", trigger: "blur" }
        ],
        timeRange: [
          { required: true, message: "额外打卡时间段不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询打卡奖励积分列表 */
    getList() {
      this.loading = true;
      listClock(this.queryParams).then(response => {
        this.form = response.rows.length >0 ? response.rows[0]: {};
        if (this.form.isExtraClock == 1) {
          this.form.isExtraClock = true;
        } else {
          this.form.isExtraClock = false;
        }
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        dailyClockCredit: null,
        isExtraClock: null,
        extraClockCredit: null,
        startTime: null,
        endTime: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加打卡奖励积分";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getClock(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改打卡奖励积分";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          const regex = /[^0-9]/g;
          if (regex.test(this.form.dailyClockCredit)) {
            this.$modal.msgError("日常打卡奖励积分仅能输入数字");
            return;
          }
          if (this.form.isExtraClock) {
            if (regex.test(this.form.extraClockCredit)) {
              this.$modal.msgError("额外打卡奖励积分仅能输入数字");
              return;
            }
            if (this.form.extraClockCredit == null && this.form.extraClockCredit == undefined) {
              this.$modal.msgError("积分不能为空");
              return;
            }
            if (this.form.timeRange && this.form.timeRange.length > 0) {
              this.form.startTime = this.form.timeRange[0]
              this.form.endTime = this.form.timeRange[1]
            } else {
              this.$modal.msgError("打卡时间不能为空");
              return;
            }
            this.form.isExtraClock = 1;
          } else {
            this.form.isExtraClock = 0;
          }
          if (this.form.id != null) {
            updateClock(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClock(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除打卡奖励积分编号为"' + ids + '"的数据项？').then(function() {
        return delClock(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/clock/export', {
        ...this.queryParams
      }, `clock_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
