<!-- Extend area where template is defined -->
<?= $this->extend('templates/adminlte/generalcontent') ?>
<!-- .Extend -->

<!-- Title of the page -->
<?= $this->section('title') ?>Employee Maintenance<?= $this->endSection() ?>
<!-- .Title -->

<!-- CSS of the page -->
<?= $this->section('css') ?>

<!-- DataTables -->
<link rel="stylesheet" href="<?= base_url() ?>adminlte/plugins/datatables-bs4/css/dataTables.bootstrap4.min.css">
<link rel="stylesheet" href="<?= base_url() ?>adminlte/plugins/datatables-responsive/css/responsive.bootstrap4.min.css">

<?= $this->endSection() ?>
<!-- .CSS -->

<!-- body attribute - class definition -->
<?= $this->section('bodyclass') ?>sidebar-mini<?= $this->endSection() ?>
<!-- .body attribute -->

<!-- Container title -->
<?= $this->section('containertitle') ?>Employee Maintenance<?= $this->endSection() ?>
<!-- .Container title -->

<!-- Active breadcrumb -->
<?= $this->section('breadcrumbs') ?>
<li class="breadcrumb-item active">Employee Maintenance</li>
<?= $this->endSection() ?>
<!-- .Active breadcrumb -->

<!-- Main content -->
<?= $this->section('main') ?>

<!-- Modal Add Employee -->
<div class="modal fade" id="mdlAddEmployee">
  <div class="modal-dialog">
    <div class="modal-content">
      <form action="<?= url_to('hr/addemp') ?>" method="post">
        <div class="modal-header">
          <h4 class="modal-title" >New Employee</h4>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          <div class="row">
            <div class="col-12">
              <p class="lead">Employee Information</p>
              <div class="row">
                <div class="col-12">
                <p><strong>Company Name</strong></p>
                    <input type="hidden" name="company_id" value="<?= session()->get('companyInfo')->company_id ?>">
                    <p><?= session()->get('companyInfo')->company_name ?></p>
                </div>
              </div>
              <div class="row">
                <div class="col-12">
                  <div class="form-group">
                    <!-- select -->
                    <div class="form-group">
                        <label>Select Branch Assignment</label>
                        <select class="form-control" name="branch_code">
                            <?Php
                            
                            foreach($branches as $branch){
                                $selected = ($branch->branch_code == old('branch_code')) ? 'selected' : '';
                                echo '<option value="'.$branch->branch_code.'" '.$selected.'>['.$branch->branch_code.'] '.$branch->branch_name.'</option>';
                            }
                            
                            ?>
                        </select>
                      </div>
                  </div>
                  <div class="form-group">
                    <!-- select -->
                    <div class="form-group">
                        <label>Select Department</label>
                        <select class="form-control" name="dept_id">
                            <?Php
                            
                            foreach($departments as $department){
                                $selected = ($department->dept_id == old('dept_id')) ? 'selected' : '';
                                echo '<option value="'.$department->dept_id.'" '.$selected.'>['.$department->department_code.'] '.$department->department_name.'</option>';
                            }
                            
                            ?>
                        </select>
                    </div>
                  </div>
                  <div class="form-group">
                    <!-- select -->
                    <div class="form-group">
                        <label>Job Title</label>
                        <select class="form-control" name="job_title_id">
                            <?Php
                            
                            foreach($jobTitles as $jobTitle){
                                $selected = ($jobTitle->job_title_id == old('job_title_id')) ? 'selected' : '';
                                echo '<option value="'.$jobTitle->job_title_id.'" '.$selected.'>'.$jobTitle->job_title_name.'</option>';
                            }
                            
                            ?>
                        </select>
                    </div>
                  </div>
                  <div class="form-group">
                    <!-- select -->
                    <div class="form-group">
                        <label>Employment Status</label>
                        <select class="form-control" name="emp_status_id">
                            <?Php
                            
                            foreach($employmentStatus as $empStatus){
                                $selected = ($empStatus->emp_status_id == old('emp_status_id')) ? 'selected' : '';
                                echo '<option value="'.$empStatus->emp_status_id.'" '.$selected.'>'.$empStatus->status_name.'</option>';
                            }
                            
                            ?>
                        </select>
                    </div>
                  </div>
                  <div class="form-group">
                    <!-- select -->
                    <div class="form-group">
                        <label>Payroll Group</label>
                        <select class="form-control" name="pay_group_id">
                            <?Php
                            
                            foreach($payGroups as $payGroup){
                                $selected = ($payGroup->pay_group_id == old('pay_group_id')) ? 'selected' : '';
                                echo '<option value="'.$payGroup->pay_group_id.'" '.$selected.'>'.$payGroup->pay_group_name.'</option>';
                            }
                            
                            ?>
                        </select>
                    </div>
                  </div>
                  <div class="form-group">
                    <label for="txtCompanyIssuedID">ID Number</label>
                    <input class="form-control" type="text" id="txtCompanyIssuedID" name="company_issued_id" value="<?= old('company_issued_id') ?>">
                  </div>
                  <div class="form-group">
                    <label for="txtDTRIDNo">ID Number used in DTR</label>
                    <input class="form-control" type="text" id="txtDTRIDNo" name="dtr_emp_id" value="<?= old('dtr_emp_id') ?>">
                  </div>
                  <div class="form-group">
                    <label for="txtLastname">Last Name</label>
                    <input class="form-control" type="text" id="txtLastname" name="last_name" value="<?= old('last_name') ?>">
                  </div>
                  <div class="form-group">
                    <label for="txtFirstName">First Name</label>
                    <input class="form-control" type="text" id="txtFirstName" name="first_name" value="<?= old('first_name') ?>">
                  </div>
                  <div class="form-group">
                    <label for="txtMiddleName">Middle Name</label>
                    <input class="form-control" type="text" id="txtMiddleName" name="middle_name" value="<?= old('middle_name') ?>">
                  </div>
                  <div class="form-group">
                    <label for="txtSuffix">Suffix (e.g. Jr, Sr, III)</label>
                    <input class="form-control" type="text" id="txtSuffix" name="suffix" value="<?= old('suffix') ?>">
                  </div>
                  <div class="form-group">
                    <label for="txtEmailAddress">Email Address</label>
                    <input class="form-control" type="text" id="txtEmailAddress" name="email_address" value="<?= old('email_address') ?>">
                  </div>
                  <div class="form-group">
                    <label for="txtContactNumber">Contact Number</label>
                    <input class="form-control" type="text" id="txtContactNumber" name="contact_number" value="<?= old('contact_number') ?>">
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="modal-footer">
            <button type="submit" class="btn btn-primary">Save changes</button>
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        </div>
      </form>
    </div>
  </div>
