import { reactive, ref } from "vue";
import { ElMessageBox } from "element-plus";
import Decimal from "decimal.js";

export function useJueceData(jcFormData, gdlxListRef) {
  const jcRadioClick = (value, key) => {
    if (jcFormData[key] !== value) {
      jcFormData[key] = value;
    } else {
      delete jcFormData[key];
    }
  };

  // 建设规模（实物量指标）
  let jcSwlzbData = reactive([]);
  const addJcSwlzb = () => {
    jcSwlzbData.push({});
  };
  const deleteJcSwlzb = (index) => {
    jcSwlzbData.splice(index, 1);
  };

  // 股东信息
  let jcGdxxData = reactive([{}]);
  const addJcGdxx = () => {
    jcGdxxData.splice(jcGdxxData.length - 1, 0, {});
  };
  const deleteJcGdxx = (index) => {
    jcGdxxData.splice(index, 1);
  };
  const jcgdxxIndexMethod = (index) => {
    return index === jcGdxxData.length - 1 ? "" : index + 1;
  };
  let jcgdxxSums = ref({});
  const changeJcGdxxSums = () => {
    let obj = {};
    jcGdxxData.forEach((item, index) => {
      if (index < jcGdxxData.length - 1) {
        if (!obj[item.gdlx]) {
          obj[item.gdlx] = {
            name: gdlxListRef.filter((gdlx) => gdlx.key == item.gdlx)[0].value,
          };
        }
        obj[item.gdlx].mycgbl = new Decimal(
          Number(obj[item.gdlx].mycgbl || 0),
        ).plus(new Decimal(Number(item.mycgbl || 0)));
        obj[item.gdlx].cgbl = new Decimal(
          Number(obj[item.gdlx].cgbl || 0),
        ).plus(new Decimal(Number(item.cgbl || 0)));
        obj[item.gdlx].lrfpbl = new Decimal(
          Number(obj[item.gdlx].lrfpbl || 0),
        ).plus(new Decimal(Number(item.lrfpbl || 0)));
        obj[item.gdlx].cze = new Decimal(Number(obj[item.gdlx].cze || 0)).plus(
          new Decimal(Number(item.cze || 0)),
        );
        obj[item.gdlx].yjzczb = new Decimal(
          Number(obj[item.gdlx].yjzczb || 0),
        ).plus(new Decimal(Number(item.yjzczb || 0)));
        obj[item.gdlx].ycxmzbj = new Decimal(
          Number(obj[item.gdlx].ycxmzbj || 0),
        ).plus(new Decimal(Number(item.ycxmzbj || 0)));
      }
    });
    jcgdxxSums.value = obj;
  };

  // 我方担保及债权融资情况
  let dbzqData = reactive([]);
  const addDbzq = () => {
    dbzqData.push({});
  };
  const deleteDbzq = (index) => {
    dbzqData.splice(index, 1);
  };

  // 风险管理
  let fxglData = reactive([]);
  const addFxgl = () => {
    fxglData.push({});
  };
  const deleteFxgl = (index) => {
    fxglData.splice(index, 1);
  };

  // 里程碑节点
  let lcbjdData = reactive([]);
  const addLcbjd = () => {
    lcbjdData.push({});
  };
  const deleteLcbjd = (index) => {
    lcbjdData.splice(index, 1);
  };

  // 里程碑计划审批表
  let lcbspData = ref([]);
  const addLcbspFile = (res, file) => {
    lcbspData.value.push(res.data);
  };
  const deleteLcbsp = (index) => {
    ElMessageBox.confirm("确认删除该项？", "提示", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        lcbspData.value.splice(index, 1);
      })
      .catch(() => {});
  };
  let lcbspSelectIds = ref([]);
  const lcbspSelectionChange = (datas) => {
    lcbspSelectIds.value = datas.map((item) => item.id);
  };
  const multiDeleteLcbsp = () => {
    ElMessageBox.confirm("确认删除选中数据？", "提示", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        lcbspData.value = lcbspData.value.filter(
          (item) => !lcbspSelectIds.value.includes(item.id),
        );
      })
      .catch(() => {});
  };

  // 投评决策资料-可研材料
  let kyclData = ref([]);
  const addKyclFile = (res, file) => {
    kyclData.value.push(res.data);
  };
  const deleteKycl = (index) => {
    ElMessageBox.confirm("确认删除该项？", "提示", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        kyclData.value.splice(index, 1);
      })
      .catch(() => {});
  };
  let kyclSelectIds = ref([]);
  const kyclSelectionChange = (datas) => {
    kyclSelectIds.value = datas.map((item) => item.id);
  };
  const multiDeleteKycl = () => {
    ElMessageBox.confirm("确认删除选中数据？", "提示", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        kyclData.value = kyclData.value.filter(
          (item) => !kyclSelectIds.value.includes(item.id),
        );
      })
      .catch(() => {});
  };

  // 投评决策资料-经济指标初步测算资料
  let jjzbcsData = ref([]);
  const addJjzbcsFile = (res, file) => {
    jjzbcsData.value.push(res.data);
  };
  const deleteJjzbcs = (index) => {
    ElMessageBox.confirm("确认删除该项？", "提示", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        jjzbcsData.value.splice(index, 1);
      })
      .catch(() => {});
  };
  let jjzbcsSelectIds = ref([]);
  const jjzbcsSelectionChange = (datas) => {
    jjzbcsSelectIds.value = datas.map((item) => item.id);
  };
  const multiDeleteJjzbcs = () => {
    ElMessageBox.confirm("确认删除选中数据？", "提示", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        jjzbcsData.value = jjzbcsData.value.filter(
          (item) => !jjzbcsSelectIds.value.includes(item.id),
        );
      })
      .catch(() => {});
  };

  // 投评决策资料-补充相关支撑材料
  let tpbczcclData = ref([]);
  const addTpbczcclFile = (res, file) => {
    tpbczcclData.value.push(res.data);
  };
  const deleteTpbczccl = (index) => {
    ElMessageBox.confirm("确认删除该项？", "提示", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        tpbczcclData.value.splice(index, 1);
      })
      .catch(() => {});
  };
  let tpbczcclSelectIds = ref([]);
  const tpbczcclSelectionChange = (datas) => {
    tpbczcclSelectIds.value = datas.map((item) => item.id);
  };
  const multiDeleteTpbczccl = () => {
    ElMessageBox.confirm("确认删除选中数据？", "提示", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        tpbczcclData.value = tpbczcclData.value.filter(
          (item) => !tpbczcclSelectIds.value.includes(item.id),
        );
      })
      .catch(() => {});
  };

  // 投评决策资料-其他相关支撑材料
  let tpzcclData = ref([]);
  const addTpzcclFile = (res, file) => {
    tpzcclData.value.push(res.data);
  };
  const deleteTpzccl = (index) => {
    ElMessageBox.confirm("确认删除该项？", "提示", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        tpzcclData.value.splice(index, 1);
      })
      .catch(() => {});
  };
  let tpzcclSelectIds = ref([]);
  const tpzcclSelectionChange = (datas) => {
    tpzcclSelectIds.value = datas.map((item) => item.id);
  };
  const multiDeleteTpzccl = () => {
    ElMessageBox.confirm("确认删除选中数据？", "提示", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        tpzcclData.value = tpzcclData.value.filter(
          (item) => !tpzcclSelectIds.value.includes(item.id),
        );
      })
      .catch(() => {});
  };

  // 投评决策资料-上会议案
  let shyaData = ref([]);
  const addShyaFile = (res, file) => {
    shyaData.value.push(res.data);
  };
  const deleteShya = (index) => {
    ElMessageBox.confirm("确认删除该项？", "提示", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        shyaData.value.splice(index, 1);
      })
      .catch(() => {});
  };
  let shyaSelectIds = ref([]);
  const shyaSelectionChange = (datas) => {
    shyaSelectIds.value = datas.map((item) => item.id);
  };
  const multiDeleteShya = () => {
    ElMessageBox.confirm("确认删除选中数据？", "提示", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        shyaData.value = shyaData.value.filter(
          (item) => !shyaSelectIds.value.includes(item.id),
        );
      })
      .catch(() => {});
  };

  // 决策-项目投资总额全口径
  let jcTzzeqkjData = reactive([
    {
      xh: "一",
      zb: "项目造价构成",
      dw: "",
      key: "1",
      children: [
        {
          xh: "1",
          zb: "建安设备费",
          dw: "万元",
          key: "1.1",
          parentKey: "1",
        },
        {
          xh: "2",
          zb: "其他费用",
          dw: "万元",
          key: "1.2",
          parentKey: "1",
          children: [
            {
              xh: "2.1",
              zb: "其中：土地相关费用",
              dw: "万元",
              key: "1.2.1",
              parentKey: "1.2",
            },
          ],
        },
        {
          xh: "3",
          zb: "基本预备费",
          dw: "万元",
          key: "1.3",
          parentKey: "1",
        },
        {
          xh: "4",
          zb: "建设期利息",
          dw: "万元",
          key: "1.4",
          parentKey: "1",
        },
      ],
    },
    {
      xh: "二",
      zb: "项目资金构成",
      dw: "",
      key: "2",
      children: [
        {
          xh: "1",
          zb: "项目资本金",
          dw: "万元",
          key: "2.1",
          parentKey: "2",
          children: [
            {
              xh: "1.1",
              zb: "能建方出资",
              dw: "万元",
              key: "2.1.1",
              parentKey: "2.1",
            },
            {
              xh: "1.2",
              zb: "外部股东",
              dw: "万元",
              key: "2.1.2",
              parentKey: "2.1",
            },
          ],
        },
        {
          xh: "2",
          zb: "贷款",
          dw: "万元",
          key: "2.2",
          parentKey: "2",
          children: [
            {
              xh: "2.1",
              zb: "其中：非并表项目我方贷款/担保额",
              dw: "万元",
              key: "2.2.1",
              parentKey: "2.2",
            },
          ],
        },
        {
          xh: "3",
          zb: "其他出资",
          dw: "万元",
          key: "2.3",
          parentKey: "2",
          children: [
            {
              xh: "3.1",
              zb: "其中：能建方出资",
              dw: "万元",
              key: "2.3.1",
              parentKey: "2.3",
            },
          ],
        },
      ],
    },
    {
      xh: "",
      zb: "批复总出资",
      dw: "万元",
      key: "3",
    },
  ]);

  // 财务评价指标
  let jcCwpjzbData = reactive([
    {
      name: "项目投资内部收益率（税前）",
      unit: "%",
    },
    {
      name: "项目资本金内部收益率（税后）",
      unit: "%",
    },
    {
      name: "项目投资回收期（税前）",
      unit: "年",
    },
    {
      name: "能建方内部收益率",
      unit: "%",
    },
    {
      name: "净现金流首次回正年份",
      type: "year",
    },
    {
      name: "净利润首次回正年份",
      type: "year",
    },
    {
      name: "可分配利润首次回正年份",
      type: "year",
    },
  ]);

  // 核心边界条件
  let jcHxbjtjData = reactive([]);
  const addJchxbjtj = () => {
    jcHxbjtjData.push({
      njfhlDw: "%",
      ykphddw: "%",
      xsfwnlDw: "万元",
      dwscfwzcbDw: "万元",
    });
  };
  const deleteJchxbjtj = (index) => {
    jcHxbjtjData.splice(index, 1);
  };

  // 决策主体审核情况
  let jcztshqkData = reactive([]);
  const addJcztshqk = () => {
    jcztshqkData.push({});
  };
  const deleteJcztshqk = (index) => {
    jcztshqkData.splice(index, 1);
  };
  const addJcztshqkFile = (res, file, row) => {
    row.hyjy = {};
    Object.assign(row.hyjy, res.data);
  };
  const deleteJcztshqkFile = (row, index) => {
    let { hyjy, ...data } = row;
    jcztshqkData[index] = data;
  };

  // 投资主体审核情况
  let tzztshqkData = reactive([]);
  const addTzztshqk = () => {
    tzztshqkData.push({});
  };
  const deleteTzztshqk = (index) => {
    tzztshqkData.splice(index, 1);
  };
  const addTzztshqkFile = (res, file, row) => {
    row.fj = {};
    Object.assign(row.fj, res.data);
  };
  const deleteTzztshqkFile = (row, index) => {
    let { fj, ...data } = row;
    tzztshqkData[index] = data;
  };

  // 审批意见
  let jcspyjData = reactive([]);
  const addJcspyj = () => {
    jcspyjData.push({});
  };
  const deleteJcspyj = (index) => {
    jcspyjData.splice(index, 1);
  };
  const addJcspyjFile = (res, file, row) => {
    row.zxhffj = {};
    Object.assign(row.zxhffj, res.data);
  };
  const deleteRowFile = (row, index) => {
    let { zxhffj, ...data } = row;
    jcspyjData[index] = data;
  };

  // 申请报告
  let bhqkData = reactive([
    { lx: "投标保证金", tjzt: "", je: null, tjsj: "", thsj: "" },
    { lx: "投资人履约保函", tjzt: "", je: null, tjsj: "", thsj: "" },
    { lx: "建设期履约保函", tjzt: "", je: null, tjsj: "", thsj: "" },
    { lx: "运营期履约保函", tjzt: "", je: null, tjsj: "", thsj: "" },
    { lx: "移交履约保函", tjzt: "", je: null, tjsj: "", thsj: "" },
  ]);

  // 经济可行性-净现金流
  let jxjlData = reactive([]);
  const addJxjl = () => {
    jxjlData.push({ nf: "", je: null });
  };
  const deleteJxjl = (index) => {
    jxjlData.splice(index, 1);
  };

  // 经济可行性-净利润
  let jlrData = reactive([]);
  const addJlr = () => {
    jlrData.push({ nf: "", je: null });
  };
  const deleteJlr = (index) => {
    jlrData.splice(index, 1);
  };

  return {
    jcRadioClick,
    jcSwlzbData,
    addJcSwlzb,
    deleteJcSwlzb,
    jcGdxxData,
    addJcGdxx,
    deleteJcGdxx,
    jcgdxxSums,
    changeJcGdxxSums,
    jcgdxxIndexMethod,
    dbzqData,
    addDbzq,
    deleteDbzq,
    fxglData,
    addFxgl,
    deleteFxgl,
    lcbjdData,
    addLcbjd,
    deleteLcbjd,
    lcbspData,
    addLcbspFile,
    deleteLcbsp,
    lcbspSelectIds,
    lcbspSelectionChange,
    multiDeleteLcbsp,
    kyclData,
    addKyclFile,
    deleteKycl,
    kyclSelectIds,
    kyclSelectionChange,
    multiDeleteKycl,
    jjzbcsData,
    addJjzbcsFile,
    deleteJjzbcs,
    jjzbcsSelectIds,
    jjzbcsSelectionChange,
    multiDeleteJjzbcs,
    tpbczcclData,
    addTpbczcclFile,
    deleteTpbczccl,
    tpbczcclSelectIds,
    tpbczcclSelectionChange,
    multiDeleteTpbczccl,
    tpzcclData,
    addTpzcclFile,
    deleteTpzccl,
    tpzcclSelectIds,
    tpzcclSelectionChange,
    multiDeleteTpzccl,
    shyaData,
    addShyaFile,
    deleteShya,
    shyaSelectIds,
    shyaSelectionChange,
    multiDeleteShya,
    jcTzzeqkjData,
    jcCwpjzbData,
    jcHxbjtjData,
    addJchxbjtj,
    deleteJchxbjtj,
    jcztshqkData,
    addJcztshqk,
    deleteJcztshqk,
    addJcztshqkFile,
    deleteJcztshqkFile,
    tzztshqkData,
    addTzztshqk,
    deleteTzztshqk,
    addTzztshqkFile,
    deleteTzztshqkFile,
    jcspyjData,
    addJcspyj,
    deleteJcspyj,
    addJcspyjFile,
    deleteRowFile,
    bhqkData,
    jxjlData,
    addJxjl,
    deleteJxjl,
    jlrData,
    addJlr,
    deleteJlr,
  };
}
