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

if (!isset($_SESSION['sess_userid'])) { header("Location: " . PROJECT_ADMIN_URL); exit(); }

$output = '';
$paid_to_name = '';
$status = 'active';

// DELETE
if (isset($_GET['delete']) && ctype_digit($_GET['delete'])) {
    PaidToSetup::DeletePaidToSetup($_GET['delete']);
    header("Location: paid_to_setup.php?view&sucess");
    exit();
}

// ADD (POST)
if (isset($_GET['add']) && !empty($_POST)) {
    extract($_REQUEST);

    $paid_to_name = trim($paid_to_name);
    $status = ($status === 'inactive') ? 'inactive' : 'active';

    $errors = [];
    if ($paid_to_name === '') { $errors[] = "Please enter a name for 'Paid To'."; }
    if (PaidToSetup::CheckPaidToName($paid_to_name, 0)) { $errors[] = "This 'Paid To' name already exists."; }

    if (count($errors) === 0) {
        $data = [
            'paid_to_name' => $paid_to_name,
            'status'       => $status,
            'datetime'     => date('Y-m-d H:i:s')
        ];
        $id = PaidToSetup::AddPaidToSetup($data);
        if ($id) {
            header("Location: paid_to_setup.php?view&sucess");
            exit();
        } else {
            $output .= "<div id='card-alert' class='card orange lighten-5'><div class='card-content orange-text'><p>WARNING: Error occurred while saving.</p></div></div>";
        }
    } else {
        foreach ($errors as $val) {
            $output .= "<div id='card-alert' class='card orange lighten-5'><div class='card-content orange-text'><p>WARNING: $val</p></div></div>";
        }
    }
}