</div>


<!-- Modal Edit Emloyee -->
<div class="modal fade" id="mdlEditEmployee">
  <div class="modal-dialog">
    <div class="modal-content">
      <form action="<?= url_to('hr/editemp') ?>" method="post">
        <div class="modal-header bg-warning">
          <h4 class="modal-title" >Edit Employee</h4>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          <div class="row">
            <div class="col-12">
              <p class="lead">Employee Information</p>
              <div class="row">
                <div class="col-12">
                <p><strong>Company Name</strong></p>
                    <input type="hidden" name="employee_id" id="hdnEditEmployeeID">
                    <input type="hidden" name="company_id" id="hdnEditCompanyID">
                    <p><?= session()->get('companyInfo')->company_name ?></p> <!-- To be modified since this should get the company name from the database according to company_id -->
                </div>
              </div>
              <div class="row">
                <div class="col-12">
                  <div class="form-group">
                    <!-- select -->
                    <div class="form-group">
                        <label>Select Branch Assignment</label>
                        <select class="form-control" name="branch_code" id="cmbEditBranch">
                            <?Php
                            
                            foreach($branches as $branch){
                                echo '<option value="'.$branch->branch_code.'">['.$branch->branch_code.'] '.$branch->branch_name.'</option>';
                            }
                            
                            ?>
                        </select>
                      </div>
                  </div>
                  <div class="form-group">
                    <!-- select -->
                    <div class="form-group">
                        <label>Select Department</label>
                        <select class="form-control" name="dept_id" id="cmbEditDepartment">
                            <?Php
                            
                            foreach($departments as $department){
                                echo '<option value="'.$department->dept_id.'">['.$department->department_code.'] '.$department->department_name.'</option>';
                            }
                            
                            ?>
                        </select>
                    </div>
                  </div>
                  <div class="form-group">
                    <!-- select -->
                    <div class="form-group">
                        <label>Job Title</label>
                        <select class="form-control" name="job_title_id" id="cmbEditJobTitle">
                            <?Php
                            
                            foreach($jobTitles as $jobTitle){
                                echo '<option value="'.$jobTitle->job_title_id.'">'.$jobTitle->job_title_name.'</option>';
                            }
                            
                            ?>
                        </select>
                    </div>
                  </div>
                  <div class="form-group">
                    <!-- select -->
                    <div class="form-group">
                        <label>Employment Status</label>
                        <select class="form-control" name="emp_status_id" id="cmbEditEmploymentStatus">
                            <?Php
                            
                            foreach($employmentStatus as $empStatus){
                                echo '<option value="'.$empStatus->emp_status_id.'">'.$empStatus->status_name.'</option>';
                            }
                            
                            ?>
                        </select>
                    </div>
                  </div>
                  <div class="form-group">
                    <!-- select -->
                    <div class="form-group">
                        <label>Payroll Group</label>
                        <select class="form-control" name="pay_group_id" id="cmbEditPayGroup">
                            <?Php
                            
                            foreach($payGroups as $payGroup){
                                echo '<option value="'.$payGroup->pay_group_id.'">'.$payGroup->pay_group_name.'</option>';
                            }
                            
                            ?>
                        </select>
                    </div>
                  </div>
                  <div class="form-group">
                    <label for="txtEditCompanyIssuedID">ID Number</label>
                    <input class="form-control" type="text" id="txtEditCompanyIssuedID" name="company_issued_id">
                  </div>
                  <div class="form-group">
                    <label for="txtEditDTRIDNo">ID Number used in DTR</label>
                    <input class="form-control" type="text" id="txtEditDTRIDNo" name="dtr_emp_id">
                  </div>
                  <div class="form-group">
                    <label for="txtEditLastname">Last Name</label>
                    <input class="form-control" type="text" id="txtEditLastname" name="last_name">
                  </div>
                  <div class="form-group">
                    <label for="txtEditFirstName">First Name</label>
                    <input class="form-control" type="text" id="txtEditFirstName" name="first_name">
                  </div>
                  <div class="form-group">
                    <label for="txtEditMiddleName">Middle Name</label>
                    <input class="form-control" type="text" id="txtEditMiddleName" name="middle_name">
                  </div>
                  <div class="form-group">
                    <label for="txtEditSuffix">Suffix (e.g. Jr, Sr, III)</label>
                    <input class="form-control" type="text" id="txtEditSuffix" name="suffix">
                  </div>
                  <div class="form-group">
                    <label for="txtEditEmailAddress">Email Address</label>
                    <input class="form-control" type="text" id="txtEditEmailAddress" name="email_address">
                  </div>
                  <div class="form-group">
                    <label for="txtEditContactNumber">Contact Number</label>
                    <input class="form-control" type="text" id="txtEditContactNumber" name="contact_number">
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="modal-footer">
            <button type="submit" class="btn btn-primary">Save changes</button>
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        </div>
      </form>
    </div>
  </div>
