<template>
  <div>
    <!-- 查询 -->
    <div class='tabox'>
      <div class="headBox3">
        <el-input placeholder="请输入姓名进行查询" v-model="pageData.listQuery.name" clearable style='width:400px'
          v-if="props.showCount"></el-input>
        <el-button type="primary" @click="Searchdata" v-if="props.showCount">
          <el-icon>
            <Search />
          </el-icon>
          <span>查询</span>
        </el-button>
        <el-button type="primary" @click="addUserHander" v-if='props.obj.saveBtnShow'>
          <el-icon>
            <CirclePlus />
          </el-icon>
          <span>新增</span>
        </el-button>
      </div>
      <MOSTY.Table :table-columns="pageData.tableColumns" :tableData="pageData.tableData" :tableloading="tableloading"
        :tableHeight="'40vh'">
        <template #sex="{ row }">
          <dict-tag v-if="row.sex" :options="D_BZ_XB" :value="row.sex" :tag="false" />
        </template>
        <template #phone="{ row }">
          <span>{{ maskPhone(row.phone) }}</span>
        </template>
        <template #actions="{ row, $index }">

          <span class="operedit" @click="handlexg(row, $index)" v-if='props.obj.saveBtnShow'>
            <el-icon>
              <EditPen />
            </el-icon>修改</span>
          <el-popconfirm v-if='props.obj.saveBtnShow' confirm-button-text="是" cancel-button-text="否" icon-color="red"
            title="确定要删除?" @confirm="handledelete(row, $index)">
            <template #reference>
              <span class="operdel" size="small">
                <el-icon>
                  <Delete />
                </el-icon>
                删除</span>
            </template>
          </el-popconfirm>
          <span class="operesee" @click="handleSee(row)" v-if="props.showCount">
            <el-icon>
              <Compass />
            </el-icon>查看</span>
        </template>
        <template #fenye v-if="props.showCount">
          <div class="fenye" :style="{ top: tableHeight + 'px' }">
            <el-pagination class="pagination" @size-change="handleSizeChange" @current-change="handleCurrentChange"
              :current-page="pageData.listQuery.pageCurrent" background :page-sizes="[10, 20, 50, 100]"
              :page-size="pageData.listQuery.pageSize" layout="total,  prev, pager, next, sizes, jumper"
              :total="total"></el-pagination>
          </div>
        </template>
      </MOSTY.Table>
    </div>

  </div>
  <!-- 微型消防站人员 -->
  <div v-if="addbpdShow" class="dialog-box">
    <Addwxxfz :formData="formData" @changePage="changePage" :type="props.obj.type" />
  </div>
</template>
<script setup>
import * as MOSTY from "@/components/MyComponents/index";
import Addwxxfz from "@/components/dialog/Addwxxfz.vue";
import { reactive, ref, onMounted, watchEffect, getCurrentInstance } from "vue"
import { getApi, postApi } from "@/api/tobAcco_api.js";
import { ElMessage } from "element-plus";
const { proxy } = getCurrentInstance();
const { D_BZ_XB } = proxy.$dict("D_BZ_XB");
const emits = defineEmits(['searchChange', 'tableChange']);
const tableloading = ref(false);
const addbpdShow = ref(false)
const total = ref(0)
const editIndex = ref(null)
const props = defineProps({
  obj: {
    default: null,
    type: Object
  },
  //是否显示统计数据
  showCount: {
    type: Boolean,
    default: true
  }
});
const formData = ref({})
const pageData = reactive({

  listQuery: {
    pageCurrent: 1,
    pageSize: 10
  },
  tableColumns: [
    {
      title: "姓名",
      prop: "username"
    },
    {
      title: "性别",
      prop: "sex",
      slot: true
    },
    {
      title: "联系电话",
      prop: "phone",
      slot: true
    },
    {
      title: "备注",
      prop: "bz"
    },

    {
      title: "操作",
      prop: "actions",
      slot: true
    }

  ],
  tableData: [],
})
// 电话号码脱敏函数：保留前三后四，中间用****
const maskPhone = (num) => {
  if (!num) return ""
  const str = String(num)
  if (str.length < 7) return str // 太短不脱敏
  return str.replace(/^(\d{3})\d{4}(\d+)$/, "$1****$2")
}
watchEffect(() => {
  if (props.showCount) {
    pageData.listQuery.zbid = props.obj.id
    //获取子表
    getzblist()
  } else {

  }
});
onMounted(() => {
})

