
<?php 
 $regid = $_REQUEST['regid'];
 $conid = $_REQUEST['conid'];
 $nowdue = date('Y-m-d H:i:s');
 $sth = $db->prepare("SELECT DISTINCT(regst_id) AS regst,con_info_id AS coninfo, sub_matt AS subj, 
 info_notes AS msgenote, dueCon AS regdate,info_sts AS stats, title_name AS appoint,title_id AS title,iSd AS docsender,conTrm FROM tbl_info_register i
 INNER JOIN tbl_con_infos c ON c.reg_info_id = i.regst_id 
 INNER JOIN tbl_title t ON t.title_id = c.appntsent 
 WHERE i.regst_id ='".$regid."' ");

 $sth->execute(array());
 $QueStmt = $sth->rowCount();

 if($QueStmt > 0){
     while ($rows = $sth->fetch(PDO::FETCH_ASSOC)) {
        $coninfo = $rows['coninfo'];
        $regst = $rows['regst'];
        $subj =$rows['subj']; 
        $msgenote = $rows['msgenote'];
        $regdate = $rows['regdate'];
        $stats = $rows['stats'];
        $appoint = $rows['appoint'];
        $docsender = $rows['docsender'];
        $title = $rows['title'];
        $conTrm = $rows['conTrm'];

        if($stats==1){
            $upd = $db->prepare("UPDATE `tbl_con_infos` SET `info_sts`=9,`dRcv`='".$role_id."',`iRcv`='".$userid."',`receivTrm`='".$nowdue."' WHERE `con_info_id`='".$conid."' ");
            $upd->execute();
        }

    ### SENT DETAILS ####### 
       $sender = $db->prepare("SELECT * FROM tbl_role r
       INNER JOIN tbl_title t ON t.department_id = r.role_id 
       WHERE t.title_id ='".$title."'");  
       $sender->execute();
       while($viewSender = $sender->fetch()){

       $rlename = $viewSender['role_name'];
       $tname = $viewSender['title_name'];
       }
    ### END SENT DETAILS #######

     }

    }
?>
<!-- inbox container -->
<div class="d-flex flex-column flex-grow-1 bg-white">
    <!-- inbox header -->
    <div class="flex-grow-0">
        <!-- inbox button shortcut -->
        <div class="d-flex flex-wrap align-items-center pl-2 pr-3 py-2 px-sm-4 pr-lg-5 pl-lg-0 border-faded border-top-0 border-left-0 border-right-0">
            <div class="flex-1 d-flex align-items-center">
                <a href="<?php echo web_root;?>beam/bm?b=box" class="btn btn-icon rounded-circle mr-2 mr-lg-3">
                    <i class="fa fa-arrow-left fs-lg"></i>
                </a>
                <a href="javascript:void(0);" onclick="window.location.reload(true);" class="btn btn-icon rounded-circle mr-1">
                    <i class="fa fa-redo fs-md"></i>
                </a>
            </div>

            <div class="d-flex flex-wrap">
                <button class="btn btn-default btn-xs" onclick="showReply()"><i class="fa fa-mail-reply fs-md"></i> Reply</button>
                <button class="btn btn-default btn-xs"><i class="fa fa-share-alt fs-md"></i> Share</button>
            </div>
        </div>
        <!-- end inbox button shortcut -->
    </div>
    <!-- end inbox header -->

    <!-- inbox message -->
    <div class="flex-wrap align-items-center flex-grow-1 position-relative bg-white">
    <div class=" position-absolute pos-top pos-bottom w-100 custom-scroll">
        <div class="d-flex h-100 flex-column">
            <!-- inbox title -->
            <div class="d-flex align-items-center pl-2 pr-3 py-3 pl-sm-3 pr-sm-4 py-sm-4 px-lg-5 py-lg-3 flex-shrink-0">
                <!-- button for mobile -->
                <a href="javascript:void(0);" class="pl-3 pr-3 py-2 d-flex d-lg-none align-items-center justify-content-center mr-2 btn" data-action="toggle" data-class="slide-on-mobile-left-show" data-target="#js-inbox-menu">
                    <i class="fa fa-ellipsis-v h1 mb-0 "></i>
                </a>
                <!-- end button for mobile -->
                <h1 class="subheader-title mb-0 ml-2 ml-lg-5">
                    <?php echo $subj;?>
                </h1>
                <a href="<?php echo web_root;?>beam/bm?b=box"><span class="badge badge-primary ml-2 hidden-sm-down">INBOX</span></a>
                <div class="d-flex position-relative ml-auto">
                    <a href="#" title="starred" class="btn btn-icon ml-1 fs-lg">
                        <i class="fa fa-star color-warning-500"></i>
                    </a>
                    <a href="javascript:void(0);" class="btn btn-icon ml-1 fs-lg" data-toggle="collapse" data-target=".js-collapse">
                        <i class="fa fa-arrows-v"></i>
                    </a>
                </div>
            </div>
            <!-- end inbox title -->
        <?php 
        $repl = $db->prepare('SELECT *FROM tbl_replies r
        INNER JOIN tbl_con_infos c ON c.con_info_id = r.con_reply 
         WHERE r.con_reply = "'.$coninfo.'" ORDER BY reply_id DESC');
        $repl->execute();
        while($roww = $repl->fetch()){
        
        $replyid = $roww['reply_id']; 
        $rff = $roww['appoint_reply'];
        $appntsent = $roww['appntsent'];
        $duereply = $roww['duereply'];

        
     ### SENT DETAILS ####### 
       $replier = $db->prepare("SELECT * FROM tbl_role r
       INNER JOIN tbl_title t ON t.department_id = r.role_id 
       WHERE t.title_id ='".$rff."' ");  
       $replier->execute();
       $viewReplier = $replier->fetch();
       $rlename = $viewReplier['role_name'];
       $tlename = $viewReplier['title_name'];

    ### END SENT DETAILS #######

    ### RECEIVE DETAILS ####### 
    $snder = $db->prepare("SELECT * FROM tbl_role r
    INNER JOIN tbl_title t ON t.department_id = r.role_id 
    WHERE t.title_id ='".$appntsent."'");  
    $snder->execute();
    $viewSnder = $snder->fetch();

    $roleNam = $viewSnder['role_name'];
    $tileNam = $viewSnder['title_name'];
    
    ### RECEIVE DETAILS #######
    
        ?>
            <!-- message -->
            <div id="msg-<?php echo $replyid;?>" class="d-flex flex-column border-faded border-top-0 border-left-0 border-right-0 py-3 px-3 px-sm-4 px-lg-0 mr-0 mr-lg-5 flex-shrink-0">
                <div class="d-flex align-items-center flex-row">
                    <div class="ml-0 mr-3 mx-lg-3">
                        <img src="../img/demo/avatars/beamavatar5.png" class="profile-image profile-image-md rounded-circle" alt="<?php echo $tlename;?>">
                    </div>
                    <div class="fw-500 flex-1 d-flex flex-column cursor-pointer" data-toggle="collapse" data-target="#msg-<?php echo $replyid;?> > .js-collapse">
                        <div class="fs-lg">
                          <?php echo $tlename;?>
                            <span class="fs-nano fw-400 ml-2">|<?php echo $rlename;?></span>
                        </div>
                        <div class="fs-nano">
                            to <?php echo $tileNam;?>
                        </div>
                    </div>
                    <div class="color-fusion-200 fs-sm">
                    <?php echo $duereply;?> <span class="hidden-sm-down"></span>
                    </div>
                    <div class="collapsed-reveal">
                        <a href="javascript:void(0);" class="btn btn-icon ml-1 fs-lg rounded-circle">
                            <i class="fa fa-reply"></i>
                        </a>
                    </div>
                </div>
                <div class="collapse js-collapse">
                    <div class="pl-lg-5 ml-lg-5 pt-3 pb-4">
                    <?php echo $roww['replNotes'];?>  
                    </div>
                </div>
            </div>
            <!-- end message -->
        <?php }?>

        <!-- message -->
            <div id="msg-03" class="d-flex flex-column border-faded border-top-0 border-left-0 border-right-0 py-3 px-3 px-sm-4 px-lg-0 mr-0 mr-lg-5 flex-shrink-0">
                <div class="d-flex align-items-center flex-row">
                    <div class="ml-0 mr-3 mx-lg-3 width-2">
                        <img src="../img/demo/avatars/beamavatar5.png" class="profile-image profile-image-md rounded-circle" alt="<?php echo $tname;?>">
                    </div>
                    <div class="fw-500 flex-1 d-flex flex-column cursor-pointer" data-toggle="collapse" data-target="#msg-03 > .js-collapse">
                        <div class="fs-lg"> 
                            <?php echo $tname;?>
                            <span class="fs-nano fw-400 ml-2">|<?php echo $rlename;?></span>
                        </div>
                        <div class="fs-nano">
                            to <?php echo $title_name;?>
                        </div>
                    </div>
                    <div class="color-fusion-200 fs-sm">
                       <?php echo $conTrm;?> <span class="hidden-sm-down"></span>
                    </div>
                    <div class="collapsed-reveal">
                        <a href="javascript:void(0);" class="btn btn-icon ml-1 fs-lg rounded-circle">
                            <i class="fa fa-mail-reply"></i>
                        </a>
                    </div>
                </div>

                <div id="js-msg-1" class="js-collapse">
                    <div class="pl-lg-5 ml-lg-5 pt-3 pb-4 ">
                       <?php echo $msgenote;?> 
                    </div>

                    <div class="row">
                    <?php 
                        $i=0;
                        $select = $db->prepare("SELECT * FROM `tbl_docs` WHERE registID = '".$regst."' ORDER BY doc_id DESC");
                        $select->execute(); 
                        while($row = $select->fetch()){
                            $doc_id = $row['doc_id'];
                            $uploaded_on = $row['uploaded_on'];
                            $docName = $row['docName'];
                            $rec_dpt = $row['rec_dpt'];
                            $usID = $row['usID'];
                            $stts_doc = $row['stts_doc'];
                            $i++;
                            
                            //Hide Extension
                            $file = new SplFileInfo($docName);
                            $ext  = $file->getExtension();
                            //Get File Name
                            $fileName = basename($docName);
                            $fileNameNoExtension = preg_replace("/\.[^.]+$/", "", $fileName);

                            //DOC ICON
                            if($ext == "pdf"){
                               $imgIcon = "pdf.png";
                            }if($ext == 'xls' || $ext == 'xlsx'){
                               $imgIcon = "excel.png";  
                            }if($ext == 'doc' || $ext == 'docx'){
                                $imgIcon = "docx.png";
                            }if($ext == 'ppt' || $ext == 'pptx'){
                                $imgIcon = "pptx.png";
                            }

                            
                            ####### DEPARTMENT #####
                            $dept = $db->prepare('SELECT *FROM tbl_role WHERE role_id = "'.$rec_dpt.'"');
                            $dept->execute();
                            $fDept = $dept->fetch();
                            $role_name = $fDept['role_name'];
                            
                            ###### USER #####
                            
                            $getUser = $db->prepare('SELECT *FROM tbl_user_accounts WHERE user_id = "'.$usID.'"');
                            $getUser->execute();
                            $fUs = $getUser->fetch();
                            $ffirst = $fUs['first_name'];
                            $fLst = $fUs['family_name'];
                    ?>
                        <div class="col-md-offset-2 col-md-2">
                                <div class="panel panel-default bootcards-file">
                                    <div class="list-group">
                                        <div class="list-group-item">
                                            <a href="../docs/<?php echo $docName;?>">
                                            <img src="../img/icon/<?php echo $imgIcon;?>" width="100">
                                            </a>
                                            
                                            <p class="list-group-item-text"><strong>
                                                <a href="../docs/<?php echo $docName;?>" style="color: #777;">
                                                    <?php echo $fileNameNoExtension;?>
                                                </a>
                                                </strong></p>
                                        </div>
                                    </div>

                            </div>
                        </div>
                        <?php  
                            }
                        ?>
                    </div>
                </div>

            </div>
            
            <?php include 'reply.php';?>           
         <!-- end message -->

        </div>
    </div>
    </div>
    <!-- end inbox message -->

</div>
<!-- end inbox container -->

<script>
    $("#repl").hide();
    function showReply() {
        $("#repl").show();
    }

    $(document).ready(function(){
    $(".closebox").click(function(){
        $("#repl").hide();
    });
    });
</script>