<template>
  <div class="add-project-container">
    <div class="add-project-content" v-loading="loading">
      <routerBack
        :show-save="!loading && !isPreview"
        :show-export="!loading"
        :export-disabled="!formData.projectName"
        :loading="loading"
        @save="saveClick"
        @export="exportStatement"
      />

      <div class="tabs-content">
        <el-tabs v-model="pageActiveName" type="border-card">
          <el-tab-pane label="投资目标责任书" name="投资目标责任书">
            <div class="project-tab-content">
              <div class="tab-content">
                <el-form
                  :model="formData"
                  :label-width="180"
                  :disabled="isPreview"
                >
                  <el-collapse v-model="activeCollapse">
                    <el-collapse-item
                      title="责任书基本信息"
                      name="责任书基本信息"
                    >
                      <el-row :gutter="24">
                        <el-col :span="12">
                          <el-form-item label="责任书类型">
                            <el-tree-select
                              v-model="formData.zrslx"
                              :data="zrslxList"
                              node-key="key"
                              :props="zrslxProps"
                              placeholder="请选择"
                              render-after-expand
                              @node-click="changeZrslx"
                            >
                              <template #label>
                                <span>{{ selectedFullPath }}</span>
                              </template>
                            </el-tree-select>
                          </el-form-item>
                        </el-col>
                        <el-col :span="12">
                          <el-form-item label="项目信息" required>
                            <el-select
                              v-model="formData.projectId"
                              :disabled="projectDisabled"
                              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="12">
                          <el-form-item label="项目名称">
                            <el-input
                              v-model="formData.projectName"
                              :disabled="projectDisabled"
                            />
                          </el-form-item>
                        </el-col>
                        <el-col :span="12">
                          <el-form-item label="决策主体">
                            <el-input v-model="formData.jczt" />
                          </el-form-item>
                        </el-col>
                        <el-col :span="12">
                          <el-form-item label="监管单位">
                            <el-input v-model="formData.jgdw" />
                          </el-form-item>
                        </el-col>
                        <el-col :span="12">
                          <el-form-item label="签订单位">
                            <el-input v-model="formData.qddw" />
                          </el-form-item>
                        </el-col>
                        <el-col :span="24">
                          <el-form-item label="投资主体">
                            <el-input v-model="formData.tzzt" />
                          </el-form-item>
                        </el-col>
                        <el-col :span="24">
                          <el-form-item label="项目地点">
                            <el-input v-model="formData.xmdd" />
                          </el-form-item>
                        </el-col>
                        <el-col :span="8">
                          <el-form-item label="建设期(月份)">
                            <el-input-number
                              v-model="formData.jsqyf"
                              :min="0"
                              controls-position="right"
                            />
                          </el-form-item>
                        </el-col>
                        <el-col :span="8">
                          <el-form-item label="项目投产时间">
                            <el-date-picker
                              v-model="formData.xmtcsj"
                              type="date"
                              placeholder="请选择"
                              value-format="YYYY-MM-DD"
                            />
                          </el-form-item>
                        </el-col>
                        <el-col :span="8">
                          <el-form-item label="运营期（年）">
                            <el-input-number
                              v-model="formData.yyq"
                              :min="0"
                              controls-position="right"
                            />
                          </el-form-item>
                        </el-col>
                        <el-col :span="8">
                          <el-form-item label="计划竣工时间">
                            <el-date-picker
                              v-model="formData.jhjgsj"
                              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.jcsj"
                              type="date"
                              placeholder="请选择"
                              value-format="YYYY-MM-DD"
                            />
                          </el-form-item>
                        </el-col>
                        <el-col :span="24">
                          <el-form-item label="项目内容">
                            <el-input
                              v-model="formData.xmnr"
                              type="textarea"
                              :autosize="{ minRows: 3, maxRows: 6 }"
                            />
                          </el-form-item>
                        </el-col>
                        <el-col :span="24">
                          <el-form-item label="项目主要方案内容">
                            <el-input
                              v-model="formData.xmzyfanr"
                              type="textarea"
                              :autosize="{ minRows: 2, maxRows: 5 }"
                            />
                          </el-form-item>
                        </el-col>
                        <el-col :span="6">
                          <el-form-item label="全投资财务内部收益率%（税前）">
                            <el-input-number
                              v-model="formData.qtzcwnbsyl"
                              :min="0"
                              :max="100"
                              :precision="2"
                              controls-position="right"
                            />
                          </el-form-item>
                        </el-col>
                        <el-col :span="6">
                          <el-form-item label="资本金财务内部收益率%（税后）">
                            <el-input-number
                              v-model="formData.zbjcwnbsyl"
                              :min="0"
                              :max="100"
                              :precision="2"
                              controls-position="right"
                            />
                          </el-form-item>
                        </el-col>
                        <el-col :span="6">
                          <el-form-item label="签订日期">
                            <el-date-picker
                              v-model="formData.qdrq"
                              type="date"
                              placeholder="请选择"
                              value-format="YYYY-MM-DD"
                            />
                          </el-form-item>
                        </el-col>
                        <el-col :span="6">
                          <el-form-item label="填报人">
                            <el-input v-model="formData.tbr" />
                          </el-form-item>
                        </el-col>
                      </el-row>
                    </el-collapse-item>

                    <!-- 造价信息 -->
                    <el-collapse-item title="造价信息" name="造价信息">
                      <el-row :gutter="20">
                        <el-col :span="8">
                          <el-form-item
                            label-width="180"
                            label="项目总投资(万元)"
                          >
                            <el-input-number
                              v-model="formData.xmztz"
                              :min="0"
                              :precision="2"
                              controls-position="right"
                            />
                          </el-form-item>
                        </el-col>
                        <el-col :span="8">
                          <el-form-item label-width="180" label="建安费(万元)">
                            <el-input-number
                              v-model="formData.jaf"
                              :min="0"
                              :precision="2"
                              controls-position="right"
                            />
                          </el-form-item>
                        </el-col>
                        <el-col :span="8">
                          <el-form-item
                            label-width="180"
                            label="设备及工具购置费(万元)"
                          >
                            <el-input-number
                              v-model="formData.sbjgjgzf"
                              :min="0"
                              :precision="2"
                              controls-position="right"
                            />
                          </el-form-item>
                        </el-col>
                        <el-col :span="8">
                          <el-form-item
                            label-width="180"
                            label="土地相关费用(万元)"
                          >
                            <el-input-number
                              v-model="formData.tdxgfy"
                              :min="0"
                              :precision="2"
                              controls-position="right"
                            />
                          </el-form-item>
                        </el-col>
                        <el-col :span="8">
                          <el-form-item
                            label-width="180"
                            label="建设单位管理费(万元)"
                          >
                            <el-input-number
                              v-model="formData.jsdwglf"
                              :min="0"
                              :precision="2"
                              controls-position="right"
                            />
                          </el-form-item>
                        </el-col>
                        <el-col :span="8">
                          <el-form-item
                            label-width="180"
                            label="其他建设项目管理费(万元)"
                          >
                            <el-input-number
                              v-model="formData.qtjsxmglf"
                              :min="0"
                              :precision="2"
                              controls-position="right"
                            />
                          </el-form-item>
                        </el-col>
                        <el-col :span="8">
                          <el-form-item
                            label-width="180"
                            label="其他费用(万元)"
                          >
                            <el-input-number
                              v-model="formData.qtfy"
                              :min="0"
                              :precision="2"
                              controls-position="right"
                            />
                          </el-form-item>
                        </el-col>
                        <el-col :span="8">
                          <el-form-item
                            label-width="180"
                            label="能建方建安费(万元)"
                          >
                            <el-input-number
                              v-model="formData.njfjaf"
                              :min="0"
                              :precision="2"
                              controls-position="right"
                            />
                          </el-form-item>
                        </el-col>
                        <el-col :span="8">
                          <el-form-item
                            label-width="180"
                            label="建设期利息(万元)"
                          >
                            <el-input-number
                              v-model="formData.jsqlx"
                              :min="0"
                              :precision="2"
                              controls-position="right"
                            />
                          </el-form-item>
                        </el-col>
                      </el-row>
                    </el-collapse-item>
                    <!-- <el-collapse-item title="年度" name="年度">
                      <el-row>
                        <el-col :span="8">
                          <el-form-item label="年度" required>
                            <el-date-picker
                              v-model="formData.niandu"
                              type="year"
                              format="YYYY"
                              value-format="YYYY"
                              placeholder="请选择编制年度"
                            />
                          </el-form-item>
                        </el-col>
                      </el-row>
                    </el-collapse-item> -->

                    <el-collapse-item
                      title="投融资与运营指标（万元）"
                      name="投融资与运营指标（万元）"
                    >
                      <el-table
                        :data="zrsjtzbData"
                        style="width: 100%"
                        empty-text="暂无数据"
                        border
                        show-summary
                        :summary-method="getSummaries"
                      >
                        <el-table-column type="index" label="序号" width="60" />
                        <el-table-column prop="zbmc" label="指标名称" />
                        <el-table-column
                          prop="zbmbz"
                          label="指标目标值"
                          width="180"
                        >
                          <template #default="scope">
                            <el-input-number
                              v-model="scope.row.zbmbz"
                              :min="0"
                              :max="99999999999.99999999"
                              controls-position="right"
                            />
                          </template>
                        </el-table-column>
                        <el-table-column
                          prop="khjzf"
                          label="考核基准分"
                          width="180"
                        >
                          <template #default="scope">
                            <el-input-number
                              v-model="scope.row.khjzf"
                              :min="0"
                              :max="99999999999.99999999"
                              controls-position="right"
                            />
                          </template>
                        </el-table-column>
                        <el-table-column
                          prop="sfsy"
                          label="是否适用"
                          width="90"
                        >
                          <template #default="scope">
                            <el-switch
                              v-model="scope.row.sfsy"
                              inline-prompt
                              active-value="1"
                              active-text="是"
                              inactive-value="2"
                              inactive-text="否"
                            />
                          </template>
                        </el-table-column>
                        <el-table-column prop="khsm" label="考核说明">
                          <template #default="scope">
                            <el-input
                              v-model="scope.row.khsm"
                              type="textarea"
                            />
                          </template>
                        </el-table-column>
                      </el-table>
                    </el-collapse-item>
                    <el-collapse-item
                      title="批复意见落实情况"
                      name="批复意见落实情况"
                    >
                      <div class="tab-handle">
                        <el-button
                          type="primary"
                          size="small"
                          @click="addPfyjlsqk"
                          >新增</el-button
                        >
                      </div>
                      <el-table
                        :data="pfyjlsqkData"
                        style="width: 100%"
                        empty-text="暂无数据"
                        border
                      >
                        <el-table-column type="index" width="60" />
                        <el-table-column prop="lx" label="类型">
                          <template #default="scope">
                            <el-select
                              v-model="scope.row.lx"
                              placeholder="请选择"
                              no-data-text="暂无数据"
                            >
                              <el-option
                                v-for="item in pfyjlxList"
                                :key="item.key"
                                :label="item.name"
                                :value="item.key"
                              ></el-option>
                            </el-select>
                          </template>
                        </el-table-column>
                        <el-table-column prop="pfyj" label="批复意见">
                          <template #default="scope">
                            <el-input
                              v-model="scope.row.pfyj"
                              type="textarea"
                            />
                          </template>
                        </el-table-column>
                        <el-table-column prop="jzf" label="基准分">
                          <template #default="scope">
                            <el-input-number
                              v-model="scope.row.jzf"
                              :min="0"
                              :max="99999999999.99999999"
                              controls-position="right"
                            />
                          </template>
                        </el-table-column>
                        <el-table-column prop="lsqk" label="落实情况">
                          <template #default="scope">
                            <el-select
                              v-model="scope.row.lsqk"
                              placeholder="请选择"
                              no-data-text="暂无数据"
                            >
                              <el-option
                                v-for="item in lsqkList"
                                :key="item.key"
                                :label="item.name"
                                :value="item.key"
                              ></el-option>
                            </el-select>
                          </template>
                        </el-table-column>
                        <el-table-column prop="lssj" label="落实时间">
                          <template #default="scope">
                            <el-date-picker
                              v-model="scope.row.lssj"
                              type="datetime"
                              placeholder="请选择"
                              value-format="YYYY-MM-DD HH:mm:ss"
                            />
                          </template>
                        </el-table-column>
                        <el-table-column
                          prop="lsqkjtsm"
                          label="落实情况具体说明"
                        >
                          <template #default="scope">
                            <el-input
                              v-model="scope.row.lsqkjtsm"
                              type="textarea"
                            />
                          </template>
                        </el-table-column>
                        <el-table-column label="操作" width="60">
                          <template #default="scope">
                            <el-button
                              link
                              type="danger"
                              size="small"
                              @click="deletePfyjlsqk(scope.$index)"
                              >删除</el-button
                            >
                          </template>
                        </el-table-column>
                      </el-table>
                    </el-collapse-item>
                    <el-collapse-item
                      title="其他对项目决策与实施有颠覆性影响的边界条件"
                      name="其他对项目决策与实施有颠覆性影响的边界条件"
                    >
                      <DynamicTable
                        :columns="transferColumns2"
                        :disabled="isPreview"
                        v-model="formData.qtdxmjcyssydfxyxdbj"
                      />
                    </el-collapse-item>

                    <el-collapse-item
                      title="项目实施过程中其他提高项目投资收益或带动公司市场开拓的情况"
                      name="项目实施过程中其他提高项目投资收益或带动公司市场开拓的情况"
                    >
                      <DynamicTable
                        :columns="transferColumns2"
                        :disabled="isPreview"
                        v-model="formData.xmssgczqttgxmtzsylhddgsscktqk"
                      />
                    </el-collapse-item>
                    <el-collapse-item title="签订信息" name="签订信息">
                      <el-row :gutter="24">
                        <el-col :span="6">
                          <el-form-item label="签订状态">
                            <CommonSelector
                              v-model="formData.qdzt"
                              dictName="mbzrsqdqk"
                            ></CommonSelector>
                          </el-form-item>
                        </el-col>
                        <el-col :span="6">
                          <el-form-item label="是否按时签订">
                            <CommonSelector
                              v-model="formData.sfasqd"
                              :radio="true"
                              dictName="sf"
                            ></CommonSelector>
                          </el-form-item>
                        </el-col>
                        <el-col :span="6">
                          <el-form-item label="签订单位">
                            <el-input v-model="formData.qddw" />
                          </el-form-item>
                        </el-col>

                        <el-col :span="6">
                          <el-form-item label="签订时间">
                            <el-date-picker
                              v-model="formData.qdsj"
                              type="date"
                              placeholder="请选择"
                              value-format="YYYY-MM-DD"
                            />
                          </el-form-item>
                        </el-col>
                        <el-col :span="24">
                          <el-form-item
                            style="padding: 8px 0"
                            :label="`已盖章签字的目标责任书附件`"
                          >
                            <FileUploader
                              v-model="formData.ygzqzdyyjdmbzrsfj"
                            ></FileUploader>
                          </el-form-item>
                        </el-col>
                      </el-row>
                    </el-collapse-item>
                  </el-collapse>
                </el-form>
              </div>
            </div>
          </el-tab-pane>
          <el-tab-pane label="全生命周期管理策划" name="全生命周期管理策划">
            <div class="project-tab-content">
              <div class="tab-content">
                <el-row :gutter="20">
                  <el-col :span="24">
                    <el-form-item label="策划文件">
                      <FileUploader v-model="chwjData" />
                    </el-form-item>
                  </el-col>
                </el-row>
              </div>
            </div>
          </el-tab-pane>
          <el-tab-pane label="年度经营管理责任书" name="年度经营管理责任书">
            <div class="project-tab-content">
              <div class="tab-content">
                <el-form
                  :model="formData"
                  :label-width="100"
                  :disabled="isPreview"
                >
                  <el-col :span="24">
                    <el-form-item label="责任书文件">
                      <FileUploader v-model="zrsData" />
                    </el-form-item>
                  </el-col>
                </el-form>
              </div>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
      <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>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted, getCurrentInstance, h, watch } from "vue";
