<?php
session_start();
include 'includes/database.php';
include 'includes/functions.php';
$conn = new Functions();

$base_url = $conn->base_url();

if (!empty($_FILES['logo'])){
    $photo_name = $_FILES['logo']['name'];
    $photo_tmpname = $_FILES['logo']['tmp_name'];
    $photo_size = $_FILES['logo']['size'];

    $photofilenameAr = explode('.', $photo_name);
    $photo_extension = end($photofilenameAr);
    $photo_ext = strtolower($photo_extension);
    $settings_id = $_POST['settings_id'];

    //check if logo was selected
    if (!empty($photo_name)) {
        if ($photo_size > 2000000) {
            echo "<script>
$(function () { //ready
          toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
          toastr.error('Photo Size Exceeded, Max Size is 2MB');
      });
</script>";
            return false;
        }
        if ($photo_ext == 'png' || $photo_ext == 'jpg' || $photo_ext == 'jpeg' || $photo_ext == 'svg') {
            $strgPath = "upload/".$photo_name;
            $student_url = $conn->student_url();
            if (file_exists($strgPath)) {
               echo "<script>
$(function () { //ready
          toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
          toastr.error('Photo With Same Name Already Exist');
      });
</script>";
                return false;
            }else {
                try{
                    $photo_upload = move_uploaded_file($photo_tmpname, $strgPath);
                    if ($photo_upload){
                        //update photo details in the database
                        $sql = "UPDATE general_settings SET logo =:photo WHERE id =:settings_id";
                        $conn->query($sql);
                        $conn->bind(":photo", $photo_name);
                        $conn->bind(":settings_id", $settings_id);
                        if ($conn->execute()){
                            echo "<script>
                            $(function () { //ready
                                      toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
                                      toastr.success('Church Logo Updated Successfully');
                                  });
                            </script><meta http-equiv='refresh' content='5; settings'>";
                        }else {
                            echo "<script>
                            $(function () { //ready
                                      toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
                                      toastr.error('An Error Occured While Uploading Your Image');
                                  });
                            </script>";
                        }
                    }else {
                        echo "<script>
                            $(function () { //ready
                                      toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
                                      toastr.error('Photo Cannot Upload');
                                  });
                            </script>";
                    }

                }catch (PDOException $err) {
                    $error = $err->getMessage();
                    echo "<script>
                            $(function () { //ready
                                      toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
                                      toastr.error('Error');
                                  });
                            </script>";
                }
            }
        }
    }
}
