<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-collapse v-model="activeCollapse">
                <!-- 1. 项目基本信息 -->
                <el-collapse-item title="项目基本信息" name="项目基本信息">
                  <el-row :gutter="20">
                    <el-col :span="8">
                      <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.id"
                            :label="item.projectName"
                            :value="item.id"
                          ></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="项目编号">
                        <el-input
                          v-model="formData.projectCode"
                          placeholder="请输入项目编号"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="是否再决策">
                        <CommonSelector
                          :radio="true"
                          v-model="formData.isReDecision"
                          dictName="sf"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="项目外文名称">
                        <el-input
                          v-model="formData.projectForeignName"
                          placeholder="请输入项目外文名称"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="是否能建联合体项目">
                        <CommonSelector
                          :radio="true"
                          v-model="formData.isUnionProject"
                          dictName="sf"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="投资主体">
                        <el-input
                          v-model="formData.investmentSubject"
                          placeholder="请输入投资主体"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="是否一体化子项目">
                        <CommonSelector
                          :radio="true"
                          v-model="formData.isIntegratedSubProject"
                          dictName="sf"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="直属企业">
                        <el-input
                          v-model="formData.directEnterprise"
                          placeholder="请输入直属企业"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="是否能建重大或重要投资项目">
                        <CommonSelector
                          :radio="true"
                          v-model="formData.isImportantInvestProject"
                          dictName="sf"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="决策审批单位">
                        <el-input
                          v-model="formData.approvalUnit"
                          placeholder="请输入决策审批单位"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="财务报表类型">
                        <el-input
                          v-model="formData.financialReportType"
                          placeholder="请输入财务报表类型"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="政府(核准、备案)审批级别">
                        <el-input
                          v-model="formData.governmentApprovalLevel"
                          placeholder="请输入审批级别"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="是否自主决策">
                        <CommonSelector
                          :radio="true"
                          v-model="formData.isIndependentDecision"
                          dictName="sf"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="主业/非主业">
                        <el-select
                          v-model="formData.businessType"
                          placeholder="请选择"
                        >
                          <el-option label="主业" value="主业"></el-option>
                          <el-option label="非主业" value="非主业"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="联系人">
                        <el-input
                          v-model="formData.contactPerson"
                          placeholder="请输入联系人"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="联系电话">
                        <el-input
                          v-model="formData.contactPhone"
                          placeholder="请输入联系电话"
                        />
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-collapse-item>

                <!-- 2. 项目所在地 -->
                <el-collapse-item title="项目所在地" name="项目所在地">
                  <el-row :gutter="20">
                    <el-col :span="8">
                      <el-form-item label="境内/外">
                        <el-select
                          v-model="formData.domesticOverseas"
                          placeholder="请选择"
                        >
                          <el-option label="境内" value="境内"></el-option>
                          <el-option label="境外" value="境外"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="省(境内)/洲际(境外)">
                        <el-input
                          v-model="formData.provinceContinent"
                          placeholder="请输入"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="地市（境内）/国家（境外）">
                        <el-input
                          v-model="formData.cityCountry"
                          placeholder="请输入"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="区县(境内)/国家(境外)">
                        <el-input
                          v-model="formData.districtCountry"
                          placeholder="请输入"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="项目区域">
                        <el-input
                          v-model="formData.projectArea"
                          placeholder="请输入项目区域"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="是否一带一路沿线国家">
                        <CommonSelector
                          :radio="true"
                          v-model="formData.isBeltRoadCountry"
                          dictName="sf"
                        />
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-collapse-item>

                <!-- 3. 计量币种 -->
                <el-collapse-item title="计量币种" name="计量币种">
                  <el-row :gutter="20">
                    <el-col :span="8">
                      <el-form-item label="币种">
                        <el-select
                          v-model="formData.currencyType"
                          placeholder="请选择币种"
                        >
                          <el-option label="人民币" value="CNY"></el-option>
                          <el-option label="美元" value="USD"></el-option>
                          <el-option label="欧元" value="EUR"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="汇率">
                        <el-input
                          v-model="formData.exchangeRate"
                          placeholder="请输入汇率"
                          :precision="6"
                        />
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-collapse-item>

                <!-- 4. 实施时间 -->
                <el-collapse-item title="实施时间" name="实施时间">
                  <el-row :gutter="20">
                    <el-col :span="8">
                      <el-form-item label="项目建设期 (月)">
                        <el-input
                          v-model="formData.constructionPeriod"
                          placeholder="请输入"
                          type="number"
                          min="0"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="运营年限 (年)">
                        <el-input
                          v-model="formData.operationYears"
                          placeholder="请输入"
                          type="number"
                          min="0"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="项目合作期 (年)">
                        <el-input
                          v-model="formData.cooperationPeriod"
                          placeholder="请输入"
                          type="number"
                          min="0"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="项目开工时间(预计)">
                        <el-date-picker
                          v-model="formData.plannedStartDate"
                          type="date"
                          placeholder="请选择"
                          value-format="YYYY-MM-DD"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="项目交工时间(预计)">
                        <el-date-picker
                          v-model="formData.plannedCompletionDate"
                          type="date"
                          placeholder="请选择"
                          value-format="YYYY-MM-DD"
                        />
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-collapse-item>

                <!-- 5. 投资分类 -->
                <el-collapse-item title="投资分类" name="投资分类">
                  <el-row :gutter="20">
                    <el-col :span="24">
                      <el-tabs v-model="activeInvestTab">
                        <el-tab-pane label="投资属性分类" name="attribute">
                          <el-row :gutter="20">
                            <el-col :span="8">
                              <el-form-item label="一级分类">
                                <el-input
                                  v-model="formData.investAttributeFirst"
                                  placeholder="请输入"
                                />
                              </el-form-item>
                            </el-col>
                            <el-col :span="8">
                              <el-form-item label="二级分类">
                                <el-input
                                  v-model="formData.investAttributeSecond"
                                  placeholder="请输入"
                                />
                              </el-form-item>
                            </el-col>
                            <el-col :span="8">
                              <el-form-item label="投资板块">
                                <el-input
                                  v-model="formData.investPlate1"
                                  placeholder="请输入"
                                />
                              </el-form-item>
                            </el-col>
                            <el-col :span="8">
                              <el-form-item label="投资板块">
                                <el-input
                                  v-model="formData.investPlate2"
                                  placeholder="请输入"
                                />
                              </el-form-item>
                            </el-col>
                          </el-row>
                        </el-tab-pane>
                        <el-tab-pane label="投资目的分类" name="purpose">
                          <el-row :gutter="20">
                            <el-col :span="8">
                              <el-form-item label="一级分类">
                                <el-input
                                  v-model="formData.investPurposeFirst"
                                  placeholder="请输入"
                                />
                              </el-form-item>
                            </el-col>
                            <el-col :span="8">
                              <el-form-item label="2+9布局分类">
                                <el-input
                                  v-model="formData.layout29"
                                  placeholder="请输入"
                                />
                              </el-form-item>
                            </el-col>
                            <el-col :span="8">
                              <el-form-item label="二级分类">
                                <el-input
                                  v-model="formData.investPurposeSecond"
                                  placeholder="请输入"
                                />
                              </el-form-item>
                            </el-col>
                            <el-col :span="8">
                              <el-form-item label="国民经济行业">
                                <el-input
                                  v-model="formData.nationalEconomicIndustry"
                                  placeholder="请输入"
                                />
                              </el-form-item>
                            </el-col>
                            <el-col :span="8">
                              <el-form-item label="四新分类">
                                <el-input
                                  v-model="formData.fourNewClassification"
                                  placeholder="请输入"
                                />
                              </el-form-item>
                            </el-col>
                          </el-row>
                        </el-tab-pane>
                      </el-tabs>
                    </el-col>
                  </el-row>
                </el-collapse-item>

                <!-- 6. 特别关注、特别监管类等信息 -->
                <el-collapse-item
                  title="特别关注、特别监管类等信息"
                  name="特别关注、特别监管类等信息"
                >
                  <el-row :gutter="20">
                    <el-col :span="8">
                      <el-form-item label="是否须报国家发展改革委和商务部核准">
                        <CommonSelector
                          :radio="true"
                          v-model="formData.isReportedToNDRC"
                          dictName="sf"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="是否国家领导见签">
                        <CommonSelector
                          :radio="true"
                          v-model="formData.isLeaderSigned"
                          dictName="sf"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="是否境内特别监管类">
                        <CommonSelector
                          :radio="true"
                          v-model="formData.isDomesticSpecialSupervision"
                          dictName="sf"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="是否列入国资委负面清单">
                        <CommonSelector
                          :radio="true"
                          v-model="formData.isInSASACNegaticveList"
                          dictName="sf"
                        />
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-collapse-item>

                <!-- 7. 建设规模（实物量指标） -->
                <el-collapse-item
                  title="建设规模（实物量指标）"
                  name="建设规模（实物量指标）"
                >
                  <el-row :gutter="20">
                    <el-col :span="24">
                      <el-form-item label=" ">
                        <DynamicTable
                          :showAddButton="!isPreview"
                          :columns="constructionScaleColumns"
                          :disabled="isPreview"
                          v-model="formData.constructionScaleList"
                        />
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-collapse-item>

                <!-- 8. 股东信息 -->
                <el-collapse-item title="股东信息" name="股东信息">
                  <el-row :gutter="20">
                    <el-col :span="24">
                      <el-form-item label=" ">
                        <DynamicTable
                          :showAddButton="!isPreview"
                          :columns="shareholderColumns"
                          :disabled="isPreview"
                          v-model="formData.shareholderList"
                        />
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-collapse-item>

                <!-- 9. 我方担保及股权融资情况 -->
                <el-collapse-item
                  title="我方担保及股权融资情况"
                  name="我方担保及股权融资情况"
                >
                  <el-row :gutter="20">
                    <el-col :span="8">
                      <el-form-item label="我方负责的股权融资金额（万元）">
                        <el-input
                          v-model="formData.equityFinancingAmount"
                          placeholder="请输入"
                          type="number"
                          :precision="2"
                          min="0"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="融资成本（%）">
                        <el-input
                          v-model="formData.equityFinancingCost"
                          placeholder="请输入"
                          type="number"
                          :precision="2"
                          min="0"
                          max="100"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="期限（年）">
                        <el-input
                          v-model="formData.equityFinancingTerm"
                          placeholder="请输入"
                          type="number"
                          min="0"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="担保金额（万元）">
                        <el-input
                          v-model="formData.guaranteeAmount"
                          placeholder="请输入"
                          type="number"
                          :precision="2"
                          min="0"
                        />
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-collapse-item>

                <!-- 10. 我方担保及债权融资情况 -->
                <el-collapse-item
                  title="我方担保及债权融资情况"
                  name="我方担保及债权融资情况"
                >
                  <el-row :gutter="20">
                    <el-col :span="24">
                      <el-form-item label=" ">
                        <DynamicTable
                          :showAddButton="!isPreview"
                          :columns="debtFinancingColumns"
                          :disabled="isPreview"
                          v-model="formData.debtFinancingList"
                        />
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-collapse-item>

                <!-- 11. 项目内容及必要性 -->
                <el-collapse-item
                  title="项目内容及必要性"
                  name="项目内容及必要性"
                >
                  <el-row :gutter="20">
                    <el-col :span="24">
                      <el-form-item label="建设内容及规模">
                        <el-input
                          v-model="formData.constructionContent"
                          type="textarea"
                          rows="4"
                          placeholder="请输入"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item label="项目必要性">
                        <el-input
                          v-model="formData.projectNecessity"
                          type="textarea"
                          rows="4"
                          placeholder="请输入"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item label="其他说明">
                        <el-input
                          v-model="formData.otherDescription"
                          type="textarea"
                          rows="4"
                          placeholder="请输入"
                        />
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-collapse-item>

                <!-- 12. 风险管理 -->
                <el-collapse-item title="风险管理" name="风险管理">
                  <el-row :gutter="20">
                    <el-col :span="24">
                      <el-form-item label=" ">
                        <DynamicTable
                          :showAddButton="!isPreview"
                          :columns="riskManagementColumns"
                          :disabled="isPreview"
                          v-model="formData.riskManagementList"
                        />
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-collapse-item>

                <!-- 13. 里程碑节点 -->
                <el-collapse-item title="里程碑节点" name="里程碑节点">
                  <el-row :gutter="20">
                    <el-col :span="24">
                      <el-form-item label=" ">
                        <DynamicTable
                          :showAddButton="!isPreview"
                          :columns="milestoneColumns"
                          :disabled="isPreview"
                          v-model="formData.milestoneList"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="24" style="margin-top: 20px">
                      <el-form-item label="里程碑计划审批表">
                        <FileUploader
                          v-model="formData.milestoneApprovalFile"
                        />
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-collapse-item>

                <!-- 14. 投决申报材料 -->
                <el-collapse-item title="投决申报材料" name="投决申报材料">
                  <el-row :gutter="20">
                    <el-col :span="24">
                      <el-form-item label="可研材料">
                        <FileUploader v-model="formData.feasibilityStudyFile" />
                      </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item label="经济指标初步测算资料">
                        <FileUploader
                          v-model="formData.economicIndicatorFile"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item label="补充相关支撑材料">
                        <FileUploader
                          v-model="formData.supplementarySupportFile"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item label="其他相关支撑材料">
                        <FileUploader v-model="formData.otherSupportFile" />
                      </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item label="上会议案">
                        <FileUploader v-model="formData.meetingProposalFile" />
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-collapse-item>
              </el-collapse>
            </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 DynamicTable from "@/components/FormDynamicTable/index.vue";
