<?php 
    include'include/session.php';
    
    $tableName="course";
    $tableName1="faculty";
    
    $sql = "SELECT COUNT(*) as num FROM $tableName"; 
    $pages = mysql_fetch_array(mysql_query($sql));
    $total_records = $pages['num'];
    /*$total_pages = ceil($total_records / $limit);
    
    $stages = 3;
    if(isset($_GET['page'])){
        $page = mysql_escape_string($_GET['page']);
    }else {$page=0;}
    
    if($page){
    $start = ($page - 1) * $limit;
    }else{
    $start = 0;
    }*/
    if(isset($_GET['id'])){
        $facultyID = $_GET['id'];
        // Get page data
        $query1 = "SELECT c.*,f.name AS faculty
                    FROM course c
                    LEFT JOIN faculty f
                    ON c.facultyID = f.facultyID
                    WHERE f.facultyID='$facultyID'
                    ORDER BY c.name ASC";
        $result = mysql_query($query1);
        $rowcount=  mysql_num_rows($result);
    }else{
        // Get page data
        $query1 = "SELECT c.*,f.name AS faculty
                    FROM course c
                    LEFT JOIN faculty f
                    ON c.facultyID = f.facultyID
                    ORDER BY c.name ASC";
        $result = mysql_query($query1);
        $rowcount=  mysql_num_rows($result);
    }
        
?>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>View Course | Admissions | WAKA CMEC</title>
    <link rel="shortcut icon" href="../../../assets/img/logo.png" />
    <link rel="stylesheet" href="../../../assets/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,700">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Slab:300,400|Roboto:300,400,700">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Slab:300,400">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Romanesco">
    <link rel="stylesheet" href="../../../assets/fonts/font-awesome.min.css">
    <link rel="stylesheet" href="../../../assets/fonts/material-icons.css">
    <link rel="stylesheet" href="../../../assets/css/ebs-simple-login-form1.css">
    <link rel="stylesheet" href="../../../assets/css/ebs-simple-login-form2.css">
    <link rel="stylesheet" href="../../../assets/css/Data-Table.css">
    <link rel="stylesheet" href="../../../assets/css/Data-Table1.css">
    <link rel="stylesheet" href="../../../assets/css/ebs-simple-login-form.css">
    <link rel="stylesheet" href="../../../assets/css/FPE-Gentella-form-elements.css">
    <link rel="stylesheet" href="../../../assets/css/FPE-Gentella-form-elements1.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.15/css/dataTables.bootstrap.min.css">
    <link rel="stylesheet" href="../../../assets/css/MUSA_no-more-tables.css">
    <link rel="stylesheet" href="../../../assets/css/MUSA_no-more-tables1.css">
    <link rel="stylesheet" href="../../../assets/css/MUSA_panel-table.css">
    <link rel="stylesheet" href="../../../assets/css/MUSA_panel-table1.css">
    <link rel="stylesheet" href="../../../assets/css/nav.css">
    <link rel="stylesheet" href="../../../assets/css/Navbar---App-Toolbar--LG--MD---Mobile-Nav--SM--XS.css">
    <link rel="stylesheet" href="../../../assets/css/JLX-Fixed-Nav-on-Scroll.css">
    <link rel="stylesheet" href="../../../assets/css/Navbar---App-Toolbar--LG--MD---Mobile-Nav--SM--XS1.css">
    <link rel="stylesheet" href="../../../assets/css/Navbar-Fixed-Side1.css">
    <link rel="stylesheet" href="../../../assets/css/Pretty-Table.css">
    <link rel="stylesheet" href="../../../assets/css/Pretty-Table1.css">
    <link rel="stylesheet" href="../../../assets/css/Profile-Edit.css">
    <link rel="stylesheet" href="../../../assets/css/Profile-Edit1.css">
    <link rel="stylesheet" href="../../../assets/css/Responsive-Form.css">
    <link rel="stylesheet" href="../../../assets/css/Responsive-Form1.css">
    <link rel="stylesheet" href="../../../assets/css/Sidebar-Menu.css">
    <link rel="stylesheet" href="../../../assets/css/Sidebar-Menu1.css">
    <link rel="stylesheet" href="../../../assets/css/styles.css">
    <link rel="stylesheet" href="../../../assets/css/tabela-mloureiro1973.css">
    <link rel="stylesheet" href="../../../assets/css/Team-Clean.css">
    
    <script>
        function PrintElem(elem)
        {
            Popup($(elem).html());
        }

        function Popup(data) 
        {

            var mywindow = window.open('', 'printReport', 'height=500,width=700');
            mywindow.document.write('<html><head><title></title>');
            mywindow.document.write('<link rel="stylesheet" href="../../../assets/bootstrap/css/bootstrap.min.css"  media="print">');
            mywindow.document.write('<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,700"  media="print">');
            mywindow.document.write('<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Slab:300,400|Roboto:300,400,700"  media="print">');
            mywindow.document.write('<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Slab:300,400"  media="print">');
            mywindow.document.write('<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Romanesco"  media="print">');
            mywindow.document.write('<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Slab:300,400"  media="print">');
            mywindow.document.write('<link rel="stylesheet" href="../../../assets/fonts/font-awesome.min.css"  media="print">');
            mywindow.document.write('<link rel="stylesheet" href="../../../assets/css/FPE-Gentella-form-elements.css"  media="print">');
            mywindow.document.write('<link rel="stylesheet" href="../../../assets/css/FPE-Gentella-form-elements1.css"  media="print">');
            mywindow.document.write('<link rel="stylesheet" href="../../../assets/css/FPE-Gentella-form-elements1.css"  media="print">');
            
            mywindow.document.write('<link rel="stylesheet" href="../../../assets/css/Data-Table.css" media="print">');
            mywindow.document.write('<link rel="stylesheet" href="../../../assets/css/Data-Table1.css" media="print">');
            
            mywindow.document.write('</head><body >');
            mywindow.document.write(data);
            mywindow.document.write('</body></html>');

            mywindow.document.close(); // necessary for IE >= 10
            mywindow.focus(); // necessary for IE >= 10

            mywindow.print();
            mywindow.close();

            return true;
        }
    </script>
