<?php

include "include/db.php";
include "include/adminauth.php";

function cleanDataInput($conn,$data_in){
    $data_in1 = trim($data_in);
    $data_in2 = mysqli_real_escape_string($conn, $data_in1);
    return $data_in2;
}
function imageResize($imageSrc,$imageWidth,$imageHeight) {

    $newImageWidth = 240;
    $newImageHeight = 80;

    $newImageLayer=imagecreatetruecolor($newImageWidth,$newImageHeight);
    $whiteBackground = imagecolorallocate($newImageLayer, 255, 255, 255); 
    imagefill($newImageLayer,0,0,$whiteBackground); 
    imagecopyresampled($newImageLayer,$imageSrc,0,0,0,0,$newImageWidth,$newImageHeight,$imageWidth,$imageHeight);

    return $newImageLayer;
}

$message = '';

if(isset($_REQUEST['change_logo'])){
    $uniqueId= time().'-'.mt_rand();	
	$temp = explode(".", $_FILES["fileToUpload_logo"]["name"]);
    $pic_name =  $uniqueId . '.' . end($temp);
    $pic_name_small =  $uniqueId . '_small.' . end($temp);
	$target_dir = getcwd().DIRECTORY_SEPARATOR. "images/";
	$target_file = $target_dir . $pic_name;
	$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
	$time = date("Y-m-d H:i:s");

	$check = getimagesize($_FILES["fileToUpload_logo"]["tmp_name"]);
	if($check == false) {            
		$message = 'The file you uploaded is not an image. Upload an image';

	}else{
		if($_FILES["fileToUpload_logo"]["size"] > 2000000){
			$message = 'The image you uploaded is too large. Image size should be less than 2 MB';
		}else{
			// Allow certain file formats
			if($imageFileType != "png" && $imageFileType != "jpg" && $imageFileType != "jpeg") {
				$pic_name = '';
				$message = 'The image you uploaded is not of PNG or JPG format. Please upload a PNG or JPG image.'; 
			}else{
				if(move_uploaded_file($_FILES["fileToUpload_logo"]["tmp_name"], $target_file)) {   
					$upload_image_query = "UPDATE pdf_admin_data SET logo = '$pic_name', logo_small = '$pic_name_small' WHERE pdf_admin_id = 1";
                    $upload_image_run = mysqli_query($conn, $upload_image_query);
                    $imageType = $check[2]; 
                    $dirPath = $target_dir;          
                    $uploadedFile = $target_file;
                    switch ($imageType) {            
            
                        case IMAGETYPE_PNG:
                            $imageSrc = imagecreatefrompng($uploadedFile); 
                            $tmp = imageResize($imageSrc,$check[0],$check[1]);
                            imagepng($tmp,$dirPath. $pic_name_small);
                            break;           
            
                        case IMAGETYPE_JPEG:
                            $imageSrc = imagecreatefromjpeg($uploadedFile); 
                            $tmp = imageResize($imageSrc,$check[0],$check[1]);
                            imagejpeg($tmp,$dirPath. $pic_name_small);
                            break;
                        case IMAGETYPE_JPEG:
                            $imageSrc = imagecreatefromjpeg($uploadedFile); 
                            $tmp = imageResize($imageSrc,$check[0],$check[1]);
                            imagejpeg($tmp,$dirPath. $pic_name_small);
                            break;
            
                        default:
                            echo "Invalid Image type.";
                            exit;
                            break;
                    }
					if(mysqli_affected_rows($conn) == 1){
						$successmessage = "Image uploaded.";
					}					
				}else{
					$message = "The image could not be uploaded due to technical reasons. Try again.";
				}			
			}
		}	
	} 
}

if(isset($_REQUEST['change_sig'])){
    $uniqueId= time().'-'.mt_rand();	
	$temp = explode(".", $_FILES["fileToUpload"]["name"]);
	$pic_name =  $uniqueId . '.' . end($temp);
	$target_dir = getcwd().DIRECTORY_SEPARATOR. "pdf_data/";
	$target_file = $target_dir . $pic_name;
	$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
	$time = date("Y-m-d H:i:s");

	$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
	if($check == false) {            
		$message = 'The file you uploaded is not an image. Upload an image';

	}else{
		if($_FILES["fileToUpload"]["size"] > 2000000){
			$message = 'The image you uploaded is too large. Image size should be less than 2 MB';
		}else{
			// Allow certain file formats
			if($imageFileType != "png" && $imageFileType != "jpg" && $imageFileType != "jpeg") {
				$pic_name = '';
				$message = 'The image you uploaded is not of PNG or JPG format. Please upload a PNG or JPG image.'; 
			}else{
				if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {   
					$upload_image_query = "UPDATE pdf_admin_data SET pdf_sign = '$pic_name' WHERE pdf_admin_id = 1";
					$upload_image_run = mysqli_query($conn, $upload_image_query);
					if(mysqli_affected_rows($conn) == 1){
						$successmessage = "Image uploaded.";
					}					
				}else{
					$message = "The image could not be uploaded due to technical reasons. Try again.";
				}			
			}
		}	
	} 
}

if(isset($_REQUEST['changeEstimate'])){
    $est_message = $_REQUEST['estimate_message'];
    $est_message_u_query = "UPDATE pdf_admin_data SET pdf_est_message = '$est_message' WHERE pdf_admin_id = 1";
    $est_message_u_run = mysqli_query($conn, $est_message_u_query);
    
    header("Location:admin-set-up.php");
}

