<template>
  <div>
    <div class="titleBox">
      <div class="title">不巡防申请</div>
      <div class="btnBox">
        <el-button type="primary" @click="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="申请部门">
          <MOSTY.Department
            width="180px"
            clearable
            filterable
            v-model="listQuery.sqbmid"
          />
        </el-form-item>
        <el-form-item label="申请人">
          <el-input
            v-model="listQuery.sqrXm"
            placeholder="请填写申请人"
          ></el-input>
        </el-form-item>
        <el-form-item label="不巡日期范围">
          <el-date-picker
            v-model="QzrqS"
            style="width: 300px"
            type="daterange"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="upQzrqs"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="审核状态">
          <el-select v-model="listQuery.shzt" placeholder="请选择审核状态" style="width:180px">
            <el-option v-for="dict in D_BZ_BXFSHZT" :key="dict.value+'shzt'" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </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
        row-key="id"
        style="width: 100%"
        :key="keyCount"
        :height="tableHeight"
        v-loading="loadingtable"
        element-loading-background="rgba(0,0,0,0.3)"
        element-loading-text="数据加载中。。"
      >
        <el-table-column
          type="index"
          show-overflow-tooltip
          align="center"
          width="60px"
          label="序号"
        >
        </el-table-column>
        <el-table-column prop="bxfsj" sortable align="center" label="不巡日期">
        </el-table-column>
        <el-table-column
          prop="bxfyy"
          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="sqrXm"
          show-overflow-tooltip
          align="center"
          label="申请人"
        >
        </el-table-column>
        <el-table-column
          prop="xtCjsj"
          show-overflow-tooltip
          align="center"
          sortable
          label="申请时间"
        >
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          align="center"
          label="审批状态"
          sortable
        >
          <template #default="{ row }">
            <dict-tag :options="D_BZ_BXFSHZT" :value="row.shzt" :tag="false" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="left" fixed="right" width="200px">
          <template #default="{ row }">
            <el-button @click="info(row)" size="small">详情</el-button>
            <el-button v-if="row.shzt == '0'" @click="update(row)" size="small"
              >修改</el-button
            >
            <el-button
              v-if="row.shzt != '1'"
              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
            v-if="!infoActive"
            type="primary"
            size="small"
            @click="submit"
            :loading="btnLoading"
            >保存</el-button
          >
          <el-button size="small" @click="close">关闭</el-button>
        </div>
      </div>
      <el-form
        ref="elform"
        :model="form"
        :rules="rules"
        :inline="true"
        label-position="top"
      >
        <el-form-item prop="sqbmdm" label="申请部门">
          <MOSTY.Department
            :placeholder="form.sqbm"
            width="200px"
            clearable
            filterable
            v-model="form.sqbmdm"
          />
        
        </el-form-item>
        <el-form-item prop="sqrXm" label="申请人">
          <el-input
            @click="chooseUserVisible = true"
            placeholder="请填写申请人"
            clearable
            v-model="form.sqrXm"
          ></el-input>
        </el-form-item>
        <el-form-item prop="bxfsj" label="不巡防日期">
          <el-date-picker
            style="width: 100%"
            v-model="form.bxfsj"
            placeholder="请选择不巡防日期"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item style="width: 73%" prop="bxfyy" label="不巡原因">
          <el-input
            v-model="form.bxfyy"
            placeholder="请填写不巡原因"
            show-word-limit
            type="textarea"
          />
        </el-form-item>
        <el-form-item label="备注" style="width: 73%">
          <el-input
            v-model="form.bz"
            placeholder="请输入关键字"
            show-word-limit
            type="textarea"
          />
        </el-form-item>
      </el-form>
    </div>
    <ChooseUser
      v-model="chooseUserVisible"
      @choosedUsers="hanlderChoose"
    ></ChooseUser>
  </div>
</template>

