<template>
  <div class="app-container">
    <div class="container">
      <el-form :model="queryParams"
        ref="queryForm"
        inline
        v-show="showSearch">
        <el-row>
          <el-form-item label="规则创建人"
            prop="ruleNum">
            <el-input v-model="queryParams.createBy"
              placeholder="查询规则创建人"
              clearable
              @input="handleQuery"
              prefix-icon="Search" />
          </el-form-item>
          <el-form-item label="规则名称"
            prop="ruleName">
            <el-input v-model="queryParams.ruleName"
              placeholder="查询规则名称"
              clearable
              @input="handleQuery"
              prefix-icon="Search" />
          </el-form-item>
        </el-row>
      </el-form>
      <el-row :gutter="10"
        class="mb8">
        <el-col :span="1.5">
          <el-button type="primary"
            plain
            icon="Plus"
            @click="handleAdd">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate">修改</el-button>
        </el-col>
        <!-- <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col> -->
        <el-col :span="1.5">
          <el-button type="warning"
            plain
            icon="Download"
            @click="handleExport">导出</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch"
          @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading"
        :data="ruleList"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection"
          width="25"
          align="center" />
        <!-- <el-table-column label="规则ID" align="center" prop="ruleId" /> -->
        <el-table-column label="规则编号"
          align="center"
          prop="ruleNum" />
        <el-table-column label="规则名称"
          align="center"
          prop="ruleName" />
        <el-table-column label="上班开始时间"
          align="center"
          prop="firBegintime"
          width="120">
        </el-table-column>
        <el-table-column label="上班结束时间"
          align="center"
          prop="firEndtime"
          width="120">
        </el-table-column>
        <el-table-column label="午班开始时间"
          align="center"
          prop="secBegintime"
          width="120">
        </el-table-column>
        <el-table-column label="午班结束时间"
          align="center"
          prop="secEndtime"
          width="120">
        </el-table-column>
        <el-table-column label="下班开始时间"
          align="center"
          prop="thiBegintime"
          width="120">
        </el-table-column>
        <el-table-column label="下班结束时间"
          align="center"
          prop="thiEndtime"
          width="120">
        </el-table-column>
        <el-table-column label="创建人"
          align="center"
          prop="createBy">
        </el-table-column>
        <el-table-column label="备注"
          prop="remark"
          width="200px" />
        <el-table-column label="操作"
          align="center"
          class-name="small-padding fixed-width">
          <template v-slot="scope">
            <el-button type="primary"
              icon="Edit"
              link
              @click="handleUpdate(scope.row)">修改</el-button>
            <!-- <el-button type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</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"
        v-model="open"
        append-to-body
        draggable overflow>
        <el-form class="form"
          ref="addForm"
          :model="form"
          :rules="rules"
          label-position="right"
          label-width="140px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="规则编号"
                prop="ruleNum">
                <el-input v-model="form.ruleNum"
                  placeholder="请输入规则编号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="规则名称"
                prop="ruleName">
                <el-input v-model="form.ruleName"
                  placeholder="请输入规则名称" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="上班打卡开始时间"
                prop="firBegintime">
                <el-time-select clearable
                  v-model="form.firBegintime"
                  start="00:00"
                  step="00:30"
                  end="24:00"
                  :max-time="form.firEndtime"
                  placeholder="请选择上班打卡开始时间">
                </el-time-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="上班打卡结束时间"
                prop="firEndtime">
                <el-time-select clearable
                  v-model="form.firEndtime"
                  :start="form.firBegintime"
                  step='00:30'
                  end='24:00'
                  :minTime="form.firBegintime"
                  :max-time="form.secBegintime"
                  placeholder="请选择上班打卡结束时间">
                </el-time-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="午班打卡开始时间"
                prop="secBegintime">
                <el-time-select clearable
                  v-model="form.secBegintime"
                  :start="form.firEndtime"
                  step='00:30'
                  end='24:00'
                  :minTime="form.firEndtime"
                  :maxTime="form.secEndtime"
                  placeholder="请选择午班打卡开始时间">
                </el-time-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="午班打卡结束时间"
                prop="secEndtime">
                <el-time-select clearable
                  v-model="form.secEndtime"
                  :start="form.secBegintime"
                  step='00:30'
                  end='24:00'
                  :minTime="form.secBegintime"
                  :maxTime="form.thiBegintime"
                  placeholder="请选择午班打卡结束时间">
                </el-time-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="下班打卡开始时间"
                prop="thiBegintime">
                <el-time-select clearable
                  v-model="form.thiBegintime"
                  :start="form.secEndtime"
                  step='00:30'
                  end='24:00'
                  :minTime="form.secEndtime"
                  :maxTime="form.thiEndtime"
                  placeholder="请选择下班打卡开始时间">
                </el-time-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="下班打卡结束时间"
                prop="thiEndtime">
                <el-time-select clearable
                  v-model="form.thiEndtime"
                  :start="form.thiBegintime"
                  step='00:30'
                  end='24:00'
                  :minTime="form.thiBegintime"
                  placeholder="请选择下班打卡结束时间">
                </el-time-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="备注"
                prop="remark">
                <el-input v-model="form.remark"
                  type="textarea"
                  placeholder="请输入内容" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <template #footer
          class="dialog-footer">
          <el-button type="primary"
            @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script setup>
