<?php

    class EducationExperiences extends Core {

        function list() {

            $id = $_GET['id'];
            $sql = "SELECT * FROM education_experience WHERE lead_id = ? ORDER BY date_start DESC, date_end DESC";
            $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['dateEnd'];
            $current_course =  $_POST['currentCourse'] == 'true' ? 1 : 0;
            $education_level = $_POST['educationLevel'];

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

            /*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['dateEnd'];
            $current_course =  $_POST['currentCourse'] == 'true' ? 1 : 0;
            $education_level = $_POST['educationLevel'];
            $sql = "UPDATE education_experience SET course_name = ?, institution_name = ?, 
                date_start = ?, date_end = ?, education_level = ?, current_course = ? WHERE id = ?";
            $stmt = parent::$conn->prepare($sql);
            
            $stmt->execute([
                $course_name,
                $institution_name,
                $date_start,
                $date_end,
                $education_level,
                $current_course,
                $id
            ]);

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

        }

        function updateLevel(){
            
            $educationLevel = $_POST['educationLevel'];
            $email = $_POST['email'];

            $sql = "SELECT id FROM leads WHERE email = ?";
            $stmt = parent::$conn->prepare($sql);
            $stmt->execute([ $email ]);
            $lead = $stmt->fetch(PDO::FETCH_ASSOC);
            
            if($educationLevel){
                $sql = "UPDATE leads SET leveleducation = ? WHERE id = ?";
                $stmt = parent::$conn->prepare($sql);
                $stmt->execute([$educationLevel, $lead['id']]);
            }
            
            return ['type' => 'OK'];
        }

        function getLevelSchool() {

            $id = $_GET['id'];
            $sql = "SELECT leveleducation FROM leads WHERE id = ?";
            $stmt = parent::$conn->prepare($sql);
            $stmt->execute([ $id ]);
            $result = $stmt->fetch(PDO::FETCH_ASSOC);
            return [ 'type' => 'OK', 'data' => $result ];

        }

        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() ];

        }

    }