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

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

$file = 'prprimary.php';
$trandbexist = 0;
$grid = '';
//$keyfld = "code";
$focusfield = "name";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
//include("includes/vouchno_generate.php");
//include("includes/prev&next.php");

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

    $count = $_POST["items"];

    $mvouch = $qvouch;

    if ($a == 1) {
        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `subject` write,`exam` write");

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

        mysqli_query($conn, "DELETE FROM `$maindb` WHERE `section`='P' ") 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"];
            $first = $_POST["first_$j"];
            $second = $_POST["second_$j"];
            $third = $_POST["third_$j"];
            $fourth = $_POST["fourth_$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`, `rt1`, `rt2`, `rt3`, `rt4`, `ppackrate`,`section`)
                VALUES ('$cntr', '$subject', '$subcode', '$examcode', '$exam', '$first', '$second',  '$third', '$fourth', '$packrate','P')") or die(mysqli_error($conn));
            }
        }





        insmess();
        header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
    }
    mysqli_query($conn, "UNLOCK TABLES");
}

if ($addflag != 1) {

//    $getvouch = '';
//    if ($addflag == 0) {
//        $getvouch = $_GET[$keyfld];
//    } else {
//        $getvouch = $qvouch;
//    }

    $result = mysqli_query($conn, "SELECT * FROM `$maindb` where `section`='P' 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/prprimary.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="1%"></th>
                                        <th width="4%">Sr</th>
                                        <th width="15%">Exam</th>
                                        <th width="15%">Subject</th>
                                        <th width="8%" class="text-right">First</th>
                                        <th width="8%" class="text-right">Second</th>
                                        <th width="8%" class="text-right">Third</th>
                                        <th width="8%" class="text-right">Fourth</th>
                                        <th width="8%" 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 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='first_$ctr'     value='$tran[rt1]'  ></td>";
                                            Echo "<td><input type='number' step='any' class='form-control text-right second'   name='second_$ctr'   value='$tran[rt2]'  ></td>";
                                            Echo "<td><input type='number' step='any' class='form-control text-right third'   name='third_$ctr'   value='$tran[rt3]'  ></td>";
                                            Echo "<td><input type='number' step='any' class='form-control text-right fourth'   name='fourth_$ctr'   value='$tran[rt4]'  ></td>";
                                            Echo "<td><input type='number' step='any' class='form-control text-right packrate'   name='packrate_$ctr'   value='$tran[ppackrate]'  ></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 first'    name='first_0'      value='' ></td>
                                            <td><input type='number' step='any' class='form-control  text-right second'   name='second_0'     value=''></td>
                                            <td><input type='number' step='any' class='form-control  text-right third'   name='third_0'   value=''></td>
                                            <td><input type='number' step='any' class='form-control text-right fourth'   name='fourth_0'   value=''></td>
                                            <td><input type='number' step='any' class='form-control  text-right packrate'   name='packrate_0'   value=''></td>
                                        </tr>
                                        <?
                                    }
                                    ?>
                                </tbody>
                            </table>
                            <div>
                                <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 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>
            </div>
        </div>
    </form>

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