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

$formname = 'Donation for Samuh Lagan';
$maindb = 'donsamlagan';
$printfile = "job_print.php";
$file = 'donsamlagan.php';
$trandbexist = 0;
$grid = '';
$keyfld = "vouchno";
$focusfield = "name";
$keycond = "";
$username = $_SESSION["username_" . $dbname];
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdbroot"];
mysqli_select_db($conn, $zdb);

include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {

    $a = 1;
    $qvouch = $_POST["vouchno"];
    $vouchdt = $_POST["vouchdt"];
    $memcode = $_POST["memcode"];
    $memname = $_POST["memname"];
    $add1 = $_POST["add1"];
    $add2 = $_POST["add2"];
    $add3 = $_POST["add3"];
    $mobile = $_POST["mobile"];
    $type = $_POST["type"];
    $amount = $_POST["amount"];
    $item = $_POST["item"];
    $qty = $_POST["qty"];
    $description = $_POST["description"];
    $remark1 = $_POST["remark1"];
    $remark2 = $_POST["remark2"];

    $mvouch = $qvouch;

    if ($a == 1) {


        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX($keyfld) AS maxcode FROM `$maindb` WHERE $keyfld !=''  LIMIT 0,1 ") or die(mysqli_error($conn));

            if (mysqli_num_rows($gen1) > 0) {
                $vvouch = mysqli_fetch_assoc($gen1);
                $increvouch = $vvouch["maxcode"] + 1;
            } else {
                $increvouch = '51';
            }
            $qvouch = $increvouch;

            $mvouch = $qvouch;

            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `memcode`, `memname`, `add1`, `add2`, `add3`, `mobile`,
                                                       `amount`, `type`, `item`, `qty`, `description`, `remark1`, `remark2`)
                                                VALUES('$qvouch', '$vouchdt','$memcode', '$memname', '$add1', '$add2', '$add3', '$mobile', 
                                                       '$amount', '$type', '$item', '$qty', '$description', '$remark1', '$remark2')") or die(mysqli_error($conn));
            insmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch;
            mysqli_query($conn, "UPDATE `$maindb` SET `memcode`='$memcode', `memname`='$memname', `add1`='$add1', `add2`='$add2', `add3`='$add3',
                                                      `mobile`='$mobile', `amount`='$amount', `type`='$type', `item`='$item', `qty`='$qty', `description`='$description',
                                                      `remark1`='$remark1', `remark2`='$remark2'  where vouchno = '$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");
}

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

<body>
    <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>
                    <div class="panel-body">



                        <div class="row">
                            <div class="col-lg-3 col-md-1 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Voucher</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form vouchno" name="vouchno" value="<?php echo ($addflag != 1) ? $get["vouchno"] : " $qvouch"; ?>" readonly required>
                            </div>

                            <div class="col-lg-3 col-md-1 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-8 col-xs-8 pad">
                                <input type="date" class="form-control form focusfield vouchdt" name="vouchdt" value="<?php echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d"); ?>" required style="padding:0px;" autofocus>
                            </div>
                        </div>


                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">From</label>
                            </div>
                            <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form memcode" name="memcode" value="<?php echo ($addflag != 1) ? $get["memcode"] : ""; ?>">
                            </div>
                        </div>
                        <div class="row">
                            <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-6 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form name" name="memname" value="<?php echo ($addflag != 1) ? $get["memname"] : ""; ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Address</label>
                            </div>
                            <div class="col-lg-6 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form add1" name="add1" value="<?php echo ($addflag != 1) ? $get["add1"] : "" ?>">
                                <input type="text" class="form-control form add2" name="add2" value="<?php echo ($addflag != 1) ? $get["add2"] : ""; ?>">
                                <input type="text" class="form-control form add3" name="add3" value="<?php echo ($addflag != 1) ? $get["add3"] : ""; ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Mobile No.</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form mobile" name="mobile" value="<?php echo ($addflag != 1) ? $get["mobile"] : ""; ?>">
                            </div>
                        </div>

                    </div>

                    <div class="row">
                        <div class="col-lg-3 col-md-1 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Type</label>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 pad">
                            <select name="type" class="form-control form type select2">
                                <option value="<?php echo ($addflag != 1) ? $get['type'] : ""; ?>"><?php echo ($addflag != 1) ? $get['type'] : ""; ?></option>
                                <option <?php
                                        if ($get['type'] == 'AMOUNT') {
                                            echo "selected=selected";
                                        }
                                        ?> value='AMOUNT'>AMOUNT</option>
                                <option <?php
                                        if ($get['type'] == 'GIFT') {
                                            echo "selected=selected";
                                        }
                                        ?> value='GIFT'>GIFT</option>
                            </select>
                        </div>
                    </div>


                <div  id="otherdtl" style="">
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Amount</label>
                        </div>
                        <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                            <input type="text" class="form-control text-right form amount" name="amount" value="<?php echo ($addflag != 1) ? $get["amount"] : ""; ?>">
                        </div>
                    </div>
                </div>

                <div id="otherdtl2" style="">
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Gift</label>
                        </div>
                        <div class="col-lg-6 col-md-8 col-sm-8 col-xs-8 pad">
                            <table class="table table-bordered">
                                <tr style="height: 26px;" class="mySearchTr">
                                    <th style="width:7%">Item</th>
                                    <th style="width:2%" class="text-right">Qty</th>
                                    <th style="width:5%">Description</th>
                                </tr>
                                <tr style="height: 26px;">
                                    <td>
                                        <input type="text" class="form-control form item" name="item" value="<?php echo ($addflag != 1) ? $get["item"] : ""; ?>">
                                    </td>
                                    <td>
                                        <input type="text" class="form-control text-right form qty" name="qty" value="<?php echo ($addflag != 1) ? $get["qty"] : ""; ?>">
                                    </td>
                                    <td>
                                        <input type="text" class="form-control  form description" name="description" value="<?php echo ($addflag != 1) ? $get["description"] : ""; ?>">
                                    </td>
                                    
                                </tr>
                            </table>
                        </div>
                    </div>
                </div>

                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Remarks</label>
                        </div>
                        <div class="col-lg-6 col-md-8 col-sm-8 col-xs-8 pad">
                            <input type="text" class="form-control form " name="remark1" value="<?php echo ($addflag != 1) ? $get["remark1"] : ''; ?>">
                            <input type="text" class="form-control form " name="remark2" value="<?php echo ($addflag != 1) ? $get["remark2"] : ''; ?>">
                        </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 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">Voucher</option>
                                <option value="2">Date</option>
                                <option value="3">From</option>
                                <option value="4">Name</option>
                                <option value="5">Mobile</option>
                                <option value="6">Type</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">Voucher</option>
                                <option value="2">Date</option>
                                <option value="3">From</option>
                                <option value="4">Name</option>
                                <option value="5">Mobile</option>
                                <option value="6">Type</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" style="border-radius: 5px" 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 class="mySearchTr">
                                    <th width="5%">Voucher</th>
                                    <th width="7%">Date</th>
                                    <th width="5%">From</th>
                                    <th width="15%">Name</th>
                                    <th width="10%">Mobile</th>
                                    <th width="7%">Type</th>
                                    <th width="5%">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" style="border-radius: 5px" class="btn btn-primary sprev" value="Previous">
                            <input type="button" style="border-radius: 5px" 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>