<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-[#fff] py-2 pl-4 rounded-sm">
        <p class="flex items-center">
          <Icon
            icon="mynaui:arrow-up-down"
            class="icon pr-1 h-6 w-6"
            color="#014099"
          />OA待办任务列表
        </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: '#e9f7ff',
            color: 'black',
            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="requestName"
            label="&#35831;&#27714;&#26631;&#39064;"
            min-width="250"
            fixed="left"
            align="center"
          />
          <el-table-column
            prop="workflowBaseInfo.workflowTypeName"
            label="&#27969;&#31243;&#31867;&#22411;"
            width="140"
            align="center"
          />
          <el-table-column
            prop="requestLevel"
            label="&#32039;&#24613;&#31243;&#24230;"
            width="100"
            align="center"
          />
          <el-table-column
            prop="status"
            label="&#36335;&#24452;&#29366;&#24577;"
            width="100"
            align="center"
          />
          <el-table-column
            prop="sysName"
            label="&#31995;&#32479;&#21517;&#31216;"
            width="140"
            align="center"
          />
          <el-table-column
            prop="createTime"
            label="&#21019;&#24314;&#26102;&#38388;"
            width="180"
            align="center"
          />

          <el-table-column
            prop="creatorName"
            label="&#21019;&#24314;&#20154;&#21517;&#31216;"
            width="120"
            align="center"
          />

          <el-table-column
            prop="currentNodeName"
            label="&#24403;&#21069;&#33410;&#28857;&#21517;&#31216;"
            width="140"
            align="center"
          />
          <el-table-column
            prop="lastOperateTime"
            label="&#26368;&#21518;&#25805;&#20316;&#26102;&#38388;"
            width="180"
            align="center"
          />

          <el-table-column
            prop="lastOperatorName"
            label="&#26368;&#21518;&#25805;&#20316;&#20154;&#21517;&#31216;"
            width="140"
            align="center"
          />
          <el-table-column
            prop="receiveTime"
            label="&#25509;&#25910;&#26102;&#38388;"
            width="180"
            align="center"
          />

          <el-table-column
            prop="workflowBaseInfo.workflowName"
            label="&#27969;&#31243;&#21517;&#31216;"
            min-width="200"
            align="center"
          />

          <el-table-column
            label="&#25805;&#20316;"
            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 { getOATasks, ssoLogin } 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 goBackPage = async (row) => {
  const res = await ssoLogin({
    username: userStore.getUser.username,
  });

  if (res) {
    const ua = window.navigator.userAgent.toLowerCase();
    if (ua.includes("dingtalk") || ua.includes("dingtalkwork")) {
      dd.biz.util.openLink({
        url:
          "https://yfoa.keruioil.com/wui/index.html" +
          "?ssoToken=" +
          res +
          "#/main", // 先跳你的 SSO 链接
        onSuccess: () => {
          // 延迟跳目标业务地址（和你原来 setTimeout 逻辑一致）
          setTimeout(() => {
            dd.biz.util.openLink({
              url: `https://yfoa.keruioil.com/spa/workflow/static4form/index.html?_rdm=1776063595284#/main/workflow/req?requestid=${row.requestId}`,
            });
          }, 100);
        },
      });
    } else {
      const loading = ElLoading.service({
        lock: true,
        text: "正在跳转，请稍候...",
        background: "rgba(0, 0, 0, 0.7)",
      });
      const newTab = window.open("", "_blank");
      newTab.location.href =
        "https://yfoa.keruioil.com/wui/index.html" +
        "?ssoToken=" +
        res +
        "#/main";

      setTimeout(function () {
        newTab.location.href = `https://yfoa.keruioil.com/spa/workflow/static4form/index.html?_rdm=1776063595284#/main/workflow/req?requestid=${row.requestId}`;
        setTimeout(() => {
          loading.close();
        }, 500);
      }, 100);
    }
  }
};

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

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

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

onMounted(async () => {
  if (userStore.getUser.username) {
    loading.value = true;
    try {
      const res = await getOATasks({
        id: userStore.getUser.username,
        pageNum: pagination.value.pageNum,
        pageSize: pagination.value.pageSize,
      });
      oaTasks.value = res.todoList;
      pagination.value.total = Number(res.todoCount);
    } 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;
}
</style>