import { useRouter, useRoute } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { useUserStore } from "@/stores/user.js";
import { useProjectDetail } from "@/composables/useProjectDetail";
import { useExportExcel } from "@/composables/useExportExcel";
const userStore = useUserStore();
const router = useRouter();
const route = useRoute();
const { proxy } = getCurrentInstance();

const { projectDisabled, cachedInfo } = useProjectDetail();
const { exportExcel } = useExportExcel();
import FileUploader from "@/components/FileUploader/index.vue";
import DynamicTable from "@/components/FormDynamicTable/index.vue";
const transferColumns1 = ref([
  {
    prop: "njfcgbl",
    label: "指标名称",
    type: "input",
    placeholder: "请填写指标名称",
  },
  {
    prop: "njfcgb2",
    label: "是否考核",
    type: "select",
    optionKey: "sf",
  },
  {
    prop: "njfcgb3",
    label: "责任目标值",
    type: "number",
    placeholder: "请填写责任目标值",
  },
  {
    prop: "njfcgb4",
    label: "考核基准分",
    type: "number",
    placeholder: "请填写考核基准分",
  },

  {
    prop: "njfcgb5",
    label: "计分细责",
    type: "input",
    placeholder: "请填写计分细责",
  },
]);
const transferColumns2 = ref([
  {
    prop: "njfcgbl",
    label: "指标名称",
    type: "input",
    placeholder: "请填写指标名称",
  },

  {
    prop: "njfcgb2",
    label: "考核基准分",
    type: "number",

    placeholder: "请填写考核基准分",
  },

  {
    prop: "njfcgb3",
    label: "计分细责",
    type: "input",
    placeholder: "请填写计分细责",
  },
]);

