<?php
include("../classes/manager.class.php");
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
$m = new Manager();
$output = '';
$name = '';
$designation = '';
$machine_id = '';
$address = '';
$contact = '';
$salary = '';
$joining_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>Employee Setup</h3>
            </div>
            <script>
                $(function () {
                    $('#datepicker').pickadate({
                        format: 'dd-mm-yyyy',
                        selectMonths: true,
                        selectYears: true
                    });
                });
            </script>
            <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'] = $name1 = trim($name);
                $data['category_id'] = $category_id;
                $data['machine_userid'] = $machine_id;
                $data['designation'] = $designation;
                $data['address'] = $address;
                $data['contact'] = $contact;
                $data['salary'] = $salary;
                $data['joining_date'] = date('Y-m-d H:i:s', strtotime($joining_date));
                //$data['picture'] = $picture;
                $data['create_date'] = date('Y-m-d H:i:s');
                $data['status'] = $status;
                $employeecheck = EmployeeSetup::CheckEmployee($name, 0);

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

                if ((strlen($name1) == 0) || (strlen($name1) > 100))
                    array_push($errors, "Please enter  name");
                if ((strlen($designation) == 0))
                    array_push($errors, "Please enter designation");
                if ((strlen($address) == 0))
                    array_push($errors, "Please enter address");
                if ((strlen($contact) == 0))
                    array_push($errors, "Please enter contact number");
                if ((strlen($salary) == 0))
                    array_push($errors, "Please enter salary");
                if ((strlen($joining_date) == 0))
                    array_push($errors, "Please enter joining date");

                if (!empty ($_FILES['logo']['name'])) {
                    $logo = upload::img($_FILES['logo']);
                    if ($logo[0] == 'Upload Successfully!') {
                        $data['picture'] = $logo[1];
                    } else {
                        array_push($errors, $logo[0]);
                    }
                }

                if ($employeecheck)
                    array_push($errors, "Employee name already exist");
            if (count($errors) == 0) {
                $q = EmployeeSetup::AddEmployeeSetup($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_setup_form" novalidate="novalidate"
                          enctype="multipart/form-data">
                        <div class="col-sm-12 form-group">
                            <?php echo $output; ?>
                            <div class="col-sm-8">
                                <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="100"
                                               name="name"
                                               value='<?php echo $name; ?>'/>
                                        <label for="name" class="control-label">Name *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="designation" maxlength="100"
                                               name="designation"
                                               value='<?php echo $designation; ?>'/>
                                        <label for="designation" class="control-label">Designation *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <?php
                                        $qryCategory = EmployeeCategorySetup::GetEmployeeCategorySetupList();
                                        if ($qryCategory) {
                                            echo '<select id="category_id" name="category_id" class="form-control">';
                                            foreach ($qryCategory as $qery) {
                                                echo '<option value=' . $qery->id . '>' . $qery->name . '</option>';
                                            }
                                            echo '</select>';
                                        }
                                        ?>
                                        <label for="category_id" class="control-label">Category *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="machine_id" maxlength="100"
                                               name="machine_id"
                                               value='<?php echo $machine_id; ?>'/>
                                        <label for="machine_id" class="control-label">Machine User ID</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                                <textarea class="form-control" name="address"
                                                          id="address"><?php echo $address; ?></textarea>
                                        <label for="address" class="control-label">Address *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="contact" name="contact"
                                               maxlength="11"
                                               pattern="^[0]{1}[3]{1}\d{9}"
                                               value='<?php echo $contact; ?>'/>
                                        <label for="contact" class="control-label">Contact No *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="salary" maxlength="15"
                                               name="salary"
                                               value='<?php echo $salary; ?>'/>
                                        <label for="salary" class="control-label">Salary *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input type="text" name="joining_date" id="datepicker"
                                               value="<?= $joining_date ?>" class="form-control"/>
                                        <label for="datepicker" class="control-label">Joining Date *</label>
                                    </div>
                                </div>

                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <div class="file-field input-field">
                                            <div class="btn">
                                                <span>Picture (Upload Only Jpeg)</span>
                                                <input type="file" accept=".jpeg,.jpg,.png"
                                                       name="logo" id="file">
                                            </div>
                                            <div class="file-path-wrapper">
                                                <div class="pick">
                                                    <input class="file-path validate" type='text'> <span
                                                        id='val'></span>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <div class="row margin">
                                    <div id="status" class="input-field col s12">
                                        <select class="form-control" name="status" id="status">
                                            <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="status" 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='employee_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>Name</th>
                            <th>Designation</th>
                            <th>Contact</th>
                            <th>Salary</th>
                            <th>Machine User ID</th>
                            <th>Category Name</th>
                            <th>Status</th>
                        </tr>
                        </thead>
                        <tfoot>
                        <tr>
                            <th>Action</th>
                            <th>Name</th>
                            <th>Designation</th>
                            <th>Contact</th>
                            <th>Salary</th>
                            <th>Machine User ID</th>
                            <th>Category Name</th>
                            <th>Status</th>
                        </tr>
                        </tfoot>
                        <tbody>
                        <?php
                        $qry = EmployeeSetup::GetEmployeeSetupList();
                        if ($qry) {
                            foreach ($qry as $qery) {
                                $empCat = EmployeeCategorySetup::GetEmployeeCategorySetupById($qery->category_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 $qery->name; ?></td>
                                    <td><?php echo $qery->designation; ?></td>
                                    <td><?php echo $qery->contact; ?></td>
                                    <td><?php echo $qery->salary; ?></td>
                                    <td><?php echo $qery->machine_userid; ?></td>
                                    <td><?php echo $empCat->name; ?></td>
                                    <td><?= ($qery->status == 'active') ? ucwords($qery->status) : "Inactive"; ?></td>
                                </tr>
                                <?php
                            }
                        } ?>
                        </tbody>
                    </table>
                </div>
                <?php
            } // close view
            if (isset($_GET['edit']) && !empty($_POST)) {
                extract($_REQUEST);
                $id = $_GET['id'];
                $data['name'] = $name1 = trim($name);
                $data['category_id'] = $category_id;
                $data['machine_userid'] = $machine_id;
                $data['designation'] = $designation;
                $data['address'] = $address;
                $data['contact'] = $contact;
                $data['salary'] = $salary;
                $data['joining_date'] = date('Y-m-d H:i:s', strtotime($joining_date));
                //$data['picture'] = $picture;
                $data['create_date'] = date('Y-m-d H:i:s');
                $data['status'] = $status;
                $employeecheck = EmployeeSetup::CheckEmployee($name, $id);

                $errors = array();

                if (!empty ($_FILES['logo']['name'])) {
                    $logo = upload::img($_FILES['logo']);
                    if ($logo[0] == 'Upload Successfully!') {
                        $data['picture'] = $logo[1];
                    } else {
                        array_push($errors, $logo[0]);
                    }
                }
                // Validate the input

                if ((strlen($name1) == 0) || (strlen($name1) > 100))
                    array_push($errors, "Please enter  name");
                if ((strlen($designation) == 0))
                    array_push($errors, "Please enter designation");
                if ((strlen($address) == 0))
                    array_push($errors, "Please enter address");
                if ((strlen($contact) == 0))
                    array_push($errors, "Please enter contact number");
                if ((strlen($salary) == 0))
                    array_push($errors, "Please enter salary");
                if ((strlen($joining_date) == 0))
                    array_push($errors, "Please enter joining date");

                if ($employeecheck)
                    array_push($errors, "Employee name already exist");

                if (count($errors) == 0) {
                    $q = EmployeeSetup::UpdateEmployeeSetup($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 = EmployeeSetup::GetEmployeeSetupById($id);
                    $name = $qry->name;
                    $category_id = $qry->category_id;
                    $machine_id = $qry->machine_userid;
                    $designation = $qry->designation;
                    $address = $qry->address;
                    $contact = $qry->contact;
                    $salary = $qry->salary;
                    $joining_date = date('d-m-Y', strtotime($qry->joining_date));
                    $picture = $qry->picture;
                    $status = $qry->status;
                    $logo = $picture;
                    $Cat = EmployeeCategorySetup::GetEmployeeCategorySetupById($category_id);
                    $category_name = $Cat->name;
                }
                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-sm-12 form-group">
                            <div class="col-sm-8">
                                <div class="row">
                                    <div class="col s6">Name:</div>
                                    <div class="col s6"><?php echo $name; ?></div>
                                </div>
                                <div class="row">
                                    <div class="col s6">Designation:</div>
                                    <div class="col s6"><?php echo $designation; ?></div>
                                </div>
                                <div class="row">
                                    <div class="col s6">Category:</div>
                                    <div class="col s6"><?php echo $category_name; ?></div>
                                </div>
                                <div class="row">
                                    <div class="col s6">Machine User ID:</div>
                                    <div class="col s6"><?php echo $machine_id; ?></div>
                                </div>
                                <div class="row">
                                    <div class="col s6">Address:</div>
                                    <div class="col s6"><?php echo $address; ?></div>
                                </div>
                                <div class="row">
                                    <div class="col s6">Contact No:</div>
                                    <div class="col s6"><?php echo $contact; ?></div>
                                </div>
                                <div class="row">
                                    <div class="col s6">Salary:</div>
                                    <div class="col s6"><?php echo $salary; ?></div>
                                </div>
                                <div class="row">
                                    <div class="col s6">Joining Date:</div>
                                    <div class="col s6"><?php echo $joining_date; ?></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="employee_setup_form"
                              enctype="multipart/form-data"
                              novalidate="novalidate">
                            <div class="col-sm-12 form-group">
                                <div class="col-sm-8">
                                    <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="100"
                                                   name="name"
                                                   value='<?php echo $name; ?>'/>
                                            <label for="name" class="active control-label">Name *</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" type="text" id="designation" maxlength="100"
                                                   name="designation"
                                                   value='<?php echo $designation; ?>'/>
                                            <label for="designation" class="active control-label">Designation *</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <?php
                                            $qryCategory = EmployeeCategorySetup::GetEmployeeCategorySetupList();
                                            //print_r($qryCategory);
                                            if ($qryCategory) {
                                                echo '<select id="category_id" name="category_id" class="form-control">';
                                                foreach ($qryCategory as $qeryCat) {
                                                    if ($category_id == $qeryCat->id) {
                                                        $selected = 'selected';
                                                    } else {
                                                        $selected = '';
                                                    }
                                                    echo '<option value="' . $qeryCat->id . '" ' . $selected . '>' . $qeryCat->name . '</option>';
                                                }

                                                echo '</select>';
                                            }
                                            ?>
                                            <label for="category_id" class="active control-label">Category *</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" type="text" id="machine_id" maxlength="100"
                                                   name="machine_id"
                                                   value='<?php echo $machine_id; ?>'/>
                                            <label for="machine_id" class="active control-label">Machine User ID</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <textarea class="form-control" name="address"
                                                      id="address"><?php echo $address; ?></textarea>
                                            <label for="address" class="active control-label">Address *</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" type="text" id="contact" name="contact"
                                                   maxlength="11"
                                                   pattern="^[0]{1}[3]{1}\d{9}"
                                                   value='<?php echo $contact; ?>'/>
                                            <label for="contact" class="active control-label">Contact No *</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" type="text" id="salary" maxlength="15"
                                                   name="salary"
                                                   value='<?php echo $salary; ?>'/>
                                            <label for="salary" class="active control-label">Salary *</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input type="text" name="joining_date" id="datepicker"
                                                   value="<?= $joining_date ?>" class="form-control"/>
                                            <label for="datepicker" class="active control-label">Joining Date *</label>
                                        </div>
                                    </div>

                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <div class="file-field input-field">
                                                <div class="btn">
                                                    <span>Picture (Upload Only Jpeg)</span>
                                                    <input type="file" accept=".jpeg,.jpg,.png"
                                                           name="logo" id="file">
                                                </div>
                                                <div class="file-path-wrapper">
                                                    <div class="pick">
                                                        <input class="file-path validate" type='text'> <span
                                                            id='val'></span>
                                                        <span style="float: right;"> <a
                                                                href="employeepics/<?php echo $logo ?>"
                                                                target="_blank"><img style="width: 100px;"
                                                                                     src="<?php echo PROJECT_ADMIN_URL . 'employeepics/' . $logo ?>"/></a></span>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <br>

                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <select class="form-control" name="status" id="status">
                                                <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="status" class="active control-label">Status</label><br>
                                        </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_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">
    $('#button').click(function () {
        $("input[type='file']").trigger('click');
    })

    $("input[type='file']").change(function () {
        //$('#val').text(this.value.replace(/C:\\fakepath\\/i, ''))
    })
    $(document).ready(function () {
        var fewSeconds = 2;
        $('#btnsubmit').click(function () {
            // Ajax request
            var btn = $(this);
            btn.hide();
            setTimeout(function () {
                btn.show();
            }, fewSeconds * 1000);
        });
        $('#contact').formatter({
            'pattern': '03 {{99}}-{{9999999}}',
            'persistent': true
        });
    });
</script>
</body>
</html>