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

$formname = 'Expense Head Entry';
$maindb = 'expentry_pe';

$file = 'expheadentry.php';

$keyfld = "vouchno";
$trandbexist = 0;
$focusfld = "name";
$printfile = 'expheadentry_print.php';
$usertype = $_SESSION["usertype"];
$username = $_SESSION["username_" . $dbname];
$zroot = $_SESSION["zdir"];

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zyear = right($zdb, 4);

$zStartDate = getzdates($zyear, 'S');
$zEndDate = getzdates($zyear, 'E');
date_default_timezone_set('Asia/Kolkata');
// 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"];
    $time = $_POST["time"];
    $expense = $_POST["expense"];
    $amount = $_POST["amount"];
    $paidby = $_POST["paidby"];
    $payee = $_POST["payee"];
    $remark = $_POST["remark"];

    // browse($qvouch);
    $mvouch = $qvouch;

    if ($a == 1) {
        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE") or die(mysqli_error($conn));

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


        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `expense`, `paidby`, `amount`, 
                                                       `remark`, `payee`, `time`, `adduser`)
                                               VALUES ('$qvouch', '$vouchdt', '$expense', '$paidby', '$amount',
                                                       '$remark', '$payee', '$time', '$username')") or die(mysqli_error($conn));
        } else {
            $mvouch = $qvouch;

            mysqli_query($conn, "UPDATE `$maindb` SET `vouchdt`='$vouchdt', `expense`='$expense', `paidby`='$paidby', `amount`='$amount',
                                                      `remark`='$remark', `time`='$time', `payee`='$payee', `edituser`='$username'  
                                                       WHERE vouchno = '$mvouch' ") or die(mysqli_error($conn));
        }

        mysqli_query($conn, "UNLOCK TABLES");

        if ($addflag == 1) {
            insmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=last");
        } else {
            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);

    $getexpense = mysqli_query($conn, "SELECT * FROM `accmast_pe` WHERE `accode` = '$get[expense]'") or die(mysqli_error($conn));
    $gtpt = mysqli_fetch_object($getexpense);

    $getpayee = mysqli_query($conn, "SELECT `user` FROM $zroot.`passmain` WHERE `user` = '$get[payee]'") or die(mysqli_error($conn));
    $ftpy = mysqli_fetch_object($getpayee);
}

