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

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

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

if(Input::exists() && Token::check(Input::getInput("token"))) {


//  print_r($_POST);exit;

  if(Input::getInput("trn_upcoming_course_id")=="" && Input::getInput("package_id")!="")
  {
    Db::getInstance()->insert($mainTable,array("package_id"=>Input::getInput("package_id"),
    "province_id"=>Input::getInput("province_id"),
        "date_start"=>Input::getInput("date_start"), 
        "date_list"=>Input::getInput("date_list"),
        "mode_and_timing"=>Input::getInput("mode_and_timing")
        ));
        
    //exit('dddd');    
    Session::flash("message",'<div class="alert-success" style="padding: 5px;">Upcoming Course detail Added.</div><Br>');
    Redirect::to("upcoming_courses");
  }
  else if(Input::getInput("trn_upcoming_course_id")!="" && Input::getInput("package_id")!="")
  {
    Db::getInstance()->update($mainTable,
        array("package_id"=>Input::getInput("package_id"),
        "province_id"=>Input::getInput("province_id"),
        "date_start"=>Input::getInput("date_start"), 
        "date_list"=>Input::getInput("date_list"),
        "mode_and_timing"=>Input::getInput("mode_and_timing")
        ),
        array("trn_upcoming_course_id"=>Input::getInput("trn_upcoming_course_id"))

    );

    Session::flash("message",'<div class="alert-success" style="padding: 5px;">Upcoming Course detail Edited Successfully.</div><Br>');
    Redirect::to("upcoming_courses");
  }
  else {
    Session::flash("message",'<div class="alert-error" style="padding: 5px;">Please select Package</div><Br>');
    Redirect::to("upcoming_courses");
  }


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




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

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


  }
  else {

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

/*getting location details*/
 $populateLocation = Db::getInstance()->get_results("select id,province from province where isActive=1");

//print_R($populateLocation); exit();
/*end changes*/



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


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

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

    Session::flash("message",'<div class="alert-success" style="padding: 5px;">Upcoming course detail Deleted.</div><Br>');
    Redirect::to("upcoming_courses");

  } else {
    Redirect::to("upcoming_courses");
  }
}

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



