<?php
session_start();
require_once '../Config/Database.php';
require_once '../Model/Role.php';
require_once '../Model/Login.php';
require_once '../Model/Employee.php';
$role = new Role();
$login = new Login();
$employee = new Employee();

$all_employees = $employee->read_all_employees();

if(isset($_GET['all_employees'])){
    echo json_encode(
        array("data"=>$all_employees)
    ); 
}

if(isset($_POST['add_employee'])){
    $employee_name = $_POST['employee_name'];
    $employee_email = $_POST['employee_email'];
    $employee_phone = $_POST['employee_phone'];
    $employee_gender = $_POST['employee_gender'];
    $role_name = $_POST['role_name'];
    $role_details = $role->get_role_by_name($role_name);
    $role_id = $role_details[0]['role_id'];
    $password = implode('',str_split(rand(1000,9999),4));
    $employee_created_at = date("Y-m-d");
    $data = array(
        "employee_name"=>$employee_name,
        "employee_email"=>$employee_email,
        "employee_phone"=>$employee_phone,
        "employee_gender"=>$employee_gender,
        "employee_role_id"=>$role_id,
        "employee_created_at"=>$employee_created_at
    );
    $login_data = array(
        "email"=>$employee_email,
        "phone"=>$employee_phone,
        "password"=>md5($password),
        "passcode"=>$password
    );
    $success = $employee->add_employee($data);
    if($success){
        $login->add_login($login_data);
        //Send password via email
        $subject = "Don Publications | New Account";
        $msgBody = 	"Hello $employee_name, your new password is $password. Kindly change it after login.";
        require '../mailer/PHPMailerAutoload.php';
        date_default_timezone_set('Africa/Accra');
        $mail = new PHPMailer;
        //$mail->SMTPDebug = 3;
        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = 'millsabeiku2@gmail.com';                 // SMTP username
        $mail->Password = 'Abeiku0555733180@';                           // SMTP password
        $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 587;
            //Set who the message is to be sent from
        $mail->setFrom('millsabeiku2@gmail.com', 'Don Publications');
        //Set an alternative reply-to address
        $mail->addReplyTo('noreply@gmail.com', 'Don Publications');
        //Set who the message is to be sent to
        $mail->addAddress($employee_email,$employee_name);
        $mail->isHTML(true);
        $mail->Subject = $subject;
        $mail->Body    = $msgBody;
        $mail->AltBody = 'This is a mail from Don Publications.';
        $mail->send();

        // Send password via phone
        try{
            $response = @file_get_contents("https://sms.arkesel.com/sms/api?action=send-sms&api_key=OnY5d2pCMk1UdEV4YjhjYXY=&to=$employee_phone&from=Don&sms=$msgBody");
             if($response===FALSE){
                $networkIssue = true;
             }
        }catch(Error $e){
            
        }   
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}

if(isset($_POST['update_employee'])){
    $employee_id = $_POST['employee_id'];
    $employee_name = $_POST['employee_name'];
    $employee_email = $_POST['employee_email'];
    $employee_phone = $_POST['employee_phone'];
    $employee_gender = $_POST['employee_gender'];
    $role_name = $_POST['role_name'];
    $role_details = $role->get_role_by_name($role_name);
    $role_id = $role_details[0]['role_id'];
    $data = array(
        "employee_name"=>$employee_name,
        "employee_email"=>$employee_email,
        "employee_phone"=>$employee_phone,
        "employee_gender"=>$employee_gender,
        "employee_role_id"=>$role_id
    );
    $key = array(
        "employee_id"=>$employee_id
    );
    $success = $employee->update_employee($key,$data);
    
    if($success){
        if($employee_id==$_SESSION['employee_id']){
            $_SESSION['employee_email'] = $employee_email;
            $_SESSION['employee_phone'] = $employee_phone;
            $_SESSION['employee_name'] = $employee_name;
            $_SESSION['role_id'] = $role_id;
            $_SESSION['role_name'] = $role_name;
        }
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}

if(isset($_POST['update_employee_by_employee'])){
    $employee_id = $_POST['employee_id'];
    $employee_name = $_POST['employee_name'];
    $employee_email = $_POST['employee_email'];
    $employee_phone = $_POST['employee_phone'];
    $employee_gender = $_POST['employee_gender'];
    $current_picture = $_POST['current_picture'];
    $generated_name ='';
    if(isset($_FILES['picture_file'])){
        $ext_1 = explode('.', basename($_FILES['picture_file']['name'])); //explode file name from dot(.)
        $file_extension_1 = end($ext_1); //store extensions in the variable
        $generated_name .= md5(uniqid()) . '.' . $ext_1[count($ext_1) - 1];
        move_uploaded_file($_FILES['picture_file']['tmp_name'], '../pictures/'.$generated_name);
    }else{
        $generated_name = $current_picture;
    }
    $data = array(
        "employee_name"=>$employee_name,
        "employee_email"=>$employee_email,
        "employee_phone"=>$employee_phone,
        "employee_gender"=>$employee_gender,
        "employee_picture"=>$generated_name
    );
    $key = array(
        "employee_id"=>$employee_id
    );
    $success = $employee->update_employee($key,$data);
    $login_data = array(
        "email"=>$employee_email,
        "phone"=>$employee_phone
    );
    $key = array(
        "login_id"=>$_SESSION['login_id']
    );
    $login->update_login($key,$login_data);
    if($success){
        if($employee_id==$_SESSION['employee_id']){
            $_SESSION['employee_email'] = $employee_email;
            $_SESSION['employee_phone'] = $employee_phone;
            $_SESSION['employee_name'] = $employee_name;
            $_SESSION['employee_gender'] = $employee_gender;
            $_SESSION['employee_picture'] = $generated_name;
        }
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}

if(isset($_POST['remove_employee'])){
    $employee_id = $_POST['employee_id'];
    $employee_email = $_POST['employee_email'];
    $employee_phone = $_POST['employee_phone'];
    $data = $employee->remove_employee($employee_id);
    $login->remove_employee_login($employee_email,$employee_phone);
    if(!$data){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}