<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');

$formname = 'Paper Rate - Secondary';
$maindb = 'paprate';

$file = 'prsecondary.php';
$trandbexist = 0;
$grid = '';

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);

if (isset($_POST["submit"])) {
    $a = 1;

    $count = $_POST["items"];

    $mvouch = $qvouch;

    if ($a == 1) {

        #################################TRAN PART  #################################

        mysqli_query($conn, "DELETE FROM `$maindb` WHERE `section`='S' ") or die(mysqli_error($conn));

        $cntr = 0;
        for ($j = 0; $j < $count; $j++) {

            $srno = $_POST["srno_$j"];
            $exam = $_POST["exam_$j"];
            $subject = $_POST["subject_$j"];
            $fifth = $_POST["fifth_$j"];
            $sixth = $_POST["sixth_$j"];
            $seventh = $_POST["seventh_$j"];
            $eight = $_POST["eight_$j"];
            $ninth = $_POST["ninth_$j"];
            $tenth = $_POST["tenth_$j"];
            $packrate = $_POST["packrate_$j"];

            $chkstr = $exam . $subject;

            $query = "SELECT  `code`, `name` FROM `subject` WHERE `name`='$subject'";
            $result = mysqli_query($conn, $query)or die(mysqli_error($conn));
            $r = mysqli_fetch_assoc($result);
            $subcode = $r['code'];

            $exquery = "SELECT  `code`, `name` FROM `exam` WHERE `name`='$exam'";
            $exresult = mysqli_query($conn, $exquery)or die(mysqli_error($conn));
            $exr = mysqli_fetch_assoc($exresult);
            $examcode = $exr['code'];


            $cntr++;
            if ($chkstr != '') {
                mysqli_query($conn, "INSERT INTO `$maindb`(`srno`, `subjname`, `subjcode`, `examcode`, `examname`, `rt5`, `rt6`, `rt7`, `rt8`,
                                                    `rt9`, `rt10`,`spackrate`,`section`)
                VALUES ('$cntr', '$subject', '$subcode', '$examcode', '$exam', '$fifth', '$sixth',  '$seventh', '$eight', '$ninth',
                        '$tenth', '$packrate', 'S')") or die(mysqli_error($conn));
            }
        }






        insmess();
        header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
    }
}

if ($addflag != 1) {



    $result = mysqli_query($conn, "SELECT * FROM `$maindb` where `section`='S' order by srno") or die(mysqli_error($conn));
    $entries = mysqli_num_rows($result);
}

$senable = "";
$cenable = "";


if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
}

