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

$formname = 'Reminder';
$maindb = "remform";
$printfile = "remform_fr.php";
$file = 'remform.php';
$trandbexist = 0;
$keyfld = "vouchno";
$focusfield = "vouchdt";
$keycond = "";
$keycond1 = "";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);

////////////////// FB ////////////////////
$adduser = $_SESSION['username_' . $dbname];
date_default_timezone_set("Asia/Kolkata");
$adddate = date("Y-m-d");
$addtime = date("h:i:sa");
//$usertype = $_SESSION["usertype_" . $dbname];
$usertype = $_SESSION["usertype"];

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


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

    $a = 1;
    $qvouch = $_POST["vouchno"];
    $party = mysqli_real_escape_string($conn, $_POST["name"]);
    $vouchdt = $_POST["vouchdt"];
    $flatno = mysqli_real_escape_string($conn, $_POST["flatno"]);
    $remindfor = mysqli_real_escape_string($conn, $_POST["remindfor"]);
    $mobile = $_POST["mobile"];
    $remind = $_POST["remind"];
    $workdone = $_POST["workdone"];
    $descrip = $_POST["descrip"];
    $descrip2 = $_POST["descrip2"];

    $mvouch = $qvouch;

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



    if ($a == 1) {
        mysqli_query($conn, "LOCK TABLE `$maindb` WRITE");

        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);
                $qvouch = $vvouch["maxcode"] + 1;
            } else {
                $qvouch = '1';
            }
        }
        if ($addflag == 1) {

            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `party`, `vouchdt`, `flatno`, `remindfor`, `mobile`, `remind`, `workdone`, `descrip`
                                                       ,`adduser`, `adddate`, `addtime`, `descrip2`)
                                        VALUES('$mvouch', '$party','$vouchdt', '$flatno', '$remindfor', '$mobile', '$remind', '$workdone', '$descrip',
                                               '$adduser', '$adddate', '$addtime', '$descrip2')") or die(mysqli_error($conn));
            insmess();
            browse($keyfld);
            browse($mvouch);
            header("refresh: 1; $file?mode=edit&$keyfld=$mvouch");
        } else {
            $editdate = date("Y-m-d");
            $edittime = date("h:i:sa");


            mysqli_query($conn, "UPDATE `$maindb` SET  `party`='$party',`vouchdt`='$vouchdt', `flatno`='$flatno', `remindfor`='$remindfor', `mobile`='$mobile',
                        `remind`='$remind', `workdone`='$workdone',  descrip='$descrip',descrip2='$descrip2',`edituser`='$adduser',`edittime`='$edittime',`editdate`='$editdate'
                         where $keyfld = '$mvouch' ") or die(mysqli_error($conn));

            updmess();
            header("refresh: 1; $file?mode=edit&$keyfld=$mvouch");
        }
        mysqli_query($conn, "UNLOCK TABLES");
    }
}

if ($addflag != 1) {

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

    $result2 = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld = '$getvouch'") or die(mysqli_error($conn));
    $fetresult = mysqli_fetch_object($result2);
    
    $party = mysqli_query($conn, "SELECT accode,name FROM `accmast` WHERE accode = '$fetresult->party'") or die(mysqli_error($conn));
    $getparty = mysqli_fetch_object($party);
    
    

}

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


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

include("includes/linkshead.php");
?>
<style>
    #ccode {
        margin-left: -15px;
    }
</style>
<script src="MyScript/remform.js" type="text/javascript"></script>
<script src="MyScript/godown_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">
                    <?php include("includes/usermention.php"); ?>
                    <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">Date</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="date" class="form-control form focusfield vouchdt" name="vouchdt" value="<?php echo ($addflag != 1) ? $fetresult->vouchdt : date("Y-m-d"); ?>" autofocus>
                                <input type="hidden" class="form-control form vouchno" name="vouchno" value="<?php echo ($addflag != 1) ? $fetresult->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">Flat No</label>
                            </div>
                            <div class="col-lg-4 col-md-2 col-sm-2 col-xs-2 pad">
                                <select class='form-control form select2 flatno' name='flatno' >
                                    <option value="<?php echo ($addflag != 1) ? $fetresult->flatno : "" ?>"><?= $fetresult->flatno ?></option>
                                    <?php
                                    $ct = mysqli_query($conn, "SELECT flatno FROM `accmast` WHERE  flatno != '' ") or die(mysqli_error($conn));
                                    while ($ctro = mysqli_fetch_array($ct)) {
                                        echo "<option value='$ctro[flatno]'>$ctro[flatno]</option>";
                                    }
                                    ?>
                                </select>
                            </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">Party</label>
                            </div>
                            <div class="col-lg-4 col-md-2 col-sm-2 col-xs-2 pad">
                            <select class='form-control form select2 party' name='name' >
                                <option value="<?php echo ($addflag != 1) ? $getparty->accode : "" ?>"><?= $getparty->name ?></option>
                                <?php
                                echo "<option value=''></option>";
                                    $ct2 = mysqli_query($conn, "SELECT name,accode FROM `accmast` WHERE grpcode = '010' ") or die(mysqli_error($conn));
                                    while ($ctro2 = mysqli_fetch_array($ct2)) {
                                        echo "<option value='$ctro2[accode]'>$ctro2[name]</option>";
                                    }
                                    ?>
                                </select>
                            </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">For</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <select class='form-control form select2 remindfor' name='remindfor' >
                                    <option value="<?php echo ($addflag != 1) ? $fetresult->remindfor : "" ?>"><?= $fetresult->remindfor ?></option>
                                    <option value="Rent">Rent</option>
                                    <option value="Maintainance">Maintainance</option>
                                    <option value="Electric Bill">Electric Bill</option>
                                    <option value="Other">Other</option>
                                </select>
                            </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</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" class="form-control form mobile" name="mobile" value="<?php echo ($addflag != 1) ? $fetresult->mobile : ""; ?>">
                            </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">Description</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control descrip form" name="descrip" value="<?php echo ($addflag != 1) ? $fetresult->descrip : ""; ?>">
                                <input type="text" class="form-control descrip2 form" name="descrip2" value="<?php echo ($addflag != 1) ? $fetresult->descrip2 : ""; ?>">
                            </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">Remind On</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="date" class="form-control remind form" name="remind" value="<?php echo ($addflag != 1) ? $fetresult->remind : ""; ?>">
                            </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">Done</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                            <input type="checkbox" class="form-control form workdone" id="workdone" name="workdone" value="1" <?
                                            if ($fetresult->workdone == 1) {
                                                Echo "checked";
                                            }
                                            ?>>
                            </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">
                    <!--                    <input type="hidden" value="<?php echo $fseries ?>" class="form-control series">
                    <input type="hidden" value="<?php echo $get[vouchno]; ?>" class="form-control vouchno">
                    <input type="hidden" value="<?php echo $file ?>" class="form-control file">
                    <input type="hidden" value="<?php echo $para ?>" class="form-control para">-->
                    <div class="row">
                        <div class="col-lg-2">
                            <select class="form-control sfield">
                                <option value="2">Name</option>
                                <option value="1">Godown Code</option>
                                <option value="3">Address</option>
                                <option value="4">City</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="2">Name</option>
                                <option value="1">Godown Code</option>
                                <option value="3">Address</option>
                                <option value="4">City</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 class="mySearchTr">
                                    <th width="20%">Name</th>
                                    <th width="15%">Godown Code</th>
                                    <th width="35%">Address</th>
                                    <th width="20%">City</th>
                                    <th width="10%">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>