import { listRule, getRule, delRule, addRule, updateRule } from "@/api";
import { timeAddZero, randomCode, debounce } from '@/utils'

const { proxy } = getCurrentInstance();

// 遮罩层
const loading = ref(true)
// 选中数组
const ids = ref([])
// 非单个禁用
const single = ref(true)
// 非多个禁用
const multiple = ref(true)
// 显示搜索条件
const showSearch = ref(true)
// 总条数
const total = ref(0)
// 考勤规则表格数据
const ruleList = ref([])
// 弹出层标题
const title = ref("")
// 是否显示弹出层
const open = ref(false)
// 表单数据
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    ruleNum: null,
    ruleName: null,
  },
  rules: {
    ruleName: [
      { required: true, message: "规则名称不能为空" }
    ],
    firBegintime: [
      { required: true, message: "上班打卡开始时间不能为空", },
      // { validator: checkfirBeginTime }
    ],
    firEndtime: [
      { required: true, message: "上班打卡结束时间不能为空", },
      // { validator: checkfirEndTime }
    ],
    secBegintime: [
      // { validator: checksecBeginTime }
    ],
    secEndtime: [
      // { validator: checksecEndTime }
    ],
    thiBegintime: [
      { required: true, message: "下班打卡开始时间不能为空" },
      // { validator: checkthiBeginTime }
    ],
    thiEndtime: [
      { required: true, message: "下班打卡结束时间不能为空" },
      // { validator: checkthiEndTime }
    ]
  }
})
const { form, queryParams, rules } = toRefs(data)

const getList = () => {
  loading.value = true;
  listRule(queryParams).then(response => {
    response.data.rows.forEach(obj => {
      obj.thiEndtime = obj.thiEndtime
    })
    ruleList.value = response.data.rows
    total.value = response.data.total
    loading.value = false
  })
}
getList()


// 表单重置
const reset = () => {
  form.value = {
    ruleNum: randomCode(),
    ruleName: undefined,
    firBegintime: undefined,
    firEndtime: undefined,
    secBegintime: undefined,
    secEndtime: undefined,
    thiBegintime: undefined,
    thiEndtime: undefined,
    remark: undefined
  }
  proxy.resetForm('addForm');
}

// 取消按钮
const cancel = () => {
  open.value = false
  reset()
}


/** 搜索按钮操作 */
const handleQuery = debounce(function () {
  queryParams.value.pageNum = 1;
  getList();
})
/** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("addForm");
  handleQuery();
}
// 多选框选中数据
const handleSelectionChange = (selection) => {
  ids.value = selection.map(item => item.ruleId)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}
/** 新增按钮操作 */
const handleAdd = () => {
  reset()
  open.value = true
  title.value = "添加考勤规则"
}
/** 修改按钮操作 */
const handleUpdate = (row) => {
  reset()
  const ruleId = row.ruleId || ids.value
  getRule(ruleId).then(response => {
    response.data.thiEndtime = response.data.thiEndtime
    open.value = true
    nextTick(() => {
      form.value = response.data
      title.value = "修改考勤规则"
    })
  });
}
/** 提交按钮 */
const submitForm = () => {
  proxy.$refs["addForm"].validate(valid => {
    if (valid) {
      // 时间尾数补 0
      Object.keys(form.value).forEach(key => {
        if (key.includes('time')) {
          form.value[key] = timeAddZero(form.value[key])
        }
      })
      if (form.value.ruleId != null) {
        updateRule(form.value).then(response => {
          reset()
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addRule(form.value).then(response => {
          reset()
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
const handleDelete = (row) => {
  const ruleIds = row.ruleId || ids.value;
  proxy.$modal.confirm('是否确认删除考勤规则编号为"' + ruleIds + '"的数据项？').then(function () {
    return delRule(ruleIds);
  }).then(() => {
    getList();
  });
}
/** 导出按钮操作 */
const handleExport = () => {
  proxy.$refs["queryForm"].validate(valid => {
    if (valid) {
      proxy.$download("/rule/export", queryParams.value);
    }
  });
}
</script>

<style scoped
  lang="scss">
  ::v-deep(.el-date-editor) {
    width: 100% !important;
  }

  ::v-deep(.el-select){
    width: 100%;
  }
</style>