<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">
        <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="130"
                  :disabled="isPreview"
                >
                  <el-collapse v-model="activeCollapse">
                    <el-collapse-item
                      title="责任书基本信息"
                      name="责任书基本信息"
                    >
                      <el-row :gutter="20">
                        <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"
                              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="24">
                          <el-form-item label="责任书文件">
                            <el-upload
                              :action="
                                windowConfig.baseUrl + '/api/file/upload'
                              "
                              :headers="{ Authorization: token }"
                              :show-file-list="false"
                              multiple
                              :on-success="addZrsFile"
                            >
                              <el-button type="default">上传</el-button>
                            </el-upload>
                            <el-button
                              type="default"
                              @click="multiDeleteZrs"
                              :disabled="!zrsSelectIds.length"
                              >删除选中文件</el-button
                            >
                          </el-form-item>
                          <el-form-item label="">
                            <el-table
                              :data="zrsData"
                              style="width: 100%"
                              empty-text="暂无数据"
                              border
                              @selection-change="zrsSelectionChange"
                            >
                              <el-table-column type="selection" width="55" />
                              <el-table-column
                                prop="originalname"
                                label="文件名"
                              />
                              <el-table-column
                                prop="updatedAt"
                                label="上传时间"
                              >
                                <template #default="{ row, index }">
                                  <span>{{
                                    moment(row.updatedAt).format(
                                      "YYYY-MM-DD HH:mm:SS",
                                    )
                                  }}</span>
                                </template>
                              </el-table-column>
                              <el-table-column prop="size" label="大小">
                                <template #default="{ row }">
                                  <span>{{ row.size }} MB</span>
                                </template>
                              </el-table-column>

                              <el-table-column
                                fixed="right"
                                label="操作"
                                width="100"
                              >
                                <template #default="{ row, index }">
                                  <!-- 需要在查看表单disabled时保持允许下载 -->
                                  <span
                                    class="always-click"
                                    @click="downloadFile(row)"
                                    >下载</span
                                  >
                                  <el-button
                                    link
                                    type="danger"
                                    size="small"
                                    @click="deleteZrs(index)"
                                    >删除</el-button
                                  >
                                </template>
                              </el-table-column>
                            </el-table>
                          </el-form-item>
                        </el-col>
                        <el-col :span="8">
                          <el-form-item label="项目名称">
                            <el-input v-model="formData.projectName" />
                          </el-form-item>
                        </el-col>
                        <el-col :span="8">
                          <el-form-item label="投资主体">
                            <el-input v-model="formData.tzzt" />
                          </el-form-item>
                        </el-col>
                        <el-col :span="8">
                          <el-form-item label="项目地点">
                            <el-input v-model="formData.xmdd" />
                          </el-form-item>
                        </el-col>
                        <el-col :span="8">
                          <el-form-item label="项目开工日期">
                            <el-date-picker
                              v-model="formData.xmkgrq"
                              type="date"
                              placeholder="请选择"
                            />
                          </el-form-item>
                        </el-col>
                        <el-col :span="8">
                          <el-form-item label="项目实施期限">
                            <el-input-number
                              v-model="formData.xmssqx"
                              :min="0"
                              :max="99999999999.99999999"
                              controls-position="right"
                            ></el-input-number>
                            年
                          </el-form-item>
                        </el-col>
                        <el-col :span="12">
                          <el-form-item label="项目内容概述">
                            <el-input
                              v-model="formData.xmnrgs"
                              type="textarea"
                              :autosize="{ minRows: 2, maxRows: 5 }"
                            />
                          </el-form-item>
                        </el-col>
                        <el-col :span="12">
                          <el-form-item label="项目经济指标概述">
                            <el-input
                              v-model="formData.xmjjzbgs"
                              type="textarea"
                              :autosize="{ minRows: 2, maxRows: 5 }"
                            />
                          </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>
                </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="策划文件">
                      <el-upload
                        :action="windowConfig.baseUrl + '/api/file/upload'"
                        :headers="{ Authorization: token }"
                        :show-file-list="false"
                        multiple
                        :disabled="isPreview"
                        :on-success="addChwjFile"
                      >
                        <el-button :disabled="isPreview" type="default"
                          >上传</el-button
                        >
                      </el-upload>
                      <el-button
                        type="default"
                        @click="multiDeleteChwj"
                        :disabled="!chwjSelectIds.length"
                        >删除选中文件</el-button
                      >
                    </el-form-item>
                    <el-form-item label="">
                      <el-table
                        :data="chwjData"
                        style="width: 100%"
                        empty-text="暂无数据"
                        border
                        @selection-change="chwjSelectionChange"
                      >
                        <el-table-column type="selection" width="55" />
                        <el-table-column prop="originalname" label="文件名" />
                        <el-table-column prop="updatedAt" label="上传时间">
                          <template #default="{ row, index }">
                            <span>{{
                              moment(row.updatedAt).format(
                                "YYYY-MM-DD HH:mm:SS",
                              )
                            }}</span>
                          </template>
                        </el-table-column>
                        <el-table-column prop="size" label="大小">
                          <template #default="{ row }">
                            <span>{{ row.size }} MB</span>
                          </template>
                        </el-table-column>
                        <el-table-column fixed="right" label="操作" width="100">
                          <template #default="{ row, index }">
                            <!-- 需要在查看表单disabled时保持允许下载 -->
                            <span
                              class="always-click"
                              @click="downloadFile(row)"
                              >下载</span
                            >
                            <el-button
                              link
                              type="danger"
                              size="small"
                              @click="deleteChwj(index)"
                              >删除</el-button
                            >
                          </template>
                        </el-table-column>
                      </el-table>
                    </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-row :gutter="20">
                  <el-col :span="24">
                    <div class="tab-handle">
                      <el-button
                        type="primary"
                        :disabled="isPreview"
                        size="small"
                        @click="addJyglzrs"
                        >新增</el-button
                      >
                    </div>
                    <el-table
                      :data="jyglzrsData"
                      style="width: 100%"
                      empty-text="暂无数据"
                      border
                    >
                      <el-table-column type="index" width="50" />
                      <el-table-column prop="nd" label="年度" width="260">
                        <template #default="scope">
                          <el-date-picker
                            v-model="scope.row.nd"
                            type="year"
                            placeholder="请选择"
                            value-format="YYYY"
                          />
                        </template>
                      </el-table-column>
                      <el-table-column label="经营管理责任书" prop="jyglzrs">
                        <template #default="scope">
                          <el-upload
                            v-if="!scope.row.jyglzrs"
                            :action="windowConfig.baseUrl + '/api/file/upload'"
                            :headers="{ Authorization: token }"
                            :show-file-list="false"
                            :on-success="
                              (res, file) =>
                                addJyglzrsFile(res, file, scope.row)
                            "
                          >
                            <el-button link type="primary">上传</el-button>
                          </el-upload>
                          <div class="upload-file-wrap" v-else>
                            <span
                              class="file-name"
                              @click="downloadFile(scope.row.jyglzrs)"
                              >{{ scope.row.jyglzrs.originalname }}</span
                            >
                            <span
                              class="delete-btn"
                              @click="deleteRowFile(scope.row, scope.$index)"
                            >
                              <el-icon><CloseBold /></el-icon>
                            </span>
                          </div>
                        </template>
                      </el-table-column>
                      <el-table-column label="说明" prop="sm">
                        <template #default="scope">
                          <el-input v-model="scope.row.sm" type="textarea" />
                        </template>
                      </el-table-column>
                      <el-table-column label="操作" width="60" fixed="right">
                        <template #default="scope">
                          <el-button
                            link
                            type="danger"
                            size="small"
                            @click="deleteJyglzrs(scope.$index)"
                            >删除</el-button
                          >
                        </template>
                      </el-table-column>
                    </el-table>
                  </el-col>
                </el-row>
              </div>
            </div>
          </el-tab-pane>
        </el-tabs>
      </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";
