<?php
include("../../classes/manager.class.php");
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
$m = new Manager();
$output = '';
extract($_REQUEST);

if (!isset($_SESSION['sess_userid'])) {
    header("Location: " . PROJECT_ADMIN_URL);
    exit();
}
if (isset($_POST) && !empty($_GET['cancel'])) {
    //exit;
    $data['status'] = 'Cancel';
    $data['cancel_date'] = 'Now()';
    Refund::UpdateRefundSetup($_GET['cancel'], $data);
    header("Location: " . PROJECT_ADMIN_URL . "transaction/refund.php?view&ref=".$ref);
    exit();
}


$filter = " WHERE invoice_reference_code = '$ref' AND status <> 'Cancel' ";
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('../includes/inc_head.php'); ?>
    <style>
        .fa-arrows-alt:before {
            margin-right: 5px;
        }
    </style>
</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 Goods</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="refund_create.php?add&ref=<?=$ref; ?>">Add <i
                            class="mdi-content-add-box"></i></a>
                    <div class="clearfix"></div>
                <?php }
            } ?>
            <br/>
            <?php
            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>Date</th>
                            <th>Type</th>
                            <th>Invoice Ref.</th>
                            <th>Amount</th>
                        </tr>
                        </thead>
                        <tfoot>
                        <tr>
                            <th>Action</th>
                            <th>Date</th>
                            <th>Type</th>
                            <th>Invoice Ref.</th>
                            <th>Amount</th>
                        </tr>
                        </tfoot>
                        <tbody>
                        <?php
                        $qry = Refund::GetRefundList($filter);
                        if ($qry) {
                            foreach ($qry as $qery) {
                                $invoice = Invoice::GetInvoiceSetupById($qery->invoice_reference_code);
                                ?>
                                <tr>
                                    <td>
                                        <?php if ($_SESSION['sess_role'] == 'VEC' || $_SESSION['sess_role'] == 'VE') { ?>
                                            <a href="refund_create.php?edit&id=<?php echo $qery->id; ?>&ref=<?php echo $ref; ?>" title="Edit">
                                                                <span
                                                                    style="width: 20px; height: 20px; font-size: 12px; line-height: 20px;"
                                                                    class="mini-stat-icon orange darken-2">
                                                                    <i class="mdi-image-edit"></i>
                                                                </span>
                                            </a>
                                            <a style="cursor:pointer;font-size: 10px;padding: 1px;" title="Cancel">
                                                <span
                                                    style="width: 60px; height: 20px; border-radius: 5%; font-size: 12px; line-height: 20px;"
                                                    class="btn-warning-confirm<?= $qery->id ?> mini-stat-icon deep-orange darken-3"><i class="fa fa-arrows-alt">Cancel</i></span>
                                            </a>
                                            <script>
                                                $('.btn-warning-confirm<?= $qery->id ?>').click(function () {
                                                    swal({
                                                            title: "Are you sure?",
                                                            text: "Are you sure change status to cancel?",
                                                            type: "warning",
                                                            showCancelButton: true,
                                                            confirmButtonColor: '#DD6B55',
                                                            confirmButtonText: 'Yes',
                                                            closeOnConfirm: false
                                                        },
                                                        function () {
                                                            swal("Successful!", "Your status has been cancelled!", "success");
                                                            window.location.href = 'refund.php?cancel=<?php echo $qery->id; ?>&ref=<?php echo $ref; ?>';
                                                        });
                                                });
                                            </script>
                                        <?php } ?>
                                    </td>
                                    <td><?php echo $qery->create_date; ?></td>
                                    <td><?php echo $qery->type; ?></td>
                                    <td><?= (!empty($invoice->code) ? $invoice->code : ""); ?></td>
                                    <td><?php echo $qery->amount; ?></td>
                                </tr>
                                <?php
                            }
                        } ?>
                        </tbody>
                    </table>
                </div>
                <?php

            } // close view
            ?>
        </div>
    </div>
</div>
</div>
<?php include('../includes/inc_footer.php'); ?>

<?php include('../includes/inc_js.php'); ?>
</body>
</html>