<!--
 * @Author: ysh
 * @Date: 2024-04-18 15:14:45
 * @LastEditors: wrh
 * @LastEditTime: 2025-11-12 10:24:23
-->
<template>
  <div>
    <el-scrollbar style="height:400px">
      <el-tree class="filter-tree" icon-class="el-icon-caret-right" :data="treeData" :props="defaultProps"
        @node-click="handleNodeClick" default-expand-all highlight-current ref="tree">
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <!-- <svg-icon icon-class="folder" class="svgIcon" style="margin-right:5px;" /> -->
          <svg-icon v-if="!data.children" icon-class="file" class="svgIcon" />
          <svg-icon v-else-if="node.expanded" icon-class="folder" class="svgIcon" />
          <svg-icon v-else icon-class="folder_close" class="svgIcon" />
          <span class="fontSize14" :title="node.label || '-'">{{ node.label }}</span>
        </span>
      </el-tree>
      <el-divider></el-divider>

    </el-scrollbar>
    <div class="text-center mt20">
      <el-button type="primary" @click="confirm">确定</el-button>
      <el-button @click="$emit('close')">取消</el-button>
    </div>

    <!-- 项目编号选择对话框 -->
    <el-dialog title="请选择将要发起预算的项目" :visible.sync="projectDialogVisible" width="30%" append-to-body>
      <el-form :model="projectForm" label-width="80px">
        <el-form-item>
          是否勘察项目：
          <el-radio-group v-model="isSurvey" @change="handleIsSurveyChange">
            <el-radio :label="0">否</el-radio>
            <el-radio :label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-select v-model="queryType" style="width: 120px;">
            <el-option label="项目编号" value="1"></el-option>
            <el-option label="项目名称" value="2"></el-option>
          </el-select>
          <el-select v-model="projectId" clearable filterable remote reserve-keyword placeholder="请输入关键字"
            :remote-method="remoteMethod" style="width: 300px;">
            <el-option v-for="project in projectList" :key="project.projectId"
              :label="project.projectNumber + '-' + project.projectName" :value="project.projectId">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="projectDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleProjectConfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProject, getProject } from '@/api/oa/project/project';
export default {
  props: {
    definitionList: {
      type: Array,
      require: true,
    },
  },
  watch: {
    definitionList(newval, oldval) {
      this.initTree();
    },
  },
  data() {
    return {
      treeData: [],
      defaultProps: {
        children: "children",
        label: "name",
      },
      clickNode: {},
      projectDialogVisible: false,
      projectForm: {
        projectId: ''
      },
      projectList: [], // 项目列表数据
      timeout: null,
      queryType: '1',
      projectId: '',
      isSurvey: 0
    };
  },
  mounted() { },
  methods: {
    initTree() {
      this.treeData = [
        {
          name: "经营类",
          children: [],
        },
        {
          name: "生产类",
          children: [],
        },
        {
          name: "财务类",
          children: [],
        },
        {
          name: "办公类",
          children: [],
        },
        {
          name: "考核类",
          children: [],
        },
      ];
      for (let i of this.definitionList) {
        if (i.category == "business") {
          this.treeData[0].children.push(i);
        }
        else if (i.category == "project") {
          if (i.name != "项目预算" && i.name != "安全交底" && i.name != "技术交底" && i.name != "技术方案" && i.name != "项目核算")
            this.treeData[1].children.push(i);
          if (i.name == "项目预算" && i.version == 2) {
            if (this.$store.getters.roles.includes('projectLeader')) {
              this.treeData[1].children.push(i);
            }
          }
          if (i.name == "项目核算") {
            if (this.$store.getters.roles.includes('nfinance')) {
              this.treeData[1].children.push(i);
            }
          }
        } else if (i.category == "finance") {
          if (i.name != "借款审批")
            this.treeData[2].children.push(i);
          if (i.name == "借款审批" && i.version == 2)
            this.treeData[2].children.push(i);
        } else if (i.category == "oa") {
          this.treeData[3].children.push(i);
        } else if (i.category == "assess") {
          this.treeData[4].children.push(i);
        }
      }
    },
    handleNodeClick(data) {
      this.clickNode = data
    },
    confirm() {
      if (this.clickNode.name == "项目预算") {
        this.projectDialogVisible = true;
      } else {
        this.$emit('confirm', this.clickNode)
      }
    },
    remoteMethod(val) {
      let params1 = {
        pageNum: 1,
        pageSize: 20,
        projectNumber: val,
        undertakingDept: ''
      }
      let params2 = {
        pageNum: 1,
        pageSize: 20,
        projectName: val,
        undertakingDept: ''
      }
      if (this.isSurvey == 1) {
        params1.undertakingDept = '113'
        params2.undertakingDept = '113'
      } else {
        params1.undertakingDept = ''
        params2.undertakingDept = ''
      }
      let params = {};
      if (this.queryType == '1') {
        params = params1
      } else {
        params = params2
      }
      listProject(params).then(res => {
        this.projectList = res.rows;
      })
    },
    async handleProjectConfirm() {
      if (!this.projectId) {
        this.$message.warning('请选择项目');
        return;
      }
      const selectedData = {
        ...this.clickNode,
        projectId: this.projectId,
      };
      let res = await getProject(this.projectId);
      let prijectLeader = res.data.projectLeader;
      if (prijectLeader != this.$store.getters.userId) {
        this.$message.warning('抱歉，您不是该项目的项目负责人，无法发起该预算');
        return;
      }
      this.$emit('confirm', selectedData);
      this.projectDialogVisible = false;
    },
    handleIsSurveyChange(val) {
      this.isSurvey = val;
    }
  },
};
</script>

<style lang="scss" scoped>
.svgIcon {
  margin-right: 5px;
}
</style>
