import React, {useRef} from 'react';
import {PageContainer} from "@ant-design/pro-layout";
import {ProForm, ProFormInstance, ProFormText} from "@ant-design/pro-components";
import styles from './index.less';
import {Card, message} from "antd";
import {useModel} from "@@/plugin-model/useModel";
import {updateSelf} from "@/services/user";
import {USER_TYPE_INSTITUTION} from "@/services/constants";

const UserInfo: React.FC = (props) => {
  const formRef = useRef<ProFormInstance>();
  const {initialState} = useModel('@@initialState');
  const {currentUser} = initialState;

  const handleSubmit = async (values: API.UpdateUserParams) => {
    updateSelf({
      ...values
    }).then(data => {
      if (data.code == 200) {
        message.success("更新成功");
      } else {
        message.error(data.message)
      }
    }).catch(data => {
      console.log(data)
    })
  };

  return (
    <div>
      <PageContainer>
        <Card>
          <div className={styles.content}>
            <ProForm
              formRef={formRef}
              initialValues={{}}
              submitter={{
                render: (_, dom) => dom.pop(),
                submitButtonProps: {
                  size: 'large',
                  style: {
                    width: '100%',
                  },
                },
                searchConfig: {
                  submitText: '提交',
                },
              }}
              onFinish={async (values) => {
                await handleSubmit(values);
              }}
              request={async () => {
                return {...currentUser}
              }}
            >
              <>
                <ProFormText name="userId" hidden={true}/>
                <ProFormText name="institutionName" label="所属企业" disabled/>
                {
                  currentUser?.type == USER_TYPE_INSTITUTION &&
                  <><ProFormText name="department" label="部门" rules={[{max: 20},]} disabled placeholder={""}/>
                    <ProFormText name="profession" label="职位" rules={[{max: 20},]} disabled placeholder={""}/>
                  </>
                }

                <ProFormText name="phone" label="手机号" disabled/>
                <ProFormText name="name" label="名字" rules={[{max: 20}]}/>
                <ProFormText name="idCard" label="身份证号" rules={[{max: 18}, {min: 18}]}/>

              </>
            </ProForm>
          </div>
        </Card>
      </PageContainer>
    </div>
  );
}

export default UserInfo
