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

$formname = 'Follow Up';
$maindb = 'folwmain';
$trandb = 'folwtran';
$printfile = "followup_print.php";
$file = 'followup.php';

$keyfld = "vouchno";


$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;
    $vouchno = $_POST["vouchno"];
    $vouchdt = $_POST["vouchdt"];
    $party = $_POST["party"];
    $pin = $_POST["pincode"];
    $city = $_POST["city"];
    $mob1 = $_POST["mobile1"];
    $mob2 = $_POST["mobile2"];
    $email = $_POST["email"];
    $howknow = $_POST["howknow"];
    $interest = $_POST["interest"];
    $flat1 = $_POST["flat1"];
    $flat2 = $_POST["flat2"];
    $flat3 = $_POST["flat3"];
    $flat4 = $_POST["flat4"];
    $flat5 = $_POST["flat5"];
    $remark1 = $_POST["remark1"];
    $remark2 = $_POST["remark2"];


    $count = $_POST["items"];


    if ($party == "") {
        $a = 0;
        browse("Name Cannot Be Empty");
        if ($addflag == 1) {
            header("location:javascript://history.go(-1)");
        } else {
            header("refresh: ; $file?mode=last");
        }
    }

    if (isset($_POST["party"])) {
        $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `party`= '$party' AND vouchno != '$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 ,`$trandb` WRITE");

        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX($keyfld) AS maxcode FROM `$maindb` where vouchno != '' 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';
            }
        }
        $mvouch = $qvouch;

        mysqli_query($conn, "DELETE FROM `$trandb` WHERE vouchno='$mvouch' ") or die(mysqli_error($conn));

        $ctr = 0;

        for ($i = 0; $i <= $count; $i++) {
            $ctr++;
            $msrno = $_POST["srno_$i"];
            $trandt = $_POST["trandt_$i"];
            $descrip = $_POST["descrip_$i"];
            $remdate = $_POST["remdate_$i"];


            if (!empty($descrip)) {

                mysqli_query($conn, "INSERT INTO `$trandb`(vouchno , srno, trandt, descrip, remdate)
                                                  VALUES('$qvouch', '$ctr', '$trandt', '$descrip', '$remdate')") or die(mysqli_error($conn));
            }
        }
        if ($addflag == 1) {

            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `party`, `pincode`, `city`, `mobile1`, `mobile2`, `email`, `howknow`, `interest`,
                                                      `flat1` , `flat2` , `flat3`, `flat4`,`flat5`,`remark1`,`remark2`)
                                        VALUES($qvouch, '$vouchdt', '$party', '$pin', '$city', '$mob1', '$mob2', '$email', '$howknow', '$interest',
                                               '$flat1','$flat2', '$flat3', '$flat4', '$flat5', '$remark1', '$remark2')") or die(mysqli_error($conn));

            insmess();
            $mvouch = $_GET["vouchno"];
            $mvouch = $qvouch;
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");

//            header("refresh: 1; $file?mode=true");
        } else {
            $mvouch = $qvouch;
            mysqli_query($conn, "UPDATE `$maindb` SET `vouchdt`='$vouchdt',`party`='$party',`pincode`='$pin', `city`='$city', `mobile1`='$mob1', `mobile2`='$mob2',
                        `email`='$email', `howknow`='$howknow', `interest`='$interest', `flat1`='$flat1', flat2= '$flat2',
                         flat3= '$flat3',flat4='$flat4',flat5='$flat5', remark1='$remark1', remark2='$remark2' where vouchno = '$mvouch' ") or die(mysqli_error($conn));

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

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"])) {
    $delcond = " vouchno = '$_GET[vouchno]' ";

    delnew($zdb, "$maindb", "$delcond", "$file?mode=last");
    delnew($zdb, "$trandb", "$delcond", "$file?mode=last");
}

include("includes/linkshead.php");
?>
<script src="MyScript/followup.js" type="text/javascript"></script>
<script src="MyScript/followup_search.js" type="text/javascript"></script>
</head>
<body>
    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-10 pad">
                <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="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Voucher No.</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="text" class="form-control form" name="vouchno" value="<?php Echo ($addflag != 1) ? $get["vouchno"] : "$qvouch"; ?>" readonly>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 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" name="vouchdt" value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d"); ?>" >
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                                <label class="label-control marg">Party Name</label>
                            </div>
                            <div class="col-lg-5 col-md-5 col-sm-5 col-xs-5 pad">
                                <input type="text" class="form-control form focusfield" required name="party" value="<?php Echo ($addflag != 1) ? $get["party"] : ""; ?>" autofocus>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Pin</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="text" class="form-control form " name="pincode" value="<?php Echo ($addflag != 1) ? $get["pincode"] : ""; ?>" >
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                                <label class="label-control marg">City</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" class="form-control form" name="city" value="<?php Echo ($addflag != 1) ? $get["city"] : ""; ?>" >
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                                <label class="label-control marg">Mobile 1</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" class="form-control form mobile1" name="mobile1" value="<?php Echo ($addflag != 1) ? $get["mobile1"] : ""; ?>" >
                            </div>
                            <div class="col-lg-1 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-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" class="form-control form mobile2" name="mobile2" value="<?php Echo ($addflag != 1) ? $get["mobile2"] : ""; ?>" >
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">E-mail</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                                <input type="email" class="form-control form" name="email" value="<?php Echo ($addflag != 1) ? $get["email"] : ""; ?>" >
                            </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">How Know About Us</label>
                            </div>
                            <div class="col-lg-5 col-md-5 col-sm-5 col-xs-5 pad">
                                <input list='howknow' name='howknow' value="<?php echo ($addflag != 1 ) ? $get['howknow'] : '' ?>" class='howknow form-control form ' autocomplete="off">
                                <datalist id='howknow' char="#">
                                    <option value=''></option>
                                    <?php
                                    $mhow = mysqli_query($conn, "SELECT `howknow` FROM `$maindb` where howknow != '' group by howknow") or die(mysqli_error($conn));

                                    while ($thow = mysqli_fetch_array($mhow)) {
                                        $howknow = $thow['howknow'];
                                        echo "<option value='$howknow'>";
                                    }
                                    ?>
                                </datalist>
                            </div>
                        </div>   
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Interest</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <select id="interest" name="interest" class="select2 form-control form interest">
                                    <option value=""></option>
                                    <option
                                    <?php
                                    if ($get['interest'] == 'Y') {
                                        echo "selected=selected";
                                    }
                                    ?> value='Y'>Yes</option>
                                    <option
                                    <?php
                                    if ($get['interest'] == 'N') {
                                        echo "selected=selected";
                                    }
                                    ?> value='N'>No</option>
                                    <option
                                    <?php
                                    if ($get['interest'] == 'M') {
                                        echo "selected=selected";
                                    }
                                    ?> value='M'>May Be</option>
                                </select>
                            </div>
                        </div>
                        <hr style="border-top: 1px solid black;width:101.2%;margin-top: 10px;margin-bottom: 0px;">
                        <div class="row" style="background-color: #F5EB9F">
                            <div class="col-lg-2 col-md-1 col-sm-1 col-xs-1 text-right pad ">
                                <label class="label-control marg">1.</label>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" class="form-control form flat1" name="flat1" value="<?php Echo ($addflag != 1) ? $get["flat1"] : ""; ?>">
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad ">
                                <label class="label-control marg">2.</label>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-2 col-xs- pad">
                                <input type="text" class="form-control form flat2" name="flat2" value="<?php Echo ($addflag != 1) ? $get["flat2"] : ""; ?>">
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad ">
                                <label class="label-control marg">3.</label>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" class="form-control form flat3" name="flat3" value="<?php Echo ($addflag != 1) ? $get["flat3"] : ""; ?>">
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad ">
                                <label class="label-control marg">4.</label>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" class="form-control form flat4" name="flat4" value="<?php Echo ($addflag != 1) ? $get["flat4"] : ""; ?>">
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad ">
                                <label class="label-control marg">5.</label>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" class="form-control form flat5" name="flat5" value="<?php Echo ($addflag != 1) ? $get["flat5"] : ""; ?>">
                            </div>
                        </div>
                        <hr style="border-top: 1px solid black;width:101.2%;margin-top: 0px;margin-bottom: 0px;">
                        <div class="table-responsive">
                            <table class="table-striped table-condensed table-bordered tab" width="100%">
                                <thead  class="mySearchTr">
                                    <tr>
                                        <th style="width: 1%"></th>
                                        <th class="srno" style="width: 1%">Sr.</th>
                                        <th style="width: 8%;">Date</th> 
                                        <th style='width: 40%'>Description</th>
                                        <th style='width: 8%;'>Reminder date</th>
                                    </tr>
                                </thead>
                                <tbody class="tbody">
                                    <?php
                                    if ($addflag != 1) {
                                        $qry = mysqli_query($conn, "SELECT * FROM  `$trandb` WHERE vouchno='$getvouch' ORDER BY srno") or die(mysqli_error($conn));
                                        $ctr = 0;
                                        while ($row = mysqli_fetch_array($qry)) {


                                            Echo "<tr>";
                                            Echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                                            Echo "<td><input type='text' name='srno_$ctr' class='srno form-control form'  value='$row[srno]'></td>";
                                            Echo "<td><input type='date' name='trandt_$ctr' class='trandt form-control  form' value='$row[trandt]'></td>";
                                            Echo "<td><input type='text' name='descrip_$ctr' class='descrip form-control form' value='$row[descrip]'></td>";
                                            Echo "<td><input type='date' name='remdate_$ctr' class='remdate form-control form' value='$row[remdate]'></td>";
                                            Echo "</tr>";
                                            $ctr++;
                                        }
                                    } else {
                                        ?>
                                        <tr>                                    
                                            <td><i class='fa fa-close remove'></i></td>
                                            <td><input type='text' name='srno_0' class='srno form-control form' value='1'></td>
                                            <td><input type='date' name='trandt_0' class='trandt form-control form' value=''></td> 
                                            <td><input type='text' name='descrip_0' class='descrip form-control form pad' value=''></td> 
                                            <td><input type='date' name='remdate_0' class='remdate form-control form pad' value=''></td> 
                                        </tr>
                                        <?php
                                    }
                                    ?>
                                </tbody>
                            </table>


                        </div>
                        <div class="col-lg-12 pad">
                            <button type="button" class="btn btn-primary btn-square form"  id="addnew">
                                <i class="fa fa-plus" data-toggle='tooltip' title='Add Row'></i>    
                            </button>
                            <input type="hidden" id="items" name="items" value="<?php Echo ($addflag != 1) ? $ctr : 1; ?>" >
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Remarks</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 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">
                        <div class="col-lg-10">
                            <?php include("includes/buttons.php"); ?>
                        </div>
                        <div class="col-lg-2">
                            <button id='smscust' type="button" class="btn btn-pad btn-lg smscust" style="background-color:grey;width: 100px ">
                                SMS
                            </button>
                        </div>
                    </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">Party</option>
                                <option value="2">Vouchno</option>
                                <option value="3">Mobile</option>
                                <option value="4">Interest</option>
                                <!--<option value="5">E-mail</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="1">Party</option>
                                <option value="2">Vouchno</option>
                                <option value="3">Mobile</option>
                                <option value="4">Interest</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 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="30%">Party</th>
                                    <th width="20%">Vouchno</th>
                                    <th width="20%">Mobile</th>
                                    <th width="10%">Interest</th>
                                    <!--<th width="10%">E-mail</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>
