<?php
include('accesscheck2.php');
require_once('Connections/courier.php'); 
require_once('functions/functions.php');


$action = mysql_real_escape_string($_POST['act']);
$type = mysql_real_escape_string($_POST['type']);

list($medical_note_id, $process_claim_no, $id) = explode("@@@",mysql_real_escape_string($_POST['patient_details']));

if($action == 'save_drawing'){
$image = imagecreatefrompng($_POST['image']);
$tag = $_POST['tag'];
$id = uniqid();
$pic = 'consultation/patients_consultation_uploads/patient-' .$process_claim_no. $id . '.png';
$pic2 = 'patients_consultation_uploads/patient-' .$process_claim_no. $id . '.png';
imagealphablending($image, false);
imagesavealpha($image, true);
imagepng($image, $pic);

mysql_query("INSERT INTO consultation_drawings (process_claim_no, drawing_name, tag, type) VALUES('$process_claim_no', '$pic2', '$tag', '$type')", $courier);

$all_drawings = mysql_query("SELECT * FROM consultation_drawings WHERE process_claim_no = '$process_claim_no' AND type='$type' ORDER BY id ASC", $courier);
$i=1; ?>

    <div class="demo-gallery">
        <ul id="lightgallery" class="list-unstyled row">
            <?php while ($row = mysql_fetch_assoc($all_drawings)) { ?>
                <li class="col-xs-6 col-sm-4 col-md-3" data-responsive="consultation/<?php echo $row['drawing_name']; ?> 375, consultation/<?php echo $row['drawing_name']; ?> 480, consultation/<?php echo $row['drawing_name']; ?> 800" data-src="consultation/<?php echo $row['drawing_name']; ?>" data-sub-html="<h4><?php echo $row['tag']; ?></h4><p>Attached Consultation Drawing</p>">
                    <a href="">
                        <img style="height:120px" class="img-responsive" src="consultation/<?php echo $row['drawing_name']; ?>">
                    </a>
                </li>
                <?php $i++;
            } ?>
        </ul>
    </div>

<script type="text/javascript">
         $(document).ready(function(){
            $('#lightgallery').lightGallery({
                cssEasing: 'cubic-bezier(0.680, -0.550, 0.265, 1.550)'
            });
        });
</script>

<?php }

if($action == 'save_drawing2'){
$image = imagecreatefrompng($_POST['image']);
$tag = $_POST['tag'];
$id = uniqid();
$pic = 'consultation/patients_consultation_uploads/patient-' .$process_claim_no. $id . '.png';
$pic2 = 'patients_consultation_uploads/patient-' .$process_claim_no. $id . '.png';
imagealphablending($image, false);
imagesavealpha($image, true);
imagepng($image, $pic);

mysql_query("INSERT INTO consultation_drawings (process_claim_no, drawing_name, tag, type) VALUES('$process_claim_no', '$pic2', '$tag', '$type')", $courier);

$all_drawings = mysql_query("SELECT * FROM consultation_drawings WHERE process_claim_no = '$process_claim_no' AND type='$type' ORDER BY id ASC", $courier);
$i=1; ?>

    <div class="demo-gallery">
        <ul id="lightgallery2" class="list-unstyled row">
            <?php while ($row = mysql_fetch_assoc($all_drawings)) { ?>
                <li class="col-xs-6 col-sm-4 col-md-3" data-responsive="consultation/<?php echo $row['drawing_name']; ?> 375, consultation/<?php echo $row['drawing_name']; ?> 480, consultation/<?php echo $row['drawing_name']; ?> 800" data-src="consultation/<?php echo $row['drawing_name']; ?>" data-sub-html="<h4><?php echo $row['tag']; ?></h4><p>Attached Consultation Drawing</p>">
                    <a href="">
                        <img style="height:120px" class="img-responsive" src="consultation/<?php echo $row['drawing_name']; ?>">
                    </a>
                </li>
                <?php $i++;
            } ?>
        </ul>
    </div>

<script type="text/javascript">
         $(document).ready(function(){
            $('#lightgallery2').lightGallery({
                cssEasing: 'cubic-bezier(0.680, -0.550, 0.265, 1.550)'
            });
        });
</script>

<?php }

