<template>
  <div>
    <layout-card>
      <el-row :gutter="12">
        <el-col :span="5">
          <layout-department-tree
            :department-id="DEPARTMENT_ID"
            @node-click="
              router.push({
                path: '/archives/user',
                query: {
                  DEPARTMENT_ID: $event.id,
                },
              })
            "
          />
        </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
              show-overflow-tooltip
              prop="USERNAME"
              label="用户名"
            />
            <el-table-column show-overflow-tooltip prop="NAME" label="姓名" />
            <el-table-column
              show-overflow-tooltip
              prop="DEPARTMENT_NAME"
              label="部门"
            />
            <el-table-column
              show-overflow-tooltip
              prop="TASK_COUNT"
              label="任务数"
            />
            <el-table-column
              show-overflow-tooltip
              prop="COMPLETE_COUNT"
              label="完成任务数"
            />
            <el-table-column label="操作" width="200">
              <template v-slot="{ row }">
                <el-button
                  type="primary"
                  text
                  link
                  @click="fnChangePerson(row.USER_ID)"
                >
                  人员登记表
                </el-button>
                <el-button
                  type="primary"
                  text
                  link
                  @click="
                    router.push({
                      path: '/archives/user/task_list',
                      query: {
                        USER_ID: row.USER_ID,
                      },
                    })
                  "
                >
                  任务详情
                </el-button>
              </template>
            </el-table-column>
          </layout-table>
        </el-col>
      </el-row>
    </layout-card>
    <personal
      v-if="data.personal.visible"
      v-model:visible="data.personal.visible"
      :user-id="data.personal.userId"
    />
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import LayoutDepartmentTree from "@/components/department_tree/index.vue";
import { useUserStore } from "@/pinia/user.js";
import { onBeforeRouteUpdate, useRoute, useRouter } from "vue-router";
import { serialNumber } from "@/assets/js/utils.js";
import useListData from "@/assets/js/useListData.js";
import { getUserList } from "@/request/archives.js";
import Personal from "./components/personal.vue";

const userStore = useUserStore();
const router = useRouter();
const route = useRoute();
const departmentIdDefault = userStore.getUserInfo.DEPARTMENT_ID;
const DEPARTMENT_ID = ref(route.query.DEPARTMENT_ID || departmentIdDefault);
const { list, pagination, fnGetData, fnResetPagination } = useListData(
  getUserList,
  {
    otherParams: {
      IS_ARCHIVES: 1,
    },
    key: "userList",
  }
);

const data = reactive({
  personal: {
    visible: false,
    userId: "",
  },
});

const fnGetDataTransfer = () => {
  fnGetData({
    DEPARTMENT_ID: DEPARTMENT_ID.value,
    IS_ARCHIVES: 1,
  });
};
const fnResetPaginationTransfer = () => {
  fnResetPagination({
    DEPARTMENT_ID: DEPARTMENT_ID.value,
    IS_ARCHIVES: 1,
  });
};

const fnChangePerson = (userId) => {
  if (userId) {
    data.personal.visible = true;
    data.personal.userId = userId;
  }
};

onBeforeRouteUpdate((to) => {
  DEPARTMENT_ID.value = to.query.DEPARTMENT_ID || departmentIdDefault;
  fnResetPaginationTransfer();
});
</script>

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