<script setup>
import ChooseUser from "@/components/MyComponents/ChooseUser";
import { getItem } from "@/utils/storage";
import { dateFormat } from "@/utils/auth.js";
import * as MOSTY from "@/components/MyComponents/index";
import {
  getBxfsq,
  addBxfsq,
  deleteBxfsq,
  getBxfsqInfo,
  updateBxfsq
} from "@/api/service/application.js";
import { selectDeptPage } from "@/api/user-manage";
import { ref, reactive, onMounted, onUnmounted, getCurrentInstance } from "vue";
const { proxy } = getCurrentInstance();
const chackipt = ref(true);
const { D_BZ_BXFSHZT } = proxy.$dict("D_BZ_BXFSHZT");
const QzrqS = ref([]);
const searchBox = ref(null); // 搜索盒子
const depList = ref([]); //部门数据
const tableHeight = ref(); // 表格高度
const tableData = ref([]); //表格数据
const btnLoading = ref(false); //按钮截流
const cascader = ref(null);
const keyCount = ref(0); //tabel组件刷新值
const title = ref("新增不巡防申请");
const loadingtable = ref(false); //表格加载
const elform = ref(null);
const infoActive = ref(true);
const total = ref(0);
const checkAdd = ref(false);
const dialogFormVisible = ref(false); //弹窗
const chooseUserVisible = ref(false);
//搜索数据
const listQuery = ref({
  pageCurrent: 1,
  pageSize: 20
});
//级联选择器配置
const props = {
  expandTrigger: "children",
  children: "childDeptList",
  label: "orgName",
  value: "id",
  checkStrictly: true,
  emitPath: false
};
//查询条件选择部门
function handleChange(e) {
  let data = cascader.value.getCheckedNodes();
  form.value.sqbm = data[0].text;
  const ids = JSON.parse(JSON.stringify(e));
  form.value.sqbmdm = ids;
}
//表单数据
const form = ref({});
//表单验证
const rules = reactive({
  sqbmdm: [
    {
      required: true,
      message: "请选择申请部门",
      trigger: "change"
    }
  ],
  sqrXm: [
    {
      required: true,
      message: "请选择申请人",
      trigger: "change"
    }
  ],
  bxfsj: [
    {
      required: true,
      message: "请选择不巡防日期",
      trigger: "change"
    }
  ],
  bxfyy: [
    {
      required: true,
      message: "请填写不巡防原因",
      trigger: "change"
    }
  ]
});

//搜索
function handleFilter() {
  getListData();
}
//重置
function reset() {
  // let dept = getItem("deptId");
  QzrqS.value = [];
  listQuery.value = {
    pageCurrent: 1,
    pageSize: 20
    // sqbmdm: dept[0].deptId
  };
  getListData();
}
//新增表单重置
function resetForm() {
  form.value = {};
}
function upQzrqs() {
  if (QzrqS.value.length == 2) {
    listQuery.value.kssj = QzrqS.value[0];
    listQuery.value.jssj = QzrqS.value[1];
  }
}
//新增
function add() {
  infoActive.value = false;
  resetForm();
  let dept = getItem("deptId");
  let user = getItem("USERNAME");
  form.value.sqbmdm = dept[dept.length - 1].deptId;
  form.value.sqbm = dept[dept.length - 1].deptName;
  form.value.sqrXm = getItem("USERNAME");
  form.value.sqrId = getItem("USERID");
  chackipt.value = true;
  title.value = "新增不巡防申请";
  checkAdd.value = true;
  dialogFormVisible.value = true;
}
function focusF() {
  chackipt.value = false;
}
//修改
function update(row) {
  infoActive.value = false;
  chackipt.value = true;
  resetForm();
  getBxfsqInfo(row.id).then((res) => {
    form.value = res;
    checkAdd.value = false;
    title.value = "不巡防申请修改";
    dialogFormVisible.value = true;
  });
}
//详情
function info(row) {
  infoActive.value = true;
  chackipt.value = true;
  resetForm();
  getBxfsqInfo(row.id).then((res) => {
    form.value = res;
    checkAdd.value = false;
    title.value = "不巡防申请详情";
    dialogFormVisible.value = true;
  });
}
//注销
function delDictItem(row) {
  proxy
    .$confirm("确定删除该数据？", "警告", { type: "warning" })
    .then(() => {
      deleteBxfsq(row.id).then(() => {
        proxy.$message({
          type: "success",
          message: "删除成功"
        });
        getListData();
      });
    })
    .catch(() => {
      props.$message.info("已取消");
    });
}
//获取列表数据
function getListData() {
  loadingtable.value = true;
  getBxfsq(listQuery.value).then((res) => {
    loadingtable.value = false;
    tableData.value = res.records;
    total.value = res.total;
  }).catch(()=>{
    loadingtable.value = false;
  });;
}
//关闭弹窗
function close() {
  dialogFormVisible.value = false;
}
//选择用户
const hanlderChoose = (users) => {
  const user = users[0];
  form.value.sqrXm = user.userName;
  form.value.sqrId = user.id;
};
//提交
function submit() {
  elform.value.validate((valid) => {
    if (valid) {
      btnLoading.value = true;
      setTimeout(() => {
        btnLoading.value = false;
      }, 1500);
      form.value.sqbmid = form.value.sqbmdm
      if (checkAdd.value) {
        addBxfsq(form.value).then((res) => {
          proxy.$message({
            type: "success",
            message: "新增成功"
          });
          dialogFormVisible.value = false;
          getListData();
        });
      } else {
        updateBxfsq(form.value).then((res) => {
          proxy.$message({
            type: "success",
            message: "修改成功"
          });
          dialogFormVisible.value = false;
          getListData();
        });
      }
    }
  });
}
/**
 * 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;
};
onMounted(() => {
  getListData();
  selectDeptPage({}).then((res) => {
    depList.value = res.records;
  });
  proxy.mittBus.on("mittFn", (data) => {
    keyCount.value = data;
  });
  tabHeightFn();
  window.onresize = function () {
    tabHeightFn();
  };
});
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>