</div>


<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">List of Employees</h3>
            </div>
            <div class="card-body">
                <div class="card-body table-responsive p-0">
                    <?= $tblEmployee ?>
                </div>
            </div>
            <div class="card-footer">
                <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#mdlAddEmployee">Add Employee</button>
            </div>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
<!-- .Main content -->

<!-- Javascript -->

<?= $this->section('js') ?>

<!-- DataTables  & Plugins -->
<script src="<?= base_url() ?>adminlte/plugins/datatables/jquery.dataTables.min.js"></script>
<script src="<?= base_url() ?>adminlte/plugins/datatables-bs4/js/dataTables.bootstrap4.min.js"></script>
<script src="<?= base_url() ?>adminlte/plugins/datatables-responsive/js/dataTables.responsive.min.js"></script>
<script src="<?= base_url() ?>adminlte/plugins/datatables-responsive/js/responsive.bootstrap4.min.js"></script>


<script>
$(document).ready(function() {

  $('#tblEmployee').DataTable({
      "paging": true,
      "lengthChange": false,
      "searching": true,
      "ordering": true,
      "info": true,
      "autoWidth": false,
      "responsive": true,
    });
    
});

function editEmployee(data)
{
  $("#hdnEditEmployeeID").val($(data).data("employee_id"));
  $("#hdnEditCompanyID").val($(data).data("company_id"));
  $("#cmbEditBranch").val($(data).data("branch_code"));
  $("#cmbEditDepartment").val($(data).data("dept_id"));
  $("#cmbEditJobTitle").val($(data).data("job_title_id"));
  $("#cmbEditEmploymentStatus").val($(data).data("emp_status_id"));
  $("#cmbEditPayGroup").val($(data).data("pay_group_id"));
  $("#txtEditCompanyIssuedID").val($(data).data("company_issued_id"));
  $("#txtEditDTRIDNo").val($(data).data("dtr_emp_id"));
  $("#txtEditLastname").val($(data).data("last_name"));
  $("#txtEditFirstName").val($(data).data("first_name"));
  $("#txtEditMiddleName").val($(data).data("middle_name"));
  $("#txtEditSuffix").val($(data).data("suffix"));
  $("#txtEditEmailAddress").val($(data).data("email_address"));
  $("#txtEditContactNumber").val($(data).data("contact_number"));

  $("#mdlEditEmployee").modal("show");
}

</script>

<?= $this->endSection() ?>

<!-- .Javascript -->