<?php
include("core/init.php");
include("session.php");

/***********Table Used**********/
$mainTable = "certification";

/****************Search Filter Start Here***********/
if (!Session::exists("certificationFilterQuery")) {
  Session::put("certificationFilterQuery", '');
}

if (isset($_POST["filter_province"]) && isset($_POST["filter_status"])) {
  Session::delete("Cfilter_province");
  Session::delete("Cfilter_status");
  Session::delete("Cfilter_month");

  $filterQuery = "";
  $filterQuery .= 'WHERE 1=1';
  if (Input::getInput("filter_province") != "" && Input::getInput("filter_status") == "" && Input::getInput("filter_month") == "") {
    Session::put("Cfilter_province", Input::getInput("filter_province"));

    $filterQuery .= " AND province.id= '" . Input::getInput("filter_province") . "'";
  } else if (Input::getInput("filter_status") != "" && Input::getInput("filter_province") == "" && Input::getInput("filter_month") == "") {
    Session::put("Cfilter_status", Input::getInput("filter_status"));

    $filterQuery .= " AND certification_status.id= '" . Input::getInput("filter_status") . "'";
  } else if (Input::getInput("filter_status") == "" && Input::getInput("filter_province") == "" && Input::getInput("filter_month") != "") {
    Session::put("Cfilter_month", Input::getInput("filter_month"));

    $filterQuery .= " AND MONTH(certification.submitted_date)= '" . Input::getInput("filter_month") . "'";
  } else if (Input::getInput("filter_status") != "" && Input::getInput("filter_province") != "" & Input::getInput("filter_month") == "") {
    Session::put("Cfilter_status", Input::getInput("filter_status"));
    Session::put("Cfilter_province", Input::getInput("filter_province"));

    $filterQuery .= " AND certification_status.id= '" . Input::getInput("filter_status") . "' ";
    $filterQuery .= " AND province.id= '" . Input::getInput("filter_province") . "' ";
  } else if (Input::getInput("filter_status") == "" && Input::getInput("filter_province") != "" & Input::getInput("filter_month") != "") {

    Session::put("Cfilter_province", Input::getInput("filter_province"));
    Session::put("Cfilter_month", Input::getInput("filter_month"));

    $filterQuery .= " AND province.id= '" . Input::getInput("filter_province") . "' ";
    $filterQuery .= " AND MONTH(certification.submitted_date)= '" . Input::getInput("filter_month") . "' ";
  } else if (Input::getInput("filter_status") != "" && Input::getInput("filter_province") == "" & Input::getInput("filter_month") != "") {
    Session::put("Cfilter_status", Input::getInput("filter_status"));

    Session::put("Cfilter_month", Input::getInput("filter_month"));
    $filterQuery .= " AND certification_status.id= '" . Input::getInput("filter_status") . "' ";

    $filterQuery .= " AND MONTH(certification.submitted_date)= '" . Input::getInput("filter_month") . "' ";
  } else if (Input::getInput("filter_status") != "" && Input::getInput("filter_province") != "" & Input::getInput("filter_month") != "") {
    Session::put("Cfilter_status", Input::getInput("filter_status"));
    Session::put("Cfilter_province", Input::getInput("filter_province"));
    Session::put("Cfilter_month", Input::getInput("filter_month"));
    $filterQuery .= " AND certification_status.id= '" . Input::getInput("filter_status") . "' ";
    $filterQuery .= " AND province.id= '" . Input::getInput("filter_province") . "' ";
    $filterQuery .= " AND MONTH(certification.submitted_date)= '" . Input::getInput("filter_month") . "' ";
  } else {
    $filterQuery = "";
  }

  Session::put("certificationFilterQuery", $filterQuery);
}

/****************Category Add & Edit Process Starts Here**********************/

