<template>
  <div>
    <div class="titleBox">
      <div class="title">人员请休假</div>
      <div class="btnBox">
        <el-button type="primary" @click="addEdit('add', '')">
          <el-icon style="vertical-align: middle"><CirclePlus /> </el-icon>
          <span style="vertical-align: middle">新增</span>
        </el-button>
      </div>
    </div>
    <div class="searchBox" ref="searchBox">
      <el-form :model="listQuery" :inline="true">
        <el-form-item label="起止日期">
          <el-date-picker
            v-model="Qzrq"
            type="daterange"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="upQzrq"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="请假人部门">
          <MOSTY.Department
            width="180px"
            clearable
            filterable
            v-model="listQuery.sqbmid"
          />
        </el-form-item>
        <el-form-item label="请假人类型">
          <el-select
            style="width: 180px"
            v-model="listQuery.sqrlx"
            placeholder="请选择请假人类型"
          >
            <el-option
              v-for="dict in D_BZ_SQRLX"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="请假人">
          <el-input
            style="width: 180px"
            placeholder="请填写请假人"
            v-model="listQuery.sqrXm"
          >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleFilter"> 查询 </el-button>
          <el-button @click="reset"> 重置 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="tabBox">
      <el-table
        :data="tableData"
        border
        ref="dataTreeList"
        row-key="id"
        :tree-props="{ children: 'itemList', hasChildren: true }"
        style="width: 100%"
        :key="keyCount"
        :height="tableHeight"
      >
        <el-table-column
          type="index"
          show-overflow-tooltip
          align="center"
          width="60px"
          label="序号"
        >
        </el-table-column>
        <el-table-column
          prop="sqrlx"
          show-overflow-tooltip
          align="center"
          width="100"
          label="请假人类型"
        >
          <template #default="{ row }">
            <dict-tag :options="D_BZ_SQRLX" :value="row.sqrlx" :tag="false" />
          </template>
        </el-table-column>
        <el-table-column
          prop="sqrXm"
          show-overflow-tooltip
          align="center"
          label="请假人"
        >
        </el-table-column>
        <el-table-column
          prop="sqbm"
          show-overflow-tooltip
          align="center"
          label="请假人部门"
        >
        </el-table-column>
        <el-table-column
          prop="kssj"
          show-overflow-tooltip
          align="center"
          label="起止日期"
          width="320"
        >
          <template #default="{ row }">
            <div>
              {{ row.kssj.substring(0, 10) + "—" + row.jssj.substring(0, 10) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="qjlx"
          show-overflow-tooltip
          width="100"
          align="center"
          label="请假类型"
        >
          <template #default="{ row }">
            <dict-tag :options="D_BZ_QXJLX" :value="row.qjlx" :tag="false" />
          </template>
        </el-table-column>
        <el-table-column
          prop="xtCjsj"
          show-overflow-tooltip
          align="center"
          width="200"
          label="申请时间"
        >
        </el-table-column>
        <el-table-column
          prop="xtCjr"
          show-overflow-tooltip
          align="center"
          label="录入人"
        >
        </el-table-column>
        <el-table-column
          prop="xtCjbmmc"
          show-overflow-tooltip
          align="center"
          label="录入部门"
        >
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          fixed="right"
          width="200px"
        >
          <template #default="{ row }">
            <el-button @click="addEdit('edit', row.id)" size="small"
              >修改</el-button
            >
            <el-button type="danger" size="small" @click="delDictItem(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="fenye" :style="{ top: tableHeight + 'px' }">
        <el-pagination
          class="pagination"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.pageCurrent"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="listQuery.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <div v-if="dialogFormVisible" class="dialog">
      <div class="head_box">
        <span class="title">{{ title }}</span>
        <div>
          <el-button
            type="primary"
            size="small"
            @click="submit"
            :loading="btnLoading"
            >保存</el-button
          >
          <el-button size="small" @click="closeDialog">关闭</el-button>
        </div>
      </div>
      <el-form
        ref="elform"
        :model="form"
        :rules="rules"
        :inline="true"
        label-position="top"
      >
        <el-form-item style="width: 30%" prop="sqrlx" label="申请人类型">
          <el-select placeholder="请选择申请人类型" style="width: 100%" v-model="form.sqrlx">
            <el-option
              v-for="dict in D_BZ_SQRLX"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="width: 30%" prop="sqbmdm" label="申请人部门">
          <MOSTY.Department
            width="180px"
            clearable
            filterable
            v-model="form.sqbmdm"
          />
        </el-form-item>
        <el-form-item style="width: 30%" prop="sqrXm" label="申请人">
          <el-input
            @click="chooseUserVisible = true"
            v-model="form.sqrXm"
            placeholder="请选择申请人"
            style="width: 100%"
            clearable
          />
        </el-form-item>

        <el-form-item style="width: 30%" prop="sqrSfzh" label="身份证号">
          <el-input
            v-model="form.sqrSfzh"
            placeholder="请输入身份证号"
          ></el-input>
        </el-form-item>
        <el-form-item style="width: 30%" prop="qjlx" label="请假类型">
          <el-select
            style="width: 100%"
            v-model="form.qjlx"
            filterable
            placeholder="请选择请假类型"
          >
            <el-option
              v-for="item in D_BZ_QXJLX"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item style="width: 62%" prop="QzrqForm" required label="起止日期">
          <el-date-picker
            style="width: 100%"
            v-model="form.QzrqForm"
            type="daterange"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="upQzrqForm"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="请假原因" prop="qjyy" style="width: 94%">
          <el-input
            v-model="form.qjyy"
            placeholder="请输入请假原因"
            show-word-limit
            type="textarea"
          />
        </el-form-item>

        <el-form-item label="备注" style="width: 94%">
          <el-input
            v-model="form.bz"
            placeholder="请输入关键字"
            show-word-limit
            type="textarea"
          />
        </el-form-item>
      </el-form>
    </div>
    <ChooseUser
      v-model="chooseUserVisible"
      :PoliceType="form.sqrlx == '01' ? 'MJ' : 'FJ'"
      :Single="true"
      @choosedPolice="hanlderChoose"
    ></ChooseUser>
  </div>
</template>

<script setup>
import ChooseUser from "@/components/MyComponents/choosePolice";
import * as MOSTY from "@/components/MyComponents/index";
import { getItem } from "@/utils/storage";
import { ref, reactive, onMounted, onUnmounted, getCurrentInstance } from "vue";
import { getQwQxj, addQwQxj, deleteQwQxj, getQwQxjInfo, updateQwQxj } from "@/api/service/pleaseTake.js";
import * as rule from "@/utils/rules.js";
const { proxy } = getCurrentInstance();
const { D_BZ_QXJLX, D_BZ_SQRLX } = proxy.$dict("D_BZ_QXJLX", "D_BZ_SQRLX");
//搜索数据
const listQuery = ref({
  pageCurrent: 1,
  pageSize: 20
});
const searchBox = ref(null); // 搜索盒子
const Qzrq = ref([]); //搜索日期
const dialogFormVisible = ref(false); //弹窗
const chooseUserVisible = ref(false);//人员弹窗
const title = ref("新增人员请休假");
const tableHeight = ref(); // 表格高度
const tableData = ref([]); //表格数据
const btnLoading = ref(false); //按钮截流
const keyCount = ref(0); //tabel组件刷新值
const elform = ref(null);
const total = ref(0);
//表单数据
const form = ref({
  QzrqForm: []
});
//表单验证
const rules = ref({
  sqrXm: [{ required: true, message: "请输入申请人员", trigger: ['blur','change'] }],
  sqrlx: [{ required: true, message: "请选择申请人类型", trigger: "change" }],
  QzrqForm: [{ required: true, message: "请选择请假日期", trigger: "change" }],
  sqbmdm: [{ required: true, message: "请选择所属部门", trigger: "change" }],
  qjyy: [{ required: true, message: "请输入请假原因", trigger: "blur" }],
  qjlx: [{ required: true, message: "请输入请假类型", trigger: "change" }],
  ...rule.identityCardRule( {require: true,trigger: ['blur','change'], message: "请输入身份证号",validator: true},"sqrSfzh"), //身份证校验
});

onMounted(() => {
  getListData();
  proxy.mittBus.on("mittFn", (data) => { keyCount.value = data; });
  tabHeightFn();
  window.onresize = function () { tabHeightFn(); };
});
// 查询
function handleFilter() {
  listQuery.value.pageCurrent = 1;
  getListData();
}
//重置
function reset() {
  Qzrq.value = [];
  listQuery.value = { pageCurrent: 1, pageSize: 20 };
  getListData();
}
//获取列表数据
function getListData() {
  getQwQxj(listQuery.value).then((res) => {
    total.value = res.total;
    tableData.value = res.records;
  });
}

// 搜索切换时间
function upQzrq(val) {
  listQuery.value.kssj = val.length > 0 ? val[0] : "";
  listQuery.value.jssj = val.length > 0 ? val[1] : "";
}
//新增时间切换
function upQzrqForm(val) {
  form.value.kssj = val.length > 0 ? val[0] : "";
  form.value.jssj = val.length > 0 ? val[1] : "";
}

//选择用户
const hanlderChoose = (users) => {
  const user = users[0];
  form.value.sqrXm = user.xm;
  form.value.sqrId = user.fl === "01" ? user.ryid : user.id;
  form.value.sqbm = user.ssbm;
  form.value.sqrSfzh = user.sfzh;
  form.value.lxdh = user.lxdh;
};

//新增-编辑
async function addEdit(type, id) {
  dialogFormVisible.value = true;
  if (type == "add") {
    title.value = "新增人员请休假";
  } else {
    title.value = "人员请休假修改";
    getQwQxjInfo(id).then((res) => {
      form.value = res;
      form.value.QzrqForm = [res.kssj, res.jssj];
    });
  }
}

//注销
function delDictItem(row) {
  proxy.$confirm("确定删除该数据？", "警告", { type: "warning" }).then(() => {
    deleteQwQxj(row.id).then(() => {
      proxy.$message({ type: "success",  message: "删除成功" });
      getListData();
    });
  }).catch(() => {
    proxy.$message.info("已取消");
  });
}

//关闭弹窗
function closeDialog() {
  dialogFormVisible.value = false;
  btnLoading.value = false;
  form.value = {};
}
//提交
function submit() {
  elform.value.validate((valid) => {
    if (valid) {
      btnLoading.value = true;
      delete form.value.QzrqForm;
      if (title.value == "新增人员请休假") {
        addQwQxj(form.value).then((res) => {
          closeDialog();
          proxy.$message({ type: "success", message: "新增成功" });
          getListData();
        }).catch(() => {
          btnLoading.value = false;
        });
      } else {
        updateQwQxj(form.value).then(() => {
          closeDialog();
          proxy.$message({ type: "success", message: "修改成功" });
          getListData();
        }).catch(() => {
          btnLoading.value = false;
        });
      }
    }
  });
}
// pageSize 改变触发
const handleSizeChange = (currentSize) => {
  listQuery.value.pageSize = currentSize;
  getListData();
};

// 页码改变触发
const handleCurrentChange = (currentPage) => {
  listQuery.value.pageCurrent = currentPage;
  getListData();
};
// 表格高度计算
const tabHeightFn = () => {
  tableHeight.value = window.innerHeight - searchBox.value.offsetHeight - 240;
};

onUnmounted(() => {
  proxy.mittBus.off("mittFn");
});
</script>

<style lang="scss" scoped>
@import "~@/assets/css/layout.scss";
@import "~@/assets/css/element-plus.scss";
.min {
  position: relative;
}
.min::after {
  position: absolute;
  top: 0;
  right: 12px;
  bottom: 0;
  content: "分钟";
}
</style>
