<?php

    class EducationExperiences extends Core {

        function list() {

            $id = $_GET['id'];
            $sql = "SELECT * FROM education_experience WHERE lead_id = ?";
            $stmt = parent::$conn->prepare($sql);
            $stmt->execute([ $id ]);
            $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

            return [ 'type' => 'OK', 'data' => $result ];

        }

        function store() {

            $lead_id = $_POST['lead_id'];
            $course_name = $_POST['courseName'];
            $institution_name = $_POST['institutionName'];
            $date_start = $_POST['dateStart'];
            $date_end = $_POST['currentCourse'] == 'true' ? null : $_POST['dateEnd'];
            $education_level = $_POST['educationLevel'];

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

            /*var_dump(parent::$conn->errorInfo());
            var_dump($stmt->errorInfo());*/

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

        }

        function update() {

            $id = $_POST['id'];
            $course_name = $_POST['courseName'];
            $institution_name = $_POST['institutionName'];
            $date_start = $_POST['dateStart'];
            $date_end = $_POST['currentCourse'] == 'true' ? null : $_POST['dateEnd'];
            $education_level = $_POST['educationLevel'];
            $sql = "UPDATE education_experience SET course_name = ?, institution_name = ?, 
                date_start = ?, date_end = ?, education_level = ? WHERE id = ?";
            $stmt = parent::$conn->prepare($sql);
            
            $stmt->execute([
                $course_name,
                $institution_name,
                $date_start,
                $date_end,
                $education_level,
                $id
            ]);

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

        }

        function delete() {

            $id = $_GET['id'];

            $sql = "DELETE FROM education_experience WHERE id = ? ORDER BY id DESC";
            $stmt = parent::$conn->prepare($sql);
            $stmt->execute([ $id ]);

            return [ 'type' => 'OK', 'data' => $stmt->rowCount() ];

        }

    }