<?php
include("../classes/manager.class.php");
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
$m = new Manager();
$output = '';
$name = '';
$description = '';
$amount = '';
$installment = '';
$start_date = '';
$status = '';
if (!isset($_SESSION['sess_userid'])) {
    header("Location: " . PROJECT_ADMIN_URL);
    exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('includes/inc_head.php'); ?>
    <style>
        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>Loan Setup</h3>
                <script>
                    $(function () {
                        $('#datepicker').pickadate({
                            format: 'dd-mm-yyyy',
                            selectMonths: true, // Creates a dropdown to control month
                            selectYears: true,
                        });
                    });
                </script>
            </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'])) {
            if (isset($_POST) && !empty($_POST)) {
                extract($_REQUEST);
                $data['name'] = $name;
                $data['employee_id'] = $employee_id;
                $data['description'] = $description;
                $data['amount'] = $amount;
                $data['installment'] = $installment;
                $data['start_date'] = date('Y-m-d H:i:s', strtotime($start_date));
                $data['create_date'] = 'NOW()';
                $data['status'] = $status;
                $checkloan = LoanSetup::CheckLoan(0, $employee_id);

                $errors = array();
                // Validate the input

                if ((strlen($name) == 0) || (strlen($name) > 255))
                    array_push($errors, "Please enter loan title");
                if ((strlen($employee_id) == 0))
                    array_push($errors, "Please select employee");
                if ((strlen($description) == 0))
                    array_push($errors, "Please enter some shot description");
                if ((strlen($amount) == 0))
                    array_push($errors, "Please enter loan request amount");
                if ((strlen($installment) == 0))
                    array_push($errors, "Please enter install pay back period");
                if ((strlen($start_date) == 0))
                    array_push($errors, "Please enter start date");
                if ($checkloan)
                    array_push($errors, "Loan already exist");

            if (count($errors) == 0) {
                $q = LoanSetup::AddLoan($data);
            if ($q) {
                $output = '';
                //header("Location: ?view&sucess"); ?>
                <script>window.location.href = '?view&sucess'</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>";
                }
            }
            }
            ?>
                <div class="row">
                    <form action="" method="post" id="loan_form" novalidate="novalidate">
                        <div class="col s12 form-group">
                            <?php echo $output; ?>
                            <div class="col s8">
                                <div class="row">
                                    <span>(*) Mark Fields are mandatory</span><br/><br/>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="name" maxlength="250"
                                               name="name"
                                               value='<?php echo $name; ?>'/>
                                        <label for="name`" class="control-label">Title Name *(Purpose)</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <?php
										$filterEmployee = " ORDER BY name";
                                        $qryEmployee = EmployeeSetup::GetEmployeeSetupList($filterEmployee);
                                        if ($qryEmployee) {
                                            echo '<select id="employee_id" name="employee_id" class="form-control">';
                                            foreach ($qryEmployee as $qery) {
                                                echo '<option value=' . $qery->id . '>' . $qery->name . '</option>';
                                            }
                                            echo '</select>';
                                        }
                                        ?>
                                        <label for="employee_id" class="control-label">Employee *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                                <textarea class="materialize-textarea" id="description"
                                                          name="description"><?php echo $description; ?></textarea>
                                        <label for="description" class="control-label">Description *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="amount" name="amount"
                                               value='<?php echo $amount; ?>'/>
                                        <label for="amount" class="control-label">Amount *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="installment" name="installment"
                                               maxlength="2"
                                               value='<?php echo $installment; ?>'/>
                                        <label for="installment" class="control-label">Installment *(Payback Period)</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input type="text" name="start_date" id="datepicker"
                                               value="<?= $start_date ?>" class="form-control"/>
                                        <label for="datepicker" class="control-label">Start Date *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div id="status" class="input-field col s12">
                                        <select class="form-control" name="status" id="statusss">
                                            <option value="active" <?php if ($status == 'active') {
                                                echo "selected='selected'";
                                            } ?>>Active
                                            </option>
                                            <option value="inactive" <?php if ($status == 'inactive') {
                                                echo "selected='selected'";
                                            } ?>>Inactive
                                            </option>
                                        </select>
                                        <label for="statusss" class="control-label">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='loan_setup.php?view'">Cancel
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                <?php
            } // add close

            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>Title Name</th>
                            <th>Employee Name</th>
                            <th>Amount</th>
                            <th>installment</th>
                            <th>Start Date</th>
                            <th>Status</th>
                            <th>Date</th>
                        </tr>
                        </thead>
                        <tfoot>
                        <tr>
                            <th>Action</th>
                            <th>Title Name</th>
                            <th>Employee Name</th>
                            <th>Amount</th>
                            <th>installment</th>
                            <th>Start Date</th>
                            <th>Status</th>
                            <th>Date</th>
                        </tr>
                        </tfoot>
                        <tbody>
                        <?php
                        $qry = LoanSetup::GetLoanList();
                        if ($qry) {
                            foreach ($qry as $qery) {
                                $empId = EmployeeSetup::GetEmployeeSetupById($qery->employee_id);
                                ?>
                                <tr>
                                    <td>
                                        <?php
                                        if ($_SESSION['sess_role'] == 'VEC' || $_SESSION['sess_role'] == 'VE') {
                                            ?>
                                            <a href='?edit&id=<?php echo $qery->id; ?>&v' title="View">
                                                    <span
                                                        style="width: 20px; height: 20px; font-size: 12px; line-height: 20px;"
                                                        class="mini-stat-icon pink"><i
                                                            class="mdi-action-description"></i></span>
                                            </a>
                                            <a href="?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>
                                        <?php } else { ?>
                                            <a href="?edit&id=<?php echo $qery->id; ?>&v" title="View">
                                                    <span
                                                        style="width: 20px; height: 20px; font-size: 12px; line-height: 20px;"
                                                        class="mini-stat-icon pink"><i
                                                            class="mdi-action-description"></i></span>
                                            </a>
                                        <?php } ?>
                                    </td>
                                    <td><?php echo ucwords($qery->name); ?></td>
                                    <td><?php echo ucwords($empId->name); ?></td>
                                    <td><?php echo ucwords($qery->amount); ?></td>
                                    <td><?php echo $qery->installment; ?></td>
                                    <td><?php echo date('Y-m-d', strtotime($qery->start_date)); ?></td>
                                    <td><?= ucwords($qery->status); ?></td>
                                    <td><?php echo $qery->create_date; ?></td>
                                </tr>
                                <?php
                            }
                        } ?>
                        </tbody>
                    </table>
                </div>
                <?php

            } // close view


            if (isset($_GET['edit']) && !empty($_POST)) {
                extract($_REQUEST);
                $id = $_GET['id'];

                $data['name'] = $name;
                $data['employee_id'] = $employee_id;
                $data['description'] = $description;
                $data['amount'] = $amount;
                $data['installment'] = $installment;
                $data['start_date'] = date('Y-m-d H:i:s', strtotime($start_date));
                $data['create_date'] = 'NOW()';
                $data['status'] = $status;
                $checkloan = LoanSetup::CheckLoan($id, $employee_id);

                $errors = array();
                // Validate the input

                if ((strlen($name) == 0) || (strlen($name) > 255))
                    array_push($errors, "Please enter loan title");
                if ((strlen($employee_id) == 0))
                    array_push($errors, "Please select employee");
                if ((strlen($description) == 0))
                    array_push($errors, "Please enter some shot description");
                if ((strlen($amount) == 0))
                    array_push($errors, "Please enter loan request amount");
                if ((strlen($installment) == 0))
                    array_push($errors, "Please enter install pay back period");
                if ((strlen($start_date) == 0))
                    array_push($errors, "Please enter start date");
                if ($checkloan)
                    array_push($errors, "Loan already exist");

                if (count($errors) == 0) {
                    $q = LoanSetup::UpdateLoan($id, $data);

                    if ($q) {
                        $output = '';
                        //header("Location: ?view&sucess"); ?>
                        <script>window.location.href = '?view&sucess'</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>";
                    }
                }
            }
            if (isset($_GET['edit'])) {
                $id = $_GET['id'];
                if (empty($_POST)) {

                    $qry = LoanSetup::GetLoanById($id);
                    $name = $qry->name;
                    $employee_id = $qry->employee_id;
                    $description = $qry->description;
                    $amount = $qry->amount;
                    $installment = $qry->installment;
                    $start_date = date('d-m-Y', strtotime($qry->start_date));
                    $status = $qry->status;

                    $employee = EmployeeSetup::GetEmployeeSetupById($employee_id);
                    $employeeName = $employee->name;
                }
                if ($_REQUEST['id'] && isset($_REQUEST['v'])) {
                    ?>
                    <div class="row">
                        <!--  The form that will be parsed by jQuery before submit  -->
                        <div class="col s12 form-group">
                            <div class="col s8">
                                <div class="row">
                                    <div class="col s6">Title Name:</div>
                                    <div class="col s6"><?php echo $name; ?></div>
                                </div>
                                <div class="row">
                                    <div class="col s6">Start Date:</div>
                                    <div class="col s6"><?php echo $start_date; ?></div>
                                </div>
                                <div class="row">
                                    <div class="col s6">Description:</div>
                                    <div class="col s6"><?php echo $description; ?></div>
                                </div>
                                <div class="row">
                                    <div class="col s6">Amount:</div>
                                    <div class="col s6"><?php echo $amount; ?></div>
                                </div>
                                <div class="row">
                                    <div class="col s6">Installment:</div>
                                    <div class="col s6"><?php echo $installment; ?></div>
                                </div>
                                <div class="row">
                                    <div class="col s6">Employee:</div>
                                    <div class="col s6"><?php echo $employeeName; ?></div>
                                </div>
                                <div class="row">
                                    <div class="col s6">Status</div>
                                    <div class="col s6">
                                        <?php
                                        if ($status == 'active') {
                                            echo "Active";
                                        } else if ($status == 'inactive') {
                                            echo "Inactive";
                                        } ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php } else {
                    ?>
                    <div class="row">
                        <!--  The form that will be parsed by jQuery before submit  -->
                        <form action="?edit&id=<?php echo $id ?>" method="post" id="loan_form"
                              novalidate="novalidate">
                            <div class="col s12 form-group">
                                <div class="col s8">
                                    <div class="row">
                                        <?php echo $output; ?>
                                    </div>
                                    <div class="row">
                                        <span>(*) Mark Fields are mandatory</span><br/><br/>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" type="text" id="name" maxlength="250"
                                                   name="name"
                                                   value='<?php echo $name; ?>'/>
                                            <label for="name" class="control-label">Title Name *(Purpose)</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <?php
                                            $qryEmployee = EmployeeSetup::GetEmployeeSetupList();
                                            if ($qryEmployee) {
                                                echo '<select id="employee_id" name="employee_id" class="form-control">';
                                                foreach ($qryEmployee as $qery) {
                                                    echo '<option value=' . $qery->id;
                                                    if ($qery->id == $employee_id) {
                                                        echo ' selected ';
                                                    }
                                                    echo '>' . $qery->name . '</option>';
                                                }
                                                echo '</select>';
                                            }
                                            ?>
                                            <label for="employee_id" class="control-label">Employee *</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                        <textarea class="materialize-textarea" id="description"
                                                  name="description"><?php echo $description; ?></textarea>
                                            <label for="description" class="control-label">Description *</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" type="text" id="amount" name="amount"
                                                   value='<?php echo $amount; ?>'/>
                                            <label for="amount" class="control-label">Amount *</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" type="text" id="installment" name="installment"
                                                   maxlength="2"
                                                   value='<?php echo $installment; ?>'/>
                                            <label for="installment" class="control-label">Installment *(Payback Period)</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input type="text" name="start_date" id="datepicker"
                                                   value="<?= $start_date ?>" class="form-control"/>
                                            <label for="datepicker" class="control-label">Start Date *</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div id="status" class="input-field col s12">
                                            <select class="form-control" name="status" id="statusss">
                                                <option value="active" <?php if ($status == 'active') {
                                                    echo "selected='selected'";
                                                } ?>>Active
                                                </option>
                                                <option value="inactive" <?php if ($status == 'inactive') {
                                                    echo "selected='selected'";
                                                } ?>>Inactive
                                                </option>
                                            </select>
                                            <label for="statusss" class="control-label">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='loan_setup.php?view'">
                                                Cancel
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                <?php }
            }
            ?>
        </div>
    </div>
</div>
</div>
<?php include('includes/inc_footer.php'); ?>
<?php include('includes/inc_js.php'); ?>
<script type="application/javascript">
    $(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>