/** @format */
import * as React from 'react';
import AdminSet from './components/adminSet';
import {useAccountEdit, useEditDatas, AccountSaveProps} from '../../../service/accountList';
import {decodeBase64} from '../../../utils/base64';

interface ValProps extends AccountSaveProps {
    admin_id: number | string | null;
}
const AccountEdit: React.FC = props => {
    const {history} = props;
    const [addState, requestAdd] = useAccountEdit();
    const admin_id: string | number | null = new URLSearchParams(decodeBase64(location.search.slice(1))).get('id');
    const [editState, requestEdit] = useEditDatas(admin_id);
    const handleSubmit = (value: AccountSaveProps) => {
        value.admin_id = parseInt(admin_id);
        delete value.admin_name;
        requestAdd(value).then(error => {
            if (error) return;
            history.push('/home/users/list');
        });
    };
    return <AdminSet onsubmit={handleSubmit} values={editState.dataSource} history={history} />;
};
export default AccountEdit;
