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

$formname = 'Widow Help Monthly';
$maindb = 'widowhelp';
// $printfile = "widowhelp.php";
$file = 'widowhelp.php';
$trandbexist = 0;
$grid = '';
$fseries = $_SESSION["series_" . $dbname];
$keycond = " and `series`='$fseries'";
$keyfld = "vouchno";
$focusfield = "memcode";
$username = $_SESSION["username_" . $dbname];
$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;
    $qvouch = $_POST["vouchno"];
    $memcode = $_POST["memcode"];
    $name = $_POST["name"];
    $city = $_POST["city"];
    $add1 = $_POST["add1"];
    $add2 = $_POST["add2"];
    $add3 = $_POST["add3"];
    $pin = $_POST["pin"];
    $station = $_POST["station"];
    $country = $_POST["country"];
    $tel = $_POST["tel"];
    $mobile = $_POST["mobile"];
    $mobile2 = $_POST["mobile2"];
    $email = $_POST["email"];
    $gender = $_POST["gender"];
    $mstatus = $_POST["mstatus"];
    $active = $_POST["active"];

    if (isset($_POST["gender"])) {
        if ($_POST["gender"] == 'M') {
            browse("Gender Cannot Be Male");
            header("refresh: 1; $file?mode=last");
            die();
        }
    }
    
    if (isset($_POST["mstatus"])) {
        if ($_POST["mstatus"] != 'Widow') {
            browse("This Member is not a Widow");
            header("refresh: 1; $file?mode=last");
            die();
        }
    }

    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`, `memcode`, `name`, `city`, `add1`, `add2`, `add3`, `pin`, `station`,
                                                       `country`, `tel`,`mobile`, `mobile2`, `email`,`series`, `gender`, `mstatus`, `active`)
                                                VALUES('$qvouch', '$memcode', '$name', '$city', '$add1', '$add2', '$add3', 
                                                '$pin', '$station', '$country', '$tel', '$mobile','$mobile2', '$email', '$fseries', '$gender','$mstatus', '$active')") 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', `name`='$name', `city`='$city', 
                                                     `add1`='$add1', `add2`='$add2', `add3`='$add3', `pin`='$pin', `station`='$station',
                                                     `country`='$country', `tel`='$tel', `mobile`='$mobile',
                                                     `mobile2`='$mobile2', `email`='$email', `gender`='$gender', `mstatus`='$mstatus', 
                                                     `series`='$fseries', `active`='$active' 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/widowhelp.js" type="text/javascript"></script>
<script src="MyScript/widowhelp_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-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-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Memcode</label>
                            </div>
                            <div class="col-lg-3 col-md-8 col-sm-8 col-xs-8 pad" style="background-color:blue;border-radius:4px">
                                <input type="text" class="form-control form memcode focusfield" name="memcode" value="<?php echo ($addflag != 1) ? $get["memcode"] : ""; ?>">
                                <input type="hidden" name="vouchno" class="form-control form vouchno" value="<?php echo ($addflag != 1) ? $get["vouchno"] : $qvouch; ?>">
                            </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-8 col-md-8 col-sm-8 col-xs-8 pad" style="background-color:blue;border-radius:4px">
                                <input type="text" class="form-control name form" name="name" value="<?php echo ($addflag != 1) ? $get["name"] : ""; ?>" tabindex="-1" readonly>
                            </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-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control add1 form" name="add1" value="<?php echo ($addflag != 1) ? $get["add1"] : ""; ?>" tabindex="-1">
                                <input type="text" class="form-control add2 form" name="add2" value="<?php echo ($addflag != 1) ? $get["add2"] : ""; ?>" tabindex="-1">
                                <input type="text" class="form-control add3 form" name="add3" value="<?php echo ($addflag != 1) ? $get["add3"] : ""; ?>" tabindex="-1">
                            </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">City</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" class="form-control city form" name="city" value="<?php echo ($addflag != 1) ? $get["city"] : ""; ?>" tabindex="-1">
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Pin</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" class="form-control pin form" name="pin" value="<?php echo ($addflag != 1) ? $get["pin"] : ""; ?>" tabindex="-1">
                            </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">Station</label>
                            </div>
                            <div class="col-lg-3 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control station form" name="station" value="<?php echo ($addflag != 1) ? $get["station"] : ""; ?>" tabindex="-1">
                            </div>
                            <div class="col-lg-3 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-8 col-sm-8 col-xs-8 pad">
                                <?php
                                if ($get['gender'] == 'F') {
                                    $gender = 'Female';
                                } elseif ($get['gender'] == 'M') {
                                    $gender = 'Male';
                                    browse('Gender Cannot be Male');
                                    header("refresh: 1; $file?mode=last");
                                    die();
                                }
                                ?>
                                <input type="text" class="form-control gender form" name="gender" value="<?php echo ($addflag != 1) ? $get['gender'] : ""; ?>" tabindex="-1">
                            </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">Country</label>
                            </div>
                            <div class="col-lg-3 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control country form" name="country" value="<?php echo ($addflag != 1) ? $get["country"] : ""; ?>" tabindex="-1">
                            </div>
                            <div class="col-lg-3 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-8 col-sm-8 col-xs-8 pad">
                                <?php
                                if ($get['mstatus'] == 'S') {
                                    $mastatus = 'single';
                                    browse('This Member is not a Widow');
                                } elseif ($get['mstatus'] == 'M') {
                                    $mastatus = 'Married';
                                    browse('This Member is not a Widow');
                                } elseif ($get['mstatus'] == 'W') {
                                    $mastatus = 'Widow';
                                }
                                ?>
                                <input type="text" class="form-control mstatus form" name="mstatus" value="<?php echo ($addflag != 1) ? $get['mstatus'] : ""; ?>" tabindex="-1">
                            </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">Tel.</label>
                            </div>
                            <div class="col-lg-3 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control tel form" name="tel" value="<?php echo ($addflag != 1) ? $get["tel"] : ""; ?>" tabindex="-1">
                            </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 1</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" class="form-control mobile form" name="mobile" value="<?php echo ($addflag != 1) ? $get["mobile"] : ""; ?>" tabindex="-1">
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Mobile 2</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" class="form-control mobile2 form" name="mobile2" value="<?php echo ($addflag != 1) ? $get["mobile2"] : ""; ?>" tabindex="-1">
                            </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">E-mail</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control email form" name="email" value="<?php echo ($addflag != 1) ? $get["email"] : ""; ?>" tabindex="-1">
                            </div>
                        </div>
                        <div class="row">
                            <label class="control-label col-lg-3 text-right padd">Active</label>
                            <div class="col-lg-8 pad"> 
                                <input type="checkbox" name="active" value="1" class="form form-control" style="width: 30px" <?
                                if ($get['active'] == 1) {
                                    Echo "checked";
                                } else {
                                    Echo "unchecked";
                                }
                                ?>>
                            </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">Sr.</option>
                                <option value="2">Name</option>
                                <option value="3">Address</option>
                                <option value="4">City</option>
                                <option value="5">Station</option>
                                <option value="6">Tel</option>
                                <option value="7">Mobile</option>
                                <option value="8">Email</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">Sr.</option>
                                <option value="2">Name</option>
                                <option value="3">Address</option>
                                <option value="4">City</option>
                                <option value="5">Station</option>
                                <option value="6">Tel</option>
                                <option value="7">Mobile</option>
                                <option value="8">Email</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="3%">Sr.</th>
                                    <th width="7%">Name</th>
                                    <th width="5%">Address</th>
                                    <th width="7%">City</th>
                                    <th width="5%">Station</th>
                                    <th width="10%">Tel</th>
                                    <th width="10%">Mobile</th>
                                    <th width="6%">Email</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>