// @ts-nocheck
import React, { useEffect } from 'react';
import { loginService } from '@/service';

const OtherAuthLogin = (props) => {
  const { location } = props;

  useEffect(() => {
    cbUrl();
  }, []);

  const cbUrl = async () => {
    const { query } = location || {};
    const { code, loginType, callback } = query;
    if (loginType && code) {
      if (loginType === 'wechat') {
        const params = {
          kind: 8,
          wechatMP: {
            code: code,
          },
        };

        const loginRes = await loginService.loginByWechatCode(params);
        if (loginRes?.resultCode !== 'OK') {
          if (props?.isShowMsg) {
            message.error(loginRes?.msg || '登录失败');
          }
          if (props?.onFailed) {
            props?.onFailed(loginRes);
          }
          return;
        }
        if (props?.onSuccess && loginRes?.resultCode === 'OK') {
          if (props?.isShowMsg) {
            message.success('登录成功');
          }
          props.onSuccess(loginRes?.data);
        }
      }
    } else if (code) {
      const params = {
        grant_type: 'password',
        login_type: 'external',
        kind: 5,
        username: code,
      };

      const loginRes = await loginService.loginByParams(params);
      if (loginRes?.error) {
        if (props?.isShowMsg) {
          message.error(loginRes?.msg || '登录失败');
        }
        if (props?.onFailed) {
          props?.onFailed(loginRes);
        }
        return;
      }
      if (props?.onSuccess) {
        if (props?.isShowMsg) {
          message.success('登录成功');
        }
        props.onSuccess(loginRes);
      }
    } else {
      props?.onFailed('登录失败');
      // 登录失败
    }
  };
  // /wechatlogin?callback=&code=061rrYm12KA65U08Cpn12SqYm12rrYmf&state=STATE

  return React.cloneElement(props.children);
};

export default OtherAuthLogin;
