<template>
  <div>
    <layout-card>
      <el-row :gutter="12">
        <el-col :span="5">
          <electronic-fence-tree
            v-model:refresh="data.refreshTreeData"
            :electronic-fence-area-id="ELECTRONIC_FENCE_AREA_ID"
            @throw-data="data.treeData = $event"
            @node-click="
              router.push({
                path: '/enterprise_management/information/electronic_fence',
                query: {
                  ELECTRONIC_FENCE_AREA_ID: $event.id,
                  ELECTRONIC_FENCE_AREA_NAME: $event.name,
                },
              })
            "
          />
        </el-col>
        <el-col :span="19">
          <el-button
            class="mb-10"
            @click="data.structuralDiagramVisible = true"
          >
            结构图
          </el-button>
          <layout-table
            v-model:pagination="pagination"
            :data="list"
            @get-data="fnGetDataTransfer"
          >
            <el-table-column label="序号" width="70">
              <template #default="{ $index }">
                {{ serialNumber(pagination, $index) }}
              </template>
            </el-table-column>
            <el-table-column label="电子围栏区域名称">
              <template #default="{ row }">
                <el-button
                  type="primary"
                  text
                  link
                  @click="
                    router.push({
                      path: '/enterprise_management/information/electronic_fence',
                      query: {
                        ELECTRONIC_FENCE_AREA_ID: row.ELECTRONIC_FENCE_AREA_ID,
                        ELECTRONIC_FENCE_AREA_NAME:
                          row.ELECTRONIC_FENCE_AREA_NAME,
                      },
                    })
                  "
                >
                  {{ row.ELECTRONIC_FENCE_AREA_NAME
                  }}<el-icon><arrow-right /></el-icon>
                </el-button>
              </template>
            </el-table-column>
            <template v-if="ELECTRONIC_FENCE_AREA_ID !== '0'">
              <el-table-column prop="HEADMAN" label="负责人" />
              <el-table-column
                prop="ELECTRONIC_FENCE_NAME"
                label="所属电子围栏"
              />
              <el-table-column prop="TEL" label="电话" />
              <el-table-column prop="BZ" label="备注" />
              <el-table-column label="操作" width="100">
                <template #default="{ row }">
                  <el-button
                    v-if="buttonJurisdiction.edit"
                    type="primary"
                    text
                    link
                    @click="fnAddOrEdit(row.ELECTRONIC_FENCE_AREA_ID, 'edit')"
                  >
                    编辑
                  </el-button>
                  <el-button
                    v-if="buttonJurisdiction.del"
                    type="primary"
                    text
                    link
                    @click="
                      fnDelete(
                        row.ELECTRONIC_FENCE_AREA_ID,
                        row.ELECTRONIC_FENCE_AREA_NAME
                      )
                    "
                  >
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </template>
            <template v-if="ELECTRONIC_FENCE_AREA_ID !== '0'" #button>
              <el-button
                v-if="buttonJurisdiction.add"
                type="primary"
                @click="fnAddOrEdit('', 'add')"
              >
                新增
              </el-button>
              <el-button @click="router.back"> 返回 </el-button>
            </template>
          </layout-table>
        </el-col>
      </el-row>
    </layout-card>
    <structural-diagram
      v-model:visible="data.structuralDiagramVisible"
      :tree-data="data.treeData"
    />
    <add
      v-model:visible="data.addOrEditDialog.visible"
      v-model:form="data.addOrEditDialog.form"
      :type="data.addOrEditDialog.type"
      :parent-name="ELECTRONIC_FENCE_AREA_NAME"
      :parent-id="ELECTRONIC_FENCE_AREA_ID"
      @get-data="
        () => {
          fnResetPaginationTransfer();
          data.refreshTreeData = true;
        }
      "
    />
  </div>
</template>

<script setup>
import { serialNumber } from "@/assets/js/utils.js";
import useListData from "@/assets/js/useListData.js";
import {
  getElectronicFenceList,
  getElectronicFenceView,
  setElectronicFenceDelete,
} from "@/request/enterprise_management.js";
import { nextTick, reactive, ref } from "vue";
import { onBeforeRouteUpdate, useRoute, useRouter } from "vue-router";
import { ArrowRight } from "@element-plus/icons-vue";
import { debounce } from "throttle-debounce";
import { ElMessage, ElMessageBox } from "element-plus";
import StructuralDiagram from "@/views/enterprise_management/department/components/structural_diagram.vue";
import ElectronicFenceTree from "@/components/electronic_fence_tree/index.vue";
import Add from "./components/add.vue";
import useButtonJurisdiction from "@/assets/js/useButtonJurisdiction.js";

const router = useRouter();
const route = useRoute();
const parentIdDefault = "0";
const parentNameDefault = "（无）此项为顶级电子围栏";
const ELECTRONIC_FENCE_AREA_ID = ref(
  route.query.ELECTRONIC_FENCE_AREA_ID || parentIdDefault
);
const ELECTRONIC_FENCE_AREA_NAME = ref(
  route.query.ELECTRONIC_FENCE_AREA_NAME || parentNameDefault
);
const { list, pagination, fnGetData, fnResetPagination } = useListData(
  getElectronicFenceList,
  {
    otherParams: { ELECTRONIC_FENCE_AREA_ID: ELECTRONIC_FENCE_AREA_ID.value },
  }
);
const data = reactive({
  treeData: [],
  refreshTreeData: false,
  structuralDiagramVisible: false,
  addOrEditDialog: {
    visible: false,
    type: "",
    form: {
      NAME: "",
      LEVEL: "",
      DEP_ORDER: "",
      HEADMAN: "",
      TEL: "",
      FUNCTIONS: "",
      BZ: "",
      ISSUPERVISE: "0",
      checkedIds: [],
    },
  },
});
const buttonJurisdiction = await useButtonJurisdiction("department");
const fnGetDataTransfer = () => {
  fnGetData({
    ELECTRONIC_FENCE_AREA_ID: ELECTRONIC_FENCE_AREA_ID.value,
  });
};
const fnResetPaginationTransfer = () => {
  fnResetPagination({
    ELECTRONIC_FENCE_AREA_ID: ELECTRONIC_FENCE_AREA_ID.value,
  });
};
onBeforeRouteUpdate((to) => {
  ELECTRONIC_FENCE_AREA_ID.value =
    to.query.ELECTRONIC_FENCE_AREA_ID || parentIdDefault;
  ELECTRONIC_FENCE_AREA_NAME.value =
    to.query.ELECTRONIC_FENCE_AREA_NAME || parentNameDefault;
  fnResetPaginationTransfer();
});
const fnAddOrEdit = async (ELECTRONIC_FENCE_AREA_ID, type) => {
  data.addOrEditDialog.visible = true;
  await nextTick();
  data.addOrEditDialog.type = type;
  if (type === "edit") {
    const resData = await getElectronicFenceView({
      ELECTRONIC_FENCE_AREA_ID,
    });
    data.addOrEditDialog.form = resData.pd;
  }
};
const fnDelete = debounce(
  1000,
  async (ELECTRONIC_FENCE_AREA_ID, NAME) => {
    await ElMessageBox.confirm(`确定要删除【${NAME}】吗？`, {
      type: "warning",
    });
    await setElectronicFenceDelete({ ELECTRONIC_FENCE_AREA_ID });
    ElMessage.success("删除成功");
    fnResetPaginationTransfer();
    data.refreshTreeData = true;
  },
  { atBegin: true }
);
</script>

<style scoped lang="scss"></style>
