<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>

        <!-- <el-button @click="batchDelete" :disabled="ids.length == 0" typeof="danger">
          <el-icon style="vertical-align: middle">
            <Delete />
          </el-icon>
          <span style="vertical-align: middle">批量注销</span>
        </el-button> -->
      </div>
    </div>
    <div class="searchBox" ref="searchBox">
      <el-form :model="form" :inline="true">
        <el-form-item label="姓名">
          <el-input v-model="form.xm" placeholder="姓名" clearable style="width: 100%" />
        </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" :tree-props="{ children: 'itemList', hasChildren: true }"
        style="width: 100%"  :height="tableHeight" :key="keyCount"
        v-loading="loadingTable" element-loading-background="rgba(0,0,0,0.3)" element-loading-text="数据加载中。。">
        <!-- <el-table-column type="selection" width="40" align="center" /> -->
        <el-table-column label="序号" type="index" align="center" sortable width="80" />
        <el-table-column sortable prop="xm" show-overflow-tooltip align="center" width="250px" label="姓名">
        </el-table-column>
        <el-table-column sortable prop="sjh" show-overflow-tooltip align="center" width="350px" label="手机号码">
        </el-table-column>
        <el-table-column sortable prop="sfzh" show-overflow-tooltip align="center" width="350px" label="人员身份证号">
        </el-table-column>
        <el-table-column label="人员类型" align="center">
          <template #default="{ row }">
            <span class="textBtn" v-if="row.rylx == '01'">群众</span>
            <span class="textBtn" v-if="row.rylx == '02'">党员</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="200px">
          <template #default="{ row }">
            <el-button @click="setInfo(row, 'edit')" size="small" >修改</el-button>
            <el-button @click="setInfo(row, 'detail')" size="small">详情</el-button>
            <el-button @click="delDictItem(row.id)"  type="danger" size="small">注销</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="form.current" :page-sizes="[10, 20, 50]" :page-size="form.size"
          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" v-if="!isDisabled" :loading="loading">保存</el-button>
          <el-button size="small" @click="close()">关闭</el-button>
        </div>
      </div>
      <el-form ref="felform" :rules="rules" :model="addForm" :inline="true" :disabled="isDisabled" label-position="top">
        <el-form-item label="姓名" prop="xm">
          <el-input v-model="addForm.xm" placeholder="请输入姓名" style="width: 100%" />
        </el-form-item>
        <el-form-item label="人员类型"  prop="rylx" >
          <el-select v-model="addForm.rylx" placeholder="请选择人员类型" clearable>
            <el-option label="群众" value="01"></el-option>
            <el-option label="党员" value="02"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="手机号码" prop="sjh">
          <el-input v-model="addForm.sjh" placeholder="请输入手机号码" style="width: 100%" />
        </el-form-item>
        <el-form-item label="身份证号码" prop="sfzh">
          <el-input v-model="addForm.sfzh" placeholder="请输入身份证号码" style="width: 100%" />
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, onUnmounted, getCurrentInstance } from "vue";
import { ElMessage } from "element-plus";
import { qfqzAdd, qfqzEdit,qfqzSelectPage,qfqzDelete } from '@/api/lz/backstage'
const { proxy } = getCurrentInstance();
const searchBox = ref(null); // 搜索盒子
const loadingTable = ref(true);
const tableHeight = ref(); // 表格高度
const title = ref("新增专业技能");

//查询参数
const form = ref({
  current: 1,
  size: 20,
});
//新增表单参数
const addForm = ref({});
//表单验证
const rules = reactive({
  xm: [
    {
      required: true,
      message: "请输入姓名"
    }
  ],
  rylx: [
    {
      required: true,
      message: "请选择人员类型"
    },
  ],
  sjh: [
    {
      required: true,
      message: "请输入手机号码"
    }
  ],
  sfzh: [
    {
      required: true,
      message: "请输入身份证号码"
    }
  ],
});
const tableData = ref([]); //表单数据
const total = ref(0); //总数据
const dialogFormVisible = ref(false); //是否打开新增弹窗
const felform = ref({}); //表当对象
const isDisabled = ref(false); //是否警用保存按钮
const loading = ref(false); //是否显示按钮加载
const checkAdd = ref(false);
const keyCount = ref(0); //tabel组件刷新值
onMounted(() => {
  getListData();
  tabHeightFn();
  window.onresize = function () {
    tabHeightFn();
  };
});
onUnmounted(() => {
  proxy.mittBus.off("mittFn");
});

