<?php   

    class MauticEducationExperience extends Core {        

        function store() {
            
            $educations = json_decode($_POST['educations'], true);
            $email = $_POST['email'];

            $sql = "SELECT id FROM leads WHERE email = ?";
            $stmt = parent::$conn->prepare($sql);
            $stmt->execute([ $email ]);
            $lead = $stmt->fetch(PDO::FETCH_ASSOC);

            $sql = "INSERT INTO education_experience(lead_id, education_level, institution_name, course_name, date_start, date_end)
                VALUES (?, ?, ?, ?, ?, ?)";
            $stmt = parent::$conn->prepare($sql);

            foreach($educations as $education) {

                $stmt->execute([
                    $lead['id'],
                    $education['educationLevel'],
                    $education['institutionName'],
                    $education['courseName'],
                    $education['dateStart'],
                    $education['currentCourse'] == 'true' ? null : $education['dateEnd']
                ]);

            }

            return ['type' => 'OK'];

        }

    }