<template>
  <div class="todo-list">
    <Header />

    <div class="content-wrapper mt-15 max-w-[1200px] mx-auto">
      <div
        class="flex gap-5 items-center mb-4 bg-[#091126] py-2 pl-4 rounded-sm"
      >
        <p class="flex items-center text-white">
          <Icon
            icon="mynaui:arrow-up-down"
            class="icon pr-1 h-8 w-8"
            color="#014099"
          />SRM已办任务列表
        </p>

        <el-button
          type="primary"
          round
          size="small"
          color="#02409b"
          @click="router.back()"
          ><Icon
            icon="mynaui:corner-up-left"
            class="icon pr-1"
            width="20"
            height="20"
          />&#36820;&#22238;</el-button
        >
      </div>
      <div v-loading="loading" class="table-wrapper">
        <el-table
          v-loading="loading"
          :data="oaTasks"
          style="width: 100%"
          stripe
          height="70vh"
          element-loading-text="加载中..."
          :empty-text="loading ? '' : '暂无数据'"
          :header-cell-style="{
            backgroundColor: '#091126',
            color: '#b8bfd0',
            fontWeight: '400',
          }"
          :cell-style="{
            color: 'black',
          }"
        >
          <el-table-column
            type="index"
            label="&#24207;&#21495;"
            width="80"
            fixed="left"
            align="center"
            :index="getIndex"
          />
          <el-table-column
            prop="title"
            label="请求标题"
            min-width="250"
            fixed="left"
            align="center"
          />
          <el-table-column
            prop="creatorName"
            label="创建人"
            width="140"
            align="center"
          />

          <el-table-column label="创建日期" width="140" align="center">
            <template #default="scope">
              {{ scope.row.createdDate.substring(0, 10) }}
            </template>
          </el-table-column>

          <el-table-column
            label="操作"
            width="120"
            fixed="right"
            align="center"
          >
            <template #default="scope">
              <span
                class="text-[#1e90ff] cursor-pointer text-[13px]"
                @click="goBackPage(scope.row)"
                >&#22788;&#29702;</span
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.pageNum"
          v-model:page-size="pagination.pageSize"
          :total="pagination.total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next"
          background
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
    </div>

    <Footer />
  </div>
</template>

<script setup>
import Header from "@components/home/header.vue";
import * as dd from "dingtalk-jsapi";
import { ref, onMounted } from "vue";
import { getSRMTasks, srmLogin } from "@/api/user";
import { useUserStore } from "@/stores/useUserStore";
import { Icon } from "@iconify/vue";
import router from "@/router";
const userStore = useUserStore();
import { ElLoading, ElMessage } from "element-plus";

const oaTasks = ref([]);
const loading = ref(false);

