<template>
  <div>
    <div class="titleBox">
      <PageTitle title="身份标签管理">
        <template #left>
          <el-button v-for="(it,idx) in D_GS_BQ_LB " :key="idx" :type="type == it.zdmc ? 'success':''" @click="chooseListType(it.zdmc)">
            <span style="vertical-align: middle">{{ it.zdmc }}</span>
          </el-button>
        </template>
        <el-button type="primary" @click="addEdit('add', '')">
          <el-icon style="vertical-align: middle"><CirclePlus /></el-icon>
          <span style="vertical-align: middle">新增</span>
        </el-button>
      </PageTitle>
    </div>
    <!-- 搜索 -->
    <div ref="searchBox">
      <Search :searchArr="searchConfiger" @submit="onSearch" :key="pageData.keyCount"></Search>
    </div>
    <!-- 表格 -->
    <div class="tabBox" :style="{height:pageData.tableHeight+20+'px'}">
      <MyTable
        :tableData="pageData.tableData"
        :tableColumn="pageData.tableColumn"
        :tableHeight="pageData.tableHeight"
        :key="pageData.keyCount"
        :tableConfiger="pageData.tableConfiger"
        :controlsWidth="pageData.controlsWidth"
        @chooseData="chooseData"
      >
        <template #bqLx="{ row }">
          <DictTag :value="row.bqLx" :tag="false" :options="D_GS_BQ_LX" />
        </template>
        <template #bqLb="{ row }">
          <DictTag :value="row.bqLb" :tag="false" :options="D_GS_BQ_LB" />
        </template>
        <template #bqYs="{ row }">
          <DictTag :value="row.bqYs" :tag="false" :options="D_GS_SSYJ" />
        </template>
        <template #bqDj="{ row }">
          <DictTag :tag="false"  :value="row.bqDj" :options="D_GS_BQ_DJ" />
        </template>
        <!-- 操作 -->
        <template #controls="{ row }">
          <el-link size="small" @click="addEdit('edit', row)">编辑</el-link>
          <el-link size="small" @click="delDictItem(row.id)" type="danger">删除</el-link>
        </template>
      </MyTable>
      <Pages
        @changeNo="changeNo"
        @changeSize="changeSize"
        :tableHeight="pageData.tableHeight"
        :pageConfiger="{
          ...pageData.pageConfiger,
          total: pageData.total
        }"
      ></Pages>
    </div>
    <!-- 编辑详情 -->
    <EditAddForm ref="detailDiloag" :type="type" :dic="{D_GS_BQ_LX,D_GS_BQ_DJ,D_GS_SSYJ,D_GS_BQ_LB}" @updateDate="getList" />
  </div>
</template>

<script setup>
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 EditAddForm from "./components/editAddForm.vue";
import { qcckGet, qcckPost, qcckDelete } from "@/api/qcckApi.js";
import { reactive, ref, onMounted, getCurrentInstance } from "vue";
const { proxy } = getCurrentInstance();
const { D_GS_BQ_DJ,D_GS_SSYJ,D_GS_BQ_LB,D_GS_BQ_LX } = proxy.$dict("D_GS_BQ_DJ","D_GS_SSYJ","D_GS_BQ_LB","D_GS_BQ_LX"); //获取字典数据
const detailDiloag = ref();
const searchBox = ref(); //搜索框
const type = ref('标签大类')

const searchConfiger = ref([
  { label: "标签名称",prop:'bqMc',placeholder: "请输入学校名称", showType: "input"},
  { label: "标签等级",prop:'bqDj' ,placeholder: "请选择标签等级", showType: "select",options:D_GS_BQ_DJ },
  { label: "标签颜色",prop:'bqYs' ,placeholder: "请选择标签颜色", showType: "select",options:D_GS_SSYJ },
]);
const queryFrom = ref({});
const pageData = reactive({
  tableData: [], //表格数据
  keyCount: 0,
  tableConfiger: {
    rowHieght: 61,
    showSelectType: "null",
    loading: false
  },
  total: 0,
  pageConfiger: {
    pageSize: 20,
    pageCurrent: 1
  }, //分页
  controlsWidth: 160, //操作栏宽度
  tableColumn: [
    { label: "标签名称", prop: "bqMc" },
    { label: "标签代码", prop: "bqDm" },
    { label: "标签类型", prop: "bqLx",showSolt:true },
    { label: "标签类别", prop: "bqLb",showSolt:true },
    { label: "标签等级", prop: "bqDj",showSolt:true},
    { label: "标签颜色", prop: "bqYs",showSolt:true},
  ]
});
onMounted(() => {
  getList()
  tabHeightFn();
});

// 选择列表的大类和细类
const chooseListType = (val) => {
  type.value = val;
  pageData.pageConfiger.pageCurrent = 1;
  switch (val) {
    case '标签大类':
      pageData.tableColumn = pageData.tableColumn.filter((it) => it.prop != "bqDlMc");
      break;
      case '标签细类':
      let obj = { label: "标签大类名称", prop: "bqDlMc" };
      pageData.tableColumn.splice(3, 0, obj);
      break;
  }
  pageData.keyCount++;

  getList()
};

// 搜索
const onSearch = (val) =>{
  queryFrom.value = {...val}
  pageData.pageConfiger.pageCurrent = 1;
  getList()
}

// 分页
const changeNo = (val) =>{
  pageData.pageConfiger.pageNum = val;
  getList()
}
// 页数
const changeSize = (val) =>{
  pageData.pageConfiger.pageSize = val;
  getList()
}

// 获取列表
const getList = (val) =>{
  pageData.tableConfiger.loading = true;
  let data = { ...pageData.pageConfiger,...queryFrom.value,bqLx:'01' };
  data.bqLb = type.value == '标签大类'? '01':'02'
  qcckGet(data,'/mosty-gsxt/tbGsxtBqgl/selectPage').then(res=>{
    pageData.tableData = res.records || [];
    pageData.total = res.total;
    pageData.tableConfiger.loading = false;
  }).catch(()=>{ pageData.tableConfiger.loading = false; })
}
// 删除
const delDictItem = (id) =>{
  proxy.$confirm("确定要删除", "警告", {type: "warning"}).then(() => {
    qcckDelete({},'/mosty-gsxt/tbGsxtBqgl/'+id).then(()=>{
      proxy.$message({ type: "success",  message: "删除成功" });
      getList();
    })
  }).catch(() => {});
}

// 新增
const addEdit = (type, row) => {
  detailDiloag.value.init(type, row);
};

// 表格高度计算
const tabHeightFn = () => {
  pageData.tableHeight = window.innerHeight - searchBox.value.offsetHeight - 250;
  window.onresize = function () { tabHeightFn(); };
};
</script>

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