//查看详情
function setInfo(e, type) {
  addForm.value = { ...e };
  dialogFormVisible.value = true;
  if (type == 'detail') {
    isDisabled.value = true;
    checkAdd.value =false
    title.value = "群防群治人员详情";
  } else {
    title.value = "修改群防群治人员";
    checkAdd.value =false
    isDisabled.value = false
  }

}

function add() {
  isDisabled.value = false;
  checkAdd.value = true;
  title.value = "新增群防群治人员";
  dialogFormVisible.value = true;
}
//新增提交
function submit() {
  loading.value = true;
  felform.value.validate((valid) => {
    if (valid) {
      let data = addForm.value;
      if (!checkAdd.value) {
        qfqzEdit(data).then((res) => {
          ElMessage({
            message: "修改成功",
            type: "success"
          });
          getListData();
        }).finally(() => {
          loading.value = false;
        });
      } else {
        qfqzAdd(data).then((res) => {
          ElMessage({
            message: "添加成功",
            type: "success"
          });
          getListData();
        }).finally(() => {
          loading.value = false;
        });
      }
      dialogFormVisible.value = false;
    }
  });
}
//点击查询
const handleFilter = () => {
  form.value.current = 1;
  getListData();
};
//获取数据
const getListData = () => {
  loadingTable.value = true;
  let data = {
    pageSize: form.value.size,
    pageCurrent: form.value.current,
    xm: form.value.xm,
  };
  qfqzSelectPage(data)
    .then((res) => {
      tableData.value = res.records;
      total.value = res.total;
      loadingTable.value = false;
    })
    .catch(() => {
      loadingTable.value = false;
    });
};
//修改查询条件
const delDictItem = (id) => {
  proxy
    .$confirm("确定要删除", "警告", { type: "warning" })
    .then(() => {
      qfqzDelete(id).then(() => {

        proxy.$message({ type: "success", message: "删除成功" });
        getListData();
      }) .catch(() => {console.log("报错");
       });
    })
    .catch((err) => {console.log(err);
     });
}
//更改查询条数
function handleSizeChange(e) {
  form.value.size = e;
  form.value.current = 1;
  getListData();
}
//分页查询
function handleCurrentChange(e) {
  xfllQuery.value.current = e;
}


//点击重置
const reset = () => {
  form.value = {
    current: 1,
    size: 20,
    textName: ""
  };
  getListData();
};
const close = () => {

  addForm.value = {};
  dialogFormVisible.value = false
}
// 表格高度计算
const tabHeightFn = () => {
  tableHeight.value = window.innerHeight - searchBox.value.offsetHeight - 240;
};
</script>

<style lang="scss" scoped>
@import "~@/assets/css/layout.scss";
@import "~@/assets/css/element-plus.scss";

::v-deep .el-tabs--card>.el-tabs__header {
  border-bottom: 1px solid #03438b;
}

::v-deep .el-tabs--card>.el-tabs__header .el-tabs__item.is-active {
  border-bottom-color: #03438b;
}

::v-deep .el-tabs--card>.el-tabs__header .el-tabs__nav {
  border: 1px solid #03438b;
  border-bottom: none;
  border-radius: 4px 4px 0 0;
  box-sizing: border-box;
}

::v-deep .el-tabs--card>.el-tabs__header .el-tabs__item {
  border-left: 1px solid #234c9e;
}

.textBtn {
  cursor: pointer;
}

.textBtn:hover {
  color: #2a5dc2;
}
</style>