</head>

<body style="background-color:rgba(42,63,84,0);">
    <nav class="navbar navbar-default" style="background-color:rgb(52,73,94);">
        <div class="container-fluid">
            <div class="navbar-header"><a class="navbar-brand navbar-link" href="#" style="font-weight:bold;background-color:rgb(52,73,94);">Admissions | Waka CMEC Training Institute</a>
                <button class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navcol-2" style="background-color:rgb(52,73,94);color:rgb(255,255,255);"><span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button>
            </div>
            <div class="collapse navbar-collapse" id="navcol-2">
                <ul class="nav navbar-nav hidden-xs hidden-sm navbar-right" id="desktop-toolbar">
                    <li class="dropdown">
                        <a class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false" href="#"> <i class="fa fa-user"></i> &nbsp; <?php echo $pro;?>  <i class="fa fa-chevron-down fa-fw"></i></a>
                        <ul class="dropdown-menu" role="menu" style="background-color:rgb(249,249,249);">
                            <li role="presentation"><a href="settings.php"><i class="fa fa-user fa-fw"></i> Profile </a></li>
                            <li role="presentation"><a href="../validate/logout.php"><i class="fa fa-power-off fa-fw"></i>Logout </a></li>
                        </ul>
                    </li>
                </ul>
                <ul class="nav navbar-nav visible-xs-block visible-sm-block" id="mobile-nav">
                    
                    <li class="dropdown">
                        <a class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false" href="#"> <i class="fa fa-user"></i> &nbsp; <?php echo $pro;?> </i> <i class="fa fa-chevron-down fa-fw"></i></a>
                        <ul class="dropdown-menu" role="menu" style="background-color:rgb(249,249,249);">
                            <li role="presentation"><a href="settings.php"><i class="fa fa-user fa-fw"></i> Profile </a></li>
                            <li role="presentation"><a href="../validate/logout.php"><i class="fa fa-power-off fa-fw"></i>Logout </a></li>
                        </ul>
                    </li>
                    <li role="presentation"><a>&nbsp;</a></li>
                    <li role="presentation"><a>&nbsp;</a></li>
                    
                </ul>
            </div>
        </div>
    </nav>
    <div class="top">
        <nav class="navbar navbar-default" id="navbar-main">
            <div class="container-fluid">
                <div class="navbar-header">
                    <a class="navbar-brand hidden navbar-link" href="#" style="padding:0px;"> </a>
                    <button class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navcol-1"><span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button>
                </div>
                <div class="collapse navbar-collapse" id="navcol-1">
                    <ul class="nav navbar-nav">
                        <li role="presentation">
                            <a href="index.php">
                                <i class="fa fa-table" style="margin-right:5px;"></i>
                                Dashboard
                            </a>
                        </li>
                        <li role="presentation">
                            <a href="pre-admission.php">
                                <i class="fa fa-book" style="margin-right:5px;"></i>
                                Pre Admit 
                            </a>
                        </li>
                        <li class="dropdown">
                            <a class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                                <i class="fa fa-users" style="margin-right:5px;"></i>
                                Students 
                                <span class="caret"></span>
                            </a>
                            <ul class="dropdown-menu" role="menu" style="background-color:rgb(249,249,249);">
                                <li role="presentation"><a href="view-preadmitted.php">Pre-Admitted </a></li>
                                <li role="presentation"><a href="view-admitted.php">Admitted </a></li>
                                <li role="presentation"><a href="view-completed.php">Alumni </a></li>
                            </ul>
                        </li>
                        <li role="presentation">
                            <a href="class.php">Class</a>
                        </li>
                        <li role="presentation">
                            <a href="view-faculty.php">Faculty</a>
                        </li>
                        <li class="active" role="presentation">
                            <a href="view-course.php">Course</a>
                        </li>
                        <li role="presentation">
                            <a href="leave.php">
                                <i class="fa fa-shield" style="margin-right:5px;"></i>
                                Leave
                            </a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
    </div>
    <div class="row" style="width:98%;margin-right:auto;margin-left:auto;padding-top:10px;-webkit-box-shadow:0px 4px 4px 0px rgba(0,0,0,1);-moz-box-shadow:0px 4px 4px 0px rgba(0,0,0,1);box-shadow:0px 4px 4px 0px rgba(0,0,0,1);margin-bottom:5%;overflow:hidden;">
        <form method="POST">
            <?php 
                if(empty($result)){
                    echo'<div class="alert alert-danger absolue center text-center" role="alert">
                            <span class="text-danger">No records found</span>
                        </div>';
                }else{
                    
            ?>
            <div class="col-md-12">
                <h2 style="font-weight:normal;">View Course</h2>
            </div>
            
                <div class="col-md-12" style="width:100%;">
                    <div class="table-responsive" style="margin-right:auto;margin-left:auto;">
                        <table class="table table-striped" id="myTable">
                            <thead style="background-color:rgb(42,63,84);color:rgb(254,255,255);">
                                <tr>
                                    <th style="width:7%;text-align:center;"># </th>
                                    <th>Code </th>
                                    <th>Course</th>
                                    <th>Years</th>
                                    <th>Faculty</th>
                                    <th><i class="fa fa-cog"></i></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php 
                                    $i=$x=$t=1;
                                    while($row = mysql_fetch_array($result)) { 
                                ?>
                                    <tr>
                                        <td style="text-align:center;font-size:12px;font-weight:normal;color:rgb(3,3,3);"> <?php echo $i++;?> </td>
                                        <td style="font-weight:normal;font-size:12px;color:rgb(6,6,6);"> <?php echo $row['no'];?> </td>
                                        <td style="font-size:12px;font-weight:normal;color:rgb(4,4,4);"> <?php echo $row['name'];?> </td>
                                        <td style="font-size:12px;font-weight:normal;color:rgb(4,4,4);"> <?php echo $row['years'];?> </td>
                                        <td style="font-size:12px;font-weight:normal;color:rgb(3,3,3);"> 
                                                <?php echo $row['faculty'];?> 
                                        </td>
                                        <td>
                                            <a href="#myModal<?php echo $x++;?>" data-toggle="modal" class="text-info" role="button" style="cursor:pointer;display:block;" title="View Fee Structure">
                                                <i class="fa fa-eye"></i>
                                            </a>
                                            
                                            
                                            <div role="dialog" tabindex="-1" class="modal fade" id="myModal<?php echo $t++;?>">
                                                <div class="modal-dialog modal-lg" role="document">
                                                    <div class="modal-content">
                                                        <div class="modal-header">
                                                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
                                                            <button type="submit" class="print" onclick="PrintElem('#printReport')" style="padding:5px 20px;color:rgb(1,1,1);">
                                                                <i class="fa fa-print"></i> Print
                                                            </button>
                                                        </div>
                                                        <div id="printReport">
                                                            <div class="modal-body">
                                                                <div class="row" style="width:100%;color:black;">
                                                                    <div class="col-md-12 text-center" style="text-align:center;width:100%;">
                                                                        <img src="../../../assets/img/logo.png" style="width:100px;" align="center">
                                                                    </div>
                                                                    <div class="col-md-12 text-center" style="text-align:center;width:100%;">
                                                                        <h2 style="font-weight:bold;;">WAKA CMEC FEE STRUCTURE</h2>
                                                                    </div>

                                                                    <div class="col-md-12">
                                                                        <div class="col-md-8" style="width:70%;font-size:14px;">
                                                                            <p><b>Course:</b> <?php echo $row['name'];?></p>
                                                                        </div>
                                                                        <div class="col-md-4" style="width:30%;font-size:14px;text-align:right;">
                                                                            <p><b>Date:</b> <?php echo date("D d/M/Y, G:i");?></p>
                                                                        </div>
                                                                    </div>
                                                                    <div class="col-md-12" style="width:100%;">
                                                                        <table border="1" style="width:100%;font-size:12px;color:black;">
                                                                            <thead>
                                                                                <tr>
                                                                                    <th style="text-align:center;">No</th>
                                                                                    <th>Item Description</th>
                                                                                    <?php for($r=1;$r <= $row['years'];$r++){ ?>
                                                                                        <th style="text-align:center;">Year <?php echo $r;?></th>
                                                                                    <?php } ?>   
                                                                                    <th>&nbsp;&nbsp;</th>    
                                                                                </tr>
                                                                                <tr>
                                                                                    <th>&nbsp;</th>
                                                                                    <th>&nbsp;</th>
                                                                                    <th style="text-align:center;" colspan="<?php echo $r-1;?>">Fees in Kenya Shillings</th>
                                                                                    <th style="text-align:center;">Total</th>    
                                                                                </tr>
                                                                            </thead>

                                                                            <tbody>
                                                                                <?php 
                                                                                    $s=1;

                                                                                    $querys = "SELECT miscellaneousID,name FROM miscellaneous ORDER BY name ASC";
                                                                                    $results = mysql_query($querys);
                                                                                    $rowcounts=  mysql_num_rows($results);
                                                                                    while($rows = mysql_fetch_array($results)) { 
                                                                                        $missID[] = $rows['miscellaneousID'];  
                                                                                        $totalMiscellaneousRecords = $s;
                                                                                ?>
                                                                                    <tr>
                                                                                        <td style="text-align:center;"><?php echo $s++;?></td>
                                                                                        <td style="padding:0px 5px 0px 5px;"><?php echo $rows['name'];?></td>
                                                                                        <?php 
                                                                                            $fee=array();
                                                                                            for($p=1;$p <= $row['years'];$p++){ 
                                                                                                $queryx = mysql_query("SELECT amount
                                                                                                   FROM misscellanioused_course 
                                                                                                   WHERE miscellaneousID='".$rows['miscellaneousID']."' 
                                                                                                   AND courseID ='".$row['courseID']."'
                                                                                                   AND year ='$p'");
                                                                                                $pagex = mysql_fetch_array($queryx);
                                                                                                $amount = $pagex['amount'];
                                                                                                $fee[] = $pagex['amount'];

                                                                                        ?>
                                                                                            <td style="padding:0px 5px 0px 5px;text-align:right;"><?php if($amount==NULL){echo'-';}else{ echo number_format($amount);}?></td>
                                                                                        <?php } $totalMissFee[]=array_sum($fee); ?>
                                                                                        <td style="padding:0px 5px 0px 5px;text-align:right;font-weight:bold;"><?php echo number_format(array_sum($fee));?></td>     
                                                                                    </tr>
                                                                                    <?php } ?>


                                                                                    <!--Tuition records-->
                                                                                    <tr>
                                                                                        <td style="text-align:center;"><?php echo $totalMiscellaneousRecords + 1;?></td>
                                                                                        <td style="padding:0px 5px 0px 5px;">Tuition</td>
                                                                                            <?php 
                                                                                                for($p=1;$p <= $row['years'];$p++){ 
                                                                                                    $queryx = mysql_query("SELECT SUM(amount) AS tuition
                                                                                                                            FROM fee
                                                                                                                            WHERE feeID IN(SELECT feeID
                                                                                                                                            FROM (SELECT MAX(feeID) AS feeID
                                                                                                                                                    FROM fee
                                                                                                                                                    WHERE courseID ='".$row['courseID']."'
                                                                                                                                                    AND yosID ='$p'
                                                                                                                                                    GROUP BY semisterID) as fee
                                                                                                                                            ORDER BY feeID DESC)");
                                                                                                    $pagex = mysql_fetch_array($queryx);
                                                                                                    $totalTuition = $pagex['tuition'];
                                                                                                    $tuitionAmount[] = $pagex['tuition'];
                                                                                            ?>
                                                                                                <td style="padding:0px 5px 0px 5px;text-align:right;"><?php if($totalTuition==NULL){echo'-';}else{ echo number_format($totalTuition);}?></td>
                                                                                            <?php } ?>
                                                                                            <td style="padding:0px 5px 0px 5px;text-align:right;font-weight:bold;"><?php echo number_format(array_sum($tuitionAmount));?></td>   
                                                                                    </tr>

                                                                                    <!--Total amounts per year-->
                                                                                    <tr>
                                                                                        <td></td>
                                                                                        <td style="padding:0px 5px 0px 5px;font-weight:bold;">Totals</td>
                                                                                            <?php 
                                                                                                for($z=1;$z <= $row['years'];$z++){ 
                                                                                                    /*annual tution fee(sem1 + sem2 + sem3)*/
                                                                                                    $queryz = mysql_query("SELECT SUM(amount) AS tuition
                                                                                                                            FROM fee
                                                                                                                            WHERE feeID IN(SELECT feeID
                                                                                                                                            FROM (SELECT MAX(feeID) AS feeID
                                                                                                                                                    FROM fee
                                                                                                                                                    WHERE courseID ='".$row['courseID']."'
                                                                                                                                                    AND yosID ='$z'
                                                                                                                                                    GROUP BY semisterID) as fee
                                                                                                                                            ORDER BY feeID DESC)");
                                                                                                    $pagez = mysql_fetch_array($queryz);
                                                                                                    $annualTuition = $pagez['tuition'];
                                                                                                    /*end of annual tuition fee*/
                                                                                                    
                                                                                                    /*Annual miscellaneous fee*/
                                                                                                    $queryg = mysql_query("SELECT SUM(amount) AS annual_amount
                                                                                                                            FROM misscellanioused_course 
                                                                                                                            WHERE miscellaneousID IN (".implode(',',$missID).") 
                                                                                                                            AND courseID ='".$row['courseID']."'
                                                                                                                            AND `year` ='$z'");
                                                                                                                         $pageg = mysql_fetch_array($queryg);
                                                                                                                         $Missfee = $pageg['annual_amount'];
                                                                                                    /*end of annual miscellaneous fee*/   
                                                                                                                         
                                                                                                    $annualFee = $annualTuition + $Missfee;                     
                                                                                            ?>
                                                                                                <td style="padding:0px 5px 0px 5px;text-align:right;font-weight:bold;"><?php if($annualFee==NULL){echo'-';}else{ echo number_format($annualFee);}?></td>
                                                                                            <?php } ?>
                                                                                        <td style="padding:0px 5px 0px 5px;text-align:right;font-weight:bold;"><?php echo number_format(array_sum($totalMissFee) + array_sum($tuitionAmount)); ?></td>
                                                                                    </tr>
                                                                            <tbody>    
                                                                        </table>
                                                                    </div>

                                                                    <div class="col-md-12" style="width:100%;text-align:center;margin-top:10px;font-size:12px;">
                                                                        <hr>
                                                                        <p>WAKA Continuing Medical Education Center (CMEC) - P.O Box 188-10100, Nyeri Kenya.</p>
                                                                        <p><b>Email:</b> <a style="text-decoration:none;">wakacmec@gmail.com</a></p>
                                                                        <p><b>Tel:</b> <a style="text-decoration:none;">0707472152</a> / <a style="text-decoration:none;">0788307979</a> / <a style="text-decoration:none;">0722963213</a></p>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                        <div class="modal-footer">
                                                            <button type="button" data-dismiss="modal" style="padding:5px 20px;color:rgb(1,1,1);">Close</button>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            
                                            
                                        </td>
                                    </tr>
                                <?php } ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            <?php } ?>
        </form>    
    </div>
    <script src="../../../assets/js/jquery.min.js"></script>
    <script src="../../../assets/bootstrap/js/bootstrap.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.15/js/dataTables.bootstrap.min.js"></script>
    <script src="../../../assets/js/ebs-simple-login-form.js"></script>
    <script src="../../../assets/js/JLX-Fixed-Nav-on-Scroll.js"></script>
    <script src="../../../assets/js/Profile-Edit.js"></script>
    <script src="../../../assets/js/Sidebar-Menu.js"></script>
</body>
    <script>
        $(document).ready(function(){
            $('#myTable').dataTable();
        });
    </script>
</html>