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

export function useLixiangData(formData, gdlxListRef) {
  // gdlxListRef will be set later from resource data
  // 单选框点击事件
  const radioClick = (value, key) => {
    if (formData[key] !== value) {
      formData[key] = value;
    } else {
      delete formData[key];
    }
  };
  let uploadDialogVisible = ref(false);

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

  // 股东信息
  let gdxxData = reactive([{}]);
  const addGdxx = () => {
    gdxxData.splice(gdxxData.length - 1, 0, {});
  };
  const deleteGdxx = (index) => {
    gdxxData.splice(index, 1);
  };
  const gdxxIndexMethod = (index) => {
    return index === gdxxData.length - 1 ? "" : index + 1;
  };
  let gdxxSums = ref({});
  const changeGdxxSums = () => {
    let obj = {};
    gdxxData.forEach((item, index) => {
      if (index < gdxxData.length - 1) {
        if (item.gdlx) {
          if (!obj[item.gdlx]) {
            obj[item.gdlx] = {
              name: gdlxListRef.filter((gdlx) => gdlx.key == item.gdlx)[0]
                ?.value,
            };
          }
          obj[item.gdlx].cgbl = new Decimal(
            Number(obj[item.gdlx].cgbl || 0),
          ).plus(new Decimal(Number(item.cgbl || 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)));
        }
      }
    });
    gdxxSums.value = obj;
  };

  // 计算对象总和
  const getObjSums = (obj, objkey) => {
    return Object.entries(obj).reduce(
      (acc, [key, item]) => acc.plus(new Decimal(item[objkey] || 0)),
      new Decimal(0),
    );
  };

  // 支撑材料
  let zcclData = ref([]);
  const addZcclFile = (res, file) => {
    uploadDialogVisible.value = false;
    zcclData.value.push(res.data);
  };
  const deleteZccl = (index) => {
    ElMessageBox.confirm("确认删除该项？", "提示", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        zcclData.value.splice(index, 1);
      })
      .catch(() => {});
  };
  let zcclSelectIds = ref([]);
  const zcclSelectionChange = (datas) => {
    zcclSelectIds.value = datas.map((item) => item.id);
  };
  const multiDeleteZccl = () => {
    ElMessageBox.confirm("确认删除选中数据？", "提示", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        zcclData.value = zcclData.value.filter(
          (item) => !zcclSelectIds.value.includes(item.id),
        );
      })
      .catch(() => {});
  };

  // 项目投资总额全口径
  let tzzeqkjData = reactive([
    {
      xh: "1",
      zb: "项目资本金",
      dw: "万元",
      key: "1",
      children: [
        {
          xh: "1.1",
          zb: "能建方出资",
          dw: "万元",
          key: "1.1",
          parentKey: "1",
        },
        {
          xh: "1.2",
          zb: "外部股东",
          dw: "万元",
          key: "1.2",
          parentKey: "1",
        },
      ],
    },
    {
      xh: "2",
      zb: "贷款",
      dw: "万元",
      key: "2",
      children: [
        {
          xh: "2.1",
          zb: "其中：非并表项目我方贷款/担保额",
          dw: "万元",
          key: "2.1",
          parentKey: "2",
        },
      ],
    },
    {
      xh: "3",
      zb: "其他出资",
      dw: "万元",
      key: "3",
      children: [
        {
          xh: "3.1",
          zb: "其中：能建方出资",
          dw: "万元",
          key: "3.1",
          parentKey: "3",
        },
      ],
    },
    {
      xh: "",
      zb: "批复总投资",
      dw: "万元",
      key: "4",
    },
  ]);

  // 财务评价指标
  let cwpjzbData = reactive([
    {
      name: "项目投资内部收益率（税后）",
      unit: "%",
    },
    {
      name: "项目资本金内部收益率（税后）",
      unit: "%",
    },
    {
      name: "项目投资回收期（税前）",
      unit: "年",
    },
  ]);

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

  // 项目立项信息--立项批复文件
  let lxpfwjData = ref([]);
  const addLxpfFile = (res, file) => {
    lxpfwjData.value.push(res.data);
  };
  const deleteLxpf = (index) => {
    ElMessageBox.confirm("确认删除该项？", "提示", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        lxpfwjData.value.splice(index, 1);
      })
      .catch(() => {});
  };
  let lxpfSelectIds = ref([]);
  const lxpfSelectionChange = (datas) => {
    lxpfSelectIds.value = datas.map((item) => item.id);
  };
  const multiDeleteLxpf = () => {
    ElMessageBox.confirm("确认删除选中数据？", "提示", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        lxpfwjData.value = lxpfwjData.value.filter(
          (item) => !lxpfSelectIds.value.includes(item.id),
        );
      })
      .catch(() => {});
  };

  // 审批意见
  let spyjTableData = reactive([]);
  const addSpyj = () => {
    spyjTableData.push({});
  };
  const deleteSpyj = (index) => {
    spyjTableData.splice(index, 1);
  };
  let shclData = ref([]);
  const addShclFile = (res, file) => {
    shclData.value.push(res.data);
  };
  const deleteShcl = (index) => {
    ElMessageBox.confirm("确认删除该项？", "提示", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        shclData.value.splice(index, 1);
      })
      .catch(() => {});
  };
  let shclSelectIds = ref([]);
  const shclSelectionChange = (datas) => {
    shclSelectIds.value = datas.map((item) => item.id);
  };
  const multiDeleteShcl = () => {
    ElMessageBox.confirm("确认删除选中数据？", "提示", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        shclData.value = shclData.value.filter(
          (item) => !shclSelectIds.value.includes(item.id),
        );
      })
      .catch(() => {});
  };

  return {
    radioClick,
    swlzbData,
    addSwlzb,
    deleteSwlzb,
    gdxxData,
    addGdxx,
    deleteGdxx,
    gdxxSums,
    changeGdxxSums,
    gdxxIndexMethod,
    getObjSums,
    zcclData,
    addZcclFile,
    deleteZccl,
    zcclSelectIds,
    zcclSelectionChange,
    multiDeleteZccl,
    tzzeqkjData,
    cwpjzbData,
    hxbjtjData,
    addHxbjtj,
    deleteHxbjtj,
    lxpfwjData,
    addLxpfFile,
    deleteLxpf,
    lxpfSelectIds,
    lxpfSelectionChange,
    multiDeleteLxpf,
    spyjTableData,
    addSpyj,
    deleteSpyj,
    shclData,
    addShclFile,
    deleteShcl,
    shclSelectIds,
    shclSelectionChange,
    multiDeleteShcl,
    uploadDialogVisible,
  };
}