const userStore = useUserStore();
const router = useRouter();
const route = useRoute();
const { proxy } = getCurrentInstance();
const pageActiveName = ref("全生命周期责任书");
let token = ref("");
token.value = userStore.authToken || sessionStorage.getItem("authToken") || "";

const activeCollapse = reactive([
  "责任书基本信息",
  "责任书具体指标",
  "批复意见落实情况",
]);
let formData = reactive({});
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();
let buildNodeMap;
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 = () => {
  proxy.$post({
    url: "/api/project/listProject",
    data: {
      page: 1,
      pagesize: 1000,
      attributes: [],
      menuType: "xmjc",
    },
    callback: (data) => {
      projectList.value = data.rows;
    },
  });
};
// 批复意见落实情况
let pfyjlxList = reactive([]);
let lsqkList = reactive([]);
let pfyjlsqkData = 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(jyglzrsData.value, res.tzmbzrsNds);
      Object.assign(pfyjlsqkData.value, res.tzmbzrsPfyjs);
    },
  });
};
onMounted(() => {
  getProjectData();
  let resourceData = JSON.parse(sessionStorage.getItem("resourceData"));
  zrslxList = resourceData.level;
  pfyjlxList = resourceData.ssjd;
  lsqkList = resourceData.lszxqk;

  statementId && getStatementInfo();
  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([]);
let qsmzqZrs = [
  {
    xh: "1",
    zbmc: "项目总投资(亿元)",
  },
  {
    xh: "1.1",
    zbmc: "建安费用(亿元)",
  },
  {
    xh: "2",
    zbmc: "项目带动工程收益(亿元)",
  },
  {
    xh: "2.1",
    zbmc: "工程收益率(%)",
  },
  {
    xh: "3",
    zbmc: "政府补助资金到位(亿元)",
  },
  {
    xh: "4",
    zbmc: "我方资本金投入(亿元)",
  },
  {
    xh: "5",
    zbmc: "我方负责引入的股权融资(亿元)",
  },
  {
    xh: "6",
    zbmc: "项目融资额(亿元)",
  },
  {
    xh: "6.1",
    zbmc: "融资利率(%)",
  },
  {
    xh: "7",
    zbmc: "项目实现营业收入累计(亿元)",
  },
  {
    xh: "8",
    zbmc: "项目经营净现金流累计(亿元)",
  },
  {
    xh: "9",
    zbmc: "项目全投资财务内部收益率（税前）(%)",
  },
  {
    xh: "10",
    zbmc: "项目资本金财务内部收益率(%)",
  },
  {
    xh: "11",
    zbmc: "项目竣工投产时间(年月)",
  },
];
let jsqZrs = [
  {
    xh: "1",
    zbmc: "项目总投资 (亿元)",
  },
  {
    xh: "1.1",
    zbmc: "建安费用(亿元)",
  },
  {
    xh: "2",
    zbmc: "项目带动工程收益 (亿元)",
  },
  {
    xh: "2.1",
    zbmc: "工程收益率(%)",
  },
  {
    xh: "3",
    zbmc: "建设期政府补助资金(亿元)",
  },
  {
    xh: "4",
    zbmc: "我方资本金投入(亿元)",
  },
  {
    xh: "5",
    zbmc: "我方负责引入的股权融资(亿元)",
  },
  {
    xh: "5.1",
    zbmc: "股权融资成本(%)",
  },
  {
    xh: "6",
    zbmc: "项目建设期融资额(亿元)",
  },
  {
    xh: "6.1",
    zbmc: "融资利率(%)",
  },
  {
    xh: "7",
    zbmc: "项目全投资财务内部收益率（税前）(%)",
  },
  {
    xh: "8",
    zbmc: "项目资本金财务内部收益率(%)",
  },
  {
    xh: "9",
    zbmc: "项目竣工投产时间(年月)",
  },
];
let yyqZrs = [
  {
    xh: "1",
    zbmc: "运营期政府补助资金到位(亿元)",
  },
  {
    xh: "2",
    zbmc: "项目运营期融资额(亿元)",
  },
  {
    xh: "2.1",
    zbmc: "融资利率(%)",
  },
  {
    xh: "3",
    zbmc: "项目实现营业收入累计(亿元)",
  },
  {
    xh: "3.1",
    zbmc: "利润总额累计(亿元)",
  },
  {
    xh: "4",
    zbmc: "项目经营净现金流累计(亿元)",
  },
  {
    xh: "5",
    zbmc: "项目全投资财务内部收益率（税前）(%)",
  },
  {
    xh: "6",
    zbmc: "项目资本金财务内部收益率(%)",
  },
];
let ndZrs = [
  {
    xh: "1",
    zbmc: "本年度完成投资(亿元)",
  },
  {
    xh: "1.1",
    zbmc: "建安费用(亿元)",
  },
  {
    xh: "1.2",
    zbmc: "项目征地拆迁费(亿元)",
  },
  {
    xh: "2",
    zbmc: "本年度工程收益(亿元)",
  },
  {
    xh: "2.1",
    zbmc: "工程收益率(%)",
  },
  {
    xh: "3",
    zbmc: "本年度政府补助资金(亿元)",
  },
  {
    xh: "4",
    zbmc: "本年度我方资本金投入(亿元)",
  },
  {
    xh: "5",
    zbmc: "本年度我方负责引入的股权融资(亿元)",
  },
  {
    xh: "5.1",
    zbmc: "股权融资成本(%)",
  },
  {
    xh: "6",
    zbmc: "本年度项目融资额(亿元)",
  },
  {
    xh: "6.1",
    zbmc: "融资利率(%)",
  },
  {
    xh: "7",
    zbmc: "本年度投资项目实现营业收入(亿元)",
  },
  {
    xh: "8",
    zbmc: "本年度项目经营净现金流(亿元)",
  },
  {
    xh: "9",
    zbmc: "项目全投资财务内部收益率（税前）(%)",
  },
  {
    xh: "10",
    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(() => {});
};
// 经营管理责任书
let jyglzrsData = ref([]);
const addJyglzrs = () => {
  jyglzrsData.value.push({});
};
const deleteJyglzrs = (index) => {
  jyglzrsData.value.splice(index, 1);
};
const addJyglzrsFile = (res, file, row) => {
  row.jyglzrs = {};
  Object.assign(row.jyglzrs, res.data);
};
const deleteRowFile = (row, index) => {
  let { jyglzrs, ...data } = row;
  jyglzrsData.value[index] = data;
};

// 下载已上传文件
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();
};

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,
        tzmbzrsNds: jyglzrsData.value,
        tzmbzrsPfyjs: pfyjlsqkData.value,
      },
      callback: (data) => {
        router.back(-1);
      },
    });
  } else {
    ElMessage.warning("请选择项目信息");
  }
};
</script>

<style lang="less">
@import "@/styles/manage.less";
.add-project-header {
  margin-bottom: 10px;
}
</style>