//获取子表
function getzblist() {
  postApi(pageData.listQuery, '/mosty-jcgl/xfkzzs/queryWxxfzList').then(res => {
    pageData.tableData = res.records
      ? res.records : [];
    total.value = res.total
  })
}
//新增
function addUserHander() {
  editIndex.value = null;
  formData.value.siteid = props.obj.siteid ? props.obj.siteid : null
  formData.value.zbid = props.obj.id ? props.obj.id : null
  formData.value.sitename = props.obj.sitename ? props.obj.sitename : null
  formData.value.buildingname = props.obj.buildingname ? props.obj.buildingname : null
  formData.value.buildingid = props.obj.buildingid ? props.obj.buildingid : null
  formData.value.floorid = props.obj.floorid ? props.obj.floorid : null
  formData.value.floorname = props.obj.floorname ? props.obj.floorname : null
  formData.value.partname = props.obj.partname ? props.obj.partname : null
  formData.value.partid = props.obj.partid ? props.obj.partid : null
  formData.value.qtmc = props.obj.qtmc ? props.obj.qtmc : null
  formData.value.publicarea = props.obj.publicarea ? props.obj.publicarea : null;
  formData.value.sfcq = props.obj.sfcq ? props.obj.sfcq : null;
  formData.value.responsibilitydeptname = props.obj.responsibilitydeptname ? props.obj.responsibilitydeptname : null
  formData.value.responsibilitydeptid = props.obj.responsibilitydeptid ? props.obj.responsibilitydeptid : null
  formData.value.responsibilitypersonname = props.obj.responsibilitypersonname ? props.obj.responsibilitypersonname : null
  formData.value.responsibilityperson = props.obj.responsibilityperson ? props.obj.responsibilityperson : null
  formData.value.xtCityCode = props.obj.xtCityCode ? props.obj.xtCityCode : null
  formData.value.title = '消防站人员新增';
  formData.value.saveBtnShow = true;
  addbpdShow.value = true
  tableloading.value = false
}
//修改
function handlexg(row, index) {
  getApi({ id: row.id }, "/mosty-jcgl/xfkzzs/queryWxxfzById").then((res) => {
    if (res) {
      formData.value = res;
      formData.value.siteid = props.obj.siteid ? props.obj.siteid : null
      formData.value.zbid = props.obj.id ? props.obj.id : null
      formData.value.sitename = props.obj.sitename ? props.obj.sitename : null
      formData.value.buildingname = props.obj.buildingname ? props.obj.buildingname : null
      formData.value.buildingid = props.obj.buildingid ? props.obj.buildingid : null
      formData.value.floorid = props.obj.floorid ? props.obj.floorid : null
      formData.value.floorname = props.obj.floorname ? props.obj.floorname : null
      formData.value.partname = props.obj.partname ? props.obj.partname : null
      formData.value.partid = props.obj.partid ? props.obj.partid : null
      formData.value.qtmc = props.obj.qtmc ? props.obj.qtmc : null
      formData.value.publicarea = props.obj.publicarea ? props.obj.publicarea : null;
      formData.value.sfcq = props.obj.sfcq ? props.obj.sfcq : null;
      formData.value.responsibilitydeptname = props.obj.responsibilitydeptname ? props.obj.responsibilitydeptname : null
      formData.value.responsibilitydeptid = props.obj.responsibilitydeptid ? props.obj.responsibilitydeptid : null
      formData.value.responsibilitypersonname = props.obj.responsibilitypersonname ? props.obj.responsibilitypersonname : null
      formData.value.responsibilityperson = props.obj.responsibilityperson ? props.obj.responsibilityperson : null
      formData.value.xtCityCode = props.obj.xtCityCode ? props.obj.xtCityCode : null
      editIndex.value = index;
      formData.value.ind = index;
      formData.value.title = '消防站人员修改';
      formData.value.saveBtnShow = true;
      addbpdShow.value = true
    }
  });

}
//查看
function handleSee(row) {
  getApi({ id: row.id }, "/mosty-jcgl/xfkzzs/queryWxxfzById").then((res) => {
    if (res) {
      formData.value = res;
      formData.value.siteid = props.obj.siteid ? props.obj.siteid : null
      formData.value.zbid = props.obj.id ? props.obj.id : null
      formData.value.sitename = props.obj.sitename ? props.obj.sitename : null
      formData.value.buildingname = props.obj.buildingname ? props.obj.buildingname : null
      formData.value.buildingid = props.obj.buildingid ? props.obj.buildingid : null
      formData.value.floorid = props.obj.floorid ? props.obj.floorid : null
      formData.value.floorname = props.obj.floorname ? props.obj.floorname : null
      formData.value.partname = props.obj.partname ? props.obj.partname : null
      formData.value.partid = props.obj.partid ? props.obj.partid : null
      formData.value.qtmc = props.obj.qtmc ? props.obj.qtmc : null
      formData.value.publicarea = props.obj.publicarea ? props.obj.publicarea : null;
      formData.value.sfcq = props.obj.sfcq ? props.obj.sfcq : null;
      formData.value.responsibilitydeptname = props.obj.responsibilitydeptname ? props.obj.responsibilitydeptname : null
      formData.value.responsibilitydeptid = props.obj.responsibilitydeptid ? props.obj.responsibilitydeptid : null
      formData.value.responsibilitypersonname = props.obj.responsibilitypersonname ? props.obj.responsibilitypersonname : null
      formData.value.responsibilityperson = props.obj.responsibilityperson ? props.obj.responsibilityperson : null
      formData.value.xtCityCode = props.obj.xtCityCode ? props.obj.xtCityCode : null
      formData.value.title = '消防站人员详情';
      formData.value.saveBtnShow = false;
      addbpdShow.value = true
    }
  });
}

