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

$formname = 'Medium';
$maindb = 'medium';
$trandb = 'medprim';
$trandb2 = 'medseco';
$trandb3 = 'medprel';

$printfile = "medium_print.php";
$file = 'medium.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;
    $name = mysqli_real_escape_string($conn, $_POST["name"]);
    $count = $_POST["items"];
    $count2 = $_POST["items2"];
    $count3 = $_POST["items3"];


    $mvouch = $qvouch;

    if (isset($_POST["name"])) {
        $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `name`= '$name' AND code != '$qvouch'") or die(mysqli_error($conn));
        if (mysqli_num_rows($result) > 0) {
            $a = 0;
            browse("Name Already Exist");
            header("refresh: 1; $file?code=$qvouch&mode=last");
        }
    }

    if ($a == 1) {

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

        mysqli_query($conn, "DELETE FROM `$trandb` WHERE `code`='$mvouch' ") or die(mysqli_error($conn));

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

            $srno = $_POST["srno_$j"];
            $subject = $_POST["subject_$j"];
            $subname = $_POST["subname_$j"];
            $first = $_POST["first_$j"];
            $second = $_POST["second_$j"];
            $third = $_POST["third_$j"];
            $fourth = $_POST["fourth_$j"];

            $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];
            if (!empty($subject)) {
                $cntr++;
                mysqli_query($conn, "INSERT INTO `$trandb`(`code`, `srno`, `subjname`, `subjcode`, `f1`, `f2`, `f3`, `f4`)
                VALUES ('$mvouch', '$cntr', '$subject', '$subcode', '$first', '$second',  '$third', '$fourth')") or die(mysqli_error($conn));
            }
        }

        #################################TRAN PART 3 #################################

        mysqli_query($conn, "DELETE FROM `$trandb2` WHERE `code`='$mvouch'") or die(mysqli_error($conn));

        $cntr2 = 0;
        for ($k = 0; $k < $count2; $k++) {
            $srno2 = $_POST["srno2_$k"];
            $subject2 = $_POST["subject2_$k"];
//            $subname2 = $_POST["subname2_$k"];
            $fifth = $_POST["fifth_$k"];
            $sixth = $_POST["sixth_$k"];
            $seventh = $_POST["seventh_$k"];
            $eight = $_POST["eight_$k"];
            $ninth = $_POST["ninth_$k"];
            $tenth = $_POST["tenth_$k"];

            $query2 = "SELECT  `code`, `name` FROM `subject` WHERE `name`='$subject2'";
            $result2 = mysqli_query($conn, $query2)or die(mysqli_error($conn));
            $r2 = mysqli_fetch_assoc($result2);
            $subcode2 = $r2[code];

            if (!empty($subject2)) {

                $cntr2++;
                mysqli_query($conn, "INSERT INTO `$trandb2`(`code`, `srno`, `subjname`, `subjcode`, `f5`, `f6`, `f7`, `f8`, `f9`, `f10`)
                            VALUES ('$mvouch', '$cntr2', '$subject2', '$subcode2', '$fifth', '$sixth', '$seventh', '$eight', '$ninth', '$tenth')") or die(mysqli_error($conn));
            }
        }

        mysqli_query($conn, "DELETE FROM `$trandb3` WHERE `code`='$mvouch'") or die(mysqli_error($conn));

        $cntr3 = 0;
        for ($i = 0; $i < $count3; $i++) {
            $srno3 = $_POST["srno3_$i"];
            $subject3 = $_POST["subject3_$i"];
//            $subname3 = $_POST["subname3_$i"];
            $fprel = $_POST["fprel_$i"];

            $query3 = "SELECT  `code`, `name` FROM `subject` WHERE `name`='$subject3'";
            $result3 = mysqli_query($conn, $query3)or die(mysqli_error($conn));
            $r3 = mysqli_fetch_assoc($result3);
            $subcode3 = $r[code];

            if (!empty($subject3)) {
                $cntr3++;
                mysqli_query($conn, "INSERT INTO `$trandb3`(`code`, `srno`, `subjname`, `subjcode`,`fprel`)
                            VALUES ('$mvouch', '$cntr3', '$subject3', '$subcode3', '$fprel')") or die(mysqli_error($conn));
            }
        }


        ############################## MAIN  ######################################################
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`code`, `name`)
                                        VALUES($qvouch, '$name')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $qvouch; //1
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch; //1

            mysqli_query($conn, "UPDATE `$maindb` SET `name`='$name' WHERE `code` = '$mvouch' ") or die(mysqli_error($conn));
            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
    }
}

if ($addflag != 1) {

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

    $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld='$getvouch'") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);
}

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


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

