// @ts-nocheck
import React, { useState } from 'react';
import styles from './styles.less';
import BaseModal from '../../base';
import { message } from 'antd4';

import { CommonLoginFormPropType } from '@/propType/form/loginForm';

import { BindOrgInfoForm } from '../../component/form';

import { registerService } from '@/service';
import { errorCodeInfo } from '@/utils/errorInfo';

interface PropTypes {
  // 是否显示
  visible: boolean;
  // 是否显示手机号信息
  isShowUserInfo?: boolean;
  // 是否显示机构信息
  isShowOrgInfoGroup?: boolean;
  // 登录用户获取的token
  token: string;
  // 传入用户信息，不包含机构信息
  userInfo: {
    username: string;
    phone?: string;
  };
  // 传入机构信息
  orgInfo: {
    name?: string;
    type?: string;
    service_id: string;
  };
}

const BindOrgInfoModal = (props: PropTypes) => {
  const {
    visible,
    isShowPhoneGroup,
    isShowOrgInfoGroup,
    isShowUserName,
    orgInfo,
    userInfo,
  } = props;

  const handleBindOrgInfoSuccess = (values) => {
    if (props?.onSuccess) {
      props.onSuccess(values);
    }
  };

  const handleBindOrgInfoFailed = (errorInfo) => {
    if (props?.onFailed) {
      props.onFailed(errorInfo);
    }
  };

  const commonFormProps: CommonLoginFormPropType = {
    isShowMsg: true,
    onSuccess: handleBindOrgInfoSuccess,
    onFailed: handleBindOrgInfoFailed,
  };

  return (
    <BaseModal {...props} width={600}>
      <BindOrgInfoModalContent
        userInfo={userInfo}
        orgInfo={orgInfo}
        isShowPhoneGroup={isShowPhoneGroup}
        isShowOrgInfoGroup={isShowOrgInfoGroup}
        isShowUserName={isShowUserName}
        {...commonFormProps}
      />
    </BaseModal>
  );
};

BindOrgInfoModal.defaultProps = {
  visible: false,
  token: true,
  isShowPhoneGroup: true,
  isShowOrgInfoGroup: true,
  isShowUserName: false,
  userInfo: {
    username: '',
  },
  orgInfo: null,
};

export default BindOrgInfoModal;

export const BindOrgInfoModalContent = (props) => {
  const [errorInfo, setErrorInfo] = useState('');

  const {
    userInfo,
    orgInfo,
    isShowPhoneGroup,
    isShowOrgInfoGroup,
    isShowUserName,
  } = props;

  const handleFormFinish = async (values) => {
    if (values) {
      try {
        const result = await registerService.bindOrgInfoByBusiness({
          ...values,
          access_token: props?.token || '',
        });
        if (result?.code === 0 && result?.message === 'success') {
          message.success('添加机构成功');
          if (props?.onSuccess) {
            props.onSuccess(result?.data);
          }
        } else {
          const errorInfo =
            errorCodeInfo.find((item) => item.code === result?.code)?.title ||
            '';
          setErrorInfo(errorInfo || '添加机构失败');
          if (props?.onFailed) {
            props?.onFailed(result);
          }
          throw new Error('添加机构失败');
        }
      } catch (e) {
        message.error('添加机构失败');
      }
    }
  };

  const renderHeaderGroup = () => {
    return (
      <div className={styles.headerGroup}>
        <div>添加机构信息</div>
      </div>
    );
  };

  const handleClearErrorInfo = () => {
    setErrorInfo('');
  };

  return (
    <div className={styles.bindOrgInfoContainer}>
      {renderHeaderGroup()}
      <BindOrgInfoForm
        errorInfo={errorInfo}
        userInfo={userInfo}
        orgInfo={orgInfo}
        isShowPhoneGroup={isShowPhoneGroup}
        isShowOrgInfoGroup={isShowOrgInfoGroup}
        isShowUserName={isShowUserName}
        onFinish={handleFormFinish}
        onClearErrorInfo={handleClearErrorInfo}
      />
    </div>
  );
};
