<?php
include("../classes/manager.class.php");
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
$m = new Manager();
$output = '';
$datefrom = '';
$dateto = '';
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()';
    Purpayment::UpdatePurpaymentSetup($_GET['cancel'], $data);
    header("Location: " . PROJECT_ADMIN_URL . "purpayment.php?view");
    exit();
}
extract($_REQUEST);
if ($datefrom == "") {
    $datefrom = date('Y-m-d', strtotime("-30 days"));
} else {
    $datefrom = date("Y-m-d", strtotime($datefrom));
}
if ($dateto == "") {
    $dateto = date('Y-m-d');
} else {
    $dateto = date("Y-m-d", strtotime($dateto));
}
$datefrom = date("d-m-Y", strtotime($datefrom));
$dateto = date("d-m-Y", strtotime($dateto));

$qryDate = date("Y-m-d", strtotime($datefrom));
$qryTo = date("Y-m-d", strtotime($dateto));
$filter = " WHERE DATE_FORMAT(create_date,'%Y-%m-%d')>='$qryDate' and DATE_FORMAT(create_date,'%Y-%m-%d')<='$qryTo' AND status <> 'Cancel' ";
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('includes/inc_head.php'); ?>
    <style>
        .fa-arrows-alt:before {
            margin-right: 5px;
        }
        table{
            font-size:18px;
        }
    </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>Purchase Payments</h3>
            </div>
            <script>
                $(function () {
                    $('#datepicker').pickadate({
                        format: 'dd-mm-yyyy',
                        selectMonths: true, // Creates a dropdown to control month
                        selectYears: true, // Creates a dropdown to control year
                    });
                    $('#datepicker1').pickadate({
                        format: 'dd-mm-yyyy',
                        selectMonths: true, // Creates a dropdown to control month
                        selectYears: true, // Creates a dropdown to control year
                    });
                });
            </script>
            <form action="" method="get" name="search"
                  id="searchfrm">
                <table class="table">
                    <tr>
                        <td class="col l4">Date From<br>
                            <input type="text" name="datefrom" id="datepicker"
                                   placeholder="Date From"
                                   value="<?= $datefrom ?>" class="form-control"/>
                            <input type="hidden" name="view" id="view" class="form-control"/>
                        </td>
                        <td class="col l4"> Date To<br>
                            <input type="text" name="dateto" id="datepicker1"
                                   placeholder="Date To"
                                   value="<?= $dateto ?>" class="form-control"/>
                        </td>
                        <td class="col l4">&nbsp;<br>
                            <button type="submit" class="btn waves-effect waves-light"
                                    style="float:left;">
                                Search
                            </button>
                        </td>
                    </tr>
                </table>
            </form>
            <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="purpayment_create.php?add">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>Customer</th>
                            <th>Amount</th>
                            <th>Payment Type</th>
                            <th>Bank/Party</th>
                            <th>Account Number</th>
                        </tr>
                        </thead>
                        <tfoot>
                        <tr>
                            <th>Action</th>
                            <th>Date</th>
                            <th>Customer</th>
                            <th>Amount</th>
                            <th>Payment Type</th>
                            <th>Bank/Party</th>
                            <th>Account Number</th>
                        </tr>
                        </tfoot>
                        <tbody>
                        <?php
                        $qry = Purpayment::GetPurpaymentList($filter);
                        if ($qry) {
                            foreach ($qry as $qery) {
                                ?>
                                <tr>
                                    <td>
                                        <?php if ($_SESSION['sess_role'] == 'VEC' || $_SESSION['sess_role'] == 'VE') { ?>
                                            <a href="purpayment_create.php?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>
                                            <a style="cursor:pointer;font-size: 10px;padding: 1px;" data-toggle="modal"
                                               data-target="#myModalCancel<?= $qery->id ?>" 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 = 'purpayment.php?cancel=<?php echo $qery->id; ?>';
                                                        });
                                                });
                                            </script>
                                        <?php } ?>
                                    </td>
                                    <td><?php echo $qery->create_date; ?></td>
                                    <?php
                                    $supplier = PurchaseCategorySetup::GetPurchaseCategorySetupById($qery->supplier_id);
                                    $bank = BankSetup::GetBankSetupById($qery->bank_name);
                                    ?>
                                    <td><?php echo $supplier->name; ?></td>
                                    <td><?php echo $qery->amount; ?></td>
                                    <td><?php echo $qery->payment_type; ?></td>
                                    <td><?php
                                        if ($qery->payment_type == 'party') {
                                            echo 'Party: ' . $qery->party_name . " - " . $qery->cheque_number . "<br>";
                                            echo 'Cheque Details: ' . $qery->cheque_payee_name . " (" . $qery->cheque_bank_name . " - " . date('d-m-Y', strtotime($qery->cheque_date)) . ")";
                                        } else if ($qery->payment_type == 'online') {
                                            echo "From: " . $qery->from_bank_name . "<br>";
                                            echo 'To: ' . $bank->bank_name;
                                        } else {
                                            echo $bank->bank_name;
                                            if ($qery->payment_type == 'bank') {
                                                echo " - " . $qery->cheque_number . "<br>";
                                                echo 'Cheque Details: ' . $qery->cheque_payee_name . " (" . $qery->cheque_bank_name . " - " . date('d-m-Y', strtotime($qery->cheque_date)) . ")";
                                            }
                                        } ?></td>
                                    <td><?php echo $qery->bank_account_number; ?></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>