/* DELETE */
if (isset($_GET["delete"])) {
    mysqli_query($conn, "LOCK TABLES `$maindb` WRITE") or die(mysqli_error($conn));

    delete($zroot, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");

    mysqli_query($conn, "UNLOCK TABLES");
}

include("includes/linkshead.php");
?>
<script src="MyScript/expheadentry.js?a=<?= mt_rand() ?>" type="text/javascript"></script>
<script src="MyScript/expheadentry_search.js?b=<?= mt_rand() ?>" type="text/javascript"></script>
<script src="MyScript/shortkeys.js" type="text/javascript"></script>

<style type="text/css">
    .bgsale {
        background-color: #e3f2e1;
    }

    .bgpurch {
        background-color: #8fcccc;
    }

    .row {
        width: 100%;
        margin-right: 0px;
        margin-left: 0px;
    }

    .styled-table {
        width: 100%;
        border-collapse: collapse;
        font-family: 'Segoe UI', sans-serif;
        font-size: 14px;
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        margin-bottom: 20px;
    }

    .styled-table thead tr {
        background-color: #a98467;
        color: #ffffff;
        text-align: center;
    }

    .styled-table th,
    .styled-table td {
        font-size: 12px;
        padding: 7px 8px;
        border: 1px solid #ddd;
    }

    .styled-table tbody tr {
        background-color: #ffffff;
        transition: background-color 0.2s ease;
    }

    .styled-table tbody tr:hover {
        background-color: #f1f1f1;
    }

    .styled-table .text-right {
        text-align: right;
    }

    .styled-table .text-center {
        text-align: center;
    }

    .styled-table tfoot td {
        font-weight: bold;
        color: #e74c3c;
    }

    .skpad {
        padding: 15px;
    }

    @media (max-width: 768px) {
        .styled-table {
            font-size: 12px;
        }
    }
</style>

</head>

<body>

    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-5 col-md-5 col-sm-12 col-xs-12 pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title" style="color:#78909C"><a class="myPrint"><?php echo $formname; ?></a>
                        </h3>
                    </div>

                    <div class="panel-body">

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

                            <div class="col-lg-2 col-md-2 col-sm-1 col-xs-1 pad text-right">
                                <label class="label-control marg">Time</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-4 col-xs-4 pad">
                                <input type="time" name="time" id="time"
                                       value="<?php echo ($addflag != 1) ? $get['time'] : date('H:i'); ?>"
                                       class="form-control form time" readonly tabindex="-1">
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3  text-right pad">
                                <label class="label-control marg">Expense</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-9 col-xs-9 pad">
                                <select id="expense" name="expense" class="form-control form expense select2" required>
                                    <option value="<?php echo ($addflag != 1) ? $gtpt->accode : " "; ?>"><?php echo ($addflag != 1) ? $gtpt->name : " "; ?></option>
                                    <?php
                                    $mown = mysqli_query($conn, "SELECT * FROM `accmast_pe` WHERE grpcode = '022' order by `name`") or die(mysqli_error($conn));
                                    while ($trow = mysqli_fetch_array($mown)) {
                                        echo "<option value=$trow[accode]>$trow[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>

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

                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad ">
                                <label class="label-control marg">Paid By</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-4 col-xs-4 pad">
                                <select id="paidby" name="paidby" class="select2 form-control form paidby" required>
                                    <?php
                                    $paidbyOptions = [
                                        'R' => 'Credit',
                                        'C' => 'Cash',
                                        'U' => 'UPI'
                                    ];

                                    foreach ($paidbyOptions as $value => $label) {
                                        $selected = ($get['paidby'] == $value) ? 'selected' : '';
                                        echo "<option value=\"$value\" $selected>$label</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad ">
                                <label class="label-control marg">Payee</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                                <select id="payee" name="payee" class="form-control form payee select2" required>
                                    <option value="<?php echo ($addflag != 1) ? $ftpy->user : "$username"; ?>"><?php echo ($addflag != 1) ? $ftpy->user : "$username"; ?></option>
                                    <?php
                                    if ($usertype == 1) {
                                        $gtuser = mysqli_query($conn, "SELECT `user` FROM $zroot.`passmain` ") or die(mysqli_error($conn));
                                        while ($ftuser = mysqli_fetch_array($gtuser)) {
                                            echo "<option value='$ftuser[user]'>$ftuser[user]</option>";
                                        }
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>

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


                    <div class="panel-footer">
                        <?php include("includes/buttons.php"); ?>
                    </div>
                </div>
            </div>

            <div class="col-lg-7 col-md-7 col-sm-12 col-xs-12 pad">
                <div class="row">
                    <table class="styled-table">
                        <thead>
                            <tr>
                                <td style="width:3%;">Sr.</td>
                                <td style="width:8%;"  class='text-center'>Date</td>
                                <td style="width:15%;">Expense</td>
                                <td style="width:10%;" class='text-right'>Amount</td>
                                <td style="width:4%;"  class='text-center'>Paidby</td>
                                <td style="width:12%;" class='text-left'>Payee</td>
                            </tr>
                        </thead>

                        <tbody class="tbodyy">

                        </tbody>
                    </table>
                </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 $file ?>" class="form-control file">
                    <div class="row">
                        <div class="col-lg-2">
                            <select class="form-control sfield">
                                <option value="">Sort By</option>
                                <option value="1">Voucher</option>
                                <option value="2">Date</option>
                                <option value="3">Expense</option>
                                <option value="4">Unit</option>
                                <option value="5">Paid By</option>
                                <option value="6">Payee</option>
                                <option value="7">Amount</option>
                                <!--<option value="6">Amount</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">Expense</option>
                                <option value="4">Unit</option>
                                <option value="5">Paid By</option>
                                <option value="6">Payee</option>
                                <option value="7">Amount</option>
                                <!--<option value="6">Amount</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="5%">Sr</th>
                                    <th width="10%">Date</th>
                                    <th width="25%">Expense</th>
                                    <!-- <th width="10%">Unit</th> -->
                                    <th width="10%">Paid By</th>
                                    <th width="10%">Payee</th>
                                    <th width="10%" class="text-right">Amount</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" 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>