//删除
function handledelete(row) {
  if (props.showCount) {
    getApi({ id: row.id }, `/mosty-jcgl/xfkzzs/deleteWxxfzOne`).then(res => {
      ElMessage.success("删除成功");
      emits("searchChange", 'hzzdbj');
      getzblist()
    })
  } else {
    pageData.tableData.forEach((item, index) => {
      if (row == item) {
        pageData.tableData.splice(index, 1);
      }
    });
  }

}
//关闭
const changePage = async (val, obj) => {
  //新增人员时子表不发任何请求
  //修改人员时子表的增删改都要发送请求
  formData.value = {}
  addbpdShow.value = false
  if (props.showCount) {
    if (val == 'xz') {
      //新增成功不刷新表格直接追加
      pageData.tableData.unshift(obj);
      total.value + 1
    } else {
      //修改不追加，直接刷新
      getzblist()
    }
    emits("searchChange", 'hzzdbj');
  } else {
    if (val == 'xz') {
      total.value++;
      //新增成功不刷新表格直接追加
      pageData.tableData.unshift(obj);

    } else {
      pageData.tableData[editIndex.value] = obj
    }
  }
  emits("tableChange", pageData.tableData);
}
//查询
function Searchdata() {
  getzblist()
}
//页码发生变化
const handleCurrentChange = (val) => {
  pageData.listQuery.pageCurrent = val;
  getzblist()
};
//查询条数发生变化
const handleSizeChange = (val) => {
  pageData.listQuery.pageSize = val;
  getzblist()
};
</script>

<style lang="scss" scoped>
.headBox3 {
  display: flex;
  justify-content: end;
  width: 100%;
  margin-bottom: 10px;
}

.top_ul {
  display: flex;
  justify-content: start;
  flex-wrap: wrap;

  >li {
    width: 15%;
    height: 83px;
    margin: 6px 32px;
  }
}
</style>
