import { reactive, ref } from "vue";

export function useResource(formData, jcFormData) {
  let zyList = reactive([]); // 主业/非主业
  let cwbblxList = reactive([]); // 财务报表类型
  let zfspjbList = reactive([]); // 政府审批（核准、备案）级别
  let xmjdList = ref([]); // 项目阶段
  let jnwList = reactive([]); // 境内/外
  let szjList = ref([]); // 省（境内）/洲际（境外）
  let dsgjList = ref([]); // 地市（境内）/国家（境外）
  let qxgjList = ref([]); // 区县（境内）/国家（境外）
  let xmqyList = reactive([]); // 项目区域
  let bzList = reactive([]); // 币种
  let tzsxyjflList = reactive([]); // 投资属性一级分类
  let tzsxejflList = ref([]); // 投资属性二级分类
  let tzsxtzbkList = reactive([]); // 投资属性投资板块
  let tzsxxfbkList = ref([]); // 投资属性细分板块
  let tzmdyjflList = reactive([]); // 投资目的一级分类
  let tzmdejflList = ref([]); // 投资目的二级分类
  let tzmdsxflList = reactive([]); // 投资目的四新分类
  let tzznyjflList = reactive([]); // 投资指南一级分类
  let tzznejflList = ref([]); // 投资指南二级分类
  let gmjjhyList = reactive([]); // 国民经济行业
  const gmjjhyProps = {
    label: "name",
  };
  let gdlxList = reactive([]); // 股东类型
  let hxbjtjlxlxList = reactive([]); // 核心边界条件立项类型
  let hxbjtjzjdwList = reactive([]); // 核心边界条件造价单位
  let hxbjtjykphdlxList = reactive([]); // 核心边界条件盈亏平衡点类型
  let lxzzspyjList = ref([]); // 立项最终审批意见
  let spyjlxList = reactive([]); // 审批意见类型
  // 决策
  let qyggList = reactive([]); // 申报单位企业规格
  let jcSzjList = ref([]); // 省（境内）/洲际（境外）
  let jcDsgjList = ref([]); // 地市（境内）/国家（境外）
  let jcQxgjList = ref([]); // 区县（境内）/国家（境外）
  let xmzjlyList = reactive([]); // 项目资金来源
  let swbxmlxList = reactive([]); // 商务部项目类型
  let xmlxList = reactive([]); // 项目类型
  let tzhsfsList = reactive([]); // 投资回收方式
  let hylxList = reactive([]); // 会议类型
  let jcspyjlx = reactive([]); // 决策审批意见类型
  // 决策二级分类
  let jcTzsxejflList = ref([]);
  let jcTzsxxfbkList = ref([]);
  let jcTzmdejflList = ref([]);
  let jcTzznejflList = ref([]);

  // 级联选择 change 事件 - 立项
  const changeJnw = (val) => {
    if (val || val === 0) {
      let selectData = jnwList.filter((item) => item.key == val);
      szjList.value = (selectData[0] && selectData[0].children) || [];
    } else {
      szjList.value = [];
    }
    dsgjList.value = [];
    qxgjList.value = [];
    delete formData.sjnzjjw;
    delete formData.dsjngjjw;
    delete formData.qxjngjjw;
  };
  const changeSzj = (val) => {
    if (val || val === 0) {
      let selectData = szjList.value.filter((item) => item.key == val);
      dsgjList.value = (selectData[0] && selectData[0].children) || [];
    } else {
      dsgjList.value = [];
    }
    qxgjList.value = [];
    delete formData.dsjngjjw;
    delete formData.qxjngjjw;
  };
  const changeDsgj = (val) => {
    if (val || val === 0) {
      let selectData = dsgjList.value.filter((item) => item.key == val);
      qxgjList.value = (selectData[0] && selectData[0].children) || [];
    } else {
      qxgjList.value = [];
    }
    delete formData.qxjngjjw;
  };
  const changeTzsxyjfl = (val) => {
    if (val || val === 0) {
      let selectData = tzsxyjflList.filter((item) => item.key == val);
      tzsxejflList.value = (selectData[0] && selectData[0].children) || [];
    } else {
      tzsxejflList.value = [];
    }
    delete formData.tzsxejfl;
  };
  const changeTzsxtzbk = (val) => {
    if (val || val === 0) {
      let selectData = tzsxtzbkList.filter((item) => item.key == val);
      tzsxxfbkList.value = (selectData[0] && selectData[0].children) || [];
    } else {
      tzsxxfbkList.value = [];
    }
    delete formData.subsector;
  };
  const changeTzmdyjfl = (val) => {
    if (val || val === 0) {
      let selectData = tzmdyjflList.filter((item) => item.key == val);
      tzmdejflList.value = (selectData[0] && selectData[0].children) || [];
    } else {
      tzmdejflList.value = [];
    }
    delete formData.tzmdejfl;
  };
  const changeTzznyjfl = (val) => {
    if (val || val === 0) {
      let selectData = tzznyjflList.filter((item) => item.key == val);
      tzznejflList.value = (selectData[0] && selectData[0].children) || [];
    } else {
      tzznejflList.value = [];
    }
    delete formData.tzznejfl;
  };

  // 级联选择 change 事件 - 决策
  const changeJcJnw = (val) => {
    if (val || val === 0) {
      let selectData = jnwList.filter((item) => item.key == val);
      jcSzjList.value = (selectData[0] && selectData[0].children) || [];
    } else {
      jcSzjList.value = [];
    }
    jcDsgjList.value = [];
    jcQxgjList.value = [];
    delete jcFormData.sjnzjjw;
    delete jcFormData.dsjngjjw;
    delete jcFormData.qxjngjjw;
  };
  const changeJcSzj = (val) => {
    if (val || val === 0) {
      let selectData = jcSzjList.value.filter((item) => item.key == val);
      jcDsgjList.value = (selectData[0] && selectData[0].children) || [];
    } else {
      jcDsgjList.value = [];
    }
    jcQxgjList.value = [];
    delete jcFormData.dsjngjjw;
    delete jcFormData.qxjngjjw;
  };
  const changeJcDsgj = (val) => {
    if (val || val === 0) {
      let selectData = jcDsgjList.value.filter((item) => item.key == val);
      jcQxgjList.value = (selectData[0] && selectData[0].children) || [];
    } else {
      jcQxgjList.value = [];
    }
    delete jcFormData.qxjngjjw;
  };
  const changeJcTzsxyjfl = (val) => {
    if (val || val === 0) {
      let selectData = tzsxyjflList.filter((item) => item.key == val);
      jcTzsxejflList.value = (selectData[0] && selectData[0].children) || [];
    } else {
      jcTzsxejflList.value = [];
    }
    delete jcFormData.tzsxejfl;
  };
  const changeJcTzsxtzbk = (val) => {
    if (val || val === 0) {
      let selectData = tzsxtzbkList.filter((item) => item.key == val);
      jcTzsxxfbkList.value = (selectData[0] && selectData[0].children) || [];
    } else {
      jcTzsxxfbkList.value = [];
    }
    delete jcFormData.subsector;
  };
  const changeJcTzmdyjfl = (val) => {
    if (val || val === 0) {
      let selectData = tzmdyjflList.filter((item) => item.key == val);
      jcTzmdejflList.value = (selectData[0] && selectData[0].children) || [];
    } else {
      jcTzmdejflList.value = [];
    }
    delete jcFormData.tzmdejfl;
  };
  const changeJcTzznyjfl = (val) => {
    if (val || val === 0) {
      let selectData = tzznyjflList.filter((item) => item.key == val);
      jcTzznejflList.value = (selectData[0] && selectData[0].children) || [];
    } else {
      jcTzznejflList.value = [];
    }
    delete jcFormData.tzznejfl;
  };

  const replaceArr = (arr, data) => {
    arr.splice(0, arr.length, ...data);
  };

  const initResourceData = () => {
    const resourceData = JSON.parse(sessionStorage.getItem("resourceData"));
    replaceArr(zyList, resourceData.zyfzy);
    replaceArr(cwbblxList, resourceData.cwbblx);
    replaceArr(zfspjbList, resourceData.spjb);
    xmjdList.value = resourceData.xmjd;
    replaceArr(jnwList, resourceData.dqlx);
    replaceArr(xmqyList, resourceData.xmqy);
    replaceArr(bzList, resourceData.bizhong);
    replaceArr(tzsxyjflList, resourceData.tzsx);
    replaceArr(tzsxtzbkList, resourceData.tzbk);
    replaceArr(tzmdyjflList, resourceData.tzmd);
    replaceArr(tzmdsxflList, resourceData.sxfl);
    replaceArr(tzznyjflList, resourceData.tzzn);
    replaceArr(gmjjhyList, resourceData.gmjjhy);
    replaceArr(gdlxList, resourceData.gdlx);
    replaceArr(hxbjtjlxlxList, resourceData.hxbjtjywlx);
    replaceArr(hxbjtjzjdwList, resourceData.gczjdw);
    replaceArr(hxbjtjykphdlxList, resourceData.ykphdlx);
    lxzzspyjList.value = resourceData.spyj;
    replaceArr(spyjlxList, resourceData.yjlx);
    // 决策
    replaceArr(qyggList, resourceData.sbdwqygg);
    replaceArr(xmzjlyList, resourceData.xmzjly);
    replaceArr(swbxmlxList, resourceData.swbxmlx);
    replaceArr(xmlxList, resourceData.xmlx);
    replaceArr(tzhsfsList, resourceData.tzhsfs);
    replaceArr(hylxList, resourceData.hylx);
    replaceArr(jcspyjlx, resourceData.jcspyjlx);
  };

  return {
    // resource lists
    zyList, cwbblxList, zfspjbList, xmjdList,
    jnwList, szjList, dsgjList, qxgjList,
    xmqyList, bzList,
    tzsxyjflList, tzsxejflList, tzsxtzbkList, tzsxxfbkList,
    tzmdyjflList, tzmdejflList, tzmdsxflList,
    tzznyjflList, tzznejflList,
    gmjjhyList, gmjjhyProps,
    gdlxList,
    hxbjtjlxlxList, hxbjtjzjdwList, hxbjtjykphdlxList,
    lxzzspyjList, spyjlxList,
    qyggList, jcSzjList, jcDsgjList, jcQxgjList,
    xmzjlyList, swbxmlxList, xmlxList, tzhsfsList,
    hylxList, jcspyjlx,
    jcTzsxejflList, jcTzsxxfbkList, jcTzmdejflList, jcTzznejflList,
    // cascading handlers
    changeJnw, changeSzj, changeDsgj,
    changeTzsxyjfl, changeTzsxtzbk, changeTzmdyjfl, changeTzznyjfl,
    changeJcJnw, changeJcSzj, changeJcDsgj,
    changeJcTzsxyjfl, changeJcTzsxtzbk, changeJcTzmdyjfl, changeJcTzznyjfl,
    // init
    initResourceData,
  };
}