// 表单数据

const pageActiveName = ref("投资目标责任书");
let token = ref("");
token.value = userStore.authToken || sessionStorage.getItem("authToken") || "";
import routerBack from "@/components/common/routerBack.vue";

const activeCollapse = reactive([
  "责任书基本信息",
  "造价信息",
  "年度",
  "投融资与运营指标（万元）",
  "其他对项目决策与实施有颠覆性影响的边界条件",
  "项目实施过程中其他提高项目投资收益或带动公司市场开拓的情况",
  "批复意见落实情况",
  "签订信息",
]);

let formData = reactive({
  qtdxmjcyssydfxyxdbj: [],
  xmssgczqttgxmtzsylhddgsscktqk: [],
  trzyyyzb: [],
});
let loading = ref(false);
// 责任书类型
let zrslxList = reactive([]);
let zrslxProps = {
  label: "name",
};
let selectedFullPath = ref(""); //已选项的全路径名称
watch(
  () => formData.zrslx,
  (newVal) => {
    if (newVal) {
      selectedFullPath.value = getFullPathById(newVal);
    } else {
      selectedFullPath.value = "";
    }
  },
);
// 构建节点映射表
const nodeMap = new Map();
const getFullPathById = (id) => {
  const node = nodeMap.get(id);
  if (!node) return "";

  const path = [];
  let current = node;

  while (current) {
    path.unshift(current.name);
    current = current.parent;
  }

  return path.join(" / ");
};
// 项目列表
let projectList = ref([]);
const getProjectData = (params = {}) => {
  proxy.$post({
    url: "/api/project/listProject",
    data: {
      page: 1,
      pagesize: 1000,
      attributes: [],
      menuType: "xmjc",
      ...params,
    },
    callback: (data) => {
      projectList.value = data.rows;
    },
  });
};
// 批复意见落实情况
let pfyjlxList = reactive([]);
let lsqkList = reactive([]);
let pfyjlsqkData = ref([]);
// 年度经营管理责任书相关数据
let jgdwList = reactive([]);
let qddwList = reactive([]);
let qdztList = reactive([]);
let qdfjData = ref([]);
let qdfjSelectIds = ref([]);
const addPfyjlsqk = () => {
  pfyjlsqkData.value.push({});
};
const deletePfyjlsqk = (index) => {
  ElMessageBox.confirm("确认删除该项？", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    pfyjlsqkData.value.splice(index, 1);
  });
};