if ($_SERVER['REQUEST_METHOD'] === 'POST' && Input::exists() && Token::check(Input::getInput("token"))) {

  if (Input::getInput("certificationId") == "" && Input::getInput("certification_id") != "" && Input::getInput("firstname") != "" && Input::getInput("lastname") != "" && Input::getInput("province_id") != "" && Input::getInput("status") != "" && Input::getInput("submitted_date") != "") {
    Db::getInstance()->insert($mainTable, array(
      "submitted_date" => Input::getInput("submitted_date"),
      // "approval_date"=>Input::getInput("approval_date"),
      "status" => Input::getInput("status"),
      "certification_id"=>addslashes(Input::getInput("certification_id")),
      "province_id" => Input::getInput("province_id"),
      "firstname" => Input::getInput("firstname"),
      "lastname" => Input::getInput("lastname")
    ));
    Session::flash("message", '<div class="alert-success" style="padding: 5px;">Certificate Added.</div><Br>');
    Redirect::to("Certification");
  } else if (Input::getInput("certificationId") != "" && Input::getInput("firstname") != "" && Input::getInput("province_id") != "" && Input::getInput("status") != "" && Input::getInput("submitted_date") != "") {
    Db::getInstance()->update(
      $mainTable,
      array(
        "submitted_date" => Input::getInput("submitted_date"),
        // "approval_date"=>Input::getInput("approval_date"),
        "status" => Input::getInput("status"),
        "certification_id"=>addslashes(Input::getInput("certification_id")),
        "province_id" => Input::getInput("province_id"),
        "firstname" => Input::getInput("firstname"),
        "lastname" => Input::getInput("lastname")
      ),
      array("id" => Input::getInput("certificationId"))
    );

    Session::flash("message", '<div class="alert-success" style="padding: 5px;">Certificate Edited Successfully.</div><Br>');
    Redirect::to("Certification");
  } else {
    Session::flash("message", '<div class="alert-error" style="padding: 5px;">Please Fill * Fields.</div><Br>');
    Redirect::to("Certification");
  }
}
/****************Category Add & Edit Process Starts Here**********************/

/****************Category Edit Populate**********************/
$populateFields = null;
if (Input::particularInputExists("get", "editId")) {

  if (Db::getInstance()->num_rows("select id from {$mainTable} where id='" . Input::getInput("editId") . "'") > 0) {
    $populateFields = Db::getInstance()->get_row("select * from {$mainTable} where id='" . Input::getInput("editId") . "'");
  } else {

    Redirect::to("Certification");
  }
}
/****************Category Edit Populate Ends**********************/





/****************Category Delete**********************/


if (Input::particularInputExists("get", "deleteId")) {

  if (Db::getInstance()->num_rows("select id from {$mainTable} where id='" . Input::getInput("deleteId") . "'") > 0) {
    Db::getInstance()->delete($mainTable, array("id" => Input::getInput("deleteId")));

    Session::flash("message", '<div class="alert-success" style="padding: 5px;">Certificate Deleted.</div><Br>');
    Redirect::to("Certification");
  } else {
    Redirect::to("Certification");
  }
}

/****************Category Delete Ends**********************/



?>
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Certification - <?php echo Config::get("project/site_title"); ?></title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <link rel="shortcut icon" href="dist/img/admin-panel.png">
  <!-- Bootstrap 3.3.6 -->
  <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="plugins/datatables/dataTables.bootstrap.css">

  <link rel="stylesheet" href="plugins/datepicker/datepicker3.css">
  <link rel="stylesheet" href="dist/css/AdminLTE.min.css">
  <!-- AdminLTE Skins. Choose a skin from the css/skins
       folder instead of downloading all of them to reduce the load. -->
  <link rel="stylesheet" href="dist/css/skins/_all-skins.min.css">

  <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->


  <Style>
    #result {
      margin-left: 5px;
    }

    .short {
      color: #FF0000;
    }

    .weak {
      color: #E66C2C;
    }

    .good {
      color: #2D98F3;
    }

    .strong {
      color: #006400;
    }
  </Style>

</head>