import FileUploader from "@/components/FileUploader/index.vue";

// ========== 动态表格列配置 ==========
// 7. 建设规模列配置
const constructionScaleColumns = ref([
  {
    prop: "indicatorName",
    label: "指标名称",
    type: "input",
    placeholder: "请输入指标名称",
  },
  {
    prop: "quantity",
    label: "数量",
    type: "input",
    placeholder: "请输入数量",
    typeParams: { type: "number", min: 0 },
  },
  { prop: "unit", label: "单位", type: "input", placeholder: "请输入单位" },
  {
    prop: "supplement",
    label: "补充说明",
    type: "input",
    placeholder: "请输入补充说明",
  },
]);

// 8. 股东信息列配置
const shareholderColumns = ref([
  {
    prop: "shareholderType",
    label: "股东类型",
    type: "input",
    placeholder: "请输入股东类型",
  },
  {
    prop: "shareholderName",
    label: "股东名称",
    type: "input",
    placeholder: "请输入股东名称",
  },
  {
    prop: "shareholdingRatio",
    label: "持股比例（%）",
    type: "input",
    placeholder: "请输入",
    typeParams: { type: "number", min: 0, max: 100, precision: 2 },
  },
  {
    prop: "contributionAmount",
    label: "出资额(万元)",
    type: "input",
    placeholder: "请输入",
    typeParams: { type: "number", min: 0, precision: 2 },
  },
  {
    prop: "registeredCapital",
    label: "应缴注册资本(万元)",
    type: "input",
    placeholder: "请输入",
    typeParams: { type: "number", min: 0, precision: 2 },
  },
  {
    prop: "projectCapital",
    label: "应出项目资本金(万元)",
    type: "input",
    placeholder: "请输入",
    typeParams: { type: "number", min: 0, precision: 2 },
  },
  {
    prop: "cooperatorSituation",
    label: "合作方情况",
    type: "input",
    placeholder: "请输入",
  },
  { prop: "remark", label: "备注", type: "input", placeholder: "请输入备注" },
]);