// EDIT (POST)
if (isset($_GET['edit']) && ctype_digit($_GET['id'] ?? '') && !empty($_POST)) {
    $id = (int)$_GET['id'];
    extract($_REQUEST);

    $paid_to_name = trim($paid_to_name);
    $status = ($status === 'inactive') ? 'inactive' : 'active';

    $errors = [];
    if ($paid_to_name === '') { $errors[] = "Please enter a name for 'Paid To'."; }
    if (PaidToSetup::CheckPaidToName($paid_to_name, $id)) { $errors[] = "This 'Paid To' name already exists."; }

    if (count($errors) === 0) {
        $data = [
            'paid_to_name' => $paid_to_name,
            'status'       => $status
        ];
        PaidToSetup::UpdatePaidToSetup($id, $data);
        header("Location: paid_to_setup.php?view&sucess");
        exit();
    } else {
        foreach ($errors as $val) {
            $output .= "<div id='card-alert' class='card orange lighten-5'><div class='card-content orange-text'><p>WARNING: $val</p></div></div>";
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('includes/inc_head.php'); ?>
</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>Paid To Setup</h3>
    </div>
    <hr>

    <?php
    // ADD FORM (GET)
    if (isset($_GET['add'])) { ?>
        <div class="row">
            <form action="?add" method="post" id="paid_to_setup_form" novalidate="novalidate">
                <div class="col s12 form-group">
                    <div class="col s8">
                        <?php echo $output; ?>
                        <div class="row"><span>(*) Marked fields are mandatory</span><br/><br/></div>

                        <div class="row margin">
                            <div class="input-field col s12">
                                <input type="text" id="paid_to_name" name="paid_to_name" value="<?php echo htmlspecialchars($paid_to_name); ?>" required>
                                <label for="paid_to_name" class="active">Paid To Name *</label>
                            </div>
                        </div>

                        <div class="row margin">
                            <div class="input-field col s12">
                                <select name="status" id="status">
                                    <option value="active"  <?php echo ($status === 'active' ? 'selected' : ''); ?>>active</option>
                                    <option value="inactive"<?php echo ($status === 'inactive' ? 'selected' : ''); ?>>inactive</option>
                                </select>
                                <label for="status">Status</label>
                            </div>
                        </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='paid_to_setup.php?view'">Cancel</button>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    <?php
    // EDIT/VIEW BLOCK
    } elseif (isset($_GET['edit']) && ctype_digit($_GET['id'] ?? '')) {

        $id = (int)$_GET['id'];
        $row = PaidToSetup::GetPaidToSetupById($id);

        if ($row) {
            if (empty($_POST)) {
                $paid_to_name = $row->paid_to_name;
                $status       = $row->status;
            }

            // VIEW MODE
            if (isset($_GET['v'])) { ?>
                <div class="row">
                    <div class="col s12 form-group">
                        <div class="col s8">
                            <div class="row"><div class="col s6">Paid To Name:</div><div class="col s6"><?php echo htmlspecialchars($row->paid_to_name); ?></div></div>
                            <div class="row"><div class="col s6">Status:</div><div class="col s6"><?php echo htmlspecialchars($row->status); ?></div></div>
                            <div class="row"><div class="col s6">Created:</div><div class="col s6"><?php echo htmlspecialchars($row->datetime); ?></div></div>
                        </div>
                    </div>
                </div>
            <?php
            // EDIT FORM
            } else { ?>
                <div class="row">
                    <form action="?edit&id=<?php echo $id; ?>" method="post" id="paid_to_setup_form" novalidate="novalidate">
                        <div class="col s12 form-group">
                            <div class="col s8">
                                <?php echo $output; ?>
                                <div class="row"><span>(*) Marked fields are mandatory</span><br/><br/></div>

                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input type="text" id="paid_to_name" name="paid_to_name" value="<?php echo htmlspecialchars($paid_to_name); ?>" required>
                                        <label for="paid_to_name" class="active">Paid To Name *</label>
                                    </div>
                                </div>

                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <select name="status" id="status">
                                            <option value="active"  <?php echo ($status === 'active' ? 'selected' : ''); ?>>active</option>
                                            <option value="inactive"<?php echo ($status === 'inactive' ? 'selected' : ''); ?>>inactive</option>
                                        </select>
                                        <label for="status">Status</label>
                                    </div>
                                </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='paid_to_setup.php?view'">Cancel</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            <?php } // end edit form/view
        }
    // LIST
    } else { ?>
        <?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></div>
        <?php } ?>
        <?php if (isset($_GET['error'])) { ?>
            <div id='card-alert' class='card orange lighten-5'><div class='card-content orange-text'><p>WARNING: Error occurred!</p></div></div>
        <?php } ?>

        <?php if ($_SESSION['sess_role'] == 'VEC') { ?>
            <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/>

        <div class="table-datatables">
            <table id="data-table-simple" class="responsive-table display">
                <thead>
                    <tr>
                        <th>Action</th>
                        <th>Paid To</th>
                        <th>Status</th>
                        <th>Created</th>
                    </tr>
                </thead>
                <tfoot>
                    <tr>
                        <th>Action</th>
                        <th>Paid To</th>
                        <th>Status</th>
                        <th>Created</th>
                    </tr>
                </tfoot>
                <tbody>
                    <?php
                    $filter = " ORDER BY paid_to_name";
                    $rows = PaidToSetup::GetPaidToSetupList($filter);
                    if ($rows) {
                        foreach ($rows as $r) { ?>
                            <tr>
                                <td>
                                    <?php if ($_SESSION['sess_role'] == 'VEC' || $_SESSION['sess_role'] == 'VE') { ?>
                                        <a href='?edit&id=<?php echo $r->id; ?>&v' title="View">
                                            <span class="mini-stat-icon pink" style="width:20px;height:20px;font-size:12px;line-height:20px;"><i class="mdi-action-description"></i></span>
                                        </a>
                                        <a href='?edit&id=<?php echo $r->id; ?>' title="Edit">
                                            <span class="mini-stat-icon orange" style="width:20px;height:20px;font-size:12px;line-height:20px;"><i class="mdi-image-edit"></i></span>
                                        </a>
                                        <a style="cursor:pointer;font-size:10px;padding:1px;" title="Delete">
                                            <span class="btn-warning-confirm<?php echo $r->id; ?> mini-stat-icon deep-orange darken-2" style="width:60px;height:20px;border-radius:5%;font-size:12px;line-height:20px;">Delete</span>
                                        </a>
                                        <script>
                                            $('.btn-warning-confirm<?php echo $r->id; ?>').click(function () {
                                                swal({
                                                    title: "Are you sure?",
                                                    text: "Are you sure you want to delete?",
                                                    type: "warning",
                                                    showCancelButton: true,
                                                    confirmButtonColor: '#DD6B55',
                                                    confirmButtonText: 'Yes',
                                                    closeOnConfirm: false
                                                }, function () {
                                                    swal("Successful!", "Deleted successfully", "success");
                                                    window.location.href = 'paid_to_setup.php?delete=<?php echo $r->id; ?>';
                                                });
                                            });
                                        </script>
                                    <?php } else { ?>
                                        <a href='?edit&id=<?php echo $r->id; ?>&v' title="View">
                                            <span class="mini-stat-icon pink" style="width:20px;height:20px;font-size:12px;line-height:20px;"><i class="mdi-action-description"></i></span>
                                        </a>
                                    <?php } ?>
                                </td>
                                <td><?php echo htmlspecialchars($r->paid_to_name); ?></td>
                                <td><?php echo htmlspecialchars($r->status); ?></td>
                                <td><?php echo htmlspecialchars($r->datetime); ?></td>
                            </tr>
                        <?php }
                    } ?>
                </tbody>
            </table>
        </div>
    <?php } // end list ?>
  </div>
</div>
</div>

<?php include('includes/inc_footer.php'); ?>
<?php include('includes/inc_js.php'); ?>
<script>
(function initM(){
  function go(){
    if (window.M && M.FormSelect) { try { M.FormSelect.init(document.querySelectorAll('select')); } catch(e) {} }
  }
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', go);
  } else { go(); }
})();
</script>
</body>
</html>
