import { last } from 'lodash';
import { ref, computed } from 'vue';
import { useRoute } from 'vue-router';
import { getGoafSelectOption } from '../monitor.api';
import { useMineStore } from '/@/store/modules/mine';
import { findNodeAll, findPath } from '/@/utils/helper/treeHelper';

export function useInitForm() {
  const config = {
    id: 'id',
    pid: 'parentId',
    children: 'childDepart',
  };
  const route = useRoute();
  const mineStroe = useMineStore();
  const mineCodes = ref<string[]>([]);
  const getMineTree = computed(() => {
    const id = last(route.fullPath.split('/'));
    return findNodeAll(mineStroe.mineTree, (item) => item.id === id, config);
  });
  const paths = findPath(getMineTree.value, (item) => item.isLeaf, config);

  mineCodes.value = paths.map((e) => e.id);

  // 采空区选择器
  const goafId = ref('');
  const goafOptions = ref<any[]>([]);

  function initGoafOptions() {
    getGoafSelectOption({ mineCode: last(mineCodes.value) }).then(({ options, defaultValue }) => {
      goafOptions.value = options;
      goafId.value = defaultValue;
    });
  }
  initGoafOptions();

  return {
    goafId,
    goafOptions,
    mineCodes,
    getMineTree,
    initGoafOptions,
  };
}
