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

$formname = 'Medical Help';
$maindb = 'medihelp';
$printfile = "medihelp_pdf.php";
$file = 'medihelp.php';
$trandbexist = 0;
$grid = '';
$fseries = $_SESSION["series_" . $dbname];
$keycond = " and `series`='$fseries'";
$keyfld = "vouchno";
$focusfield = "name";
$username = $_SESSION["username_" . $dbname];
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zroot"];
$source = 'MDHP';

mysqli_select_db($conn, $zdb);

date_default_timezone_set('Asia/Kolkata');
$sktime = date('h:i a');

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

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

    $a = 1;
    $qvouch = $_POST["vouchno"];
    $vouchdt = $_POST["vouchdt"];
    $memno = $_POST["memno"];
    $memname = $_POST["memname"];
    $add1 = $_POST["add1"];
    $add2 = $_POST["add2"];
    $add3 = $_POST["add3"];
    $mobile = $_POST["mobile"];
    $gender = $_POST["gender"];
    $marstat = $_POST["marstat"];
    $bloodgrp = $_POST["bloodgrp"];
    $qualification = $_POST["qualification"];
    $occupation = $_POST["occupation"];
    $phydis = $_POST["phydis"];
    $reqamt = $_POST["reqamt"];
    $reqreason = $_POST["reqreason"];
    $reqreason2 = $_POST["reqreason2"];
    $sancamt = $_POST["sancamt"];
    $sancdt = $_POST["sancdt"];
    $recvby = $_POST["recvby"];
    $givenby = $_POST["givenby"];
    $remark1 = $_POST["remark1"];
    $remark2 = $_POST["remark2"];

    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 = '1';
            }
            $qvouch = $increvouch;
        }
        $mvouch = $qvouch;

        if ($addflag == 1) {

            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `memno`, `memname`, `add1`, `add2`, `add3`, 
                                                       `mobile`, `gender`, `marstat`, `bloodgrp`, `qualification`, `occupation`, 
                                                       `phydis`, `reqamt`, `reqreason`, `reqreason2`, `sancamt`, `sancdt`, `recvby`, `givenby`, 
                                                       `remark1`, `remark2`,`series`)
                                                VALUES('$qvouch', '$vouchdt', '$memno', '$memname', '$add1', '$add2', '$add3',
                                                       '$mobile', '$gender ', '$marstat', '$bloodgrp', '$qualification', '$occupation', 
                                                       '$phydis', '$reqamt', '$reqreason', '$reqreason2', '$sancamt', '$sancdt', '$recvby', '$givenby',
                                                       '$remark1', '$remark2', '$fseries')") or die(mysqli_error($conn));
            insmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch;
            mysqli_query($conn, "UPDATE `$maindb` SET `memno`='$memno', `memname`='$memname', `add1`='$add1', `add2`='$add2', `add3`='$add3', 
                                                      `mobile`='$mobile', `gender`='$gender', `marstat`='$marstat', `bloodgrp`='$bloodgrp', 
                                                      `qualification`='$qualification', `occupation`='$occupation', `phydis`='$phydis', `reqamt`='$reqamt',
                                                      `reqreason`='$reqreason', `reqreason2`='$reqreason2', `sancamt`='$sancamt', `sancdt`='$sancdt', `recvby`='$recvby', `givenby`='$givenby',
                                                       `remark1`='$remark1',`remark2`='$remark2', `series`='$fseries'  where vouchno = '$mvouch' $keycond") 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' $keycond ") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);
}

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

if (isset($_GET["delete"])) {
    $delcondn = " $keyfld = '$_GET[$keyfld]' $keycond";
    delnew($zdb, "$maindb", "$delcondn", "$file?mode=last&series=$fseries");
}

include("includes/linkshead.php");
?>

<script src="MyScript/medihelp.js" type="text/javascript"></script>
<script src="MyScript/medihelp_search.js" type="text/javascript"></script>

</head>

