<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Createclass extends CI_Controller 
{

 public function __construct()
 {
  parent::__construct();
  $this->load->helper('url');
  $this->load->helper('form');
  $this->load->library('form_validation');
  $this->load->model('Create_model');
  $this->load->library('userauth');
}

public function cl_create()
{
  $this->userauth->logged_in();
  $data['class_name']=$this->Create_model->getData('tbl_classname');
  $data['class_instructor']=$this->Create_model->getData('tbl_instructors');
  $data['class_location']=$this->Create_model->getData('tbl_classlocations');
  $data['all_class']=$this->Create_model->getAll();
  $this->load->view('admin/class-create',$data);
}

public function manage_create()
{
  $action = $this->input->post('submit');

  $form_data = array('class_id'=>$this->input->post('class_id'),
   'class_seats'=>$this->input->post('class_seats'),
   'remain_seats'=>$this->input->post('class_seats'),
   'instr_id'=>$this->input->post('instr_id'),
   'class_hours'=>$this->input->post('class_hours'),
   'class_registrants'=>implode(",",$this->input->post('class_registrants')),
   'class_price_m'=>$this->input->post('class_price_m'),
   'class_price_m2'=>$this->input->post('class_price_m2'),
   'class_price_n'=>$this->input->post('class_price_n'),
   'class_price_n2'=>$this->input->post('class_price_n2'),
   'class_price_s'=>$this->input->post('class_price_s'),
   'class_day'=>implode(',', (array) $this->input->post('class_day')),
   'calss_start_date'=>date("y-m-d",strtotime($this->input->post('calss_start_date'))),
   'calss_end_date'=>date("y-m-d",strtotime($this->input->post('calss_end_date'))),
   'calss_start_time'=>date('H:i:s',strtotime($this->input->post('calss_start_time'))),
   'calss_end_time'=>date('H:i:s',strtotime($this->input->post('calss_end_time'))),
   'locat_id'=>$this->input->post('locat_id'),
   'class_room'=>$this->input->post('class_room'),
   'class_address'=>$this->input->post('class_address'),
   'class_city'=>$this->input->post('class_city'),
   'class_state'=>$this->input->post('class_state'),
   'class_code'=>$this->input->post('class_code'),
   'class_phone'=>$this->input->post('class_phone'),
   'class_description'=>$this->input->post('class_description'));

  if($action == "Add")
  { 
    $data = $this->Create_model->insertRow('tbl_classform',$form_data);
    if($data)
    {
     $this->session->set_flashdata('success_msg','Class Added Successfully');
     redirect('admin/create-class');
   }
   else
   {
    $this->session->set_flashdata('error_msg','Error Occur');
    redirect('admin/create-class');
  }
}
elseif($action == "Update")
{
  $form_id = $this->input->post('form_id');
  $data = $this->Create_model->updateRow('tbl_classform','form_id',$form_id,$form_data);
  if($data)
  {
   $this->session->set_flashdata('success_msg','Class Updated Successfully');
   redirect('admin/create-class');
 }
 else
 {
  $this->session->set_flashdata('error_msg','Error Occur');
  redirect('admin/create-class');
}
}
}

/*Return form*/

public function return_form($form_id)
{
  $this->userauth->logged_in();
  $return_form = $this->Create_model->returnForm($form_id);
  $data['form_id']=$return_form->form_id;
  $data['class_name']=$return_form->class_name;
  $data['class_id']=$return_form->class_id;

  $data['class_seats']=$return_form->class_seats;
  $data['instr_name']=$return_form->instr_name;
  $data['instr_id']=$return_form->instr_id;

  $data['class_hours']=$return_form->class_hours;
  $data['class_registrants']=explode(",",$return_form->class_registrants);
  $data['class_price_m']=$return_form->class_price_m;
  $data['class_price_m2']=$return_form->class_price_m2;
  $data['class_price_n']=$return_form->class_price_n;
  $data['class_price_n2']=$return_form->class_price_n2;
  $data['class_price_s']=$return_form->class_price_s;
  $data['class_day']=explode(",",$return_form->class_day);

  $data['calss_start_date']=date("d-m-Y",strtotime($return_form->calss_start_date));
  $data['calss_end_date']=date("d-m-Y",strtotime($return_form->calss_end_date));
  
  $data['calss_start_time'] = date('h:i A',strtotime($return_form->calss_start_time));
  $data['calss_end_time'] = date('h:i A',strtotime($return_form->calss_end_time));

  $data['locat_id']=$return_form->locat_id;
  $data['locat_name']=$return_form->locat_name;

  $data['class_room']=$return_form->class_room;
  $data['class_address']=$return_form->class_address;
  $data['class_city']=$return_form->class_city;
  $data['class_state']=$return_form->class_state;
  $data['class_code']=$return_form->class_code;
  $data['class_phone']=$return_form->class_phone;
  $data['class_description']=$return_form->class_description;

  $data['class_name']=$this->Create_model->getData('tbl_classname');
  $data['class_instructor']=$this->Create_model->getData('tbl_instructors');
  $data['class_location']=$this->Create_model->getData('tbl_classlocations');
  $data['all_class']=$this->Create_model->getAll();
  $this->load->view('admin/class-create',$data);
}

/*Delete Form*/

function delete_form($form_id="")
{
  if($form_id!="")
  {
    $delete_class = $this->Create_model->deleteForm('tbl_classform','form_id',$form_id); /*Delete Class By ID*/
    if($delete_class)
    {
      $this->session->set_flashdata('success_msg','Class Deleted Successfully');
      redirect('admin/create-class');
    } else {
      $this->session->set_flashdata('error_msg','Error Occur');
      redirect('admin/create-class');   
    }
  }
  elseif(!empty($this->input->post('check_id')))
  { 
    $action = $this->input->post('action');
    $check_id = array();
    $check_id = $this->input->post('check_id');

    if($action=='delete') /*Delete Multiple Classes*/
    {
      foreach ($check_id as $key => $value) 
      {
        $delete_class = $this->Create_model->deleteForm('tbl_classform','form_id',$value);
      }
      if($delete_class)
      {
        $this->session->set_flashdata('success_msg','Records Deleted Successfully');
        redirect('admin/create-class');
      } else {
        $this->session->set_flashdata('error_msg','Error Occur');
        redirect('admin/create-class');   
      }
    }
    elseif ($action=='duplicate') /*Duplicate Class*/
    {
     foreach ($check_id as $key => $value) 
     {
      $duplicate_class = $this->Create_model->returnForm($value);
      $form_data = array('class_id'=>$duplicate_class->class_id,
       'class_seats'=>$duplicate_class->class_seats,
       'remain_seats'=>$duplicate_class->class_seats,
       'instr_id'=>$duplicate_class->instr_id,
       'class_hours'=>$duplicate_class->class_hours,
       'class_registrants'=>$duplicate_class->class_registrants,
       'class_price_m'=>$duplicate_class->class_price_m,
       'class_price_m2'=>$duplicate_class->class_price_m2,
       'class_price_n'=>$duplicate_class->class_price_n,
       'class_price_n2'=>$duplicate_class->class_price_n2,
       'class_price_s'=>$duplicate_class->class_price_s,
       'class_day'=>$duplicate_class->class_day,
       'calss_start_date'=>$duplicate_class->calss_start_date,
       'calss_end_date'=>$duplicate_class->calss_end_date,
       'calss_start_time'=>$duplicate_class->calss_start_time,
       'calss_end_time'=>$duplicate_class->calss_end_time,
       'locat_id'=>$duplicate_class->locat_id,
       'class_room'=>$duplicate_class->class_room,
       'class_address'=>$duplicate_class->class_address,
       'class_city'=>$duplicate_class->class_city,
       'class_state'=>$duplicate_class->class_state,
       'class_code'=>$duplicate_class->class_code,
       'class_phone'=>$duplicate_class->class_phone,
       'class_description'=>$duplicate_class->class_description);
      $data = $this->Create_model->insertRow('tbl_classform',$form_data);
    }
    if ($data)
    {
      $this->session->set_flashdata('success_msg','Duplicated Successfully');
      redirect('admin/create-class');
    } else {
      $this->session->set_flashdata('error_msg','Error Occur');
      redirect('admin/create-class');   
    }
  }
}
else
{
  $this->session->set_flashdata('error_msg','Select Records First.');
  redirect('admin/create-class');
}

} 

}

?>
