<!--
 * @Author: ysh
 * @Date: 2024-12-17 10:32:27
 * @LastEditors: wrh
 * @LastEditTime: 2026-01-08 14:56:17
-->
<template>
  <div>
    <div class="text-center header">
      项目列表
    </div>
    <el-table v-loading="loading" :data="projectList" height="220px">
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column label="项目编号" align="center" key="projectNumber" prop="projectNumber" />
      <el-table-column label="项目名称" align="center" key="projectName" prop="projectName" show-overflow-tooltip />
      <el-table-column label="承担部门" align="center" key="undertakingDeptName" prop="undertakingDeptName" />
      <el-table-column label="项目负责人" align="center" key="projectLeaderName" prop="projectLeaderUser.nickName"
        width="120px" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="rowClickFn(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-drawer :visible.sync="drawerOpen" title="" :size="'70%'" append-to-body @close="closeDrawerFn">
      <projectInfo :needReturn="false"></projectInfo>
    </el-drawer>
  </div>
</template>

<script>
import { listProject, getProject, listProjectFuzzy, submitProject, modifyProject, delProject } from "@/api/oa/project/project";
import projectInfo from '@/views/oa/project/info'
export default {
  components:{projectInfo},
  props: {
    projectId: {
      type: Array,
    },
    contractId: {
      type: String,
      default: ''
    }
  },
  watch: {
    projectId: {
      handler(newVal) {
        this.loading = true
        this.projectList = []
        for (let i of newVal) {
          this.getList(i)
        }
        this.total = this.projectList.length;
        this.loading = false
      },
      deep: true
    },
    contractId() {
      if (this.projectId.length == 0) {
        this.projectList = []
        this.loading = false
      }
    }
  },
  created() {
    if (this.projectId.length == 0) {
      this.loading = false
      this.projectList = []
    }
  },
  data() {
    return {
      loading: true,
      projectList: [],
      total: 0,
      drawerOpen: false,
    }
  },
  methods: {
    async getList(projectId) {
      let res = await getProject(projectId)
      if (res.data)
        this.projectList.push(res.data)
    },
    rowClickFn(row) {
      this.$router.replace({ query: { ...this.$route.query, projectId: row.projectId } });
      this.drawerOpen = true;
      // 找到当前tab并修改当前tab显示的标题
      let currentView = this.$store.state.tagsView.visitedViews.find(item => item.path === this.$route.path);
      if (currentView != null) {
        currentView.title = row.projectName;
      }
    },
    closeDrawerFn(){
      this.drawerOpen = false;
      // 只需要删除 query 中的 projectId参数，保留其他参数
      const { projectId, ...otherQuery } = this.$route.query;
      this.$router.replace({ 
        path: this.$route.path, 
        query: otherQuery 
      });
      // 找到当前tab并修改当前tab显示的标题为"合同统计"
      let currentView = this.$store.state.tagsView.visitedViews.find(item => item.path === this.$route.path);
      if (currentView != null) {
        currentView.title = '合同统计';
      }
    }
  },
}
</script>

<style lang="scss" scoped>
.header {
  font-family: 'puhuiti';
  font-size: 18px;
  line-height: 30px;
}
</style>