<body class="hold-transition skin-blue sidebar-mini">
  <div class="wrapper">

    <?php
    $dbProvinces = Db::getInstance()->get_results("select id,province from province");

    $provinces = array();
    if (count($dbProvinces)) foreach ($dbProvinces as $dbProvince) {
      $provinces[$dbProvince["id"]] = $dbProvince["province"];
    }
    $dbStatuses = Db::getInstance()->get_results("select id,status from certification_status");

    $statuses = array();
    if (count($dbStatuses)) foreach ($dbStatuses as $dbStatus) {
      $statuses[$dbStatus["id"]] = $dbStatus["status"];
    }


    $monthArray = array(
      "1" => "Jan",
      "2" => "Feb",
      "3" => "Mar",
      "4" => "Apr",
      "5" => "May",
      "6" => "Jun",
      "7" => "Jul",
      "8" => "Aug",
      "9" => "Sep",
      "10" => "Oct",
      "11" => "Nov",
      "12" => "Dec"
    );
    ?>


    <?php include("includes/header.php"); ?>






    <?php include("includes/sidebar.php"); ?>


    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
      <!-- Content Header (Page header) -->
      <section class="content-header">
        <h1>
          Certification
          <small></small>
        </h1>
        <ol class="breadcrumb">
          <li><a href="Dashboard"><i class="fa fa-dashboard"></i> Home</a></li>
          <li class="active">Certification</li>
        </ol>
      </section>

      <!-- Main content -->
      <section class="content">

        <div class="row">


          <div class="col-md-12">

            <?php echo Session::flashDisplay("message"); ?>

            <a href="Certification?addNew" class="btn btn-primary" style="margin-bottom: 10px;">Add New</a>
            <?php if (isset($_GET["addNew"]) || isset($_GET["editId"])) { ?>
              <!-- general form elements -->
              <div class="box box-primary">
                <div class="box-header with-border">

                </div>
                <!-- /.box-header -->
                <!-- form start -->
                <form role="form" action="Certification" method="post" id="form_">
                  <div class="box-body">
                    <div class="alert-danger" style="padding: 5px;display: none;" id="error">* Fields Required.</div>

                    <div class="form-group">
                      <label for="firstname">First Name *</label>
                      <input type="text" class="form-control" name="firstname" id="firstname" value="<?php echo Input::getInput("firstname", $populateFields); ?>">
                    </div>

                    <div class="form-group">
                      <label for="lastname">Last Name *</label>
                      <input type="text" class="form-control" name="lastname" id="lastname" value="<?php echo Input::getInput("lastname", $populateFields); ?>">
                    </div>

                    <div class="form-group">
                      <label for="exampleInputPassword1">License Id*</label>
                      <input type="text" class="form-control" name="certification_id"  id="certification_id" value="<?php echo Input::getInput("certification_id", $populateFields); ?>">
                    </div>

                    <!--  <div class="form-group">
                  <label for="exampleInputPassword1">Approval Date </label>
                  <input type="text" class="form-control" name="approval_date"  id="approval_date" value="<?php /*echo Input::getInput("approval_date",$populateFields); */ ?>">
                </div>-->

                    <div class="form-group">
                      <label for="province_id">Select Location *</label>
                      <select name="province_id" id="province_id" class="form-control">
                        <?php echo Input::getSelected("province_id", $populateFields, $provinces); ?>
                      </select>
                    </div>

                    <div class="form-group">
                      <label for="status">Select Status *</label>
                      <select name="status" class="form-control">
                        <?php echo Input::getSelected("status", $populateFields, $statuses); ?>
                      </select>
                    </div>

                    <div class="form-group">
                      <label for="submitted_date">Add Date *</label>
                      <input type="text" class="form-control" name="submitted_date" id="submitted_date" value="<?php echo Input::getInput("submitted_date", $populateFields); ?>">
                    </div>

                    <input type="hidden" name="certificationId" value="<?php echo Input::getInput("id", $populateFields); ?>">

                    <input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
                  </div>
                  <!-- /.box-body -->

                  <div class="box-footer">
                    <button type="submit" class="btn btn-primary" onclick="checkFields();return false;">Submit</button>
                  </div>
                </form>
              </div>
              <!-- /.box -->
            <?php } ?>



          </div>
          <!-- /.col -->






          <div class="col-md-12">
            <div class="box">
              <div class="box-header">
                <h3 class="box-title">Certifications</h3>


                <div class="col-md-12">
                  <?php

                  $newProvinceArray = array("" => "All Locations");
                  $newStatusArray = array("" => "All Status");
                  $provinces = $newProvinceArray + $provinces;
                  $statuses = $newStatusArray + $statuses;
                  $newMonthArray = array("" => "All Months");
                  $monthArray = $newMonthArray + $monthArray;


                  ?>
                  <form id="_filterform" action="certification" method="post">
                    <div class="col-md-3">
                      <select name="filter_province" class="form-control">
                        <?php echo Input::getSelected("filter_province", Session::get("Cfilter_province"), $provinces); ?>
                      </select>
                    </div>

                    <div class="col-md-3">
                      <select name="filter_status" class="form-control">
                        <?php echo Input::getSelected("filter_status", Session::get("Cfilter_status"), $statuses); ?>
                      </select>

                    </div>

                    <div class="col-md-3">
                      <select name="filter_month" class="form-control">
                        <?php echo Input::getSelected("filter_month", Session::get("Cfilter_month"), $monthArray); ?>
                      </select>

                    </div>


                    <div class="col-md-3">
                      <button type="submit" class="btn btn-primary" onclick="filterResults();">Filter</button>
                    </div>

                  </form>
                </div>
              </div>
              <!-- /.box-header -->
              <div class="box-body">
                <table id="example1" class="table table-bordered table-striped">
                  <thead>
                    <tr>
                      <th>No.</th>
                      <th>FirstName</th>
                      <th>Lastname</th>
                      <th>License Id</th>
                      <th>Location</th>
                      <th>Status</th>
                      <th>Add Date</th>
                      <!-- <th>Creation Date</th>-->
                      <th>Edit</th>
                      <th>Delete</th>
                    </tr>
                  </thead>
                  <tbody>

                    <?php
                    //  echo "select certification.*,province.province,certification_status.status from `certification`
                    //   JOIN province ON (province.id=certification.province_id)
                    //   JOIN certification_status ON (certification_status.id=certification.status) ".Session::get("certificationFilterQuery");die();


                    $fields = Db::getInstance()->get_results("select certification.*,province.province,certification_status.status from `certification`
                JOIN province ON (province.id=certification.province_id)
                JOIN certification_status ON (certification_status.id=certification.status) " . Session::get("certificationFilterQuery") . " ORDER BY certification.id DESC");
                    $sno = 1;
                    if (count($fields)) {
                      foreach ($fields as $field) {

                        /*  $date = new DateTime($field["date_time"]);*/

                        echo '<tr>';
                        echo '<td>' . $sno . '</td>';
                        echo '<td>' . $field["firstname"] . '</td>';
                        echo '<td>' . $field["lastname"] . '</td>';
                        echo '<td>' . $field["certification_id"] . '</td>';
                        echo '<td>' . $field["province"] . '</td>';

                        if ($field["status"] == "Approved") {
                          echo '<td><span style="color: green">' . $field["status"] . '</span></td>';
                        } else if ($field["status"] == "Pending") {
                          echo '<td><span style="color:#E1DA29;font-weight: 600">' . $field["status"] . '</span></td>';
                        } else if ($field["status"] == "Rejected") {
                          echo '<td><span style="color: red">' . $field["status"] . '</span></td>';
                        }

                        echo '<td>' . $field["submitted_date"] . '</td>';



                        /* echo '<td>' . $date->format("Y-m-d") . '</td>';*/

                        echo '<td><A href="Certification?editId=' . $field["id"] . '"> <i class="fa fa-pencil-square-o fa-lg" aria-hidden="true"></i></A> </td>';

                        echo '<td><A href="Certification?deleteId=' . $field["id"] . '" onclick="return confirm(\'Are you sure you want to delete Certificate?\');"> <i class="fa fa-trash fa-lg" aria-hidden="true"></i></A> </td>';

                    ?>


                    <?php




                        echo '</tr>';

                        $sno++;
                      }
                    }


                    ?>


                  </tbody>

                </table>
              </div>
              <!-- /.box-body -->
            </div>

          </div>










        </div>





      </section>
      <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->

    <?php include("includes/footer.php"); ?>


    <!-- Add the sidebar's background. This div must be placed
       immediately after the control sidebar -->
    <div class="control-sidebar-bg"></div>
  </div>
  <!-- ./wrapper -->




  <!-- jQuery 2.2.3 -->
  <script src="plugins/jQuery/jquery-2.2.3.min.js"></script>
  <!-- Bootstrap 3.3.6 -->
  <script src="bootstrap/js/bootstrap.min.js"></script>
  <!-- Slimscroll -->
  <script src="plugins/slimScroll/jquery.slimscroll.min.js"></script>
  <!-- FastClick -->
  <script src="plugins/fastclick/fastclick.js"></script>
  <!-- AdminLTE App -->
  <script src="plugins/datepicker/bootstrap-datepicker.js"></script>
  <script src="dist/js/app.min.js"></script>
  <!-- AdminLTE for demo purposes -->
  <script src="dist/js/demo.js"></script>
  <script src="plugins/datatables/jquery.dataTables.min.js"></script>
  <script src="plugins/datatables/dataTables.bootstrap.min.js"></script>

  <script>
    $(function() {
      $("#example1").DataTable();
      $('#example2').DataTable({
        "paging": true,
        "lengthChange": false,
        "searching": false,
        "ordering": true,
        "info": true,
        "autoWidth": false
      });
    });


    $('#submitted_date').datepicker({
      todayHighlight: true,
      format: 'yyyy-mm-dd',
      autoclose: true
    });

    $('#approval_date').datepicker({
      todayHighlight: true,
      format: 'yyyy-mm-dd',
      autoclose: true
    });

    function checkFields() {
      $("#error").hide();
      var firstname = $("#firstname").val();
      var lastname = $("#lastname").val();
      var province_id = $("#province_id").val();
      var submitted_date = $("#submitted_date").val();
      var status = $("#status").val();
      if (firstname == "" || lastname == "" || province_id == "" || submitted_date == "" || status == "") {
        $("#error").show();
        return false;
      }
      else {
        $("#form_").submit();
      }
    }

    function filterResults() {
      $("#_filterform").submit();
      return true;
    }
  </script>


</body>

</html>