<?php session_start();
require_once('../Connections/courier.php'); 
require_once('../functions/functions.php'); 
$path = "patients_consultation_uploads/";
$path2 = "consultation/patients_consultation_uploads/";

 $file_identifier = $_POST['file_name_add'];
 $process_claim_no = $_POST['process_claim_no_'.$file_identifier];
 $medical_note_id = $_POST['medical_note_id_'.$file_identifier];
 $tags = $_POST['tags_'.$file_identifier];
  $info = mysql_fetch_assoc(mysql_query("SELECT patient_id FROM process_claim_no WHERE process_claim_no = '$process_claim_no' LIMIT 1"));
 $patient_id = $info['patient_id'];
 
	$valid_formats = array("jpg","png","gif","bmp","icon");
	if(isset($_POST) && ($_SERVER['REQUEST_METHOD'] == "POST"))
		{
			$name = $_FILES['documentname_'.$file_identifier]['name'];
			$size = $_FILES['documentname_'.$file_identifier]['size'];
			if(strlen($name))
				{
					list($txt, $ext) = explode(".", $name);
                                        $ext = strtolower($ext);
							$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
							$tmp = $_FILES['documentname_'.$file_identifier]['tmp_name'];
                                                        $absolute_path = $path.$actual_image_name ;
                                                        $absolute_path2 = $path2.$actual_image_name ;
							if(move_uploaded_file($tmp,$absolute_path))
							{
                                                             mysql_query("INSERT INTO patient_file_attachments(patient_id,process_claim_no,attending_officer_id,filename,file_path,tags) VALUES('$patient_id','$process_claim_no','".$_SESSION['myMM_Userid']."','$name','$absolute_path2','$tags')");  
                                                             $date = mysql_fetch_assoc(mysql_query("SELECT * FROM patient_file_attachments WHERE id = '".  mysql_insert_id()."'"));
                                                             $tmp_abs_path = $absolute_path2;
                                                             $path_info = pathinfo($absolute_path);
                                                             $ext = $path_info['extension'];
                                                        ?>

                                                            <?php 
                                                            $valid_formats = array("jpg","png","gif","bmp","icon");
                                                            $valid_formats2 = array("docx","doc");
                                                            $valid_formats3 = array("xlsx","xls");
                                                            $valid_formats4 = array("pptx");
                                                            $valid_formats5 = array("csv");
                                                            $valid_formats6 = array("xml");
                                                            $valid_formats7 = array("pdf");
                                                            $valid_formats8 = array("txt"); ?>
                                            <div class="mt-comment-img">
                                                <?php
                                                 if(in_array(strtolower($ext),$valid_formats)) { ?>
                                                                    <img src="<?php echo $absolute_path2;?>" style="height: 50px;width:50px;">
                                                            <?php      
                                                             }
                                                            else if(in_array(strtolower($ext),$valid_formats2)) { ?>
                                                                    <img src="<?php echo '../'.FOLDER_NAME;?>images/word-iconx.png" style="width: 30px;height: 30px;">
                                                            <?php      
                                                             }
                                                            else if(in_array(strtolower($ext),$valid_formats3)) { ?>
                                                                    <img src="<?php echo '../'.FOLDER_NAME;?>images/excel-iconx.png" style="width: 30px;height: 30px;">
                                                            <?php      
                                                             }
                                                            else if(in_array(strtolower($ext),$valid_formats4)) { ?>
                                                                    <img src="<?php echo '../'.FOLDER_NAME;?>images/pp-iconx.png" style="width: 30px;height: 30px;">
                                                            <?php      
                                                             }
                                                            else if(in_array(strtolower($ext),$valid_formats5)) { ?>
                                                                    <img src="<?php echo '../'.FOLDER_NAME;?>images/csv-iconx.png" style="width: 30px;height: 30px;">
                                                            <?php      
                                                             }
                                                            else if(in_array(strtolower($ext),$valid_formats6)) { ?>
                                                                    <img src="<?php echo '../'.FOLDER_NAME;?>images/xml-iconx.png" style="width: 30px;height: 30px;">
                                                            <?php      
                                                             }
                                                            else if(in_array(strtolower($ext),$valid_formats7)) { ?>
                                                                    <img src="<?php echo '../'.FOLDER_NAME;?>images/PDFx.png" style="width: 30px;height: 30px;">
                                                            <?php      
                                                             }
                                                            else if(in_array(strtolower($ext),$valid_formats8)) { ?>
                                                                    <img src="<?php echo '../'.FOLDER_NAME;?>images/txt-iconx.png" style="width: 30px;height: 30px;">
                                                            <?php      
                                                             }else{ ?>
                                                                 <img src="<?php echo '../'.FOLDER_NAME;?>images/file-iconx.png" style="width: 30px;height: 30px;">
                                                                    Other File
                                                            <?php } ?>
                                            </div>
                                            <div class="mt-comment-body">
                                                <div class="mt-comment-info">
                                                    <span class="mt-comment-author"> 
                                                        <a href="<?php echo $absolute_path2;?>" target="_blank">   FILE NAME : <?php echo $name;?> </a>
                                                    </span>
                                                    <span class="mt-comment-date"> <?php echo date('jS F, Y g:ia', strtotime($date['date_time'])); ?> </span>
                                                </div>
                                                <div class="mt-comment-text">TAGS : <?php echo $tags;?></div>
                                                <div class="mt-comment-details">
                                                    <ul class="mt-comment-actions" style="display: block">
                                                         <li><a style="color:red;" onclick="DeleteFileAttachment('delete_file','file_attachment_wrapper_', '<?php echo $file_identifier;?>','<?php echo $date['id']; ?>')"> Delete File <i class="fa fa-remove"></i></a></li>
                                                        <li><a onclick="AddNewRow('attach_another_file', 'file_attachment_wrapper_', '<?php echo $file_identifier;?>','<?php echo $patient_id.'@@@'.$medical_note_id.'@@@'.$process_claim_no?>')">Attach Another File <i class="fa fa-plus-circle"></i></a></li>
                                                    </ul>
                                                </div>
                                            </div>
                                                          <?php
                                                         if(in_array(strtolower($ext),$valid_formats)) {
                                                          //----------------RESIZING IMAGE STARTS HERE----------------//
                                                                    require_once('../functions/image_resize.class.php');
                                                                    $filename = $absolute_path;
                                                                    $image = new SimpleImage();
                                                                    $image->load($filename);
                                                                    $image->resizeToWidth(1000);
                                                                    $image->save($filename);
                                                            //----------------RESIZING IMAGE ENDS HERE----------------//
                                                             }
                                }
							else
                                                        {
								echo "failed";
						}
                                          
				}
				
			else
                        {
				echo "Please select File..!";
				
			exit;
                        }
		}
?>