<template>
  <div>
    <div class="titleBox">
      <PageTitle title="布控区域" >
        <el-button type="primary" @click="handleRow('add', '')">
          <el-icon style="vertical-align: middle"><CirclePlus /></el-icon>
            <span style="vertical-align: middle">新增</span>
          </el-button>
          <el-button type="danger" @click="handleRow('moreDelete', '')">批量删除</el-button>
      </PageTitle>
    </div>
    <!-- 搜索 -->
    <div ref="searchBox">
      <Search :searchArr="searchConfiger" @submit="onSearch" />
    </div>
    <!-- 按钮组 -->
    <div class="content">
      <!-- 表格 -->
      <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 #qyjb="{ row }">
            <DictTag :tag="false" :value="row.qyjb" :options="D_ZDY_QYJB" />
          </template>
          <template #qylx="{ row }">
            <DictTag :tag="false" :value="row.qylx" :options="D_ZDY_QYLX" />
          </template>
          <template #xzqhdm="{ row }">
            <DictTag :tag="false" :value="row.xzqhdm" :options="D_BZ_XZQHDM" />
          </template>
           <template #sxts="{ row }">
            <span v-for="(it,idx) in row.sxts" :key="idx">{{ it.sbmc }} <span v-if="idx < row.sxts.length-1">、</span></span>
          </template>
          <template #qyzt="{ row }">
            <span @click="handleSwitch(row)">
              <el-switch v-model="row.qyzt" inline-prompt :active-value="'1'" :inactive-value="'0'" active-text="是" inactive-text="否"/>
            </span>
          </template>
          <!-- 操作 -->
          <template #controls="{ row }">
            <el-link size="small" type="primary" @click="handleRow('edit', row)">编辑</el-link>
            <el-link size="small" type="danger" @click="handleRow('delete', row)">删除</el-link>
          </template>
        </MyTable>
        <Pages
          @changeNo="changeNo"
          @changeSize="changeSize"
          :tableHeight="pageData.tableHeight"
          :pageConfiger="{
            ...pageData.pageConfiger,
            total: pageData.total
          }"
        ></Pages>
      </div>
    </div>
  </div>
  <!-- 新增区域 -->
  <AddForm ref="addVisible" @change="getList" :dic="{D_BZ_SF, D_ZDY_QYJB, D_ZDY_QYLX,D_BZ_XZQHDM }"></AddForm>
</template>

<script setup>
import AddForm from "./components/addForm.vue";
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 { qcckGet, qcckPost } from "@/api/qcckApi.js";
import { ElMessage } from "element-plus";
import { reactive, ref, onMounted, getCurrentInstance } from "vue";
const { proxy } = getCurrentInstance();
const { D_BZ_SF, D_ZDY_QYJB, D_ZDY_QYLX,D_BZ_XZQHDM } = proxy.$dict("D_BZ_SF","D_ZDY_QYJB","D_ZDY_QYLX","D_BZ_XZQHDM"); //获取字典数据
const searchBox = ref(); //搜索框
const addVisible = ref(); //新增区域组件
const searchConfiger = ref([
  {
    label: "区域等级",
    prop: "qyjb",
    placeholder: "请选择区域等级",
    showType: "select",
    options: D_ZDY_QYJB
  },
  {
    label: "范围名称",
    prop: "qymc",
    placeholder: "请输入范围名称",
    showType: "input"
  },
  {
    label: "范围类型",
    prop: "qylx",
    placeholder: "请选择范围类型",
    showType: "select",
    options: D_ZDY_QYLX
  },
  {
    label: "是否启用",
    prop: "qyzt",
    placeholder: "请选择是否启用",
    showType: "select",
    options: D_BZ_SF
  }
]);
const ids = ref([])
const queryFrom = ref({});
const pageData = reactive({
  tableData: [], //表格数据
  keyCount: 0,
  tableConfiger: {
    rowHieght: 61,
    showSelectType: "checkBox",
    loading: false
  },
  total: 0,
  pageConfiger: {
    pageSize: 20,
    pageCurrent: 1
  }, //分页
  controlsWidth: 120, //操作栏宽度

  tableColumn: [
    { label: "区域等级", prop: "qyjb", showSolt: true },
    { label: "范围名称", prop: "qymc" },
    { label: "范围类型", prop: "qylx", showSolt: true },
    { label: "行政区划", prop: "xzqhdm", showSolt: true },
    { label: "感知源信息", prop: "sxts", showSolt: true },
    { label: "是否启用", prop: "qyzt", showSolt: true }
  ]
});

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

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

const changeNo = (val) => {
  pageData.pageConfiger.pageCurrent = val;
  getList();
};
const changeSize = (val) => {
  pageData.pageConfiger.pageSize = val;
  getList();
};
// 获取列表
const getList = () => {
  pageData.tableConfiger.loading = true;
  let params = { ...pageData.pageConfiger, ...queryFrom.value };
  qcckGet(params, "/mosty-gsxt/tbGsxtBkQy/selectPage").then((res) => {
      pageData.tableData = res.records || [];
      pageData.total = res.total;
      pageData.tableConfiger.loading = false;
  }).catch(() => {
      pageData.tableConfiger.loading = false;
  });
};

const chooseData = (data) => {
  ids.value = Array.isArray(data) ? data.map((item) => item.id) : [];
};

const handleRow = (type, row) => {
  switch (type) {
    case "add"://新增
    case "edit": //编辑
        addVisible.value.init(type, row);
      break;
    case "delete"://删除
      deleteItem([row.id])
      break;
    case "moreDelete": //批量删除
      if (ids.value.length === 0) return ElMessage.warning("请先选择布控数据");
      deleteItem(ids.value)
      break;
  }
};

const deleteItem = (ids) => {
  proxy.$confirm("确定要删除", "警告", { type: "warning" }).then(() => {
    qcckPost(ids, "/mosty-gsxt/tbGsxtBkQy/delete").then(() => {
      proxy.$message({ type: "success", message: '删除成功' });
      getList();
    });
  })
};

const handleSwitch = (row) =>{
  qcckPost({id:row.id,qyzt:row.qyzt}, "/mosty-gsxt/tbGsxtBkQy/updateForQyzt").then(() => {
    proxy.$message({ type: "success", message: '操作成功' });
    // getList();
  });
}
// 表格高度计算
const tabHeightFn = () => {
  pageData.tableHeight = window.innerHeight - searchBox.value.offsetHeight - 272;
  window.onresize = function () {
    tabHeightFn();
  };
};
</script>

<style lang="scss" scoped>
.el-loading-mask {
  background: rgba(0, 0, 0, 0.5) !important;
}
.content {
  background-color: #fff;
  border-radius: 4px;
  padding-top: 10px;
  .btns {
    padding: 0 10px;
  }
}
</style>
