<?php require_once('../Connections/conn_ama.php'); 
include("../../../../../../functions/functions.php"); 
    
if (!isset($_SESSION)) {
  session_start();
}
    
    if (isset($_GET['del'])) {

        $deleteID = $_GET['del'];
        
        echo $deleteID;
                    
        $query_rsForDel ="SELECT u.*, uo.* 
        FROM deptsub2 u 
        LEFT JOIN deptsub2officers uo ON u.subsubdeptID = uo.subsubdeptID
        WHERE u.subdeptID = ".$deleteID;
        $rsForDel = mysqli_query($conn_ama, $query_rsForDel ) or die(mysqli_error($conn_ama));
        $row_rsForDel = mysqli_fetch_assoc($rsForDel);
        $totalRows_rsForDel = mysqli_num_rows($rsForDel);
        
        $msg=NULL;
        $result = NULL;
        do{
            // assign all the names of the image files to variable
            $mainImage = $row_rsForDel['image'];
            $thumbnail = $row_rsForDel['thumbnail'];
                            
            //Deleting the main Image
            $mainfolder = "../sub-units/subunitsofficers/";
            $filename = $mainfolder.$mainImage;
            // @chmod($filename,0700);
            $delete_main = @unlink($filename);
            if($delete_main){$msg[] = "<p class ='green'>".$mainImage." has been deleted</p>";} else{ $msg[] = $mainImage." <p class='red'>could not be deleted</p>";}
        
            //Deleting the thumbnail Image
            $thumbfolder = $mainfolder."thumbs/";
            $filename_1 = $thumbfolder.$thumbnail;
            //@chmod($filename_1,0700); 
            $delete_thumb = @unlink($filename_1);
            if($delete_thumb){$msg[] = "<p class ='green'>".$thumbnail." has been deleted</p>";} else{ $msg[] = $thumbnail."<p class='red'>could not be deleted</p>";}
        
            if(isset($msg) && $msg!=NULL)
            {
                $deleteSql = "DELETE FROM deptsub2officers WHERE officerID = ".$row_rsForDel['officerID'];
                $query_result = mysqli_query($conn_ama,$deleteSql) or die(mysqli_error($conn_ama));
                
                if($query_result)
                {
                    $msg[] = 'unitofficerdeleted';
                }
            }     
        
        }while($row_rsForDel = mysqli_fetch_assoc($rsForDel));

        if(isset($msg) && $msg!=NULL && in_array("unitofficerdeleted",$msg))
        {
            $deleteOthers= "DELETE u, uc FROM deptsub2 u LEFT JOIN deptsub2contact uc ON u.subsubdeptID = uc.subsubdeptID 
                            WHERE u.subsubdeptID = ".$row_rsForDel['subsubdeptID'];
            $query_result = mysqli_query($conn_ama,$deleteOthers) or die(mysqli_error($conn_ama));
            
            if($query_result)
            {
                $result='deleted';
                // print_r($msg);
            }
        } 
        
        if(isset($result) && ($result !=NULL))
        {
            $query_rsForDeptDel ="SELECT officerID, image, thumbnail FROM deptofficers WHERE deptdetailsID = ".$deleteID;
            $rsForDeptDel = mysqli_query($conn_ama, $query_rsForDeptDel ) or die(mysqli_error($conn_ama));
            $row_rsForDeptDel = mysqli_fetch_assoc($rsForDeptDel);
            $totalRows_rsForDeptDel = mysqli_num_rows($rsForDeptDel);
        
            //$msg=NULL;
            //$result = NULL;
            do{
                // assign all the names of the image files to variable
                $mainImage = $row_rsForDeptDel['image'];
                $thumbnail = $row_rsForDeptDel['thumbnail'];
                            
                //Deleting the main Image
                $mainfolder = "../deptofficers/";
                $filename = $mainfolder.$mainImage;
                // @chmod($filename,0700);
                $delete_main = @unlink($filename);
                if($delete_main){$msg[] = "<p class ='green'>".$mainImage." has been deleted</p>";} else{ $msg[] = $mainImage." <p class='red'>could not be deleted</p>";}
        
                //Deleting the thumbnail Image
                $thumbfolder = $mainfolder."thumbs/";
                $filename_1 = $thumbfolder.$thumbnail;
                //@chmod($filename_1,0700); 
                $delete_thumb = @unlink($filename_1);
                if($delete_thumb){$msg[] = "<p class ='green'>".$thumbnail." has been deleted</p>";} else{ $msg[] = $thumbnail."<p class='red'>could not be deleted</p>";}
        
                if(isset($msg) && $msg!=NULL)
                {
                    $deleteSql = "DELETE FROM deptofficers WHERE officerID = ". $row_rsForDeptDel['officerID'];
                    $query_result = mysqli_query($conn_ama,$deleteSql) or die(mysqli_error($conn_ama));
                
                    if($query_result)
                    {
                        $msg[] = 'deptofficerdeleted';
                    }
                }     
        
            }while($row_rsForDel = mysqli_fetch_assoc($rsForDel));

            if(isset($msg) && $msg!=NULL && in_array("deptofficerdeleted",$msg))
            {
                $deleteDeptDetails= "DELETE d, dd, dc 
                                    FROM departments d 
                                    LEFT JOIN deptsubdetails dd ON d.deptID = dd.deptID 
                                    LEFT JOIN deptcontact dc ON dd.subdeptdetailsID = dc.subdeptdetailsID 
                                    WHERE dd.subdeptdetailsID = ".$deleteID;
                $query_result = mysqli_query($conn_ama,$deleteDeptDetails) or die(mysqli_error($conn_ama));
            
                if($query_result)
                {
                    //$result='deleted';
                    echo 'deleted';
                    // print_r($msg);
                }
            }  
        }
    } 


?>
