

<?php 
if(!empty($_POST['infregid'])){
  $officeid=$_REQUEST['officeid'];  
  $infregid=$_REQUEST['infregid'];
 ######## CON ########
 $userSent = $_REQUEST['userSent'];
 $sentAppoint = $_REQUEST['sentAppoint'];
 $DeptSent = $_REQUEST['DeptSent'];
 
 $dueDate = date('Y-m-d');
 $today = date('Y-m-d H:i:s');
 $year= date('Y');

 foreach($officeid as $value)
 {
     if ($value<>0)
     {

         $stmt = $db->prepare("INSERT INTO tbl_con_infos (reg_info_id, dSent, appntsent, iSd,info_sts,conTrm,dRcv,recvappnt,iRcv,dueCon,con_doctype) VALUES(?,?,?,?,?,?,?,?,?,?,?)");
         $stmt->execute(array($infregid, $DeptSent, $sentAppoint,$userSent,1,$today,$value,0,0,$dueDate,3));
     }
 }

    $update = $db->prepare("UPDATE `tbl_info_register` SET stts_reg=5 WHERE `regst_id`='".$infregid."' ");
    $update->execute();
    
    echo'<script type="text/javascript">
        jQuery(function validation(){
        swal("Successfully", "Request Assigned!", "success", {
        button: "Continue",
        });
        });
    </script>';
  
} // CoN

####### CANCEL ORDER #######
if(isset($_REQUEST['cancereq'])){
    $regid=$_REQUEST['regid'];

    $upte = $db->prepare("UPDATE `tbl_info_register` SET stts_reg=7 WHERE `regst_id`='".$regid."' ");
    $upte->execute();

    echo'<script type="text/javascript">
        jQuery(function validation(){
        swal("Oooh Snap", "Request Cancelled!", "error", {
        button: "Continue",
        });
        });
    </script>';
}

?>

<form  class="form-horizontal" id="" method="POST" action="" autocomplete="off">
<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%;">

    <table id="dt-basic-example" class="table table-hover w-100" style="font-size: 11px;">
        <thead class="header bg-primary" style="color: #fff;">
            <tr>
                <th style="font-weight: bold;width: 5px;"></th>
                <th style="font-weight: bold;">CLIENT NAME</th>
                <th style="font-weight: bold;">SUBJECT MATTER</th>
                <th style="font-weight: bold;">REQUEST DATE</th>
                <th style="font-weight: bold;">TYPE</th>
                <th style="width: 12%;"></th>
                <th style="font-weight: bold;">ACTION</th>
            </tr>
        </thead>

        <tbody>
            <?php 
             ####### FETCH ######
             $stmtReq = $db->prepare('SELECT *FROM tbl_info_register i
             INNER JOIN tbl_clients c ON c.ct_id=i.clnt_id 
             INNER JOIN tbl_reqs_type r ON r.reqtype_id=i.reqtype
             WHERE i.doctype=3 AND i.stts_reg=1');
             $stmtReq->execute();
             while($ffReq = $stmtReq->fetch()){
                $regstid = $ffReq['regst_id'];
            ?>
            <tr class="b-tr">
                <td>
                    <div class="custom-control custom-radio">
                        <input type="radio" name="infregid" value="<?php echo $regstid;?>" class="custom-control-input active" id="<?php echo $regstid;?>">
                        <label class="custom-control-label" for="<?php echo $regstid;?>"></label>
                    </div>
                </td>
                
                <td><?php echo $ffReq['ct_names'];?></td>
                <td><?php echo $ffReq['sub_matt'];?></td>
                <td><?php echo date('d/m/Y',strtotime($ffReq['requestDate']));?></td>
                <td><?php echo "REQUEST FOR ".$ffReq['reqtype_name'];?></td>  

                <td class="text-left">
                   <?php 
                    $i=0;
                    $select = $db->prepare("SELECT * FROM `tbl_docs` WHERE registID = '".$regstid."' 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";
                         }
                   ?>                    
                    <a href="../docs/<?php echo $docName;?>">
                        <img src="../img/icon/<?php echo $imgIcon;?>" width="30" title="<?php echo $docName;?>">
                    </a> 
                   <?php 
                    }
                   ?>
                </td>

                <td>
                <button type="button" class="btn btn-danger btn-xs" data-toggle="modal" data-target="#btncancel_<?php echo $regstid;?>" 
                    style="border-radius: 4px;" title="Cancel Request"><i class="fa fa-times" aria-hidden="true"></i></button>
                </td>
            </tr>
            
        <?php 
            include 'cancelReq.php';
          }
        ?>
        </tbody>                           
    </table>

    <hr>    
    <div class="row">
        <label class="col-md-1"><b>Department: </b></label>
        <div class="col-sm-3">
            <select multiple="multiple" id="multiple-basic" name="officeid[]" data-placeholder="~select dept~"
                class="select2 form-control border-top-0 border-left-0 border-right-0 px-0 rounded-0 fs-md mt-2 pr-5" tabindex="2">                            
                <?php
                $stmt = $db->query('select * from tbl_role');

                    try
                    {
                        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                            ?>
                                <option value="<?php echo $row['role_id']; ?>"><?php echo $row['role_name']; ?></option>
                            <?php
                            }
                        }
                        catch (PDOException $ex) {
                        //Something went wrong rollback!
                        echo $ex->getMessage();
                    }
                ?>	
            </select>
        </div>                       
    </div>
    <button type="submit" name="btnAssign" class="btn btn-primary btn-sm"style="border-radius: 4px;font-weight: bold;" 
    title="Assign to head office"><i class="fa fa-check" aria-hidden="true"></i> Assign</button>  
</form>
