<template>
  <div class="add-project-container">
    <div class="add-project-content" v-loading="loading">
      <div class="add-project-header">
        <div class="header-left"></div>
        <div class="header-right">
          <el-button type="default" @click="backClick">返回</el-button>
          <template v-if="!loading && !isPreview">
            <el-button type="primary" @click="saveClick">保存</el-button>
          </template>
        </div>
      </div>
      <div class="tabs-content">
        <div class="project-tab-content">
          <div class="tab-content">
            <el-form :model="formData" :label-width="200" :disabled="isPreview">
              <!-- 项目信息选择 -->
              <el-row :gutter="20" style="margin-bottom: 20px">
                <el-col :span="12">
                  <el-form-item label="项目信息" required>
                    <el-select
                      v-model="formData.projectId"
                      placeholder="请选择项目信息"
                      no-data-text="暂无数据"
                      @change="changeProject"
                    >
                      <el-option
                        v-for="item in projectList"
                        :key="item.key"
                        :label="item.projectName"
                        :value="item.id"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>

              <!-- 分类标签页 -->
              <el-tabs v-model="pageActiveName" type="border-card">
                <!-- 合同类 -->
                <el-tab-pane label="合同类" name="合同类">
                  <el-row :gutter="20">
                    <!-- 权益类合同 -->
                    <el-col :span="24">
                      <el-form-item label="权益类合同">
                        <FileUploader v-model="formData.qylht" />
                      </el-form-item>
                    </el-col>
                    <!-- 经济类合同 -->
                    <el-col :span="24">
                      <el-form-item label="经济类合同">
                        <FileUploader v-model="formData.jjlht" />
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-tab-pane>

                <!-- 造价类 -->
                <el-tab-pane label="造价类" name="造价类">
                  <el-row :gutter="20">
                    <!-- 造价文件 -->
                    <el-col :span="24">
                      <el-form-item label="造价文件">
                        <FileUploader v-model="formData.zjwj" />
                      </el-form-item>
                    </el-col>
                    <!-- 动用预备费审批文件 -->
                    <el-col :span="24">
                      <el-form-item label="动用预备费审批文件">
                        <FileUploader v-model="formData.ybfspwj" />
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-tab-pane>

                <!-- 变更索赔类 -->
                <el-tab-pane label="变更索赔类" name="变更索赔类">
                  <el-row :gutter="20">
                    <!-- 变更索赔文件 -->
                    <el-col :span="24">
                      <el-form-item label="变更索赔文件">
                        <FileUploader v-model="formData.bgsp" />
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-tab-pane>

                <!-- 工商出资类 -->
                <el-tab-pane label="工商出资类" name="工商出资类">
                  <el-row :gutter="20">
                    <!-- 股权结构 -->
                    <el-col :span="24">
                      <el-form-item
                        label="股权结构、资金来源及构成发生重大变化的报告及审批文件"
                      >
                        <FileUploader v-model="formData.gqjg" />
                      </el-form-item>
                    </el-col>
                    <!-- 接入财务金融部资本金出资台账，台账更新，系统上跟着更新 -->
                    <el-col :span="24">
                      <el-form-item
                        label="接入财务金融部资本金出资台账，台账更新，系统上跟着更新"
                      >
                        <FileUploader v-model="formData.cwjr" />
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-tab-pane>

                <!-- 其他类 -->
                <el-tab-pane label="其他类" name="其他类">
                  <el-row :gutter="20">
                    <!-- 其他文件 -->
                    <el-col :span="24">
                      <el-form-item label="其他文件">
                        <FileUploader v-model="formData.qt" />
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-tab-pane>
              </el-tabs>
            </el-form>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted, getCurrentInstance } from "vue";
import { useRouter, useRoute } from "vue-router";
import { ElMessage } from "element-plus";
import FileUploader from "../../components/FileUploader/index.vue";

// 初始化全局变量
const router = useRouter();
const route = useRoute();
const { proxy } = getCurrentInstance();

// 标签页默认激活项
const pageActiveName = ref("合同类");

// 表单数据 - 补充所有用到的字段
const formData = reactive({
  projectName: "",
  projectId: "",
  del: 0, // 删除标记，0为正常
  // 合同类
  qylht: "", // 权益类合同
  jjlht: "", // 经济类合同
  // 造价类
  zjwj: "", // 造价文件
  ybfspwj: "", // 动用预备费审批文件
  // 变更索赔类
  bgsp: "", // 变更索赔文件
  // 工商出资类
  gqjg: "", // 股权结构
  cwjr: "", // 资金来源及构成
  // 其他类
  qt: "", // 其他文件
});

// 加载状态
const loading = ref(false);
// 是否预览模式
const isPreview = ref(!!route.query.isPreview);
// 项目列表数据
const projectList = ref([]);
// 当前编辑的记录ID
const rcCgqyglId = ref(route.query.id || "");

// ========== 通用方法 ==========
// 获取项目列表
const getProjectData = () => {
  proxy.$post({
    url: "/api/project/listProject",
    data: {
      page: 1,
      pagesize: 1000,
      attributes: [],
      menuType: "xmjc",
    },
    callback: (data) => {
      projectList.value = data.rows || [];
    },
    errorCallback: () => {
      ElMessage.error("获取项目列表失败");
    },
  });
};

// 选择项目后同步项目名称
const changeProject = (val) => {
  const selectItem = projectList.value.find((item) => item.id === val);
  if (selectItem) {
    formData.projectName = selectItem.projectName;
  }
};

// 获取单条记录详情（编辑/预览）
const getJsqtzjcDetail = () => {
  if (!rcCgqyglId.value) return;

  loading.value = true;
  proxy.$post({
    url: "/api/project/getZdsxspInfo",
    data: { id: rcCgqyglId.value },
    callback: (data) => {
      loading.value = false;
      // 赋值表单数据
      Object.assign(formData, data);
    },
    errorCallback: () => {
      loading.value = false;
      ElMessage.error("网络异常，获取数据失败");
    },
  });
};

// 返回上一页
const backClick = () => {
  router.back(-1);
};

// 保存/提交表单
const saveClick = () => {
  // 基础校验
  if (!formData.projectId) {
    ElMessage.warning("请选择项目信息");
    return;
  }

  loading.value = true;
  // 区分新增/编辑
  const url = rcCgqyglId.value
    ? "/api/project/updateZdsxsp"
    : "/api/project/createZdsxsp";

  // 组装提交数据
  const submitData = {
    ...formData,
    projectId: formData.projectId + "", // 确保为字符串类型
  };

  proxy.$post({
    url: url,
    data: submitData,
    callback: () => {
      loading.value = false;
      ElMessage.success(rcCgqyglId.value ? "编辑成功" : "新增成功");
      router.back(-1);
    },
    errorCallback: () => {
      loading.value = false;
      ElMessage.error("保存失败，请重试");
    },
  });
};

// 页面初始化
onMounted(() => {
  // 获取项目列表
  getProjectData();
  if (rcCgqyglId.value) {
    getJsqtzjcDetail();
  }
});
</script>

<style scoped lang="less">
@import "@/styles/verticalManages.less";

// 标签页样式优化
:deep(.el-tabs--border-card) {
  --el-tabs-card-border-color: var(--el-border-color);
  --el-tabs-header-text-color: var(--el-text-color-primary);
  --el-tabs-active-text-color: var(--el-color-primary);
}

// 表单间距优化
.el-form-item {
  margin-bottom: 16px;
}

.el-row {
  margin-bottom: 8px;
}
</style>
