<?php
include("../../classes/manager.class.php");
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
$m = new Manager();
$output = '';
$amount = '';
$customer_id = "";
$type = "";
if (!isset($_SESSION['sess_userid'])) {
    header("Location: " . PROJECT_ADMIN_URL);
    exit();
}
$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,
                selectYears: true,
            });
        });
    </script>
    <script>
        $(function () {
            $('#datepicker').pickadate({
                format: 'dd-mm-yyyy',
                selectMonths: true,
                selectYears: true,
            });
            $('#cheque_date').pickadate({
                format: 'dd-mm-yyyy',
                selectMonths: true,
                selectYears: 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>Return</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>
                    <div class="clearfix"></div>
                <?php }
            } ?>
            <br/>
            <?php
            if (isset($_GET['add']) || isset($_GET['edit'])) {
                if (isset($_GET['edit'])) {
                    extract($_REQUEST);
                    $getRefund = Refund::GetRefundSetupById($id);
                    $customer_id = $getRefund->customer_id;
                    $invoice_reference_code = $getRefund->invoice_reference_code;
                    $type = $getRefund->type;
                    $amount = $getRefund->amount;
                    $description = $getRefund->description;
                }
                if (isset($_GET['add']) && isset($_GET['ref'])) {
                    $selectedInvoice = Invoice::GetInvoiceSetupById($_GET['ref']);
                    $amount = $selectedInvoice->total_amount;
                    $invoice_reference_code = $selectedInvoice->id;
                    $customer_id = $selectedInvoice->customer_id;
                    $refund_date = date('d-m-Y');
                }
            }
            if (isset($_POST) && !empty($_POST)) {
                extract($_REQUEST);
                $data['customer_id'] = $customer_name;
                $data['invoice_reference_code'] = $invoice_reference_code;
                $data['type'] = $type;
                $data['amount'] = $amount;
                $data['status'] = 'active';
                $data['create_date'] = date('Y-m-d H:i:s', strtotime(date('H:i:s'), strtotime($refund_date)));
                $data['datetime'] = 'NOW()';
                $data['description'] = $description;
                $errors = array();


                if ((strlen($customer_name) == 0) || (strlen($customer_name) > 255))
                    array_push($errors, "Please enter customer name");
                if ((strlen($refund_date) == 0))
                    array_push($errors, "Please enter return date");
                if ((strlen($type) == 0))
                    array_push($errors, "Please enter return type");
                if ((strlen($amount) == 0 || $amount == 0))
                    array_push($errors, "Please enter return amount");

                if (count($errors) == 0) {
                    if (isset($_GET['add']) && isset($_GET['ref'])) {
                        $q = Refund::AddRefund($data);
                    } else {
                        $q = Refund::UpdateRefundSetup($id, $data);
                    }
                    if ($q) {
                        $output = '';
                        //header("Location: payment.php?view&sucess"); ?>
                        <script>window.location.href = 'refund.php?view&sucess&ref=<?=$_GET['ref']?>'</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>";
                    }
                }
            }
            ?>
            <form action="" method="post" id="payment_create_form" novalidate="novalidate">
                <div class="row">
                    <div class="col s12 form-group">
                        <?php echo $output; ?>
                        <div class="row">
                            <div class="col m6 s12">
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <?php
                                        $customersfilter = " WHERE status = 'active' ORDER BY name";
                                        $customers = CustomerSetup::GetCustomerSetupList($customersfilter);
                                        ?>
                                        <select class="form-control" required id="customer_name"
                                                name="customer_name">
                                            <?php
                                            if ($customers) {
                                                foreach ($customers AS $customersRow) {
                                                    if ($customersRow->id == $customer_id) {
                                                        echo '<option selected value="' . $customersRow->id . '" >' . $customersRow->name . '</option>';
                                                    }
                                                }
                                            }
                                            ?>
                                        </select>
                                        <label for="customer_name" class="control-label">Customer *</label>
                                    </div>
                                </div>
                            </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="refund_date"
                                               placeholder="<?= date('01-01-Y') ?>"
                                               value='<?= !empty($refund_date) ? $refund_date : $current_date; ?>'/>
                                        <label for="refund_date" class="control-label">Return Date *</label>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <div class="row">
                            <div class="col m6 s12">
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <select class="form-control" id="type" required
                                                name="type">
                                            <option></option>
                                            <option <?= $type == 'damage' ? 'selected' : '' ?>
                                                value="damage">Damage
                                            </option>
                                            <option <?= $type == 'bilti' ? 'selected' : '' ?>
                                                value="bilti">Bilti
                                            </option>
                                            <option <?= $type == 'other' ? 'selected' : '' ?>
                                                value="other">Other
                                            </option>
                                        </select>
                                        <label for="type" class="control-label">Type *</label>
                                    </div>
                                </div>
                            </div>
                            <div class="col m6 s12">
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" placeholder="00000.00"
                                               onkeypress="return isDoubleNumberKey(event)" required type="text"
                                               id="amount"
                                               value="<?= $amount ?>"
                                               name="amount"/>
                                        <label for="amount" class="control-label">Amount *</label>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col m6 s12">
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <?php
                                        if (isset($_GET)) {
                                            $filterInvoices = " WHERE customer_id = '$customer_id'";
                                        }
                                        $invoices = Invoice::GetInvoiceList($filterInvoices);
                                        ?>
                                        <select class="form-control" id="invoice_reference_code"
                                                name="invoice_reference_code">
                                            <?php
                                            //echo $invoice_reference_code;
                                            if ($invoices) {
                                                foreach ($invoices AS $invoicesRow) {
                                                    if ($invoicesRow->id == $invoice_reference_code) {
                                                        echo '<option selected value="' . $invoicesRow->id . '" >' . $invoicesRow->code . '</option>';
                                                    }
                                                }
                                            }
                                            ?>
                                        </select>
                                        <label for="invoice_reference_code" class="control-label">Invoice Ref.</label>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <div class="col m12 col s12">
                            <div class="row margin">
                                <div class="input-field col s12">
                                    <input class="form-control" type="text" id="description"
                                           name="description" maxlength="100"
                                           value='<?php echo $description; ?>'/><br/>
                                    <label for="description" class="control-label">Description</label>
                                </div>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <br><br>
                    </div>
                </div>

                <div class="clearfix"></div>
                <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='refund.php?view&ref=<?=$_GET['ref']?>'">Cancel
                </button>
            </form>
        </div>
    </div>
</div>
</div>
<?php include('../includes/inc_footer.php'); ?>

<?php include('../includes/inc_js.php'); ?>
<script type="application/javascript">

    // initialize
    $('#invoice_reference_code').material_select();

    // setup listener for custom event to re-initialize on change
    $('#invoice_reference_code').on('contentChanged', function () {
        $(this).material_select();
    });

    $('#customer_name').on('change', function () {
        $(this).material_select();
        $('body.container').block({message: 'Loading...'});
        $.get('getFilterInvoices.php?id=' + $(this).val(), function (data) {
            $("#invoice_reference_code").empty();
            $("#invoice_reference_code").append(data);
            $('body.container').unblock();
            $('#invoice_reference_code').trigger('contentChanged');
        });
    });
    function isDoubleNumberKey(evt) {
        var charCode = (evt.which) ? evt.which : evt.keyCode;
        if (charCode != 46 && charCode > 31
            && (charCode < 48 || charCode > 57))
            return false;

        return true;
    }
    $(document).ready(function () {
        var fewSeconds = 2;
        $('#btnsubmit').click(function () {
            // Ajax request
            var btn = $(this);
            btn.hide();
            setTimeout(function () {
                btn.show();
            }, fewSeconds * 1000);
        });
    });
</script>
</body>
</html>