import { initializeApp } from "https://www.gstatic.com/firebasejs/10.12.0/firebase-app.js";
import {
getFirestore,
collection,
addDoc,
serverTimestamp
} from "https://www.gstatic.com/firebasejs/10.12.0/firebase-firestore.js";
import {
getStorage,
ref,
uploadBytes,
getDownloadURL
} from "https://www.gstatic.com/firebasejs/10.12.0/firebase-storage.js";
const firebaseConfig = {
apiKey: "AIzaSyDG0b8DFgY0EzWXze3rJdzyy7CDhnSHNjI",
authDomain: "amg-admin-637c2.firebaseapp.com",
projectId: "amg-admin-637c2",
storageBucket: "amg-admin-637c2.firebasestorage.app",
messagingSenderId: "800937803275",
appId: "1:800937803275:web:a116b9dbb96365045f510e"
};
const EMAILJS_PUBLIC_KEY = "fJEZgUSn3fGMW0IKW";
const EMAILJS_SERVICE_ID = "service_jjryfbn";
const EMAILJS_TEMPLATE_ID = "template_oui6gvz";
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
const storage = getStorage(app);
if (window.emailjs) {
window.emailjs.init(EMAILJS_PUBLIC_KEY);
}
const btnStep2 = document.getElementById("btn-step2");
function isValidEmail(email) {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
}
function sanitizeFileName(name) {
return name.replace(/[^\w.\-]/g, "_");
}
if (btnStep2) {
btnStep2.addEventListener("click", async () => {
const btn = btnStep2;
const nom = document.getElementById("f-nom")?.value.trim() || "";
const email = document.getElementById("f-email")?.value.trim() || "";
const tel = document.getElementById("f-tel")?.value.trim() || "Non renseigné";
const poste = document.getElementById("f-poste")?.value || "";
const experience = document.querySelector("select[name='user_experience']")?.value || "Non renseigné";
const cvInput = document.getElementById("cv");
const cvFile = cvInput?.files?.[0] || null;
if (!nom || !email || !poste) {
alert("Merci de remplir tous les champs obligatoires.");
return;
}
if (!isValidEmail(email)) {
alert("L'adresse e-mail n'est pas valide.");
return;
}
btn.innerHTML = ' Envoi en cours...';
btn.disabled = true;
try {
let cvFilename = "Aucun fichier joint";
let cvUrl = "";
let cvPath = "";
if (cvFile) {
cvFilename = cvFile.name;
const safeFileName = sanitizeFileName(cvFile.name);
const filePath = `cvs/${Date.now()}_${safeFileName}`;
const storageRef = ref(storage, filePath);
await uploadBytes(storageRef, cvFile);
cvUrl = await getDownloadURL(storageRef);
cvPath = filePath;
}
if (!window.emailjs) {
throw new Error("EmailJS n'est pas chargé sur la page.");
}
await window.emailjs.send(
EMAILJS_SERVICE_ID,
EMAILJS_TEMPLATE_ID,
{
user_name: nom,
user_email: email,
user_phone: tel,
user_post: poste,
user_experience: experience,
cv_filename: cvFilename
}
);
await addDoc(collection(db, "candidatures"), {
nom,
email,
tel,
poste,
experience,
cv_filename: cvFilename,
cv_url: cvUrl,
cv_path: cvPath,
statut: "nouveau",
createdAt: serverTimestamp()
});
btn.innerHTML = ' Envoye';
if (typeof window.goToStep === "function") {
window.goToStep(2);
}
} catch (error) {
console.error("Erreur lors de l'envoi :", error);
btn.innerHTML = "Erreur - Reessayer";
btn.style.background = "#8b2020";
btn.disabled = false;
}
});
}