<?php 
if(isset($_REQUEST['sendReply'])){
    $msgecontent = $_REQUEST['msgecontent'];
    $userSent = $_REQUEST['userSent'];
    $sentAppoint = $_REQUEST['sentAppoint'];
    $DeptSent = $_REQUEST['DeptSent'];
    $replCon = $_REQUEST['replCon'];
    $ireg = $_REQUEST['ireg'];

    $dueDate = date('Y-m-d');
    $today = date('Y-m-d H:i:s');
    $year= date('Y');
    ###### GENERATE CODE ######
    $this_Yr =  substr($year,2,2);
    $mon = date('m');
    $dy = date('d');

    ####### INFO #####
    $statement = $db->prepare("INSERT INTO tbl_replies(con_reply, repl_dpt, appoint_reply, usreply, replNotes, duereply, stsreply)
    VALUES(?,?,?,?,?,?,?)");
    $statement->execute(array($replCon,$DeptSent,$sentAppoint,$userSent,$msgecontent,$today,1));
    // $conLastId = $db->lastInsertId();

    #### UPDATE CON ####
    $upd = $db->prepare("UPDATE `tbl_con_infos` SET `info_sts`=1 WHERE `con_info_id`='".$replCon."' ");
    $upd->execute();
    
   ####### FILES ##########
   
   $stt = 1;

   ######## FILES UPLOAD #############
$countfiles = count($_FILES['file']['name']);

$totalFileUploaded = 0;
for($i=0;$i<$countfiles;$i++){
 $stms_last = $db->prepare("SELECT doc_id FROM tbl_docs  ORDER BY doc_id DESC LIMIT 1");
    try {
        $stms_last->execute(array());
        $row_count_last = $stms_last->rowCount();
        if ($row_count_last > 0) {
            $rows = $stms_last->fetch(PDO::FETCH_ASSOC);
                $doc_id = $rows['doc_id']+1;
         }else{
            $doc_id = 1;
         }
    }
    catch (PDOException $ex) {
     echo $ex->getMessage();
    } 
      
     $filename = $doc_id."_".$_FILES['file']['name'][$i];

     ## Location
     $location = $path_upload.$filename;
     $extension = pathinfo($location,PATHINFO_EXTENSION);
     $extension = strtolower($extension);

     ## File upload allowed extensions
     $valid_extensions = array("jpg","jpeg","png","pdf","doc","docx","xls","xlsx","ppt","pptx","zip","tar"."gz");

     $response = 0;
     ## Check file extension
     if(in_array(strtolower($extension), $valid_extensions)) {
        ## Upload file
          if(move_uploaded_file($_FILES['file']['tmp_name'][$i],$location)){

        //    echo "file name : ".$filename."<br/>";
         $stmt2 = $db->prepare("INSERT INTO tbl_docs(rec_dpt,registID,usID,docAppoint,docName,uploaded_on,stts_doc) VALUES(:rec_dpt,:registID,:usID,:docAppoint,:docName,:uploaded_on,:stts_doc)");
         $stmt2->execute(array(':rec_dpt'=>$DeptSent,':registID' => $ireg, ':usID' => $userSent, ':docAppoint' => $sentAppoint, ':docName' => $filename, ':uploaded_on' => $today,':stts_doc'=>$stt));
         // end to insert tbl_doc

            $totalFileUploaded++;
          }
     }

}

echo'<script type="text/javascript">
    jQuery(function validation(){
    swal("Message Sent", "Successfully", "success", {
    button: "Continue",
    });
    });
</script>';

echo'<meta http-equiv="refresh"'.'content="2;URL=bm?b=vwbox&regid='.$regid.'&conid='.$conid.' ">'; 

}
?>
    
    
    <div class="flex-wrap align-items-center flex-grow-1 position-relative bg-white mx-lg-3" id="repl">
        <div class=" position-absolute pos-top pos-bottom w-100">
            <div class="d-flex h-100 flex-column">

            <form method="post" action="" enctype="multipart/form-data" autocomplete="off">
                <!-- desktop view -->
                <div class="panel-hdr bg-fusion-600 height-4 d-none d-sm-none d-lg-flex">
                    <h4 class="flex-1 fs-lg color-white mb-0 pl-3">
                        Reply 
                    </h4>
                    <div class="panel-toolbar pr-2">
                        <a href="javascript:void(0);"  class="btn btn-icon btn-icon-light fs-xl closebox" data-placement="bottom">
                            <i class="fa fa-times" aria-hidden="true"></i>
                        </a>
                    </div>
                </div>
                <!-- end desktop view -->

                <input type="hidden" name="userSent" value="<?php echo $userid;?>" required style="width: 90%;">
                <input type="hidden" name="sentAppoint" value="<?php echo $tle_id;?>" required style="width: 90%;">
                <input type="hidden" name="DeptSent" value="<?php echo $role_id;?>" required style="width: 90%;">
                <input type="hidden" name="replCon" value="<?php echo $coninfo;?>" required style="width: 90%;">
                <input type="hidden" name="ireg" value="<?php echo $regst;?>" required style="width: 90%;">
                <div class="px-3">
                    <textarea class="form-controlounded-0 w-100 h-100 border-0 py-3" name=" msgecontent" placeholder="Message here..." 
                    id="saveToLocal" rows="2" required></textarea>
                </div>

                <br>

                <div class="px-3">
                    <div class="form-group">
                        <label class="form-label" for="example-fileinput"><i class="fa fa-paperclip" aria-hidden="true"></i>Attachment(s)</label>
                        <input type="file" name="file[]" id="file" class="form-control-file" multiple>
                    </div>
                </div>

                <input type="checkbox" class="custom-control-input" style="display: none;" id="autoSave" checked="checked">

                <div class="px-3 py-4 d-flex flex-row align-items-center flex-wrap flex-shrink-0">                    
                    <button type="submit" name="sendReply" class="btn btn-primary mr-3"><i class="fa fa-paper-plane" aria-hidden="true"></i> Send</button>                   
                </div>
            </form>

            </div>
        </div>
    </div>