<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="6">
        <el-card @click.native="navigateToMes('unfinishedProjects',counts)" class="card-item">
          <div class="card-title">我参与的未结物料</div>
          <div class="card-count">{{ counts.unfinishedProjectsCount }}</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card @click.native="navigateToMes('pendingUploads',counts)" class="card-item">
          <div class="card-title">待上传文件</div>
          <div class="card-count">{{ counts.pendingUploadsCount }}</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card @click.native="navigateToMes('pendingConfirmations',counts)" class="card-item">
          <div class="card-title">待生产确认的项目</div>
          <div class="card-count">{{ counts.pendingConfirmationsCount }}</div>
        </el-card>
      </el-col>
    </el-row>
    <el-row :gutter="20" style="margin-top: 10px;">
      <el-col :span="6">
        <el-card @click.native="navigateToMes('allProjects',counts)" class="card-item">
          <div class="card-title">我参与的所有物料</div>
          <div class="card-count">{{ counts.allProjectsCount }}</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card @click.native="navigateToMes('overdueUploads',counts)" class="card-item">
          <div class="card-title">逾期的待上传文件</div>
          <div class="card-count">{{ counts.overdueUploadsCount }}</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card @click.native="navigateToMes('overdueConfirmations',counts)" class="card-item">
          <div class="card-title">逾期的待生产确认的项目</div>
          <div class="card-count">{{ counts.overdueConfirmationsCount }}</div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { agencyMatter } from "../../../api/eam/eamProject";

export default {
  data() {
    return {
      counts: {
        unfinishedProjectsCount: 0,
        allProjectsCount: 0,
        pendingUploadsCount: 0,
        overdueUploadsCount: 0,
        pendingConfirmationsCount: 0,
        overdueConfirmationsCount: 0,
        unfinishedProjects: [],
        allProjects: [],
        pendingUploads: [],
        overdueUploads: [],
        pendingConfirmations: [],
        overdueConfirmations: [],
      },
      intervalId: null,
    };
  },
  created() {
    this.fetchCounts();
    this.startAutoRefresh();
  },
  beforeDestroy() {
    this.stopAutoRefresh();
  },
  methods: {
    fetchCounts() {
      agencyMatter()
        .then(({ data }) => {
          this.counts = data;
          this.counts.unfinishedProjectsCount = this.counts.unfinishedProjects.length;
          this.counts.allProjectsCount = this.counts.allProjects.length;
          this.counts.pendingUploadsCount = this.counts.pendingUploads.length;
          this.counts.overdueUploadsCount = this.counts.overdueUploads.length;
          this.counts.pendingConfirmationsCount = this.counts.pendingConfirmations.length;
          this.counts.overdueConfirmationsCount = this.counts.overdueConfirmations.length;
        })
        .catch((error) => {
          console.error("Error fetching project counts:", error);
        });
    },
    navigateToMes(path, row) {
      let projectNos = '';
      let testPartNos = '';
      if (path === 'unfinishedProjects') {
        for (let i = 0; i < row.unfinishedProjects.length; i++) {
          // 使用逗号拼接 unfinishedProjects.projectNo，如果是最后一个则不加逗号
          projectNos += row.unfinishedProjects[i].projectNo + (i === row.unfinishedProjects.length - 1 ? '' : ',');
          testPartNos += row.unfinishedProjects[i].testPartNo + (i === row.unfinishedProjects.length - 1 ? '' : ',');
        }
        this.$router.push({ path: 'eam-eamProjectPartInfo', query: { projectNo: projectNos,testPartNo: testPartNos} });
      }
      else if (path === 'allProjects') {
        for (let i = 0; i < row.allProjects.length; i++) {
          projectNos += row.allProjects[i].projectNo + (i === row.allProjects.length - 1 ? '' : ',');
          testPartNos += row.allProjects[i].testPartNo + (i === row.allProjects.length - 1 ? '' : ',');
        }
        this.$router.push({ path: 'eam-eamProjectPartInfo', query: { projectNo: projectNos,testPartNo: testPartNos } });
      }
      else if (path === 'pendingUploads') {
        for (let i = 0; i < row.pendingUploads.length; i++) {
          projectNos += row.pendingUploads[i].projectNo + (i === row.pendingUploads.length - 1 ? '' : ',');
          testPartNos += row.pendingUploads[i].testPartNo + (i === row.pendingUploads.length - 1 ? '' : ',');
        }
        this.$router.push({ name: 'eam-eamProjectInfoForUploads', query: { projectNo: projectNos,flag:'1',testPartNo: testPartNos } });
      }
      else if (path === 'overdueUploads') {
        for (let i = 0; i < row.overdueUploads.length; i++) {
          projectNos += row.overdueUploads[i].projectNo + (i === row.overdueUploads.length - 1 ? '' : ',');
          testPartNos += row.overdueUploads[i].testPartNo + (i === row.overdueUploads.length - 1 ? '' : ',');
        }
        this.$router.push({ path: 'eam-eamProjectInfoForUploads', query: { projectNo: projectNos,flag:'2',testPartNo: testPartNos } });
      }
      else if (path === 'pendingConfirmations') {
        for (let i = 0; i < row.pendingConfirmations.length; i++) {
          projectNos += row.pendingConfirmations[i].projectNo + (i === row.pendingConfirmations.length - 1 ? '' : ',');
          testPartNos += row.pendingConfirmations[i].testPartNo + (i === row.pendingConfirmations.length - 1 ? '' : ',');
        }
        this.$router.push({ path: 'eam-eamProjectInfoForConfirm', query: { projectNo: projectNos,flag:'3',testPartNo: testPartNos } });
      }
      else if (path === 'overdueConfirmations') {
        for (let i = 0; i < row.overdueConfirmations.length; i++) {
          projectNos += row.overdueConfirmations[i].projectNo + (i === row.overdueConfirmations.length - 1 ? '' : ',');
          testPartNos += row.overdueConfirmations[i].testPartNo + (i === row.overdueConfirmations.length - 1 ? '' : ',');
        }
        this.$router.push({ path: 'eam-eamProjectInfoForConfirm', query: { projectNo: projectNos,flag:'4',testPartNo: testPartNos } });
      }
    },
    startAutoRefresh() {
      this.intervalId = setInterval(() => {
        this.fetchCounts();
      }, 20000); // 每20秒刷新一次
    },
    stopAutoRefresh() {
      if (this.intervalId) {
        clearInterval(this.intervalId);
      }
    },
  },
};
</script>

<style scoped>
.card-item {
  cursor: pointer;
  margin-bottom: 20px;
  transition: transform 0.3s, box-shadow 0.3s;
  overflow: hidden;
  //padding: 10px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  height: 208px;
  width: 230px;
  margin-left: 40px;
  margin-top: 10px;
}

.card-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.card-title {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 10px;
  margin-top: 20px;
}

.card-count {
  font-size: 55px;
  color: #17B3A3;
  text-align: center;
  font-weight: bold;
  margin-top: 50px;
}

.project-list {
  font-size: 12px;
  color: #333;
  line-height: 1.5;
  margin-bottom: 5px;
}

.el-divider {
  margin: 10px 0;
}
</style>
