import Button, { EButtonVariant } from "@Front/Components/DesignSystem/Button";
import Form from "@Front/Components/DesignSystem/Form";
import InputField from "@Front/Components/DesignSystem/Form/Elements/InputField";
import Typography, { ITypo } from "@Front/Components/DesignSystem/Typography";
import BackArrow from "@Front/Components/Elements/BackArrow";
import DefaultNotaryDashboard, { IDashBoardFolder } from "@Front/Components/LayoutTemplates/DefaultNotaryDashboard";
import Module from "@Front/Config/Module";
import Link from "next/link";
import { NextRouter, useRouter } from "next/router";

import BasePage from "../../Base";
import classes from "./classes.module.scss";
import Folders from "@Front/Api/LeCoffreApi/SuperAdmin/Folders/Folders";

type IProps = {};

type IPropsClass = IProps & {
	folderUid: string;
	router: NextRouter;
};

type IState = {
	folder: IDashBoardFolder | null;
};
class UpdateFolderDescriptionClass extends BasePage<IPropsClass, IState> {
	private backwardPath = Module.getInstance().get().modules.pages.Folder.pages.FolderInformation.props.path.replace("[folderUid]", this.props.folderUid);
	constructor(props: IPropsClass) {
		super(props);
		this.state = {
			folder: null,
		};
		this.onFormSubmit = this.onFormSubmit.bind(this);
	}
	public override render(): JSX.Element {
		return (
			<DefaultNotaryDashboard title={"Ajouter client(s)"}>
				<div className={classes["root"]}>
					<div className={classes["back-arrow"]}>
						<BackArrow url={this.backwardPath} />
					</div>
					<Typography typo={ITypo.H1Bis}>Modifier la note du dossier</Typography>

					<Form className={classes["form"]} onSubmit={this.onFormSubmit}>
						<div className={classes["content"]}>
							<InputField
								name="description"
								fakeplaceholder="Note du dossier"
								textarea
								defaultValue={this.state.folder?.description ?? ""}
							/>
						</div>

						<div className={classes["button-container"]}>
							<Link href={this.backwardPath} className={classes["cancel-button"]}>
								<Button variant={EButtonVariant.GHOST}>Annuler</Button>
							</Link>
							<Button type="submit">Enregistrer</Button>
						</div>
					</Form>
				</div>
			</DefaultNotaryDashboard>
		);
	}

	public override async componentDidMount() {
		const folder = await Folders.getInstance().getByUid(this.props.folderUid);
		this.setState({ folder });
	}

	private async onFormSubmit(e: React.FormEvent<HTMLFormElement> | null, values: { [key: string]: string }) {
		try {
			await Folders.getInstance().put(this.props.folderUid, values);
			this.props.router.push(this.backwardPath);
		} catch (error) {
			console.error(error)	
		}
	}
}

export default function UpdateFolderDescription(props: IProps) {
	const router = useRouter();
	let { folderUid } = router.query;
	folderUid = folderUid as string;

	return <UpdateFolderDescriptionClass folderUid={folderUid} router={router} />;
}
