<?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();
}
extract($_REQUEST);

if (isset($_GET['delete']) && is_numeric($_GET['delete'])) {
    WhtPaymentTax::DeleteWhtPaymentTax($_GET['delete']);
    header("Location: " . PROJECT_ADMIN_URL . "whttax_payment_setup.php?view&success");
    exit();
}

if (empty($datefrom)) {
    $datefrom = date('Y-m-d', strtotime("-30 days"));
} else {
    $datefrom = date("Y-m-d", strtotime($datefrom));
}
if (empty($dateto)) {
    $dateto = date('Y-m-d');
} else {
    $dateto = date("Y-m-d", strtotime($dateto));
}

$qryDate = $datefrom;
$qryTo = $dateto;

$filter = " WHERE DATE_FORMAT(payment_date,'%Y-%m-%d') >= '$qryDate' AND DATE_FORMAT(payment_date,'%Y-%m-%d') <= '$qryTo' ORDER BY payment_date DESC, id DESC ";

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('includes/inc_head.php'); ?>
    <style> table { font-size: 16px; } </style>
</head>
<body>
<?php include('includes/inc_top.php'); ?>
<div class="row">
    <div class="col s12 m12">
        <h3>WHT Payment Expense</h3>
        <hr>

        <?php if (isset($_GET['view'])): ?>
            <form method="get" class="row">
                <input type="hidden" name="view" value="1">
                <div class="col m3">
                    <label>Date From</label>
                    <input type="text" name="datefrom" id="datepicker1" value="<?= date('d-m-Y', strtotime($datefrom)) ?>" />
                </div>
                <div class="col m3">
                    <label>Date To</label>
                    <input type="text" name="dateto" id="datepicker2" value="<?= date('d-m-Y', strtotime($dateto)) ?>" />
                </div>
                <div class="col m3">
                    <label>&nbsp;</label><br>
                    <button type="submit" class="btn">Search</button>
                </div>
                <div class="col m3">
                    <label>&nbsp;</label><br>
                    <a href="whttax_payment_expense_create.php?add" class="btn green right">Add WHT Payment</a>
                </div>
            </form>
        <?php endif; ?>

        <?php if (isset($_GET['success'])) { ?>
    <div id='card-alert' class='card green lighten-5'>
        <div class='card-content green-text'>
            <p>Record saved/updated successfully.</p>
        </div>
        <button type='button' class='close green-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>Payment No</th>
                    <th>Payment Date</th>
                    <th>Supplier</th>
                    <th>Payment Ref</th>
                    <th>Payment Type</th>
                    <th>Expense Amount</th>
                    <th>WHT Amount</th>
                    <th>Bank Name</th>
                    <th>Account No</th>
                    <th>Cheque No</th>
                    <th>Cheque Date</th>
                    <th>PSID</th>
                    <th>CPR</th>
                    <th>Description</th>
                    <th>Created By</th>
                    <th>Create Date</th>
                </tr>
                </thead>
                <tbody>
                <?php
                $records = WhtPaymentTax::GetWhtPaymentTaxList($filter);
                if ($records) {
                    foreach ($records as $row) {
                        $supplier = $row->supplier_id ? SupplierSetup::GetSupplierSetupById($row->supplier_id) : null;
                        $bank = $row->bank_id ? BankSetup::GetBankSetupById($row->bank_id) : null;
                        ?>
                        <tr>
                            <td>
                            <!--    <a href="whttax_payment_expense_create.php?edit&id=<?= $row->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 href="?view&delete=<?= $row->id ?>" title="Delete" onclick="return confirm('Delete this record?');">
                                    <span style="width: 20px; height: 20px; font-size: 12px; line-height: 20px;"
                                          class="mini-stat-icon red">
                                        <i class="mdi-action-delete"></i>
                                    </span>
                                </a>
                            </td>
                            <td><?= $row->payment_no ?></td>
                            <td data-order="<?= $row->payment_date ?>"><?= date('d-m-Y', strtotime($row->payment_date)) ?></td>
                            <td><?= $supplier ? $supplier->name : '' ?></td>
                            <td><?= $row->payment_ref ?></td>
                            <td><?= ucfirst($row->payment_type) ?></td>
                            <td><?= number_format($row->expense_amount, 2) ?></td>
                            <td><?= number_format($row->wht_amount, 2) ?></td>
                            <td><?= $bank ? $bank->bank_name : '' ?></td>
                            <td><?= $row->account_no ?></td>
                            <td><?= $row->chq_no ?></td>
                            <td><?= $row->chq_date && $row->chq_date !== '0000-00-00' ? date('d-m-Y', strtotime($row->chq_date)) : '' ?></td>
                            <td><?= $row->psid ?></td>
                            <td><?= $row->cpr ?></td>
                            <td><?= $row->description ?></td>
                            <td><?= $row->created_by ?></td>
                            <td><?= date('d-m-Y H:i:s', strtotime($row->create_date)) ?></td>
                        </tr>
                        <?php
                    }
                } else {
                    echo "<tr><td colspan='17'>No records found.</td></tr>";
                }
                ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php include('includes/inc_footer.php'); ?>
<?php include('includes/inc_js.php'); ?>
<script>
    $('#datepicker1, #datepicker2').pickadate({ format: 'dd-mm-yyyy' });
  
  $(document).ready(function () {
    setTimeout(function () {
        $('#card-alert').fadeOut('slow');
    }, 4000); // 4 seconds
});
</script>
</body>
</html>