import { getCsckSystemGroup } from '@/api/common';
import { FormSchema } from '@/components/Form';
import { isNotEmpty } from '@/utils/is';
import { groupBy } from 'lodash-es';
import { onMounted, ref } from 'vue';

export default (
  formMethods,
  componentProps: FormSchema['componentProps'] = {},
  { currentRow, label = '系统组别/组别版本号', actionType }: { currentRow?: any; label?: string; actionType?: string },
) => {
  const options = ref<any[]>([]);

  onMounted(() => {
    formMethods = formMethods?.value ? formMethods.value : formMethods;
    if (currentRow?.systemGroupName && currentRow?.checkVersion) formMethods.setValues({ system_version: [currentRow.systemGroupName, currentRow.checkVersion] });

    if (actionType === 'add') {
      getCsckSystemGroup().then(res => {
        const groups = groupBy(res, 'checkVersionName');
        options.value = Object.keys(groups).map(sysCode => {
          const parent = res.find(item => item['checkVersionName'] === sysCode);
          return {
            value: parent['checkVersionName'],
            label: parent['checkVersionName'],
            children: groups[sysCode].map(item => ({
              value: item['checkVersion'],
              label: item['checkVersion'],
              ...item,
            })),
          };
        });
      });
    }
  });

  const formItem: FormSchema[] = [
    {
      field: 'system_version',
      label,
      component: 'Cascader',
      remove: actionType === 'edit',
      componentProps: {
        style: { width: '100%' },
        ...componentProps,
        options,
        on: {
          change: async (value: string[]) => {
            if (isNotEmpty(value) && Array.isArray(value) && value.length === 2) {
              formMethods = formMethods?.value ? formMethods.value : formMethods;
              const [checkVersionName, checkVersion] = value;
              const instance = await formMethods.getComponentExpose('system_version');
              const nodes = instance.getCheckedNodes();
              console.log(nodes);
              if (nodes.length === 1) {
                const { pathLabels, data } = nodes[0];
                formMethods.setValues({ systemGroupName: pathLabels[0], sourceSystemCode: data.sourceSystemCode, targetSystemCode: data.targetSystemCode });
              }
              formMethods.setValues({ systemGroupName: checkVersionName, checkVersion });
            }
          },
        },
      },
      colProps: { span: 24 },
    },
    {
      field: 'systemGroupName',
      label: '系统组',
      component: 'Input',
      hidden: actionType === 'add',
      componentProps,
    },
    {
      field: 'checkVersion',
      label: '组别版本号',
      component: 'Input',
      hidden: actionType === 'add',
      componentProps,
    },
    {
      field: 'sourceSystemCode',
      label: '源系统编码',
      component: 'Input',
      hidden: true,
    },
    {
      field: 'targetSystemCode',
      label: '目标系统编码',
      component: 'Input',
      hidden: true,
    },
  ];

  return {
    formItem,
  };
};
