<?php
include("../../classes/manager.class.php");
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
$m = new Manager();
$output = '';
if (!isset($_SESSION['sess_userid'])) {
    header("Location: " . PROJECT_ADMIN_URL);
    exit();
}

$bank_account_number = "";
$status = "";
$type = "";
$particulars = "";
$statement_bank_id = "";
$amount = "";
$current_date = date('d-m-Y');
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('../includes/inc_head.php'); ?>
    <script>
        $(function () {
            $('#datepicker').pickadate({
                format: 'dd-mm-yyyy',
                selectMonths: true,
            });
        });
    </script>
</head>
<body>
<!-- Start Page Loading -->
<div id="loader-wrapper">
    <div id="loader"></div>
    <div class="loader-section section-left"></div>
    <div class="loader-section section-right"></div>
</div>
<!-- End Page Loading -->
<div id="container">
    <?php include('../includes/inc_top.php'); ?>
    <div class="row">
        <div class="col s12 m12">
            <div class="">
                <h3>Bank Transaction</h3>
            </div>
            <hr>
            <?php if ($_SESSION['sess_role'] == 'VEC') {
                if (isset($_GET['view'])) { ?>
                    <a class="btn waves-effect waves-light" style="float:right;margin-left:5px;" href="?add">Add<i class="mdi-content-add-box"></i></a>
                    <a class="btn waves-effect waves-light" style="float:right;margin-left:5px;" href="reportbanktransaction.php">Report<i class="mdi-content-add-box"></i></a>
                    <div class="clearfix"></div>
                <?php }
            } ?>
            <br/>
            <?php
            if (isset($_GET['add'])) {
            if (isset($_POST) && !empty($_POST)) {
                extract($_REQUEST);
                $data['particulars'] = $particulars;
                $data['statement_bank_id'] = $statement_bank_id;
                $data['type'] = $type;
                $data['amount'] = $amount;
                $data['status'] = $status;
                $data['create_date'] = date('Y-m-d H:i:s', strtotime(date('H:i:s'), strtotime($create_date)));

                $errors = array();
                // Validate the input

                if ((strlen($particulars) == 0) || (strlen($particulars) > 255))
                    array_push($errors, "Please enter particulars");
                if ((strlen($amount) == 0))
                    array_push($errors, "Please enter amount");

            if (count($errors) == 0) {
                $q = BankTransactionSetup::AddBankTransactionSetup($data);

            if ($q) {
                $output = '';
                //header("Location: ?view&sucess"); ?>
                <script>window.location.href = '?view&sucess'</script>
            <?php
            echo "<div id='card-alert' class='card green lighten-5'>
                      <div class='card-content green-text'>
                        <p>SUCCESS : <strong>Attention! </strong>Submitted!</p>
                      </div>
                      <button type='button' class='close green-text' data-dismiss='alert' aria-label='Close'>
                        <span aria-hidden='true'>x</span>
                      </button>
                    </div>";
            } else {
                $output = '';
                echo "<div id='card-alert' class='card orange lighten-5'>
                      <div class='card-content orange-text'>
                        <p>WARNING : <strong>Attention! </strong>Some error occurred!</p>
                      </div>
                      <button type='button' class='close orange-text' data-dismiss='alert' aria-label='Close'>
                        <span aria-hidden='true'>x</span>
                      </button>
                    </div>";
            }
            } else {
                //Prepare errors for output
                foreach ($errors as $val) {
                    $output .= "<div id='card-alert' class='card orange lighten-5'>
                      <div class='card-content orange-text'>
                        <p>WARNING : <strong>Attention! </strong>$val</p>
                      </div>
                      <button type='button' class='close orange-text' data-dismiss='alert' aria-label='Close'>
                        <span aria-hidden='true'>x</span>
                      </button>
                    </div>";
                }
            }
            }
            ?>
                <div class="row">
                    <form action="" method="post" id="bank_setup_form" novalidate="novalidate">
                        <div class="col s12">
                            <?php echo $output; ?>
                            <div class="row">
                                <span>(*) Mark Fields are mandatory</span><br/><br/>
                            </div>
                            <div class="col m6 s12">
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" required type="text" id="datepicker"
                                               name="create_date"
                                               placeholder="<?= date('01-01-Y') ?>"
                                               value='<?= !empty($create_date) ? $create_date : $current_date; ?>'/><br/>
                                        <label for="invoice_date" class="control-label">Date *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <?php
                                        $bankfilter = " WHERE status = 'active' ORDER BY bank_name";
                                        $banks = StatementBankSetup::GetBankSetupList($bankfilter);
                                        ?>
                                        <select class="form-control" required id="statement_bank_id"
                                                name="statement_bank_id">
                                            <?php
                                            foreach ($banks AS $banksRow) {
                                                $selected = $banksRow->id == $statement_bank_id ? "selected" : "";
                                                echo '<option ' . $selected . ' value="' . $banksRow->id . '">' . $banksRow->bank_name . ' - ' . $banksRow->bank_account_number . '</option>';
                                            }
                                            ?>
                                        </select>
                                        <label for="customer_name" class="control-label">Bank Account *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input type="text" name="particulars" id="particulars" maxlength="250"
                                               class="form-control"
                                               value='<?php echo $particulars; ?>'>
                                        <label for="name" class="center-align">Particulars *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="amount"
                                               maxlength="30"
                                               name="amount"
                                               value='<?php echo $amount; ?>'/>
                                        <label for="bank_account_number" class="center-align">Amount *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div id="status" class="input-field col s12">
                                        <select class="form-control" name="type">
                                            <option value="debit" <?php if ($type == 'debit') {
                                                echo "selected='selected'";
                                            } ?>>Debit
                                            </option>
                                            <option value="credit" <?php if ($type == 'credit') {
                                                echo "selected='selected'";
                                            } ?>>Credit
                                            </option>
                                        </select>
                                        <label for="status" class="center-align">Type</label>
                                    </div>
                                    <br/>
                                </div>
                                <div class="row margin">
                                    <div id="status" class="input-field col s12">
                                        <select class="form-control" name="status">
                                            <option value="active" <?php if ($status == 'active') {
                                                echo "selected='selected'";
                                            } ?>>Active
                                            </option>
                                            <option value="cancel" <?php if ($status == 'cancel') {
                                                echo "selected='selected'";
                                            } ?>>Cancel
                                            </option>
                                        </select>
                                        <label for="status" class="center-align">Status</label>
                                    </div>
                                    <br/>
                                </div>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <div class="row margin">
                            <div class="input-field col s12">
                                <button id="btnsubmit" type="submit" class="btn waves-effect waves-light">Submit
                                </button>
                                <button type="button" class="btn waves-effect waves-light"
                                        onclick="location.href='bank_transaction.php?view'">Cancel
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
                <?php
            } // add close

            if (isset($_GET['view'])) {

                ?>
                <?php if (isset($_GET['sucess'])) { ?>
                    <div id='card-alert' class='card green lighten-5'>
                        <div class='card-content green-text'>
                            <p>Successfully updated!</p>
                        </div>
                        <button type='button' class='close green-text' data-dismiss='alert' aria-label='Close'>
                            <span aria-hidden='true'>x</span>
                        </button>
                    </div>
                <?php } ?>
                <?php if (isset($_GET['error'])) { ?>
                    <div id='card-alert' class='card orange lighten-5'>
                        <div class='card-content orange-text'>
                            <p>WARNING : <strong>Attention! </strong>Error occurred!</p>
                        </div>
                        <button type='button' class='close orange-text' data-dismiss='alert' aria-label='Close'>
                            <span aria-hidden='true'>x</span>
                        </button>
                    </div>
                <?php } ?>
                <div class="table-datatables">
                    <table id="data-table-simple" class="responsive-table display">
                        <thead>
                        <tr>
                            <th>Action</th>
                            <th>Bank Name</th>
                            <th>Particulars</th>
                            <th>Amount</th>
                            <th>Type</th>
                            <th>Status</th>
                            <th>Date</th>
                        </tr>
                        </thead>
                        <tfoot>
                        <tr>
                            <th>Action</th>
                            <th>Bank Name</th>
                            <th>Particulars</th>
                            <th>Amount</th>
                            <th>Type</th>
                            <th>Status</th>
                            <th>Date</th>
                        </tr>
                        </tfoot>
                        <tbody>
                        <?php
                        $qry = BankTransactionSetup::GetBankTransactionSetupList();
                        if ($qry) {
                            foreach ($qry as $qery) {
                                $bankStatement = StatementBankSetup::GetBankSetupById($qery->statement_bank_id);
                                ?>
                                <tr>
                                    <td>
                                        <?php
                                        if ($_SESSION['sess_role'] == 'VEC' || $_SESSION['sess_role'] == 'VE') {
                                            ?>
                                            <a href='?edit&id=<?php echo $qery->id; ?>&v' title="View">
                                                    <span
                                                        style="width: 20px; height: 20px; font-size: 12px; line-height: 20px;"
                                                        class="mini-stat-icon pink"><i
                                                            class="mdi-action-description"></i></span>
                                            </a>
                                            <a href="?edit&id=<?php echo $qery->id; ?>" title="Edit">
                                                    <span
                                                        style="width: 20px; height: 20px; font-size: 12px; line-height: 20px;"
                                                        class="mini-stat-icon orange"><i
                                                            class="mdi-image-edit"></i></span>
                                            </a>
                                        <?php } else { ?>
                                            <a href="?edit&id=<?php echo $qery->id; ?>&v" title="View">
                                                    <span
                                                        style="width: 20px; height: 20px; font-size: 12px; line-height: 20px;"
                                                        class="mini-stat-icon pink"><i
                                                            class="mdi-action-description"></i></span>
                                            </a>
                                        <?php } ?>
                                    </td>
                                    <td><?php echo ucwords($bankStatement->bank_name); ?></td>
                                    <td><?php echo $qery->particulars; ?></td>
                                    <td><?php echo $qery->amount; ?></td>
                                    <td><?= ($qery->type == 'debit') ? ucwords($qery->type) : "Credit"; ?></td>
                                    <td><?= ($qery->status == 'active') ? ucwords($qery->status) : "Inactive"; ?></td>
                                    <td><?php echo $qery->create_date; ?></td>
                                </tr>
                                <?php
                            }
                        } ?>
                        </tbody>
                    </table>
                </div>
                <?php

            } // close view


            if (isset($_GET['edit']) && !empty($_POST)) {
                extract($_REQUEST);
                $id = $_GET['id'];
                $data['particulars'] = $particulars;
                $data['statement_bank_id'] = $statement_bank_id;
                $data['type'] = $type;
                $data['amount'] = $amount;
                $data['status'] = $status;
                $data['create_date'] = date('Y-m-d H:i:s', strtotime(date('H:i:s'), strtotime($create_date)));

                $errors = array();
                // Validate the input

                if ((strlen($particulars) == 0) || (strlen($particulars) > 255))
                    array_push($errors, "Please enter particulars");
                if ((strlen($amount) == 0))
                    array_push($errors, "Please enter amount");

                if (count($errors) == 0) {
                    $q = BankTransactionSetup::UpdateBankTransactionSetup($id, $data);

                    if ($q) {
                        $output = '';
                        //header("Location: ?view&sucess"); ?>
                        <script>window.location.href = '?view&sucess'</script>
                        <?php
                        echo "<div id='card-alert' class='card green lighten-5'>
                      <div class='card-content green-text'>
                        <p>SUCCESS : <strong>Attention! </strong>Submitted!</p>
                      </div>
                      <button type='button' class='close green-text' data-dismiss='alert' aria-label='Close'>
                        <span aria-hidden='true'>x</span>
                      </button>
                    </div>";
                    } else {
                        $output = '';
                        echo "<div id='card-alert' class='card orange lighten-5'>
                      <div class='card-content orange-text'>
                        <p>WARNING : <strong>Attention! </strong>Some error occurred!</p>
                      </div>
                      <button type='button' class='close orange-text' data-dismiss='alert' aria-label='Close'>
                        <span aria-hidden='true'>x</span>
                      </button>
                    </div>";
                    }
                } else {
                    //Prepare errors for output
                    foreach ($errors as $val) {
                        $output .= "<div id='card-alert' class='card orange lighten-5'>
                      <div class='card-content orange-text'>
                        <p>WARNING : <strong>Attention! </strong>$val</p>
                      </div>
                      <button type='button' class='close orange-text' data-dismiss='alert' aria-label='Close'>
                        <span aria-hidden='true'>x</span>
                      </button>
                    </div>";
                    }
                }
            }
            if (isset($_GET['edit'])) {
            $id = $_GET['id'];
            if (empty($_POST)) {
                $qry = BankTransactionSetup::GetBankTransactionSetupById($id);
                $particulars = $qry->particulars;
                $statement_bank_id = $qry->statement_bank_id;
                $amount = $qry->amount;
                $type = $qry->type;
                $status = $qry->status;
                $bankStatement = StatementBankSetup::GetBankSetupById($statement_bank_id);
            }
            if ($_REQUEST['id'] && isset($_REQUEST['v'])) {
                ?>
                <div class="row">
                    <!--  The form that will be parsed by jQuery before submit  -->
                    <div class="col s12">
                        <div class="col s8">
                            <div class="row">
                                <div class="col s6">Bank Name:</div>
                                <span class="col s6"><?php echo $bankStatement->bank_name; ?></span>
                            </div>
                            <div class="row">
                                <div class="col s6">Particulars:</div>
                                <span class="col s6"><?php echo $particulars; ?></span>
                            </div>
                            <div class="row">
                                <div class="col s6">Amount:</div>
                                <span class="col s6"><?php echo $amount; ?></span>
                            </div>
                            <div class="row">
                                <div class="col s6">Type</div>

                                <span class="col s6">
                                    <?php
                                    if ($type == 'debit') {
                                        echo "Debit";
                                    } else if ($type == 'credit') {
                                        echo "Credit";
                                    } ?>
                                </span>
                            </div>
                            <div class="row">
                                <div class="col s6">Status</div>

                                <span class="col s6">
                                    <?php
                                    if ($status == 'active') {
                                        echo "Active";
                                    } else if ($status == 'cancel') {
                                        echo "Cancel";
                                    } ?>
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
            <?php } else {
                ?>
                <div class="row">
                    <!--  The form that will be parsed by jQuery before submit  -->
                    <form action="?edit&id=<?php echo $id ?>" method="post" id="bank_transaction"
                          name="bank_transaction"
                          novalidate="novalidate">
                        <div class="col s12">
                            <div class="col s8">
                                <div class="row">
                                    <?php echo $output; ?>
                                </div>
                                <div class="row margin">
                                    <span>(*) Mark Fields are mandatory</span><br/><br/>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" required type="text" id="datepicker"
                                               name="create_date"
                                               placeholder="<?= date('01-01-Y') ?>"
                                               value='<?= !empty($create_date) ? $create_date : $current_date; ?>'/><br/>
                                        <label for="invoice_date" class="control-label">Date *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <?php
                                        $bankfilter = " WHERE status = 'active' ORDER BY bank_name";
                                        $banks = StatementBankSetup::GetBankSetupList($bankfilter);
                                        ?>
                                        <select class="form-control" required id="statement_bank_id"
                                                name="statement_bank_id">
                                            <?php
                                            foreach ($banks AS $banksRow) {
                                                $selected = $banksRow->id == $statement_bank_id ? "selected" : "";
                                                echo '<option ' . $selected . ' value="' . $banksRow->id . '">' . $banksRow->bank_name . ' - ' . $banksRow->bank_account_number . '</option>';
                                            }
                                            ?>
                                        </select>
                                        <label for="customer_name" class="control-label">Bank Account *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="particulars" maxlength="250"
                                               name="particulars"
                                               value="<?php echo $particulars; ?>"/>
                                        <label for="particulars" class="control-label">Particulars *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="amount"
                                               maxlength="30"
                                               name="amount"
                                               value="<?php echo $amount; ?>"/>
                                        <label for="amount" class="control-label">Amount
                                            *</label><br/>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <label class="control-label">Type</label><br>
                                        <select class="form-control" name="type">
                                            <option value="debit" <?php if ($type == 'debit') {
                                                echo "selected='selected'";
                                            } ?>>Debit
                                            </option>
                                            <option value="credit" <?php if ($type == 'credit') {
                                                echo "selected='selected'";
                                            } ?>>Credit
                                            </option>
                                        </select>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div id="status" class="input-field col s12">
                                        <label class="control-label">Status</label><br>
                                        <select class="form-control" name="status">
                                            <option value="active" <?php if ($status == 'active') {
                                                echo "selected='selected'";
                                            } ?>>Active
                                            </option>
                                            <option value="cancel" <?php if ($status == 'cancel') {
                                                echo "selected='selected'";
                                            } ?>>Cancel
                                            </option>
                                        </select>
                                    </div>
                                    <br/>
                                </div>
                                <div class="clearfix"></div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <?php if ($_SESSION['sess_role'] == 'VE' || $_SESSION['sess_role'] == 'VEC') { ?>
                                            <button id="btnsubmit" type="submit" class="btn waves-effect waves-light">
                                                Submit
                                            </button>
                                        <?php } ?>
                                        <button type="button" class="btn waves-effect waves-light"
                                                onclick="location.href='bank_transaction.php?view'">
                                            Cancel
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            <?php } ?>
        </div>
        <?php
        } ?>
    </div>
</div>
</div>
</div>
</div>
<?php include('../includes/inc_footer.php'); ?>

<?php include('../includes/inc_js.php'); ?>
<script type="application/javascript">
    $(document).ready(function () {
        var fewSeconds = 2;
        $('#btnsubmit').click(function () {
            // Ajax request
            var btn = $(this);
            btn.hide();
            setTimeout(function () {
                btn.show();
            }, fewSeconds * 1000);
        });
    });
</script>
<script type="text/javascript">
    /*Show entries on click hide*/
    $(document).ready(function () {
        $(".dropdown-content.select-dropdown li").on("click", function () {
            var that = this;
            setTimeout(function () {
                if ($(that).parent().hasClass('active')) {
                    $(that).parent().removeClass('active');
                    $(that).parent().hide();
                }
            }, 100);
        });
    });
</script>
</body>
</html>