// 10. 债权融资列配置
const debtFinancingColumns = ref([
  {
    prop: "amount",
    label: "金额(万元)",
    type: "input",
    placeholder: "请输入",
    typeParams: { type: "number", min: 0, precision: 2 },
  },
  {
    prop: "financingCost",
    label: "融资成本(%)",
    type: "input",
    placeholder: "请输入",
    typeParams: { type: "number", min: 0, max: 100, precision: 2 },
  },
  {
    prop: "term",
    label: "期限(年)",
    type: "input",
    placeholder: "请输入",
    typeParams: { type: "number", min: 0 },
  },
  {
    prop: "expectedLandingTime",
    label: "预计落地时间",
    type: "date",
    placeholder: "请选择",
    typeParams: { valueFormat: "YYYY-MM-DD" },
  },
]);

// 12. 风险管理列配置
const riskManagementColumns = ref([
  {
    prop: "riskType",
    label: "风险类别",
    type: "input",
    placeholder: "请输入风险类别",
  },
  {
    prop: "riskDescription",
    label: "风险描述",
    type: "input",
    placeholder: "请输入风险描述",
  },
  {
    prop: "responseMeasures",
    label: "应对措施",
    type: "input",
    placeholder: "请输入应对措施",
  },
]);

// 13. 里程碑节点列配置
const milestoneColumns = ref([
  {
    prop: "milestoneName",
    label: "里程碑节点名称",
    type: "input",
    placeholder: "请输入节点名称",
  },
  {
    prop: "completionDescription",
    label: "节点完成描述",
    type: "input",
    placeholder: "请输入完成描述",
  },
  {
    prop: "expectedCompletionTime",
    label: "预计完成时间",
    type: "date",
    placeholder: "请选择",
    typeParams: { valueFormat: "YYYY-MM-DD" },
  },
]);