?><!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Upcoming Courses - <?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="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 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>
       Upcoming Courses
        <small></small>
      </h1>
      <ol class="breadcrumb">
        <li><a href="Dashboard"><i class="fa fa-dashboard"></i> Home</a></li>
        <li class="active">Upcoming Courses</li>
      </ol>
    </section>

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

      <div class="row">


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

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

          <a href="upcoming_courses?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="upcoming_courses" method="post">
              <div class="box-body">

                <div class="form-group">
                  <label for="exampleInputPassword1">Select Province *</label>
                  <select name="province_id" id="ddProvinceselect" class="form-control">
                      <option value="">Select</option>
                      <?php foreach($populateLocation as $data) { ?>
                      <option  <?php  if(Input::getInput("province_id",$populateFields)==$data['id']) echo 'selected'; else echo '';  ?>   value='<?=$data['id']?>'><?=$data['province']?></option>
                      <?php } ?>
                  </select>
                </div>
                
                
                 <div class="form-group">
                  <label for="exampleInputPassword1">Select Package *</label>
                  <select id="ddPackageselect" name="package_id" class="form-control">
                      
                  </select>
                </div>
                
          
                
                <div class="form-group">
                  <label for="exampleInputPassword1">Start Date *(in words)</label>
                  <input type="text" class="form-control" name="date_start"  id="startDate" value="<?php echo Input::getInput("date_start",$populateFields); ?>">
                </div>
                
                 <div class="form-group">
                  <label for="exampleInputPassword1">Date List *(in words)</label>
                  <input type="text" class="form-control" name="date_list"  id="dateList" value="<?php echo Input::getInput("date_list",$populateFields); ?>">
                </div>
                
                 
                  

                <div class="form-group">
                  <label for="exampleInputPassword1">Mode and Timing detail *</label>
                  <textarea id="mode_and_timing" type="text" class="form-control" name="mode_and_timing"  ><?php echo Input::getInput("mode_and_timing",$populateFields); ?></textarea>
                </div>


                <input type="hidden" name="trn_upcoming_course_id" value="<?php echo Input::getInput("trn_upcoming_course_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 btnSubmit">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">Upcoming Courses</h3>
            </div>
            <!-- /.box-header -->
            <div class="box-body">
              <table id="example1" class="table table-bordered table-striped">
                <thead>
                <tr>
                  <th>No.</th>

                  <th>Province</th>
                  <th>Package</th>
                  <th>Start Date (in words)</th>
                  <th>Date List (in words)</th>
                  <th>Mode and Timing Detail</th>
                  <th>Edit</th>
                  <th>Delete</th>
                </tr>
                </thead>
                <tbody>

                <?php $dbFields=Db::getInstance()->get_results("select m.	trn_upcoming_course_id,m.date_start,m.date_list,m.mode_and_timing,p.province,pac.package_name from trn_upcoming_courses m inner join province p on (p.id=m.province_id and p.isActive=1) inner join packages pac on (pac.id=m.package_id and pac.display_package=1) where m.is_active=1");
                $sno=1;
                if(count($dbFields)) {
                  foreach ($dbFields as $dbField) {

                    echo '<tr>';
                    echo '<Td>' . $sno . '</Td>';
                    echo '<Td>' . $dbField["province"] . '</Td>';
                    echo '<Td>' . $dbField["package_name"] . '</Td>';
                    echo '<Td>' . $dbField["date_start"] . '</Td>';
                    echo '<Td>' . $dbField["date_list"] . '</Td>';
                    echo '<Td>' . $dbField["mode_and_timing"] . '</Td>';


                    echo '<Td><A href="upcoming_courses?editId='.$dbField["trn_upcoming_course_id"].'"> <i class="fa fa-pencil-square-o fa-lg" aria-hidden="true"></i></A> </Td>';

                    echo '<Td><A href="upcoming_courses?deleteId='.$dbField["trn_upcoming_course_id"].'" onclick="return confirm(\'Are you sure you want to delete Upcoming course detail?\');"> <i style="color:red;" 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="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
    });
    
    
    /*changes done 06-08-2022*/
    
    $('.btnSubmit').on('click',function(){
       if($('#ddProvinceselect').val()=='' || $('#ddPackageselect').val()=='' || $('#startDate').val()=='' || $('#dateList').val()=='' || $('#mode_and_timing').val()=='') {
           
           alert('Please fill all the Mandatory Fields!');
           return false;
       } 
    });
    
    
    var isEdit='<?php echo Input::particularInputExists("get","editId"); ?>';
    
 
    
    $('#ddProvinceselect').on('change',function(){
        
        if($(this).val()!='') {
         var iProvinceid=$(this).val();    
         $.ajax({
            type: "POST",
            url: 'process_data.php',
            data: {provinceId:iProvinceid},
            success: function(response)
            {
                var jsonData = JSON.parse(response);
  
                if (jsonData.success == 1)
                {
                    //console.log(jsonData.data);
                    //return false;
                    
                    var htmlOptions="<option value=''>Select</option>";
                    for(var i=0;i<jsonData.data.length;i++){
                        
                        htmlOptions+="<option value='"+jsonData.data[i]['id']+"'>"+jsonData.data[i]['package_name']+"</option>";
                    }
                    
                    //console.log(htmlOptions);
                    
                    //return false;
                    $('#ddPackageselect').html(htmlOptions);
                    
                       $('#ddPackageselect option').each(function(){
            
                            if(Number($(this).val())==Number('<?php echo Input::getInput("package_id",$populateFields) ?>')) {
                                $(this).attr('selected',true);
                                
                            }
                            else {
                                $(this).attr('selected',false);
                                
                            }
                        });
                    
                    //location.href = 'my_profile.php';
                }
                else
                {
                    alert('Error in response!');
                    return false
                }
           }
       }); 
       }
        
    });
    
   
    
    if(isEdit!=''){
        
        $('#ddProvinceselect').val('<?php echo Input::getInput("province_id",$populateFields) ?>');
        $('#ddProvinceselect').change();
        
     
    }
    
    /*end changes */
  });



</script>


</body>
</html>