include("includes/linkshead.php");
?>
<script src="MyScript/prsecondary.js" type="text/javascript"></script>
</head>
<body>

    <form action='' method='POST' onsubmit="return confirm('Do you really want to save the changes ?');">
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-10">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?php Echo $formname; ?></h3>
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                            <i class="fa fa-times"></i>
                        </div>
                    </div>
                    <div class="panel-body">
                        <div>
                            <table class="table table-striped table-condensed table-bordered">
                                <thead>
                                    <tr style="background-color: #6082BF;">
                                        <th width="2%"></th>
                                        <th width="5%">Sr</th>
                                        <th width="15%">Exam</th>
                                        <th width="15%">Subject</th>
                                        <th width="7%" class="text-right">Fifth</th>
                                        <th width="7%" class="text-right">Sixth</th>
                                        <th width="7%" class="text-right">Seventh</th>
                                        <th width="7%" class="text-right">Eight</th>
                                        <th width="7%" class="text-right">Ninth</th>
                                        <th width="7%" class="text-right">Tenth</th>
                                        <th width="9%" class="text-right">Packet Rate</th>
                                    </tr>
                                </thead>
                                <tbody class="tbody">
                                    <?php
                                    if ($entries > 0) {
                                        $read = 'readonly';
                                        $ctr = 0;
                                        while ($tran = mysqli_fetch_array($result)) {


                                            Echo "<tr>";
                                            Echo "<td><i class='fa fa-close remove'></i></td>";
                                            Echo "<td><input type='text' class='form-control form text-right srno'    name='srno_$ctr'      value='$tran[srno]' $read></td>";
                                            Echo "<td>";
                                            Echo "<select class='form-control exam select2' name='exam_$ctr' style='width:100%'>";
                                            if ($entries > 0) {
                                                Echo "<option value='$tran[examname]'>$tran[examname]</option>";
                                            } else {
                                                $getmexm = mysqli_query($conn, "SELECT code, name FROM `exam` order by name")or die(mysqli_error($conn));
                                                while ($mexm = mysqli_fetch_array($getmexm)) {
                                                    Echo "<option value='$mexm[name]'>$mexm[name]</option>";
                                                }
                                            }
                                            Echo "</select>";
//                                            echo "<input type='hidden' name='examname_$ctr' class='examname' value='$tran[examname]'>";
                                            Echo "</td>";
                                            Echo "<td>";
                                            Echo "<select class='form-control subject select2' name='subject_$ctr' style='width:100%'>";
                                            if ($entries > 0) {
                                                Echo "<option value='$tran[subjname]'>$tran[subjname]</option>";
                                            } else {
                                                $getmsub = mysqli_query($conn, "SELECT code, name FROM `subject` order by name")or die(mysqli_error($conn));
                                                while ($msub = mysqli_fetch_array($getmsub)) {
                                                    Echo "<option value='$msub[name]'>$msub[name]</option>";
                                                }
                                            }
                                            Echo "</select>";
//                                            echo "<input type='hidden' name='subname_$ctr' class='subname' value='$tran[subjcode]'>";
                                            Echo "</td>";
                                            Echo "<td><input type='number' step='any' class='form-control text-right first'   name='fifth_$ctr'     value='$tran[rt5]' ></td>";
                                            Echo "<td><input type='number' step='any' class='form-control  text-right sixth'   name='sixth_$ctr'   value='$tran[rt6]' ></td>";
                                            Echo "<td><input type='number' step='any' class='form-control  text-right seventh'   name='seventh_$ctr'   value='$tran[rt7]' ></td>";
                                            Echo "<td><input type='number' step='any' class='form-control  text-right eight'   name='eight_$ctr'   value='$tran[rt8]' ></td>";
                                            Echo "<td><input type='number' step='any' class='form-control text-right ninth'   name='ninth_$ctr'   value='$tran[rt9]' ></td>";
                                            Echo "<td><input type='number' step='any' class='form-control  text-right tenth'   name='tenth_$ctr'   value='$tran[rt10]' ></td>";
                                            Echo "<td><input type='number' step='any' class='form-control  text-right packrate'   name='packrate_$ctr'   value='$tran[spackrate]'></td>";
                                            Echo "</tr>";
                                            $ctr++;
                                        }
                                    } else {
                                        ?>
                                        <tr>
                                            <td><i class='fa fa-close remove'></i></td>
                                            <td><input type='text' class='form-control text-right srno'    name='srno_0'      value='1'></td>
                                            <td>
                                                <select class='form-control exam select2' name='exam_0' style='width:100%'>
                                                    <option value=''></option>
                                                    <?
                                                    Echo $getmexm = mysqli_query($conn, "SELECT code, name FROM `exam` order by name")or die(mysqli_error($conn));
                                                    while ($mexm = mysqli_fetch_array($getmexm)) {
                                                        Echo "<option value='$mexm[name]'>$mexm[name]</option>";
                                                    }
                                                    ?>
                                                </select>
                                                <!--<input type='hidden' name='examname_0' class='examname'>-->
                                            </td>
                                            <td>
                                                <select class='form-control subject select2' name='subject_0' style='width:100%' required>
                                                    <option value=''></option>
                                                    <?
                                                    Echo $getmsub = mysqli_query($conn, "SELECT code, name FROM `subject` order by name")or die(mysqli_error($conn));
                                                    while ($msub = mysqli_fetch_array($getmsub)) {
                                                        Echo "<option value='$msub[name]'>$msub[name]</option>";
                                                    }
                                                    ?>
                                                </select>
                                                <!--<input type='hidden' name='subname_0' class='subname'>-->
                                            </td>
                                            <td><input type='number' step='any' class=' form-control   text-right fifth'    name='fifth_0'      value='' ></td>
                                            <td><input type='number' step='any' class=' form-control   text-right sixth'   name='sixth_0'     value=''></td>
                                            <td><input type='number' step='any' class=' form-control   text-right seventh'   name='seventh_0'   value=''></td>
                                            <td><input type='number' step='any' class=' form-control   text-right eight'   name='eight_0'   value=''></td>
                                            <td><input type='number' step='any' class=' form-control   text-right ninth'   name='ninth_0'   value=''></td>
                                            <td><input type='number' step='any' class=' form-control   text-right tenth'   name='tenth_0'   value=''></td>
                                            <td><input type='number' step='any' class=' form-control   text-right packrate'   name='packrate_0'   value=''></td>
                                        </tr>
                                        <?
                                    }
                                    ?>
                                </tbody>
                            </table>

                            <button type="button"  class="btn btn-success btn-square"  id="addnew">
                                <i class="fa fa-plus" style="color:#ffffff" data-toggle='tooltip' title='Add Row'></i>    
                            </button>
                            <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr : 1; ?>" />
                        </div>
                    </div>
                    <div class="panel-footer" style="text-align: center">
                        <button id='view' type='submit' name='submit' class="btn btn-pad btn-square " style="background-color:#008D4C; ">
                            <i class="fa fa-check" style="color:#ffffff"></i>
                        </button>
                    <!--<input type="submit" name="exit" value="Exit" id="exit" class="btn btn-danger" onclick="window.location.href='followup.php'">-->
                        <button id='cancel' type="reset" class="btn btn-pad btn-square" style="background-color:red;" onclick="window.parent.location.href = 'menu.php'">
                            <i class="fa fa-power-off" style="color:#ffffff"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </form>

    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
