<?php
include("../classes/manager.class.php");
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
$m = new Manager();
$output = '';
$name = '';
$casual_leave = '';
$annual_leave = '';
$shift = '';
$shift_cat = '';
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>Employee Category Setup</h3>
            </div>
            <hr>
            <script>
                $(function () {
                    $('#shift_start_time').pickatime({
                        default: 'now',
                        twelvehour: false, // change to 12 hour AM/PM clock from 24 hour
                        donetext: 'OK',
                        autoclose: false,
                        vibrate: true // vibrate the device when dragging clock hand
                    });
                });
            </script>
            <?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['casual_leave'] = $casual_leave;
                $data['annual_leave'] = $annual_leave;
                $data['shift'] = $shift;
                $data['shift_cat'] = $shift_cat;
                $data['shift_start_time'] = date('H:i', strtotime($shift_start_time));
                $data['late_grace_time'] = $late_grace_time;
                $data['late_deduction'] = $late_deduction == 'on' ? 1 : 0;
                $data['by_pass'] = $by_pass == 'on' ? 1 : 0;
                $data['late_per_absent'] = $late_per_absent;
                $data['create_date'] = date('Y-m-d');
                $usercheck = EmployeeCategorySetup::CheckEmployeeCategory($name, 0);

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

                if ((strlen($name) == 0) || (strlen($name) > 255))
                    array_push($errors, "Please enter category name");
                if ($usercheck)
                    array_push($errors, "category name already exist");
                if ($shift_start_time == 0)
                    array_push($errors, "Please enter shift start time");
                if ($late_grace_time == 0)
                    array_push($errors, "Please enter late grace time");
                if ($late_deduction == 1) {
                    if ($late_per_absent == 0) {
                        array_push($errors, "Please enter number of late per absent");
                    }
                }

            if (count($errors) == 0) {
                $q = EmployeeCategorySetup::AddEmployeeCategorySetup($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="employee_cat_setup_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"
                                               required
                                               value='<?php echo $name; ?>'/>
                                        <label for="name" class="control-label">Category Name *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="casual_leave" maxlength="2"
                                               name="casual_leave"
                                               required
                                               value='<?php echo $casual_leave; ?>'/>
                                        <label for="casual_leave" class="control-label">Casual Leaves *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="annual_leave" maxlength="2"
                                               name="annual_leave"
                                               required
                                               value='<?php echo $annual_leave; ?>'/>
                                        <label for="annual_leave" class="control-label">Annual Leaves *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <select class="form-control" name="shift_cat" id="shift">
                                            <option value="a" <?= $shift_cat == 'a' ? 'selected' : '' ?>>A</option>
                                            <option value="b" <?= $shift_cat == 'b' ? 'selected' : '' ?>>B
                                            </option>
                                        </select>
                                        <label for="shift" class="control-label">Shift *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <select class="form-control" name="shift" id="shift">
                                            <option value="day" <?= $shift == 'day' ? 'selected' : '' ?>>Day</option>
                                            <option value="night" <?= $shift == 'night' ? 'selected' : '' ?>>Night
                                            </option>
                                        </select>
                                        <label for="shift" class="control-label">Shift *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input type="checkbox" name="late_deduction" id="late_deduction"/>
                                        <label for="late_deduction" class="control-label">Late Deduction *</label>
                                        <br>
                                        <br>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input type="checkbox" name="by_pass" id="by_pass"/>
                                        <label for="by_pass" class="control-label">By Pass Deductions</label>
                                        <br>
                                        <br>
                                    </div>
                                </div>
                                <div class="row margin" style="display: none; " id="start_time">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" name="shift_start_time"
                                               id="shift_start_time"/>
                                        <label for="shift_start_time" class="control-label">Shift Start Time *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" maxlength="2" type="text"
                                               name="late_grace_time" id="late_grace_time"/>
                                        <label for="late_grace_time" class="control-label">late grace time(min)
                                            *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" maxlength="1" name="late_per_absent"
                                               id="late_per_absent"/>
                                        <label for="late_per_absent" class="control-label">late per absent *</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='employee_category_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>Category Name</th>
                            <th>Casual Leaves</th>
                            <th>Annual Leaves</th>
                            <th>Shift Category</th>
                            <th>Shift</th>
                            <th>Late Grace Time</th>
                            <th>Late/Absent</th>
                        </tr>
                        </thead>
                        <tfoot>
                        <tr>
                            <th>Action</th>
                            <th>Category Name</th>
                            <th>Casual Leaves</th>
                            <th>Annual Leaves</th>
                            <th>Shift Category</th>
                            <th>Shift</th>
                            <th>Late Grace Time</th>
                            <th>Late/Absent</th>
                        </tr>
                        </tfoot>
                        <tbody>
                        <?php
                        $qry = EmployeeCategorySetup::GetEmployeeCategorySetupList();
                        if ($qry) {
                            foreach ($qry as $qery) { ?>
                                <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($qery->casual_leave); ?></td>
                                    <td><?php echo ucwords($qery->annual_leave); ?></td>
                                    <td><?php echo ucwords($qery->shift_cat); ?></td>
                                    <td><?php echo ucwords($qery->shift); ?></td>
                                    <td><?php echo ucwords($qery->late_grace_time) . ' min.'; ?></td>
                                    <td><?php echo ucwords($qery->late_per_absent); ?></td>
                                </tr>
                                <?php
                            }
                        } ?>
                        </tbody>
                    </table>
                </div>
                <?php

            } // close view


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

                $data['name'] = $name;
                $data['casual_leave'] = $casual_leave;
                $data['annual_leave'] = $annual_leave;
                $data['shift'] = $shift;
                $data['shift_cat'] = $shift_cat;
                $data['shift_start_time'] = date('H:i', strtotime($shift_start_time));
                $data['late_grace_time'] = $late_grace_time;
                $data['late_deduction'] = $late_deduction == 'on' ? 1 : 0;
                $data['by_pass'] = $by_pass == 'on' ? 1 : 0;
                $data['late_per_absent'] = $late_per_absent;
                $usercheck = EmployeeCategorySetup::CheckEmployeeCategory($name, $id);

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

                if ((strlen($name) == 0) || (strlen($name) > 255))
                    array_push($errors, "Please enter category name");

                if ($usercheck)
                    array_push($errors, "Customer name already exist");
                if ($shift_start_time == 0)
                    array_push($errors, "Please enter shift start time");
                if ($late_grace_time == 0)
                    array_push($errors, "Please enter late grace time");
                if ($late_deduction == 1) {
                    if ($late_per_absent == 0) {
                        array_push($errors, "Please enter number of late per absent");
                    }
                }

                if (count($errors) == 0) {
                    $q = EmployeeCategorySetup::UpdateEmployeeCategorySetup($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 = EmployeeCategorySetup::GetEmployeeCategorySetupById($id);
                    $name = $qry->name;
                    $casual_leave = $qry->casual_leave;
                    $annual_leave = $qry->annual_leave;
                    $shift = $qry->shift;
                    $shift_cat = $qry->shift_cat;
                    $shift_start_time = $qry->shift_start_time;
                    $late_grace_time = $qry->late_grace_time;
                    $late_deduction = $qry->late_deduction;
                    $by_pass = $qry->by_pass;
                    $late_per_absent = $qry->late_per_absent;
                }
                if ($_REQUEST['id'] && isset($_REQUEST['v'])) {
                    ?>
                    <style>
                        h4 {
                            float: left;
                            margin: 0;
                            width: 30%;
                        }

                        p {
                            float: left;
                        }

                        .activity-icon {
                            margin: 0;
                        }
                    </style>
                    <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">Category Name:</div>
                                    <div class="col s6"><?php echo $name; ?></div>
                                </div>
                            </div>
                        </div>
                        <div class="col s12 form-group">
                            <div class="col s8">
                                <div class="row">
                                    <div class="col s6">Casual Leave:</div>
                                    <div class="col s6"><?php echo $casual_leave; ?></div>
                                </div>
                            </div>
                        </div>
                        <div class="col s12 form-group">
                            <div class="col s8">
                                <div class="row">
                                    <div class="col s6">Annual Leave:</div>
                                    <div class="col s6"><?php echo $annual_leave; ?></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="employee_cat_setup_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"
                                                   required
                                                   value="<?php echo $name; ?>"/>
                                            <label for="name" class="control-label">Category Name *</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" type="text" id="casual_leave" maxlength="2"
                                                   name="casual_leave"
                                                   required
                                                   value="<?php echo $casual_leave; ?>"/>
                                            <label for="casual_leave" class="control-label">Casual Leave *</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" type="text" id="annual_leave" maxlength="2"
                                                   name="annual_leave"
                                                   required
                                                   value="<?php echo $annual_leave; ?>"/>
                                            <label for="annual_leave" class="control-label">Annual Leave *</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <select class="form-control" name="shift_cat" id="shift_cat">
                                                <option value="a" <?= $shift_cat == 'a' ? 'selected' : '' ?>>A
                                                </option>
                                                <option value="b" <?= $shift_cat == 'b' ? 'selected' : '' ?>>B
                                                </option>
                                            </select>
                                            <label for="shift_cat" class="control-label">Shift Category*</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <select class="form-control" name="shift" id="shift">
                                                <option value="day" <?= $shift == 'day' ? 'selected' : '' ?>>Day
                                                </option>
                                                <option value="night" <?= $shift == 'night' ? 'selected' : '' ?>>Night
                                                </option>
                                            </select>
                                            <label for="shift" class="control-label">Shift *</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input type="checkbox" <?= $late_deduction == 1 ? 'checked' : '' ?>
                                                   name="late_deduction"
                                                   id="late_deduction"/>
                                            <label for="late_deduction" class="control-label">Late Deduction *</label>
                                            <br>
                                            <br>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input type="checkbox" <?= $by_pass == 1 ? 'checked' : '' ?>
                                                   name="by_pass" id="by_pass"/>
                                            <label for="by_pass" class="control-label">By Pass Deductions</label>
                                            <br>
                                            <br>
                                        </div>
                                    </div>
                                    <div class="row margin" style="<?= $late_deduction == 1 ? '' : 'display:none;' ?>"
                                         id="start_time">
                                        <div class="input-field col s12">
                                            <input class="form-control"
                                                   value="<?= date('H:i', strtotime($shift_start_time)) ?>"
                                                   type="text" name="shift_start_time" id="shift_start_time"/>
                                            <label for="shift_start_time" class="control-label">Shift Start Time
                                                *</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" value="<?= $late_grace_time ?>"
                                                   maxlength="2" type="text" name="late_grace_time"
                                                   id="late_grace_time"/>
                                            <label for="late_grace_time" class="control-label">late grace time(min)
                                                *</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" type="text" value="<?= $late_per_absent ?>"
                                                   maxlength="1"
                                                   name="late_per_absent" id="late_per_absent"/>
                                            <label for="late_per_absent" class="control-label">late per absent *</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='employee_category_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);
        });
    });
    $('#late_deduction').change(function () {
        if ($(this).is(":checked")) {
            var returnVal = confirm("Are you sure?");
            $(this).attr("checked", returnVal);
            if (returnVal == true) {
                $('#start_time').show();
            } else {
                $('#start_time').hide();
            }
        } else {
            $('#start_time').hide();
        }
        $('#textbox1').val($(this).is(':checked'));
    });
</script>
</body>
</html>