let isPreview = !!route.query.isPreview;
let statementId = route.query.statementId;
// 查看编辑详情
const getStatementInfo = () => {
  loading.value = true;
  proxy.$post({
    url: "/api/project/getTzmbzrsInfo",
    data: {
      id: statementId,
    },
    callback: (res) => {
      loading.value = false;
      Object.assign(formData, res);
      Object.assign(zrsData.value, res.zrswj);
      Object.assign(
        zrsjtzbData.value,
        res.tzmbzrsZbs.map((item) => {
          return {
            ...item,
            sfsy: item.sfsy && item.sfsy.toString(),
          };
        }),
      );
      Object.assign(chwjData.value, res.chwj);
      Object.assign(pfyjlsqkData.value, res.tzmbzrsPfyjs);
      Object.assign(qdfjData.value, res.ndQdfj || []);
    },
  });
};
onMounted(() => {
  getProjectData();
  if (cachedInfo) {
    formData.projectName = cachedInfo.projectName || "";
    formData.projectId = cachedInfo.projectId || "";
  }
  let resourceData = JSON.parse(sessionStorage.getItem("resourceData"));
  zrslxList = resourceData.level;
  pfyjlxList = resourceData.ssjd;
  lsqkList = resourceData.lszxqk;
  // 年度经营管理责任书相关字典
  jgdwList = resourceData.jgdw || [];
  qddwList = resourceData.qddw || [];
  qdztList = resourceData.qdzt || [];

  statementId && getStatementInfo();
  // 构建节点映射
  const buildNodeMap = (nodes, parent = null) => {
    nodes.forEach((node) => {
      node.parent = parent;
      nodeMap.set(node.key, node);
      if (node.children && node.children.length > 0) {
        buildNodeMap(node.children, node);
      }
    });
  };
  buildNodeMap(zrslxList);
});
// 选择关联项目，同步相关字段信息
const changeProject = (val) => {
  let selectData = projectList.value.filter((item) => item.id === val)[0];
  if (selectData) {
    formData.projectName = selectData.projectName;
    formData.tzzt = selectData.tzzt;
  }
};