if(isset($_REQUEST['changeInvoice'])){
    $inv_message = $_REQUEST['invoice_message'];
    $inv_message_u_query = "UPDATE pdf_admin_data SET pdf_inv_message = '$inv_message' WHERE pdf_admin_id = 1";
    $inv_message_u_run = mysqli_query($conn, $inv_message_u_query);
    
    header("Location:admin-set-up.php");
}

$est_message_query = "SELECT * FROM pdf_admin_data WHERE pdf_admin_id = 1";
$est_message_run = mysqli_query($conn, $est_message_query);
$est_message_row = mysqli_fetch_array($est_message_run);

$estimate_message = $est_message_row['pdf_est_message'];


$inv_message_query = "SELECT * FROM pdf_admin_data WHERE pdf_admin_id = 1";
$inv_message_run = mysqli_query($conn, $inv_message_query);
$inv_message_row = mysqli_fetch_array($inv_message_run);

$invoice_message = $inv_message_row['pdf_inv_message'];

$sign_query = "SELECT * FROM pdf_admin_data WHERE pdf_admin_id = 1";
$sign_run = mysqli_query($conn, $sign_query);
$sign_row = mysqli_fetch_array($sign_run);

$sign_path = $sign_row['pdf_sign'];
$sign_path = "pdf_sign.php?i=$sign_path";
$logo_path = $sign_row['logo'];
$logo_path = "images/$logo_path";

include "header.php";

?>
<div id="page-wrapper">
<h3>Set Up</h3>

<?php                                     
    if(isset($successmessage)){
        if(strlen($successmessage) > 0){
            echo '<div class="alert alert-success">
            <strong>'. $successmessage .'</strong></div>';
        }
    } 

    if(isset($message)){
        if(strlen($message) >0){
            echo '<div class="alert alert-danger">
            <strong>'. $message .'</strong>
            </div>';
        }
    }
?>              
    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <b>Company Logo</b> 
                </div> <!-- "panel-heading" -->                
                <div class="panel-body">
                <form action = 'admin-set-up.php?change_logo=' method = 'POST' enctype = 'multipart/form-data'>
                <div class="form-group col-lg-6">
                    <label for="ref_photo">Current Logo</label>
                    <br>
                    <img src = "<?php echo $logo_path; ?>" height= "100">
                    <br>

                    <label for="fileToUpload_logo">Upload New Logo</label>
				    <input type="file" class="upload-file" data-max-size="1024" name="fileToUpload_logo" accept="image/*" id="fileToUpload_logo"> 
                    <br>
                    <input type="submit" class="btn btn-success" value = "Upload">                  
                    
                   

                </div>

                </form>
               
                </div> <!-- "panel-body" -->
            </div> <!-- panel panel-default -->
            <div class="panel panel-default">
                <div class="panel-heading">
                    <b>PDF Estimate and Invoice Signature</b> 
                </div> <!-- "panel-heading" -->                
                <div class="panel-body">
                <form action = 'admin-set-up.php?change_sig=' method = 'POST' enctype = 'multipart/form-data'>
                <div class="form-group col-lg-6">
                    <label for="ref_photo">Current Signature</label>
                    <br>
                    <img src = "<?php echo $sign_path; ?>" height= "120" width= "240">
                    <br>

                    <label for="fileToUpload">Upload New Signature</label>
				    <input type="file" class="upload-file" data-max-size="1024" name="fileToUpload" accept="image/*" id="fileToUpload"> 
                    <br>
                    <input type="submit" class="btn btn-success" value = "Upload">
                   

                </div>

                </form>
               
                </div> <!-- "panel-body" -->
            </div> <!-- panel panel-default -->
            <div class="panel panel-default">
                <div class="panel-heading">
                    <b>PDF Estimate Message</b> 
                </div> <!-- "panel-heading" -->
                <div class="panel-body">
                <form action = 'admin-set-up.php?changeEstimate=' method = 'POST' enctype = 'multipart/form-data'>
                    <div class="col-lg-6">
                        <label for="ref_photo">Current Estimate Message</label>
                        <p style = "text-align:justify"><?php echo $estimate_message; ?></p>              

                    </div>
                    

                    <div class="form-group col-lg-6">
                        <label for="ref_photo">Edit Estimate Message</label>
                        
                        <textarea class="form-control" name="estimate_message" id="estimate_message" type="text" rows="5"><?php echo $estimate_message; ?></textarea>      
                    </div>
                    <div class="form-group col-lg-2">
                        <input type = "submit" value = "Change Message" class = "btn btn-success form-control">
                    </div>

                </form>
               
                </div> <!-- "panel-body" -->
            </div> <!-- panel panel-default -->

            <div class="panel panel-default">
                <div class="panel-heading">
                    <b>PDF Invoice Message</b> 
                </div> <!-- "panel-heading" -->
                <div class="panel-body">
                <form action = 'admin-set-up.php?changeInvoice=' method = 'POST' enctype = 'multipart/form-data'>
                <div class="col-lg-6">
                    <label for="ref_photo">Current Invoice Message</label>
                    <p style = "text-align:justify"><?php echo $invoice_message; ?></p>
                </div>    

                <div class="form-group col-lg-6">    
                    <label for="ref_photo">Edit Invoice Message</label>
                    
                    <textarea class="form-control" name="invoice_message" id="invoice_message" type="text" rows="5"><?php echo $invoice_message; ?></textarea>
                </div>
                <div class="form-group col-lg-2">
                    <input type = "submit" value = "Change Message" class = "btn btn-success form-control">
                </div>

                </form>
               
                </div> <!-- "panel-body" -->
            </div> <!-- panel panel-default -->
        </div> <!--  col lg 12-->
    </div> <!-- row -->
</div> <!--  page wrapper -->                             
       
       
<?php  include "footer.php";   ?>