<?php 
session_start();
require_once '../Config/Database.php';
require_once '../Model/Messaging.php';
require_once '../Model/Role.php';
require_once '../Model/Login.php';
require_once '../Model/Employee.php';
require_once '../Model/Student.php';

$role = new Role();
$login = new Login();
$employee = new Employee();
$student = new Student();
$messaging = new Messaging();

if(isset($_GET['::'])){
    $messagings = $messaging->read_all_messagings();
    echo json_encode(
        array("data"=>$messagings)
    );
}

if(isset($_POST['add_messaging'])){
    $message = $_POST['message'];
    $tbData = array("message"=>$message);
    $dbResponse = $messaging->add_messaging($tbData);
    if($dbResponse){    
        echo json_encode(
            array("message"=>"success")
        );
    }else{
        echo json_encode(
            array("message"=>"fail")
        );
    }
}

if(isset($_POST['edit_messaging'])){
    $messaging_id = $_POST['messaging_id'];
    $messaging_name = $_POST['messaging_name'];
    $tbData = array("messaging_name"=>$messaging_name);
    $tb_primary_keys = array("messaging_id"=>$messaging_id);
    $dbResponse = $messaging->update_messaging($tb_primary_keys,$tbData);
    if($dbResponse){
        echo json_encode(
            array("message"=>"success")
        );
    }else{
        echo json_encode(
            array("message"=>"fail")
        );
    }
}

if(isset($_GET['del_messaging'])){
    $messaging_id = $_GET['del_messaging'];
    $dbResponse = $messagings = $messaging->delete_messaging($messaging_id);
    if($dbResponse){
        echo json_encode(
            array("message"=>"success")
        );
    }else{
        echo json_encode(
            array("message"=>"fail")
        );
    }
}