// ========== 路由/实例/全局方法 ==========
const router = useRouter();
const route = useRoute();
const { proxy } = getCurrentInstance();

// ========== 基础配置 ==========
const activeCollapse = ref([
  "项目基本信息",
  "项目所在地",
  "计量币种",
  "实施时间",
  "投资分类",
  "特别关注、特别监管类等信息",
  "建设规模（实物量指标）",
  "股东信息",
  "我方担保及股权融资情况",
  "我方担保及债权融资情况",
  "项目内容及必要性",
  "风险管理",
  "里程碑节点",
  "投决申报材料",
]);
const activeInvestTab = ref("attribute"); // 投资分类默认选中属性分类
const loading = ref(false);
const isPreview = ref(!!route.query.isPreview);
const projectList = ref([]);
const rcCgqyglId = ref(route.query.id || "");

// ========== 表单数据 ==========
const formData = reactive({
  // 1. 项目基本信息
  projectId: "",
  projectCode: "",
  isReDecision: "",
  projectForeignName: "",
  isUnionProject: "",
  investmentSubject: "",
  isIntegratedSubProject: "",
  directEnterprise: "",
  isImportantInvestProject: "",
  approvalUnit: "",
  financialReportType: "",
  governmentApprovalLevel: "",
  isIndependentDecision: "",
  businessType: "",
  contactPerson: "",
  contactPhone: "",

  // 2. 项目所在地
  domesticOverseas: "",
  provinceContinent: "",
  cityCountry: "",
  districtCountry: "",
  projectArea: "",
  isBeltRoadCountry: "",

  // 3. 计量币种
  currencyType: "",
  exchangeRate: "",

  // 4. 实施时间
  constructionPeriod: "",
  operationYears: "",
  cooperationPeriod: "",
  plannedStartDate: "",
  plannedCompletionDate: "",

  // 5. 投资分类
  investAttributeFirst: "",
  investAttributeSecond: "",
  investPlate1: "",
  investPlate2: "",
  investPurposeFirst: "",
  layout29: "",
  investPurposeSecond: "",
  nationalEconomicIndustry: "",
  fourNewClassification: "",

  // 6. 特别关注、特别监管类等信息
  isReportedToNDRC: "",
  isLeaderSigned: "",
  isDomesticSpecialSupervision: "",
  isInSASACNegativeList: "",

  // 7. 建设规模（实物量指标）- 动态表格
  constructionScaleList: [
    { indicatorName: "", quantity: "", unit: "", supplement: "" },
  ],

  // 8. 股东信息 - 动态表格
  shareholderList: [
    {
      shareholderType: "",
      shareholderName: "",
      shareholdingRatio: "",
      contributionAmount: "",
      registeredCapital: "",
      projectCapital: "",
      cooperatorSituation: "",
      remark: "",
    },
  ],

  // 9. 我方担保及股权融资情况
  equityFinancingAmount: "",
  equityFinancingCost: "",
  equityFinancingTerm: "",
  guaranteeAmount: "",

  // 10. 我方担保及债权融资情况 - 动态表格
  debtFinancingList: [
    { amount: "", financingCost: "", term: "", expectedLandingTime: "" },
  ],

  // 11. 项目内容及必要性
  constructionContent: "",
  projectNecessity: "",
  otherDescription: "",

  // 12. 风险管理 - 动态表格
  riskManagementList: [
    { riskType: "", riskDescription: "", responseMeasures: "" },
  ],

  // 13. 里程碑节点 - 动态表格 + 文件
  milestoneList: [
    {
      milestoneName: "",
      completionDescription: "",
      expectedCompletionTime: "",
    },
  ],
  milestoneApprovalFile: "",

  // 14. 投决申报材料 - 文件上传
  feasibilityStudyFile: "",
  economicIndicatorFile: "",
  supplementarySupportFile: "",
  otherSupportFile: "",
  meetingProposalFile: "",

  // 其他关联字段
  projectName: "",
});

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

