<template>
  <div>
    <div class="titleBox">
       <!-- 头部 -->
      <PageTitle title="单位管理">
        <el-button type="primary" @click="addEdit('add', '')">
          <el-icon style="vertical-align: middle"> <CirclePlus /> </el-icon>
          <span style="vertical-align: middle">新增</span>
        </el-button>
        <el-button @click="delDictItem(ids)" :disabled="ids.length == 0" typeof="danger">
          <el-icon style="vertical-align: middle"><Delete /> </el-icon>
          <span style="vertical-align: middle">批量删除</span>
        </el-button>
      </PageTitle>
    </div>
    <!-- 搜索 -->
    <div ref="searchBox">
        <Search :searchArr="searchConfiger" @submit="onSearch">
          <template #defaultSlot>
            <MOSTY.Department width="100%" clearable v-model="listQuery.ssbmdm" />
          </template>
        </Search>
      </div>
    <!-- 表格 -->
    <div class="tabBox">
      <MyTable
        :tableData="pageData.tableData"
        :tableColumn="pageData.tableColumn"
        :tableHeight="pageData.tableHeight"
        :key="pageData.keyCount"
        :tableConfiger="pageData.tableConfiger"
        :controlsWidth="pageData.controlsWidth"
        @chooseData="chooseData"
      >
      <template #dwlx="{row}">
          <dict-tag :value="row.dwlx" :options="D_BZ_DWLX" :tag="false"></dict-tag>
      </template>
      <template #dwfl="{row}">
          <dict-tag :value="row.dwfl" :options="D_BZ_DWFL" :tag="false"></dict-tag>
      </template>
        <!-- 操作 -->
        <template #controls="{ row }">
          <el-button size="small" @click="addEdit('edit', row.id)">编辑</el-button>
          <el-button size="small" @click="addEdit('detail', row.id)">详情</el-button>
          <el-button size="small" @click="delDictItem([row.id])">删除</el-button>
        </template>
      </MyTable>
      <Pages
        @changeNo="changeNo"
        @changeSize="changeSize"
        :tableHeight="pageData.tableHeight"
        :pageConfiger="{
          ...pageData.pageConfiger,
          total: pageData.total
        }"
      ></Pages>

    </div>
    <!-- 详情 -->
    <EditAddForm :dic="{ D_BZ_DWFL , D_BZ_DWLX}" ref="editInfo" @updateDate="getData"></EditAddForm>
  </div>
</template>

<script setup>
import { qcckGet, qcckPost ,qcckDelete} from "@/api/qcckApi.js";
import EditAddForm from './components/editAddForm.vue'
import * as MOSTY from "@/components/MyComponents/index";
import PageTitle from "@/components/aboutTable/PageTitle.vue";
import MyTable from "@/components/aboutTable/MyTable.vue";
import Pages from "@/components/aboutTable/Pages.vue";
import Search from "@/components/aboutTable/Search.vue";
import { reactive, ref ,onMounted,getCurrentInstance, nextTick } from 'vue';
const { proxy } = getCurrentInstance();
const { D_BZ_DWFL,D_BZ_DWLX } = proxy.$dict("D_BZ_DWFL","D_BZ_DWLX");
const editInfo = ref()
const ids = ref([]);//多选
const searchBox = ref() //搜索框
const searchConfiger = reactive([
  {
    showType: "defaultSlot",
    prop: "ssbmdm",
    options: [],
    placeholder: "请选择单位",
    label: "归属单位"
  },
  {
    showType: "input",
    prop: "dwmc",
    placeholder: "请输入单位名称",
    label: "单位名称"
  },{
    showType: "select",
    prop: "dwfl",
    placeholder: "请选择单位分类",
    label: "单位分类",
    options:D_BZ_DWFL
  },
  {
    showType: "select",
    prop: "dwlx",
    placeholder: "请选择单位类型",
    label: "单位类型",
    options:D_BZ_DWLX
  },
])
const pageData = reactive({
  tableData: [], //表格数据
  keyCount: 0,
  tableConfiger: {
    loading: false,
    rowHieght: 61,
    showSelectType:'checkBox'
  },
  total: 0,
  pageConfiger: {
    pageSize: 10,
    pageNum: 1
  }, //分页
  controlsWidth: 210, //操作栏宽度
  tableColumn: [
    {
      label: "归属单位",
      prop: "ssbm"
    },
    {
      label: "单位名称",
      prop: "dwmc"
    },
    {
      label: "单位类型",
      prop: "dwlx",
      showSolt:true,
    },
    {
      label: "单位分类",
      prop: "dwfl",
      showSolt:true
    },
    {
      label: "单位地址",
      prop: "dz1"
    },
    {
      label: "最后修改时间",
      prop: "xtZhgxsj"
    },
  ]
});
const listQuery = ref({ssbmsm:''})
onMounted(() => {
  tabHeightFn();
  proxy.mittBus.on("mittFn", (data) => { pageData.keyCount = data; });
  getData()
});
// 搜索
const onSearch = (val)=>{
  delete val.ssbmdm;
  listQuery.value = {...listQuery.value,...val}
  if(val.cz) listQuery.value.ssbmdm = '';
  delete listQuery.value.cz;
  pageData.pageConfiger.pageNum = 1 ;
  getData()
}

// 获取数据 
const getData = ()=>{
  let pramas = {
    pageSize:pageData.pageConfiger.pageSize,
    pageCurrent:pageData.pageConfiger.pageNum,
    ...listQuery.value
  }
  pageData.tableConfiger.loading = true
  qcckGet(pramas,'/mosty-jcgl/tbJcglXfDwgl').then(res=>{
    pageData.tableData = res.records || []
    pageData.tableConfiger.loading = false
    pageData.total = res.total
  }).catch(()=> { pageData.tableConfiger.loading = false })
}

// 详情 - 新增
const addEdit = (type,id)=>{
  nextTick(()=>{ editInfo.value.init(type,id) })
}
// 多选
const chooseData = (val)=>{
  if(!val) return false;
  ids.value = val.map(v=>{ return v.id })
}
// 批量删除
const delDictItem = (row)=> {
  proxy.$confirm("确定要删除", "警告", {type: "warning"}).then(() => {
    qcckPost(row,'/mosty-jcgl/tbJcglXfDwgl/bacth').then(() => {
      proxy.$message({type: "success", message: "删除成功" });
      pageData.pageConfiger.pageNum = 1
      getData();
    });
    }).catch(() => {
      proxy.$message.info("已取消");
  });
};
// 表格高度计算
const tabHeightFn = () => {
  pageData.tableHeight = window.innerHeight - searchBox.value.offsetHeight - 244;
  window.onresize = function () { tabHeightFn(); };
};
</script>

<style>
.el-loading-mask{
  background: rgba(0,0,0,0.3);
}
</style>