include("includes/linkshead.php");
?>
<script src="MyScript/medium.js" type="text/javascript"></script>
<script src="MyScript/medium_search.js" type="text/javascript"></script>
</head>
<body>
    <!--    <div id="preloader" class="col-lg-12 text-center">
            <h3 class="text-center">Loading</h3>
            <div id="status">&nbsp;</div>
        </div>-->
    <form action='' method='POST'>
        <!-- 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 class="row">
                            <div class="col-lg-4 col-md-4 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Name</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-6 col-xs-6 pad">
                                <input type="text" class="form-control form focusfield" name="name" value="<?php Echo ($addflag != 1) ? $get["name"] : ""; ?>" required>
                                <input type="hidden" class="form-control form " value="<?php Echo ($addflag != 1) ? $get["code"] : "$qvouch"; ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-8 col-md-8 col-sm-3 col-xs-3 text-right pad">
                                <input type="button" class="btn btn prim form" style="width: 50%;color: white;background-color: black" value="Primary">
                            </div>
                        </div>
                        <div class="primary">
                            <table class="table table-striped table-condensed table-bordered"  style="width: 76%">
                                <thead>
                                    <tr style="background-color: #6082BF;">
                                        <th width="3%"></th>
                                        <th width="7%">Sr</th>
                                        <th width="25%">Subject</th>
                                        <th width="10%">First</th>
                                        <th width="10%">Second</th>
                                        <th width="10%">Third</th>
                                        <th width="10%">Fourth</th>
                                    </tr>
                                </thead>
                                <tbody class="tbody">
                                    <?php
                                    if ($addflag != 1) {
                                        $ctr = 0;
                                        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `code`='$get[code]' ORDER BY `srno`")or die(mysqli_error($conn));
                                        while ($tran = mysqli_fetch_array($gettran)) {

                                            Echo "<tr>";
                                            Echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                                            Echo "<td><input type='text' class='form-control form text-right srno'    name='srno_$ctr'      value='$tran[srno]' readonly></td>";
                                            Echo "<td>";
                                            Echo "<select class='form-control form subject select2' name='subject_$ctr' style='width:100%' >";
                                            Echo "<option value='$tran[subjname]'>$tran[subjname]</option>";
                                            $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' class='form-control form text-right first'   name='first_$ctr'  step='any'   value='$tran[f1]'></td>";
                                            Echo "<td><input type='number' class='form-control form text-right second'   name='second_$ctr' step='any'  value='$tran[f2]'></td>";
                                            Echo "<td><input type='number' class='form-control form text-right third'   name='third_$ctr' step='any'  value='$tran[f3]'></td>";
                                            Echo "<td><input type='number' class='form-control form text-right fourth'   name='fourth_$ctr'  step='any' value='$tran[f4]'></td>";
                                            Echo "</tr>";
                                            $ctr++;
                                        }
                                    } else {
                                        ?>
                                        <tr>
                                            <td><i class='fa fa-close remove'></i></td>
                                            <td><input type='text' class='form-control form text-right srno'    name='srno_0'      value='1' readonly></td>
                                            <td>
                                                <select class='form-control form subject select2' name='subject_0' style='width:100%' >
                                                    <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' class='form-control form text-right first' step='any'   name='first_0'      value='' ></td>
                                            <td><input type='number' class='form-control form text-right second' step='any'  name='second_0'     value=''></td>
                                            <td><input type='number' class='form-control form text-right third'  step='any' name='third_0'   value=''></td>
                                            <td><input type='number' class='form-control form text-right fourth' step='any'  name='fourth_0'   value=''></td>
                                        </tr>
                                        <?
                                    }
                                    ?>
                                </tbody>
                            </table>

                            <button type="button"  class="btn btn-success btn-square form"  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="row">
                            <div class="col-lg-8 col-md-8 col-sm-3 col-xs-3 text-right pad">
                                <input type="button" class="btn btn secnd form" style="width: 50%;color: white;background-color: black" value="Secondary">
                            </div>
                        </div>
                        <div class="secondary">
                            <table class="table table-striped table-condensed table-bordered">
                                <thead>
                                    <tr style="background-color: #6082BF;">
                                        <th width="3%"></th>
                                        <th width="7%">Sr</th>
                                        <th width="25%">Subject</th>
                                        <th width="10%">Fifth</th>
                                        <th width="10%">Sixth</th>
                                        <th width="10%">Seventh</th>
                                        <th width="10%">Eight</th>
                                        <th width="10%">Ninth</th>
                                        <th width="10%">Tenth</th>
                                    </tr>
                                </thead>
                                <tbody class="tbody2">
                                    <?php
                                    if ($addflag != 1) {
                                        $ctr2 = 0;
                                        $gettran2 = mysqli_query($conn, "SELECT * FROM `$trandb2` WHERE `code`='$get[code]' ORDER BY `srno`")or die(mysqli_error($conn));
                                        while ($tran2 = mysqli_fetch_array($gettran2)) {


                                            Echo "<tr>";
                                            Echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                                            Echo "<td><input type='text' class='form-control form text-right srno2'    name='srno2_$ctr2'      value='$tran2[srno]'></td>";
                                            Echo "<td>";
                                            Echo "<select class='form-control form subject2 select2' name='subject2_$ctr2' style='width:100%' >";
                                            Echo "<option value='$tran2[subjname]'>$tran2[subjname]</option>";
                                            $getmsub4 = mysqli_query($conn, "SELECT code, name FROM `subject` order by name")or die(mysqli_error($conn));
                                            while ($msub4 = mysqli_fetch_array($getmsub4)) {
                                                Echo "<option value='$msub4[name]'>$msub4[name]</option>";
                                            }
                                            Echo "</select>";
//                                            echo "<input type='hidden' class='subname2' name='subname2_$ctr2' value='$tran2[subjcode]'>";
                                            Echo "</td>";
                                            Echo "<td><input type='number' step='any' class='form-control form text-right fifth'   name='fifth_$ctr2'     value='$tran2[f5]'></td>";
                                            Echo "<td><input type='number' step='any' class='form-control form text-right sixth'   name='sixth_$ctr2'   value='$tran2[f6]'></td>";
                                            Echo "<td><input type='number' step='any' class='form-control form text-right seventh' name='seventh_$ctr2'   value='$tran2[f7]'></td>";
                                            Echo "<td><input type='number' step='any' class='form-control form text-right eight'   name='eight_$ctr2'   value='$tran2[f8]'></td>";
                                            Echo "<td><input type='number' step='any' class='form-control form text-right ninth'   name='ninth_$ctr2'   value='$tran2[f9]'></td>";
                                            Echo "<td><input type='number' step='any' class='form-control form text-right tenth'   name='tenth_$ctr2'   value='$tran2[f10]'></td>";
                                            Echo "</tr>";
                                            $ctr2++;
                                        }
                                    } else {
                                        ?>
                                        <tr>
                                            <td><i class='fa fa-close remove'></i></td>
                                            <td><input type='text' class='form-control form text-right srno'    name='srno2_0'      value='1'></td>
                                            <td>
                                                <select class='form-control form subject2 select2' name='subject2_0' style='width:100%' >
                                                    <option value=''></option>
                                                    <?
                                                    Echo $getmsub3 = mysqli_query($conn, "SELECT code, name FROM `subject` order by name")or die(mysqli_error($conn));
                                                    while ($msub3 = mysqli_fetch_array($getmsub3)) {
                                                        Echo "<option value='$msub3[name]'>$msub3[name]</option>";
                                                    }
                                                    ?>
                                                </select>
                                                <!--<input type='hidden' name='subname2_0' class='subname2' >-->
                                            </td>
                                            <td><input type='number' step='any' class='form-control form text-right fifth'    name='fifth_0'      value='' style="width: 158px; "></td>
                                            <td><input type='number' step='any' class='form-control form text-right sixth'   name='sixth_0'     value=''></td>
                                            <td><input type='number' step='any' class='form-control form text-right seventh'   name='seventh_0'   value=''></td>
                                            <td><input type='number' step='any' class='form-control form text-right eight'   name='eight_0'   value=''></td>
                                            <td><input type='number' step='any' class='form-control form text-right ninth'   name='ninth_0'   value=''></td>
                                            <td><input type='number' step='any' class='form-control form text-right tenth'   name='tenth_0'   value=''></td>
                                        </tr>
                                        <?
                                    }
                                    ?>
                                </tbody>
                            </table>

                            <button type="button"  class="btn btn-success btn-square form"  id="addnew2">
                                <i class="fa fa-plus" style="color:#ffffff" data-toggle='tooltip' title='Add Row'></i>    
                            </button>
                            <input type="hidden" id="items2" name="items2" value="<?php echo ($addflag != 1) ? $ctr2 : 1; ?>" />
                        </div>
                        <div class="row">
                            <div class="col-lg-8 col-md-8 col-sm-3 col-xs-3 text-right pad">
                                <input type="button" class="btn btn  prelm form" style="width: 50%;color: white;background-color: black" value="Prelium">
                            </div>
                        </div>
                        <div class="prelium">
                            <table class="table table-striped table-condensed table-bordered" style="width: 37%">
                                <thead>
                                    <tr style="background-color: #6082BF;">
                                        <th width="3%"></th>
                                        <th width="7%">Sr</th>
                                        <th width="25%">Subject</th>
                                        <th width="10%">Tenth</th>
                                    </tr>
                                </thead>
                                <tbody class="tbody3">
                                    <?php
                                    if ($addflag != 1) {
                                        $ctr3 = 0;
                                        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb3` WHERE `code`='$get[code]' ORDER BY `srno`")or die(mysqli_error($conn));
                                        while ($tran = mysqli_fetch_array($gettran)) {

                                            Echo "<tr>";
                                            Echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                                            Echo "<td><input type='text' class='form-control form text-right srno3'    name='srno3_$ctr3'      value='$tran[srno]'></td>";
                                            Echo "<td>";
                                            Echo "<select class='form-control form subject3 select2' name='subject3_$ctr3' style='width:100%' >";
                                            Echo "<option value='$tran[subjname]'>$tran[subjname]</option>";
                                            $getmsub1 = mysqli_query($conn, "SELECT code, name FROM `subject` order by name")or die(mysqli_error($conn));
                                            while ($msub1 = mysqli_fetch_array($getmsub1)) {
                                                Echo "<option value='$msub1[name]'>$msub1[name]</option>";
                                            }
                                            Echo "</select>";
//                                            echo "<input type='hidden' class='subname3' name='subname3_$ctr3' value='$tran[subjcode]'>";
                                            Echo "</td>";
                                            Echo "<td><input type='number' step='any' class='form-control form text-right fprel'   name='fprel_$ctr3'   value='$tran[fprel]'></td>";
                                            Echo "</tr>";
                                            $ctr3++;
                                        }
                                    } else {
                                        ?>
                                        <tr>
                                            <td><i class='fa fa-close remove'></i></td>
                                            <td><input type='text' class='form-control form text-right srno3'    name='srno3_0'      value='1'></td>
                                            <td>
                                                <select class='form-control form subject3 select2' name='subject3_0' style='width:100%' >
                                                    <option value=''></option>
                                                    <?
                                                    Echo $getmsub2 = mysqli_query($conn, "SELECT code, name FROM `subject` order by name")or die(mysqli_error($conn));
                                                    while ($msub2 = mysqli_fetch_array($getmsub2)) {
                                                        Echo "<option value='$msub2[name]'>$msub2[name]</option>";
                                                    }
                                                    ?>
                                                </select>
                                                <input type='hidden' class='subname3' name='subname3_0' >
                                            </td>
                                            <td><input type='number' step='any' class='form-control form text-right fprel'   name='fprel_0'   value=''></td>
                                        </tr>
                                        <?
                                    }
                                    ?>
                                </tbody>
                            </table>

                            <button type="button"  class="btn btn-success btn-square form"  id="addnew3">
                                <i class="fa fa-plus" style="color:#ffffff" data-toggle='tooltip' title='Add Row'></i>    
                            </button>
                            <input type="hidden" id="items3" name="items3" value="<?php echo ($addflag != 1) ? $ctr3 : 1; ?>" />
                        </div>

                    </div>
                    <div class="panel-footer">
                        <?php include("includes/buttons.php"); ?>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <div class="modal fade bs-example-modal-lg searchModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" data-dismiss="modal"><span>&times;</span></button>
                    <h4 class="modal-title" id="myLargeModalLabel">Search<? Echo $formname; ?></h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-lg-2">
                            <select class="form-control sfield">
                                <option value="1">Name</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control soperator">
                                <option value="">Type of Search</option>
                                <option value="%">Like</option>
                                <option value="=">Exactly</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control sortBy">
                                <option value="">Sort By</option>
                                <option value="1">Name</option>
                            </select> 
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control orderAs">
                                <option value="desc">Desc</option>
                                <option value="asc">Asc</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <input type="text" value="" class="form-control ssearch" placeholder="Search Here">
                        </div>
                        <div class="col-lg-2">
                            <input type="button" class="btn btn-primary sload" value="Search" >
                        </div>
                    </div>
                    <div class="col-lg-12 sloader text-center">
                        <!--<img src="images/search.gif" alt="loading">-->       
                    </div>
                    <div class="col-lg-12 snoRecord text-center">
                        <h3>NO RECORDS FOUND</h3>
                    </div>
                    <div class="table-responsive" style="max-height: 400px;overflow: auto;">
                        <table id="" class="table table-striped table-condensed table-bordered" width="100%">
                            <thead>
                                <tr style="background-color:#6082BF;">
                                    <th width="80%">Name</th>
                                    <th width="20%">Action</th>
                                </tr>
                            </thead>
                            <tbody class="sTbody">
                            </tbody>
                        </table>
                    </div>
                    <div class="row">
                        <div class="col-xs-12 text-right search-action-bar">
                            <input type="button"  class="btn btn-primary sprev" value="Previous">
                            <input type="button"  class="btn btn-primary snext" value="Next">
                        </div>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