// 责任书文件
let zrsData = ref([]);
const addZrsFile = (res, file) => {
  zrsData.value.push(res.data);
};
const deleteZrs = (index) => {
  ElMessageBox.confirm("确认删除该项？", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      zrsData.value.splice(index, 1);
    })
    .catch(() => {});
};
let zrsSelectIds = ref([]);
const zrsSelectionChange = (datas) => {
  zrsSelectIds.value = datas.map((item) => item.id);
};
const multiDeleteZrs = () => {
  ElMessageBox.confirm("确认删除选中数据？", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      zrsData.value = zrsData.value.filter(
        (item) => !zrsSelectIds.value.includes(item.id),
      );
    })
    .catch(() => {});
};
// 投融资与运营指标（万元）
let zrsjtzbData = ref([
  {
    xh: "1",
    zbmc: "项目总投资",
  },
  {
    xh: "1.1",
    zbmc: "建安费用",
  },
  {
    xh: "2",
    zbmc: "项目带动工程收益",
  },
]);
let qsmzqZrs = [
  {
    xh: "1",
    zbmc: "项目总投资 ",
  },
  {
    xh: "2",
    zbmc: "建安费用不低于 ",
  },
  {
    xh: "3",
    zbmc: "征地拆迁费不高于 ",
  },
  {
    xh: "4",
    zbmc: "项目带动工程收益 ",
  },
  {
    xh: "5",
    zbmc: "工程收益率 (%)",
  },
  {
    xh: "6",
    zbmc: "政府补助资金到位 ",
  },
  {
    xh: "7",
    zbmc: "我方资本金投入 ",
  },
  {
    xh: "8",
    zbmc: "我方负责引入的股权融资 ",
  },
  {
    xh: "9",
    zbmc: "股权融资成本 (%)",
  },
  {
    xh: "10",
    zbmc: "项目融资额 ",
  },
  {
    xh: "11",
    zbmc: "融资利率 (%)",
  },
  {
    xh: "12",
    zbmc: "项目实现营业收入累计 ",
  },
  {
    xh: "13",
    zbmc: "利润总额累计 ",
  },
  {
    xh: "14",
    zbmc: "项目经营净现金流累计 ",
  },
  {
    xh: "15",
    zbmc: "项目全投资财务内部收益率 (税前%)",
  },
  {
    xh: "16",
    zbmc: "回收额 ",
  },
  {
    xh: "17",
    zbmc: "投资回报率 (%)",
  },
  {
    xh: "18",
    zbmc: "政府应付 ",
  },
  {
    xh: "19",
    zbmc: "政府欠付 (逾期金额) ",
  },
  {
    xh: "20",
    zbmc: "使用者应付 ",
  },
  {
    xh: "21",
    zbmc: "使用者欠付 ",
  },
  {
    xh: "22",
    zbmc: "分项销售收入 (销售收入) ",
  },
  {
    xh: "23",
    zbmc: "补贴收入 (政府补贴) ",
  },
  {
    xh: "24",
    zbmc: "销售成本 (销售费用) ",
  },
  {
    xh: "25",
    zbmc: "经营活动产生净现金流 ",
  },
  {
    xh: "26",
    zbmc: "筹资活动产生净现金流 ",
  },
  {
    xh: "27",
    zbmc: "累计净现金流合计 ",
  },
  {
    xh: "28",
    zbmc: "投资活动产生净现金流 ",
  },
  {
    xh: "29",
    zbmc: "利息备付率",
  },
  {
    xh: "30",
    zbmc: "偿债备付率",
  },
  {
    xh: "31",
    zbmc: "能建方分红 (税前) ",
  },
  {
    xh: "32",
    zbmc: "未分配利润 ",
  },
  {
    xh: "33",
    zbmc: "项目资本金财务内部收益率 (税后%)",
  },
  {
    xh: "34",
    zbmc: "项目竣工投产时间 (年_月)",
  },
];
let jsqZrs = [
  {
    xh: "1",
    zbmc: "项目总投资",
  },
  {
    xh: "2",
    zbmc: "建安费用不低于",
  },
  {
    xh: "3",
    zbmc: "征地拆迁费不高于",
  },
  {
    xh: "4",
    zbmc: "项目带动工程收益",
  },
  {
    xh: "5",
    zbmc: "工程收益率 (%)",
  },
  {
    xh: "6",
    zbmc: "建设期政府补助资金到位",
  },
  {
    xh: "7",
    zbmc: "我方资本金投入",
  },
  {
    xh: "8",
    zbmc: "我方负责引入的股权融资",
  },
  {
    xh: "9",
    zbmc: "股权融资成本 (%)",
  },
  {
    xh: "10",
    zbmc: "项目建设期项目融资额",
  },
  {
    xh: "11",
    zbmc: "融资利率 (%)",
  },
  {
    xh: "12",
    zbmc: "项目全投资财务内部收益率 (税前%)",
  },
  {
    xh: "13",
    zbmc: "回收额",
  },
  {
    xh: "14",
    zbmc: "投资回收率",
  },
  {
    xh: "15",
    zbmc: "政府应付",
  },
  {
    xh: "16",
    zbmc: "政府欠付 (逾期金额)",
  },
  {
    xh: "17",
    zbmc: "使用者应付",
  },
  {
    xh: "18",
    zbmc: "使用者欠付",
  },
  {
    xh: "19",
    zbmc: "分项销售收入 (销售收入)",
  },
  {
    xh: "20",
    zbmc: "补贴收入 (政府补贴)",
  },
  {
    xh: "21",
    zbmc: "销售成本 (销售费用)",
  },
  {
    xh: "22",
    zbmc: "经营活动产生净现金流",
  },
  {
    xh: "23",
    zbmc: "筹资活动产生净现金流",
  },
  {
    xh: "24",
    zbmc: "累计净现金流合计",
  },
  {
    xh: "25",
    zbmc: "投资活动产生净现金流",
  },
  {
    xh: "26",
    zbmc: "利息备付率",
  },
  {
    xh: "27",
    zbmc: "偿债备付率",
  },
  {
    xh: "28",
    zbmc: "能建方分红 (税前)",
  },
  {
    xh: "29",
    zbmc: "未分配利润",
  },
  {
    xh: "30",
    zbmc: "项目资本金财务内部收益率 (税后%)",
  },
  {
    xh: "31",
    zbmc: "项目竣工投产时间_年_月",
  },
];
let yyqZrs = [
  {
    xh: "1",
    zbmc: "运营期政府补助资金到位",
  },
  {
    xh: "2",
    zbmc: "项目运营期融资额",
  },
  {
    xh: "3",
    zbmc: "融资利率 (%)",
  },
  {
    xh: "4",
    zbmc: "项目实现营业收入累计",
  },
  {
    xh: "5",
    zbmc: "利润总额累计",
  },
  {
    xh: "6",
    zbmc: "项目经营净现金流累计",
  },
  {
    xh: "7",
    zbmc: "项目全投资财务内部收益率 (税前%)",
  },
  {
    xh: "8",
    zbmc: "回收额",
  },
  {
    xh: "9",
    zbmc: "投资回收率",
  },
  {
    xh: "10",
    zbmc: "政府应付",
  },
  {
    xh: "11",
    zbmc: "政府欠付 (逾期金额)",
  },
  {
    xh: "12",
    zbmc: "使用者应付",
  },
  {
    xh: "13",
    zbmc: "使用者欠付",
  },
  {
    xh: "14",
    zbmc: "分项销售收入 (销售收入)",
  },
  {
    xh: "15",
    zbmc: "补贴收入 (政府补贴)",
  },
  {
    xh: "16",
    zbmc: "销售成本 (销售费用)",
  },
  {
    xh: "17",
    zbmc: "经营活动产生净现金流",
  },
  {
    xh: "18",
    zbmc: "筹资活动产生净现金流",
  },
  {
    xh: "19",
    zbmc: "累计净现金流合计",
  },
  {
    xh: "20",
    zbmc: "投资活动产生净现金流",
  },
  {
    xh: "21",
    zbmc: "利息备付率",
  },
  {
    xh: "22",
    zbmc: "偿债备付率",
  },
  {
    xh: "23",
    zbmc: "能建方分红 (税前)",
  },
  {
    xh: "24",
    zbmc: "未分配利润",
  },
  {
    xh: "25",
    zbmc: "项目资本金财务内部收益率 (税后%)",
  },
];
let ndZrs = [
  {
    xh: "1",
    zbmc: "本年度完成投资",
  },
  {
    xh: "2",
    zbmc: "建安费用不低于",
  },
  {
    xh: "3",
    zbmc: "征地拆迁费不高于",
  },
  {
    xh: "4",
    zbmc: "本年度工程收益",
  },
  {
    xh: "5",
    zbmc: "本年度政府补助资金",
  },
  {
    xh: "6",
    zbmc: "本年度我方资本金投入",
  },
  {
    xh: "7",
    zbmc: "本年度我方负责引入的股权融资",
  },
  {
    xh: "8",
    zbmc: "股权融资成本 (%)",
  },
  {
    xh: "9",
    zbmc: "本年度项目融资额",
  },
  {
    xh: "10",
    zbmc: "融资利率 (%)",
  },
  {
    xh: "11",
    zbmc: "本年度投资项目实现营业收入",
  },
  {
    xh: "12",
    zbmc: "利润总额",
  },
  {
    xh: "13",
    zbmc: "本年度项目经营净现金流",
  },
  {
    xh: "14",
    zbmc: "项目全投资财务内部收益率 (税前%)",
  },
  {
    xh: "15",
    zbmc: "回收额",
  },
  {
    xh: "16",
    zbmc: "投资回收率",
  },
  {
    xh: "17",
    zbmc: "政府应付",
  },
  {
    xh: "18",
    zbmc: "政府欠付 (逾期金额)",
  },
  {
    xh: "19",
    zbmc: "使用者应付",
  },
  {
    xh: "20",
    zbmc: "使用者欠付",
  },
  {
    xh: "21",
    zbmc: "分项销售收入 (销售收入)",
  },
  {
    xh: "22",
    zbmc: "补贴收入 (政府补贴)",
  },
  {
    xh: "23",
    zbmc: "销售成本 (销售费用)",
  },
  {
    xh: "24",
    zbmc: "经营活动产生净现金流",
  },
  {
    xh: "25",
    zbmc: "筹资活动产生净现金流",
  },
  {
    xh: "26",
    zbmc: "累计净现金流合计",
  },
  {
    xh: "27",
    zbmc: "投资活动产生净现金流",
  },
  {
    xh: "28",
    zbmc: "利息备付率",
  },
  {
    xh: "29",
    zbmc: "偿债备付率",
  },
  {
    xh: "30",
    zbmc: "能建方分红 (税前)",
  },
  {
    xh: "31",
    zbmc: "未分配利润",
  },
  {
    xh: "32",
    zbmc: "项目资本金财务内部收益率 (税后%)",
  },
];
// 选择责任书类型，同步切换责任书具体指标数据
const changeZrslx = (data) => {
  if (!data.children || !data.children.length) {
    // 点击到最内层子节点
    if (data.name.indexOf("年度") != -1) {
      zrsjtzbData.value = ndZrs;
    } else if (data.name.indexOf("建设") != -1) {
      zrsjtzbData.value = jsqZrs;
    } else if (data.name.indexOf("运营") != -1) {
      zrsjtzbData.value = yyqZrs;
    } else {
      zrsjtzbData.value = qsmzqZrs;
    }
  }
};
const getSummaries = (param) => {
  const { columns, data } = param;
  let sums = [];
  columns.forEach((column, index) => {
    if (index === 0) {
      sums[0] = h("div", "合计");
      return;
    }
    const values = data.map((item) => Number(item[column.property]));
    if (["khjzf"].includes(column.property)) {
      sums[index] = `${values.reduce((prev, curr) => {
        const value = Number(curr);
        if (!Number.isNaN(value)) {
          return prev + curr;
        } else {
          return prev;
        }
      }, 0)}`;
    } else {
      sums[index] = "";
    }
  });
  return sums;
};
// 策划文件
let chwjData = ref([]);
const addChwjFile = (res, file) => {
  chwjData.value.push(res.data);
};
const deleteChwj = (index) => {
  ElMessageBox.confirm("确认删除该项？", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      chwjData.value.splice(index, 1);
    })
    .catch(() => {});
};
let chwjSelectIds = ref([]);
const chwjSelectionChange = (datas) => {
  chwjSelectIds.value = datas.map((item) => item.id);
};
const multiDeleteChwj = () => {
  ElMessageBox.confirm("确认删除选中数据？", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      chwjData.value = chwjData.value.filter(
        (item) => !chwjSelectIds.value.includes(item.id),
      );
    })
    .catch(() => {});
};

