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

$formname = 'Promo Code';
$maindb = 'promocode';
$printfile = "promo_print.php";
$file = 'promocode.php';
$trandbexist = 0;
$grid = '';
$keyfld = "code";
$focusfield = "name";

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

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

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

            mysqli_query($conn, "UPDATE `$maindb` SET `name`='$name', `discperc`='$discperc', `expdt`='$expdt' WHERE code = '$mvouch' ") or die(mysqli_error());
            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);

//    $findtype = mysqli_query("SELECT * FROM `type` WHERE `code`='$get[type]'") or die(mysqli_error());
//    $ftype = mysqli_fetch_array($findtype);
}

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


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

include("../includes/linkshead.php");
?>
<!--<script src="MyScript/challan.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-6">
                <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="form-group">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Name</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form focusfield" name="name" value="<?php Echo ($addflag != 1) ? $get["name"] : ""; ?>" required>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Discount Perc</label>
                            </div> 
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form focusfield" name="discperc" value="<?php Echo ($addflag != 1) ? $get["discperc"] : ""; ?>" required>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Expiry Date</label>
                            </div>
                            <div class="col-lg-3 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="date" class="form-control form focusfield" name="expdt" value="<?php Echo ($addflag != 1) ? $get["expdt"] : ""; ?>" required>
                            </div>
                        </div>
                    </div>
                    <div class="panel-footer">
                        <?php include("../includes/buttons.php"); ?>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <div class="modal fade bs-example-modal-lg" 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=" table-responsive">
                        <table id="example1" class="table table-striped">
                            <thead>
                                <tr class="mySearchTr">
                                    <th>Name</th>
                                    <th>Discount Percent</th>
                                    <th>Expiry Date</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                if ($addflag != 1) {
                                    $srchresult = mysqli_query($conn, "SELECT * FROM `$maindb`")or die(mysqli_error($conn));
                                    while ($row = mysqli_fetch_array($srchresult)) {
                                        echo "<tr class='clickable-row' data-href='$file?mode=edit&$keyfld=$row[$keyfld]'>";
                                        echo "<td align='left'>$row[name]</td>";
                                        echo "<td align='left'>$row[discperc]</td>";
                                        echo "<td align='left'>$row[expdt]</td>";
                                        echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\"><i class='fa fa-pencil' style='font-size:1.3em;color:#00C0EF;cursor:pointer;'></i></a></td>";
                                        echo "</tr>";
                                    }
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("../includes/linksfoot.php"); ?>
</body>
</html>