// ========== 选择项目同步名称及相关信息 ==========
const changeProject = (val) => {
  proxy.$post({
    url: "/api/project/getProjectFinalInfo",
    data: { id: val },
    callback: (data) => {
      loading.value = false;
      // 回填基础字段（可根据实际接口返回扩展）
      formData.projectName = data.projectName || "";
      formData.projectCode = data.projectCode || "";
      formData.investmentSubject = data.investmentSubject || "";
    },
    error: () => {
      loading.value = false;
      ElMessage.error("获取项目详情失败");
    },
  });
  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/getTzjh",
    data: { id: rcCgqyglId.value },
    callback: async (data) => {
      loading.value = false;
      if (!data) return ElMessage.error("未查询到数据");
      Object.assign(formData, data);
      // 处理动态表格数据默认值
      formData.constructionScaleList = data.constructionScaleList || [
        { indicatorName: "", quantity: "", unit: "", supplement: "" },
      ];
      formData.shareholderList = data.shareholderList || [
        {
          shareholderType: "",
          shareholderName: "",
          shareholdingRatio: "",
          contributionAmount: "",
          registeredCapital: "",
          projectCapital: "",
          cooperatorSituation: "",
          remark: "",
        },
      ];
      formData.debtFinancingList = data.debtFinancingList || [
        { amount: "", financingCost: "", term: "", expectedLandingTime: "" },
      ];
      formData.riskManagementList = data.riskManagementList || [
        { riskType: "", riskDescription: "", responseMeasures: "" },
      ];
      formData.milestoneList = data.milestoneList || [
        {
          milestoneName: "",
          completionDescription: "",
          expectedCompletionTime: "",
        },
      ];
    },
    error: () => {
      loading.value = false;
      ElMessage.error("获取投资计划详情失败");
    },
  });
};

// ========== 保存表单 ==========
const saveClick = () => {
  if (!formData.projectId) return ElMessage.warning("请选择项目信息");
  loading.value = true;
  const url = rcCgqyglId.value
    ? "/api/project/updateTzjh"
    : "/api/project/createTzjh";

  // 组装提交数据
  const submitData = {
    ...formData,
    projectId: String(formData.projectId),
  };

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

// ========== 辅助方法 ==========
const backClick = () => router.back(-1);

onMounted(() => {
  getProjectData();
  if (rcCgqyglId.value) {
    // 编辑/预览：拉取接口数据
    getJsqtzjcDetail();
  }
});
</script>

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

// 调整折叠面板和表格样式
:deep(.el-collapse-item__header) {
  font-weight: 600;
}
:deep(.el-table) {
  --el-table-header-text-color: #333;
  --el-table-row-hover-bg-color: #f8f9fa;
}
// 文件上传组件间距
:deep(.el-form-item) {
  margin-bottom: 20px;
}
</style>