if($action == 'delete_patient_drawings'){
 mysql_query("DELETE FROM consultation_drawings WHERE id = '$id'");   
$all_drawings = mysql_query("SELECT * FROM consultation_drawings WHERE process_claim_no = '$process_claim_no' ORDER BY id ASC", $courier);
$i=1;
while($row = mysql_fetch_assoc($all_drawings)){ ?>
<div id="drawings_wrapper_<?php echo $i;?>" class="mt-card-item" style="background-color:#FFF;display: inline-block;text-align: center;border:1px dashed #666666;padding: 5px;border-radius: 10px;">
                        <div class="mt-card-avatar mt-overlay-1 mt-scroll-left">
                            <img style="height:90px;width:" src="consultation/<?php echo $row['drawing_name'];?>">
                        </div>
    <span onclick="DeleteConsultationDrawing('delete_patient_drawings','<?php echo $medical_note_id."@@@".$process_claim_no."@@@".$row['id'];?>');" style="cursor: pointer;margin-top: -98px;margin-left: 76px;position: absolute" class="badge badge-danger"><?php echo $i;?></span>
                    </div>
<?php $i++; }

}

if($action == 'attach_another_file'){ 
    $total_entry = $_POST['total_entry'];
    $index = $total_entry + 1;
 list($patient_id, $medical_note_id, $process_claim_no) = explode("@@@",mysql_real_escape_string($_POST['data']));   ?>

<div id="file_attachment_wrapper_<?php echo $index;?>" class="mt-comment" style="background-color: #FAFAFA;border: 1px solid #eee;">
                                            <div class="mt-comment-img">
                                                <img style="width:50px;height: 50px;" src="<?php echo '../'.FOLDER_NAME;?>images/attach-2-icon.png"> 
                                            </div>
                                            <form name="document_form_<?php echo $index;?>" id="document_form_<?php echo $index;?>" method="post" enctype="multipart/form-data" action="consultation/ajaximage_add_document.php">
                                            <div class="mt-comment-body">
                                                <div class="mt-comment-info">
                                                    <span class="mt-comment-author"> 
                                                            <input type="file" name="documentname_<?php echo $index;?>" id="documentname_<?php echo $index;?>" onchange="UploadPatientFiles('<?php echo $index;?>');return false;"/>
                                                            <input type="hidden" name="process_claim_no_<?php echo $index;?>" id="" value="<?php echo $process_claim_no;?>">
                                                            <input type="hidden" name="medical_note_id_<?php echo $index;?>" id="" value="<?php echo $medical_note_id;?>">
                                                            <input type="hidden" name="file_name_add" id="file_name_add_<?php echo $index;?>" value="<?php echo $index;?>">
                                                    </span>
                                                    <span id="loading_file_<?php echo $index;?>" class="mt-comment-date">  </span>
                                                </div>
                                                <div class="mt-comment-text"> FILE NAME : N/A </div>
                                                <div class="mt-comment-details">
                                                    <input placeholder="ENTER TAGS HERE BEFORE UPLOADING FILE" type="text" class="form-control" name="tags_<?php echo $index;?>">

                                                    <input placeholder="" type="text" class="form-control" value="<?php echo $resultItems1['item_idd'] ?>" name="itemid_<?php echo $index;?>">

                                                    <ul class="mt-comment-actions" style="display: block">
                                                        <li><a style="color:red;" onclick="RemoveNewRow('file_attachment_wrapper_', '<?php echo $index;?>')"> Remove <i class="fa fa-remove"></i></a></li>
                                                        <li><a onclick="AddNewRow('attach_another_file', 'file_attachment_wrapper_', '<?php echo $index;?>','<?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>
                                            </form>
</div>


    
<?php }

 if ($action == 'delete_file') {
    $id = mysql_real_escape_string($_POST['id']);
    $info = mysql_fetch_assoc(mysql_query("SELECT * FROM patient_file_attachments WHERE id = '$id'"));
    $del = mysql_query("DELETE FROM patient_file_attachments WHERE id = '$id'");
    if ($del == TRUE) {
        unlink($info['file_path']);
    }
}
