<template>
  <div>
    <layout-card>
      <el-row :gutter="12">
        <el-col :span="5">
          <layout-department-tree
            :department-id="DEPARTMENT_ID"
            @node-click="
              router.push({
                path: '/enterprise_management/post',
                query: {
                  DEPARTMENT_ID: $event.id,
                  DEPARTMENT_NAME: $event.name,
                },
              })
            "
          />
        </el-col>
        <el-col :span="19">
          <layout-table
            :data="list"
            v-model:pagination="pagination"
            @get-data="fnGetDataTransfer"
          >
            <el-table-column label="序号" width="70">
              <template v-slot="{ $index }">
                {{ serialNumber(pagination, $index) }}
              </template>
            </el-table-column>
            <el-table-column post prop="DEPARTMENT_NAME" label="机构" />
            <el-table-column post prop="NAME" label="岗位" />
            <el-table-column label="状态" width="100">
              <template v-slot="{ row }">
                <span v-if="row.STATUS === '0'">启用</span>
                <span v-if="row.STATUS === '1'">禁用</span>
              </template>
            </el-table-column>
            <el-table-column label="照片">
              <template v-slot="{ row }">
                <layout-tooltip-img :imgs="row.imgs" />
              </template>
            </el-table-column>
            <el-table-column label="操作" width="100">
              <template v-slot="{ row }">
                <el-button
                  type="primary"
                  text
                  link
                  v-if="buttonJurisdiction.edit"
                  @click="fnAddOrEdit(row.POST_ID, 'edit')"
                >
                  编辑
                </el-button>
                <el-button
                  type="primary"
                  text
                  link
                  v-if="buttonJurisdiction.del"
                  @click="fnDelete(row.POST_ID, row.NAME)"
                >
                  删除
                </el-button>
              </template>
            </el-table-column>
            <template #button>
              <el-button
                type="primary"
                v-if="buttonJurisdiction.add"
                @click="fnAddOrEdit('', 'add')"
              >
                新增
              </el-button>
            </template>
          </layout-table>
        </el-col>
      </el-row>
    </layout-card>
    <add
      v-model:visible="data.addOrEditDialog.visible"
      v-model:form="data.addOrEditDialog.form"
      :type="data.addOrEditDialog.type"
      :department-name="DEPARTMENT_NAME"
      :department-id="DEPARTMENT_ID"
      @get-data="fnResetPaginationTransfer"
    />
  </div>
</template>

<script setup>
import { addingPrefixToFile, serialNumber } from "@/assets/js/utils.js";
import useListData from "@/assets/js/useListData.js";
import {
  getPostView,
  getPostList,
  setPostDelete,
} from "@/request/enterprise_management.js";
import { nextTick, reactive, ref } from "vue";
import LayoutDepartmentTree from "@/components/department_tree/index.vue";
import useButtonJurisdiction from "@/assets/js/useButtonJurisdiction.js";
import { useUserStore } from "@/pinia/user.js";
import { onBeforeRouteUpdate, useRoute, useRouter } from "vue-router";
import { debounce } from "throttle-debounce";
import { ElMessage, ElMessageBox } from "element-plus";
import Add from "./components/add.vue";
import LayoutTooltipImg from "@/components/tooltip_img/index.vue";

const userStore = useUserStore();
const router = useRouter();
const route = useRoute();
const departmentIdDefault = userStore.getUserInfo.DEPARTMENT_ID;
const departmentNameDefault = userStore.getUserInfo.DEPARTMENT_NAME;
const DEPARTMENT_ID = ref(route.query.DEPARTMENT_ID || departmentIdDefault);
const DEPARTMENT_NAME = ref(
  route.query.DEPARTMENT_NAME || departmentNameDefault
);
const { list, pagination, fnGetData, fnResetPagination } = useListData(
  getPostList,
  {
    otherParams: {
      DEPARTMENT_ID: DEPARTMENT_ID.value,
    },
  }
);
const data = reactive({
  addOrEditDialog: {
    visible: false,
    type: "",
    form: {
      NAME: "",
      DESCR: "",
      STATUS: "0",
      files: [],
    },
  },
});
const buttonJurisdiction = await useButtonJurisdiction("post");
const fnGetDataTransfer = () => {
  fnGetData({
    DEPARTMENT_ID: DEPARTMENT_ID.value,
  });
};
const fnResetPaginationTransfer = () => {
  fnResetPagination({
    DEPARTMENT_ID: DEPARTMENT_ID.value,
  });
};
onBeforeRouteUpdate((to) => {
  DEPARTMENT_ID.value = to.query.DEPARTMENT_ID || departmentIdDefault;
  DEPARTMENT_NAME.value = to.query.DEPARTMENT_NAME || departmentNameDefault;
  fnResetPaginationTransfer();
});
const fnAddOrEdit = async (POST_ID, type) => {
  data.addOrEditDialog.visible = true;
  await nextTick();
  data.addOrEditDialog.type = type;
  if (type === "edit") {
    const resData = await getPostView({
      POST_ID,
    });
    data.addOrEditDialog.form = resData.pd;
    data.addOrEditDialog.form.files = addingPrefixToFile(resData.imgs);
  }
};
const fnDelete = debounce(
  1000,
  async (POST_ID, NAME) => {
    await ElMessageBox.confirm(`确定要删除【${NAME}】吗？`, {
      type: "warning",
    });
    await setPostDelete({ POST_ID });
    ElMessage.success("删除成功");
    fnResetPaginationTransfer();
  },
  { atBegin: true }
);
</script>

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