// 签订附件
const addQdfjFile = (res, file) => {
  qdfjData.value.push(res.data);
};
const deleteQdfj = (index) => {
  ElMessageBox.confirm("确认删除该项？", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      qdfjData.value.splice(index, 1);
    })
    .catch(() => {});
};
const qdfjSelectionChange = (datas) => {
  qdfjSelectIds.value = datas.map((item) => item.id);
};
const multiDeleteQdfj = () => {
  ElMessageBox.confirm("确认删除选中数据？", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      qdfjData.value = qdfjData.value.filter(
        (item) => !qdfjSelectIds.value.includes(item.id),
      );
    })
    .catch(() => {});
};

// 下载已上传文件
const downloadFile = (data) => {
  let a = document.createElement("a");
  a.href = `${proxy.windowConfig.baseUrl}/api/file/download/${data.id}`;
  a.download = data.originalname;
  document.body.appendChild(a);
  a.click();
  a.remove();
};

// 导出整个责任书为Excel
const exportStatement = () => {
  exportExcel({
    fileName: `${formData.projectName || "责任书"}_目标责任书`,
    sheets: [
      {
        name: "基本信息",
        type: "kv",
        title: "投资目标责任书 - 基本信息",
        data: [
          ["责任书类型", selectedFullPath.value || formData.zrslx || ""],
          ["项目名称", formData.projectName || ""],
          ["决策主体", formData.jczt || ""],
          ["监管单位", formData.jgdw || ""],
          ["签订单位", formData.qddw || ""],
          ["投资主体", formData.tzzt || ""],
          ["项目地点", formData.xmdd || ""],
          ["建设期(月份)", formData.jsqyf ?? ""],
          ["项目投产时间", formData.xmtcsj || ""],
          ["运营期（年）", formData.yyq ?? ""],
          ["计划竣工时间", formData.jhjgsj || ""],
          ["决策时间", formData.jcsj || ""],
          ["项目内容", formData.xmnr || ""],
          ["项目主要方案内容", formData.xmzyfanr || ""],
          ["全投资财务内部收益率%（税前）", formData.qtzcwnbsyl ?? ""],
          ["资本金财务内部收益率%（税后）", formData.zbjcwnbsyl ?? ""],
          ["签订日期", formData.qdrq || ""],
          ["填报人", formData.tbr || ""],
          [],
          ["签订状态", formData.qdzt || ""],
          ["是否按时签订", formData.sfasqd || ""],
          ["签订时间", formData.qdsj || ""],
        ],
      },
      {
        name: "造价信息",
        type: "kv",
        title: "造价信息",
        data: [
          ["项目总投资(万元)", formData.xmztz ?? ""],
          ["建安费(万元)", formData.jaf ?? ""],
          ["设备及工具购置费(万元)", formData.sbjgjgzf ?? ""],
          ["土地相关费用(万元)", formData.tdxgfy ?? ""],
          ["建设单位管理费(万元)", formData.jsdwglf ?? ""],
          ["其他建设项目管理费(万元)", formData.qtjsxmglf ?? ""],
          ["其他费用(万元)", formData.qtfy ?? ""],
          ["能建方建安费(万元)", formData.njfjaf ?? ""],
          ["建设期利息(万元)", formData.jsqlx ?? ""],
        ],
      },
      {
        name: "投融资与运营指标",
        type: "table",
        data: zrsjtzbData.value,
        columns: [
          { prop: "zbmc", label: "指标名称" },
          { prop: "zbmbz", label: "指标目标值" },
          { prop: "khjzf", label: "考核基准分" },
          {
            prop: "sfsy",
            label: "是否适用",
            formatter: (v) => (v === "1" ? "是" : "否"),
          },
          { prop: "khsm", label: "考核说明" },
        ],
        colWidths: [35, 15, 12, 10, 30],
      },
      {
        name: "批复意见落实情况",
        type: "table",
        data: pfyjlsqkData.value,
        columns: [
          {
            prop: "lx",
            label: "类型",
            formatter: (v) =>
              pfyjlxList.find((l) => l.key === v)?.name || v || "",
          },
          { prop: "pfyj", label: "批复意见" },
          { prop: "jzf", label: "基准分" },
          {
            prop: "lsqk",
            label: "落实情况",
            formatter: (v) =>
              lsqkList.find((l) => l.key === v)?.name || v || "",
          },
          { prop: "lssj", label: "落实时间" },
          { prop: "lsqkjtsm", label: "落实情况具体说明" },
        ],
        colWidths: [12, 30, 10, 12, 20, 30],
      },
      {
        name: "边界条件",
        type: "table",
        data: formData.qtdxmjcyssydfxyxdbj || [],
        columns: [
          { prop: "njfcgbl", label: "指标名称" },
          { prop: "njfcgb2", label: "考核基准分" },
          { prop: "njfcgb3", label: "计分细责" },
        ],
        colWidths: [25, 15, 25],
      },
      {
        name: "投资收益",
        type: "table",
        data: formData.xmssgczqttgxmtzsylhddgsscktqk || [],
        columns: [
          { prop: "njfcgbl", label: "指标名称" },
          { prop: "njfcgb2", label: "考核基准分" },
          { prop: "njfcgb3", label: "计分细责" },
        ],
        colWidths: [25, 15, 25],
      },
    ],
  });
};

const backClick = () => {
  router.back(-1);
};
const saveClick = () => {
  if (formData.projectId) {
    let url = formData.id ? "updateTzmbzrs" : "createTzmbzrs";
    proxy.$post({
      url: "/api/project/" + url,
      data: {
        ...formData,
        zrswj: zrsData.value,
        tzmbzrsZbs: zrsjtzbData.value,
        chwj: chwjData.value,
        tzmbzrsPfyjs: pfyjlsqkData.value,
        ndQdfj: qdfjData.value,
      },
      callback: (data) => {
        ElMessage.success("保存成功");
      },
    });
  } else {
    ElMessage.warning("请选择项目信息");
  }
};
</script>

<style lang="less">
.add-project-header {
  margin-bottom: 10px;
}
.collapse-title {
  font-size: 14px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 10px;
  padding-left: 10px;
  border-left: 3px solid #409eff;
}
.always-click {
  color: #409eff;
  cursor: pointer;
  margin-right: 10px;
  &:hover {
    text-decoration: underline;
  }
}
.tab-handle {
  margin-bottom: 10px;
  display: flex;
  justify-content: flex-end;
}
</style>
