import SuccessButton from "@/Admin/Components/Button/SuccessButton";
import FileUpload from "@/Admin/Components/Inputs/FileUpload";
import TextInput from "@/Admin/Components/Inputs/TextInput";
import SingleMediaUploader from "@/Admin/Components/Media/SingleMediaUploader";
import FromValidationError from "@/Admin/Components/Validation/FromValidationError";
import AdminLayout from "@/Admin/Layouts/AdminLayout";
import { Head, useForm, usePage } from "@inertiajs/react";
import { produce } from "immer";

export default function Edit() {
    const { props } = usePage();
    const user = props.auth.user;

    const { data: profileData, setData: setProfileData, errors: profileErrors, post: postProfile, processing: profileProcessing } = useForm({
        _method: "patch",
        user_image: user?.photo_path,
        name: user.name,
        email: user.email,
        phone: user?.phone,
        country: user?.country,
        address: user?.address,
        password: "",
    });

    const { data: passwordData, setData: setPasswordData, errors: passwordErrors, patch: postPassword, processing: passwordProcessing, } = useForm({
        _method: "patch",
        current_password: "",
        password: "",
        password_confirmation: "",
    });

    const handleProfileUpdate = (e) => {
        e.preventDefault();
        postProfile(route("admin.profile.update", user));
    };

    const handlePasswordUpdate = async (e) => {
        e.preventDefault();
        try {
            await postPassword(route("admin.profile.update.password", user));
            setPasswordData({
                current_password: "",
                password: "",
                password_confirmation: ""
            });
        } catch (error) {
            console.error("Password update failed:", error);
        }
    };


    return (
        <AdminLayout>
            <Head title="Profile Update" />
            <div className="yoo-height-b30 yoo-height-lg-b30" />
            <div className="container">
                <div className="yoo-uikits-heading">
                    <h2 className="yoo-uikits-title">Update Profile</h2>
                </div>
                <div className="yoo-height-b20 yoo-height-lg-b20"></div>
                <form
                    onSubmit={handleProfileUpdate}
                    encType="multipart/form-data"
                >
                    <div className="col-md-10">
                        <div className="yoo-card yoo-style1">
                            <div className="yoo-card-heading">
                                <div className="yoo-card-heading-left">
                                    <h2 className="yoo-card-title">
                                        Profile Details
                                    </h2>
                                </div>
                            </div>
                            <div className="yoo-card-body">
                                <div className="yoo-padd-lr-20">
                                    <div className="yoo-height-b20 yoo-height-lg-b20" />
                                    <div className="form-group">
                                        <SingleMediaUploader
                                            onSelected={(e) => {
                                                setProfileData(
                                                    produce((draft) => {
                                                        draft.user_image = e
                                                    })
                                                )
                                            }}
                                            handleRemoved={() => setProfileData(
                                                produce((draft) => {
                                                    draft.user_image = "";
                                                })
                                            )}
                                            defaultValue={profileData.user_image}
                                        />
                                    </div>
                                    <FromValidationError
                                        message={profileErrors?.user_image}
                                    />
                                    <div className="yoo-height-b20 yoo-height-lg-b20" />
                                    <label htmlFor="name">Name</label>
                                    <TextInput
                                        title="User Name"
                                        type="text"
                                        id="name"
                                        error={profileErrors?.name}
                                        value={profileData.name}
                                        onChange={(e) =>
                                            setProfileData("name", e.target.value)
                                        }
                                    />
                                    <label htmlFor="email">Email</label>
                                    <TextInput
                                        title="User Email"
                                        type="text"
                                        id="email"
                                        error={profileErrors?.email}
                                        value={profileData.email}
                                        onChange={(e) =>
                                            setProfileData("email", e.target.value)
                                        }
                                    />
                                    <label htmlFor="phone">Phone Number</label>
                                    <TextInput
                                        title="User Phone Number"
                                        type="text"
                                        id="phone"
                                        error={profileErrors?.phone}
                                        value={profileData.phone}
                                        onChange={(e) =>
                                            setProfileData("phone", e.target.value)
                                        }
                                    />
                                    <label htmlFor="country">Country</label>
                                    <TextInput
                                        title="Enter Country"
                                        type="text"
                                        id="country"
                                        error={profileErrors?.country}
                                        value={profileData.country}
                                        onChange={(e) =>
                                            setProfileData("country", e.target.value)
                                        }
                                    />
                                    <label htmlFor="address">Address</label>
                                    <TextInput
                                        title="User Address"
                                        type="text"
                                        id="address"
                                        error={profileErrors?.address}
                                        value={profileData.address}
                                        onChange={(e) =>
                                            setProfileData("address", e.target.value)
                                        }
                                    />
                                    <div className="publish-button-group">
                                        <div>
                                            <SuccessButton
                                                isLoading={profileProcessing}
                                            >
                                                Update Profile
                                            </SuccessButton>
                                        </div>
                                    </div>
                                    <div className="yoo-height-b20 yoo-height-lg-b20" />
                                </div>
                            </div>
                        </div>
                    </div>
                </form>

                <form className="" onSubmit={handlePasswordUpdate}>
                    <div className="col-md-10">
                        <div className="yoo-card yoo-style1 mt-3">
                            <div className="yoo-card-heading">
                                <div className="yoo-card-heading-left">
                                    <h2 className="yoo-card-title">
                                        Change Password
                                    </h2>
                                </div>
                            </div>
                            <div className="yoo-card-body">
                                <div className="yoo-padd-lr-20">
                                    <div className="yoo-height-b20 yoo-height-lg-b20" />
                                    <label htmlFor="current_password">Current Password</label>
                                    <TextInput
                                        title="Current Password"
                                        type="password"
                                        id="current_password"
                                        error={passwordErrors?.current_password}
                                        value={passwordData.current_password}
                                        onChange={(e) =>
                                            setPasswordData(
                                                "current_password",
                                                e.target.value
                                            )
                                        }
                                    />
                                    <label htmlFor="password">New Password</label>
                                    <TextInput
                                        title="New Password"
                                        type="password"
                                        id="password"
                                        error={passwordErrors?.password}
                                        value={passwordData.password}
                                        onChange={(e) =>
                                            setPasswordData(
                                                "password",
                                                e.target.value
                                            )
                                        }
                                    />
                                    <label htmlFor="password_confirmation">Confirm Password</label>
                                    <TextInput
                                        title="Confirm Password"
                                        type="password"
                                        id="password_confirmation"
                                        error={
                                            passwordErrors?.password_confirmation
                                        }
                                        value={passwordData.password_confirmation}
                                        onChange={(e) =>
                                            setPasswordData(
                                                "password_confirmation",
                                                e.target.value
                                            )
                                        }
                                    />
                                    <div className="publish-button-group">
                                        <div>
                                            <SuccessButton
                                                isLoading={passwordProcessing}
                                            >
                                                Update Password
                                            </SuccessButton>
                                        </div>
                                    </div>
                                    <div className="yoo-height-b20 yoo-height-lg-b20" />
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </AdminLayout>
    );
}