if(isset($_POST['send_sms'])){
    
    $message = $_POST['message'];
    $reason = $_POST['reason'];
    $employees = $_POST['employee'];
    $role = $_POST['roles'];
    $all = $_POST['all'];
    $students = $_POST['students'];
    $custome = $_POST['student'];
    $receiver = '';
    if($employees){
        foreach($employees as $val){
            $employee_details = $employee->read_employee_by_phone($val);
            if($employee_details){
                foreach($employee_details as $key){
                    $employee_phone = $key['employee_phone'];
                    $employee_name = $key['employee_name'];
                    $receiver = $key['employee_name'];
                    $greetings = 'Dear '.$employee_name.', ';
                    $main_sms = $greetings.$message;
                    try{
                        $response = @file_get_contents("https://sms.arkesel.com/sms/api?action=send-sms&api_key=OjBTaXExWUxvNnVtY1JTSE4=&to=$employee_phone&from=3f3&sms=$main_sms");
                        $networkIssue = FALSE;
                        if($response===FALSE){
                            $networkIssue = true;
                         }
                        // set_error_handler(Exception("Database Connection Failed"),0);
                    }catch(Error $e){
                        return false;
                    }   
                    $data = json_decode($response);
                    $tbData = array(
                        "receiver"=>$receiver,
                        "message"=>$message,
                        "reason"=>$reason
                    );
                    $success = $messaging->add_messaging($tbData);
                }

                if($networkIssue){
                    echo json_encode(
                        array(
                            "message"=>"network"
                        )
                    );
                   }else{
                    $success = $messaging->add_messaging($tbData);
                    echo json_encode(
                        array(
                            "message"=>"success"
                        )
                    );
                   }
            }
        }
    }elseif($role){
        foreach($role as $val){
            $role_employees = $employee->read_employee_by_role_id($val);
            $role_details = $role->read_role($val);
            $role_name =  $role_details[0]['role_name'];
            $receiver = $role_name;
            if($role_employees){
                foreach($role_employees as $key){
                    $employee_phone = $key['employee_phone'];
                    $employee_name = $key['employee_name'];
                    $greetings = 'Dear '.$employee_name.', ';
                    $main_sms = $greetings.$message;
                    try{
                        $response = @file_get_contents("https://sms.arkesel.com/sms/api?action=send-sms&api_key=OjBTaXExWUxvNnVtY1JTSE4=&to=$employee_phone&from=3f3&sms=$main_sms");
                         if($response===FALSE){
                            $networkIssue = true;
                         }
                        // set_error_handler(Exception("Database Connection Failed"),0);
                    }catch(Error $e){
                        return false;
                    }   
                    $data = json_decode($response);
                    
                }
                $tbData = array(
                    "receiver"=>$receiver,
                    "message"=>$message,
                    "reason"=>$reason
                );
                if($networkIssue){
                    echo json_encode(
                        array(
                            "message"=>"network"
                        )
                    );
                   }else{
                    $success = $messaging->add_messaging($tbData);
                    echo json_encode(
                        array(
                            "message"=>"success"
                        )
                    );
                   }
            }
        }
    }elseif($all){
        $all_employees = $employee->read_all_employees();
        if($all_employees){
            $receiver = 'All Employees';
            foreach($all_employees as $key){
                $employee_phone = $key['employee_phone'];
                $employee_name = $key['employee_name'];
                $greetings = 'Dear '.$employee_name.', ';
                $main_sms = $greetings.$message;
                try{
                    $response = @file_get_contents("https://sms.arkesel.com/sms/api?action=send-sms&api_key=OjBTaXExWUxvNnVtY1JTSE4=&to=$employee_phone&from=3f3&sms=$main_sms");
                     if($response===FALSE){
                        $networkIssue = true;
                     }
                    // set_error_handler(Exception("Database Connection Failed"),0);
                }catch(Error $e){
                    return false;
                }   
                $data = json_decode($response);
            }
            $tbData = array(
                "receiver"=>$receiver,
                "message"=>$message,
                "reason"=>$reason
            );
            if($networkIssue){
                echo json_encode(
                    array(
                        "message"=>"network"
                    )
                );
               }else{
                $success = $messaging->add_messaging($tbData);
                echo json_encode(
                    array(
                        "message"=>"success"
                    )
                );
               }
        }
    }elseif($students){
        $all_students = $student->read_all_students();
        if($all_students){
            $receiver = 'students';
            foreach($all_students as $key){
                $student_phone = $key['student_phone'];
                $student_name = $key['student_name'];
                $greetings = 'Dear '.$student_name.', ';
                $main_sms = $greetings.$message;
                try{
                    $response = @file_get_contents("https://sms.arkesel.com/sms/api?action=send-sms&api_key=Oml2OXVyTXFjZlhoRURumFc=&to=$student_phone&from=mt%20Olivet&sms=$main_sms");
                     if($response===FALSE){
                        $networkIssue = true;
                     }
                    // set_error_handler(Exception("Database Connection Failed"),0);
                }catch(Error $e){
                    return false;
                }   
                $data = json_decode($response);
            }
            $tbData = array(
                "receiver"=>$receiver,
                "message"=>$message,
                "reason"=>$reason
            );
            
           if($networkIssue){
            echo json_encode(
                array(
                    "message"=>"network"
                )
            );
           }else{
            $success = $messaging->add_messaging($tbData);
            echo json_encode(
                array(
                    "message"=>"success"
                )
            );
           }
        }
    }elseif($multiple_students){
        foreach($multiple_students as $val){
            $employee_details = $employee->read_student($val);
            if($employee_details){
                foreach($employee_details as $key){
                    $student_phone = $key['student_phone'];
                    $student_name = $key['student_name'];
                    $receiver = $key['student_name'];
                    $greetings = 'Dear '.$student_name.', ';
                    $main_sms = $greetings.$message;
                    try{
                        $response = @file_get_contents("https://sms.arkesel.com/sms/api?action=send-sms&api_key=OjBTaXExWUxvNnVtY1JTSE4=&to=$student_phone&from=3f3&sms=$main_sms");
                        $networkIssue = FALSE;
                        if($response===FALSE){
                            $networkIssue = true;
                         }
                        // set_error_handler(Exception("Database Connection Failed"),0);
                    }catch(Error $e){
                        return false;
                    }   
                    $data = json_decode($response);
                    $tbData = array(
                        "receiver"=>$receiver,
                        "message"=>$message,
                        "reason"=>$reason
                    );
                    $success = $messaging->add_messaging($tbData);
                }

                if($networkIssue){
                    echo json_encode(
                        array(
                            "message"=>"network"
                        )
                    );
                   }else{
                    $success = $messaging->add_messaging($tbData);
                    echo json_encode(
                        array(
                            "message"=>"success"
                        )
                    );
                   }
            }
        }
    }


}