const pagination = ref({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

const SRM_LOGIN_DELAY = 2000;

const goBackPage = async (row) => {
  const res = await srmLogin({
    username: userStore.getUser.username,
  });
  if (JSON.parse(res).msg === "账号未授权") {
    dialogVisible.value = true;
    return;
  }
  if (res) {
    const ua = window.navigator.userAgent.toLowerCase();
    if (ua.includes("dingtalk") || ua.includes("dingtalkwork")) {
      dd.biz.util.openLink({
        url: `https://srm.deepoil.cc/#/mixed-page/view/MXP00048?Authorization=${JSON.parse(res).msg}`,
        onSuccess: () => {
          setTimeout(() => {
            dd.biz.util.openLink({
              url: `https://srm.deepoil.cc/#/workflow-process/request-${row.requestId}`,
            });
          }, SRM_LOGIN_DELAY);
        },
      });
    } else {
      const loading = ElLoading.service({
        lock: true,
        text: "正在跳转，请稍候...",
        background: "rgba(0, 0, 0, 0.7)",
      });
      const newTab = window.open("", "_blank");
      newTab.location.href = `https://srm.deepoil.cc/#/mixed-page/view/MXP00048?Authorization=${JSON.parse(res).msg}`;
      setTimeout(function () {
        newTab.location.href = `https://srm.deepoil.cc/#/workflow-process/request-${row.requestId}`;
        setTimeout(() => {
          loading.close();
        }, 500);
      }, SRM_LOGIN_DELAY);
    }
  }
};

const handleCurrentChange = async (page) => {
  pagination.value.pageNum = page;
  try {
    const res = await getSRMTasks({
      id: userStore.getUser.username,
      pageNum: pagination.value.pageNum,
      pageSize: pagination.value.pageSize,
    });
    oaTasks.value = res.doneList;
    pagination.value.total = Number(res.doneCount);
  } finally {
    loading.value = false;
  }
};

const handleSizeChange = async (size) => {
  pagination.value.pageSize = size;
  pagination.value.pageNum = 1;

  try {
    const res = await getSRMTasks({
      id: userStore.getUser.username,
      pageNum: pagination.value.pageNum,
      pageSize: pagination.value.pageSize,
    });
    oaTasks.value = res.doneList;
    pagination.value.total = Number(res.doneCount);
  } finally {
    loading.value = false;
  }
};

onMounted(async () => {
  if (userStore.getUser.username) {
    loading.value = true;
    try {
      const res = await getSRMTasks({
        id: userStore.getUser.username,
        pageNum: pagination.value.pageNum,
        pageSize: pagination.value.pageSize,
      });
      oaTasks.value = res.doneList;
      pagination.value.total = Number(res.doneCount);
    } finally {
      loading.value = false;
    }
  }
});
</script>

<style scoped>
.todo-list {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  /* background-color: #f5f7fa; */
}

.content {
  padding: 16px 20px;
  margin-top: 100px;
}

.pagination-wrap {
  display: flex;
  justify-content: flex-end;
  margin-top: 16px;
}

.news-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f5f7fa;
}

.content-wrapper {
  flex: 1;
  max-width: 1200px;
  /* margin: 0 auto; */
  padding: 20px;
  width: 100%;
  box-sizing: border-box;
}

.page-title {
  margin-bottom: 20px;
  color: #303133;
  border-left: 5px solid #409eff;
  padding-left: 10px;
}

.table-wrapper {
  min-height: 400px;
  background: #fff;
  padding: 20px;
  border-radius: 4px;
}

.table-title {
  font-weight: 500;
  color: #303133;
}

.table-summary {
  color: #606266;
  font-size: 13px;
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 0px;
  padding: 20px 0;
}

:deep(.el-table) {
  background-color: #091126 !important;
  color: white !important;
}

:deep(.table-wrapper) {
  background: #091126 !important;
}

:deep(.el-table__header-wrapper thead) {
  background: #091126 !important;
  color: white !important;
}

:deep(.el-table__header-wrapper .el-table__header) {
  border-bottom: none;
}
:deep(.el-table__inner-wrapper) {
  background: #091126 !important;
  color: white !important;
}
:deep(.el-table__body-wrapper .el-table__row) {
  background: #091126;
  color: white !important;
}

:deep(.el-table__body-wrapper .el-table__row .hover-row) {
  background: #091126 !important;
  color: white !important;
}

:deep(.el-table__body-wrapper .el-table__row .el-table__cell) {
  background: #091126 !important;
  color: white !important;
  border-bottom: 1px solid #313849;
}

:deep(.el-loading-mask) {
  background-color: rgba(9, 17, 38, 0.8) !important; /* 半透明深色遮罩 */
}

:deep(.el-table__body-wrapper .el-table__row .el-table__cell) {
  border-bottom: 1px solid #313849;
}

:deep(.el-pagination is-background) {
  background: #091126 !important;
}

:deep(.el-pagination .el-pager li) {
  background: #091126 !important;
  color: white !important;
}

:deep(.el-pagination .el-pager li.active) {
  background: #2d8cf0 !important;
}

:deep(.el-select .el-select__wrapper) {
  background: #091126 !important;
  border: 1px solid #313849 !important;
  outline: none !important;
}

:deep(.el-input) {
  border: 1px solid #313849 !important;
  outline: none !important;
}

:deep(.btn-next) {
  background: #091126 !important;
  color: white !important;
}

:deep(.btn-prev) {
  background: #091126 !important;
  color: white !important;
}

/* 下拉菜单弹出层的背景 */
:deep(.el-select-dropdown) {
  background-color: #091126 !important;
  border: 1px solid #313849 !important;
}

/* 下拉菜单项 */
:deep(.el-select-dropdown__item) {
  color: #fff !important;
}

:deep(.el-select-dropdown__item.hover),
:deep(.el-select-dropdown__item:hover) {
  background-color: #111a35 !important; /* 悬停深色背景 */
}

/* 关键：修复下拉选择框 (PageSize) 的白色边框/背景 */
:deep(.el-pagination .el-select .el-select__wrapper) {
  background-color: #091126 !important;
  box-shadow: none !important; /* 去掉可能的阴影 */
  border: 1px solid #313849 !important; /* 统一边框颜色 */
}

/* 去掉聚焦时的白色/蓝色轮廓 */
:deep(.el-pagination .el-select .el-select__wrapper.is-focused),
:deep(.el-pagination .el-select .el-select__wrapper:hover) {
  box-shadow: none !important;
  border-color: #409eff !important; /* 聚焦时变为主题蓝，或者保持 #313849 */
}

/* 通用输入框边框修复 (如果其他地方也有) */
:deep(.el-input__wrapper) {
  background-color: #091126 !important;
  box-shadow: none !important; /* 去掉默认的白色/灰色阴影边框 */
  border: 1px solid #313849 !important;
}

:deep(.el-input__inner) {
  color: #fff !important;
}
</style>