<body>
    <form action='' method='POST' enctype="multipart/form-data">
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-8">
                <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-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Voucher</label>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="vouchno" class="form-control form vouchno" value="<?php echo ($addflag != 1) ? $get["vouchno"] : $qvouch; ?>" readonly>
                                <input type="hidden" class="form-control form code" name="code" value="<?php echo ($addflag != 1) ? $get["code"] : ""; ?>">
                            </div>

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

                        </div>

                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Member</label>
                            </div>
                            <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form memno" name="memno" value="<?php echo ($addflag != 1) ? $get['memno'] : ''; ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Name</label>
                            </div>
                            <div class="col-lg-5 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form memname" name="memname" value="<?php echo ($addflag != 1) ? $get['memname'] : ''; ?>" readonly>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control ">Address</label>
                            </div>
                            <div class="col-lg-5 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'] : ''; ?>" readonly>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            </div>
                            <div class="col-lg-5 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form add2" name="add2" value="<?php echo ($addflag != 1) ? $get['add2'] : ''; ?>" readonly>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            </div>
                            <div class="col-lg-5 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form add3" name="add3" value="<?php echo ($addflag != 1) ? $get['add3'] : ''; ?>" readonly>
                            </div>

                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Gender</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="gender" class="form-control form gender" value="<?php echo ($addflag != 1) ? $get["gender"] : ""; ?>" readonly>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Married Status</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="marstat" class="form-control form marstat" value="<?php echo ($addflag != 1) ? $get["marstat"] : ""; ?>" readonly>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Mobile</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="mobile" class="form-control form mobile" value="<?php echo ($addflag != 1) ? $get["mobile"] : ""; ?>" readonly>
                            </div>


                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg" style="font-size: 13px;">Blood Group</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="bloodgrp" class="form-control form bloodgrp" value="<?php echo ($addflag != 1) ? $get["bloodgrp"] : ""; ?>" readonly>
                            </div>
                        </div>

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

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

                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg" style="color:red">Physically Disabled</label>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="phydis" class="form-control form phydis" value="<?php echo ($addflag != 1) ? $get["phydis"] : ""; ?>" readonly>
                            </div>
                        </div>

                        <hr>


                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Requirement</label>
                            </div>

                            <div class="col-lg-2 col-md-3 col-sm-8 col-xs-8 pad">
                                 <input type="text" class="form-control form text-right reqamt" name="reqamt" value="<?php echo ($addflag != 1) ? $get['reqamt'] : ''; ?>">
                            </div>

                            <div class="col-lg-4 col-md-3 col-sm-8 col-xs-8 pad">
                                 <input type="text" class="form-control form reqreason" name="reqreason" value="<?php echo ($addflag != 1) ? $get['reqreason'] : ''; ?>">
                                 <input type="text" class="form-control form reqreason2" name="reqreason2" value="<?php echo ($addflag != 1) ? $get['reqreason2'] : ''; ?>">
                            </div>
                        </div>


                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Sanctioned</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-8 col-xs-8 pad">
                                 <input type="text" class="form-control form text-right sancamt" name="sancamt" value="<?php echo ($addflag != 1) ? $get['sancamt'] : ''; ?>">
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Sanctioned Date</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-8 col-xs-8 pad">
                                 <input type="date" class="form-control form sancdt" name="sancdt" value="<?php echo ($addflag != 1) ? $get['sancdt'] : ''; ?>">
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Sanctioned By</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-8 col-xs-8 pad">
                                 <input type="text" class="form-control form givenby" name="givenby" value="<?php echo ($addflag != 1) ? $get['givenby'] : ''; ?>">
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Received By</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-8 col-xs-8 pad">
                                 <input type="text" class="form-control form recvby" name="recvby" value="<?php echo ($addflag != 1) ? $get['recvby'] : ''; ?>">
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-2 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-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="remark1" class="form-control form remark1" value="<?php echo ($addflag != 1) ? $get["remark1"] : ""; ?>">
                                <input type="text" name="remark2" class="form-control form 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">Member Code</option>
                                <option value="4">Name</option>
                                <option value="5">Mobile</option>
                                <option value="6">Remark</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">Member Code</option>
                                <option value="4">Name</option>
                                <option value="5">Mobile</option>
                                <option value="6">Remark</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-responsive table-striped table-condensed table-bordered" width="100%">
                            <thead>
                                <tr class="mySearchTr">
                                    <th width="4%">Voucher</th>
                                    <th width="6%">Date</th>
                                    <th width="7%">Member Code</th>
                                    <th width="10%">Name</th>
                                    <th width="10%">Mobile</th>
                                    <th width="20%">Remarks</th>
                                    <th width="2%">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>