<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Sessions extends Admin_Controller {
    function __construct() {
        parent::__construct();
        $this->add_css_theme('all.css');
        $this->add_js_theme('jquery.multi-select.min.js');
        $this->add_js_theme('session_custome_script.js');
        // load the language files
        // load the category model
        $this->load->model('SessionModel');
        // $this->load->model('CategoryModel');
        //load the form validation
        $this->load->library('form_validation');
        // set constants
        define('REFERRER', "referrer");
        define('THIS_URL', base_url('admin/session'));
        define('DEFAULT_LIMIT', 10);
        define('DEFAULT_OFFSET', 0);
        define('DEFAULT_SORT', "last_name");
        define('DEFAULT_DIR', "asc");
    }

    

    function index() {
        $this->add_external_js( base_url("/{$this->settings->themes_folder}/admin/js/dataTables.buttons.min.js"));
        $this->add_external_js( base_url("/{$this->settings->themes_folder}/admin/js/buttons.flash.min.js")); 
        $this->add_external_js( base_url("/{$this->settings->themes_folder}/admin/js/jszip.min.js")); 
        $this->add_external_js( base_url("/{$this->settings->themes_folder}/admin/js/pdfmake.min.js")); 
        $this->add_external_js( base_url("/{$this->settings->themes_folder}/admin/js/vfs_fonts.js")); 
        $this->add_external_js( base_url("/{$this->settings->themes_folder}/admin/js/buttons.html5.min.js")); 
        $this->add_external_js( base_url("/{$this->settings->themes_folder}/admin/js/buttons.print.min.js")); 
        $this->add_external_css( base_url("/{$this->settings->themes_folder}/admin/css/buttons.dataTables.min.css")); 
        $this->add_css_theme('sweetalert.css')->add_js_theme('sweetalert-dev.js')->add_js_theme('bootstrap-notify.min.js')->set_title('Session List');

        $data = $this->includes;
        $content_data = array();
        // load views
        $data['content'] = $this->load->view('admin/session/list', $content_data, TRUE);
        $this->load->view($this->template, $data);
    }
 
    function add() 
    {
        $this->form_validation->set_rules('description','description','required|trim');
        $this->form_validation->set_rules('name','name','required|trim');
        $this->form_validation->set_rules('video_link','video link','trim|required|valid_url');
        // $this->form_validation->set_rules('assessment_link','Assessment link','trim|required|valid_url');
        $this->form_validation->set_rules('duration_min','Duration Min','required|numeric');
        $this->form_validation->set_rules('category', 'Question Language', 'required|trim');
        // $this->form_validation->set_rules('testId', 'Test List', 'required|trim');
        if ($this->form_validation->run() == false) 
        {
            $this->form_validation->error_array();
        } 
        else 
        {
            action_not_permitted();
            if($this->SessionModel->insert_session($this->input->post()))
            {                
            $this->session->set_flashdata('message', lang('admin_record_added_successfully'));                  
            }
            else
            {
                $this->session->set_flashdata('error', lang('admin_error_adding_record')); 
            }
             redirect(base_url('admin/session'));
        }
        $this->set_title("Add Session");
        $data = $this->includes;
        $content_data['question_lang'] = $this->SessionModel->get_all_question_lang();
        $content_data['test_list'] = $this->SessionModel->get_all_test();
        
       // $content_data = array();
        $data['content'] = $this->load->view('admin/session/form', $content_data, TRUE);
        $this->load->view($this->template, $data);
    }

    function update($id) {

        $this->form_validation->set_rules('name','name','required|trim');
        $this->form_validation->set_rules('description','description','required|trim');
        $this->form_validation->set_rules('video_link','video link','trim|required|valid_url');
        // $this->form_validation->set_rules('assessment_link','Assessment link','trim|required|valid_url');
        $this->form_validation->set_rules('category', 'Question Language', 'required|trim');
        $this->form_validation->set_rules('duration_min','Duration Min','required|numeric');
        $session = $this->db->select('*')->from('sessions')->where('id',$id)->get()->row_array();       
        if(empty($session)){
            show_404();
        }
        if ($this->form_validation->run() == false){
             $this->form_validation->error_array();
        } 
        else{
                 action_not_permitted();
                 $data =  array('name' => $this->input->post('name'),
                        'video_link' => $this->input->post('video_link'),
                        // 'assessment_link' => $this->input->post('assessment_link'),
                        'duration_min' => $this->input->post('duration_min'),
                        'category' => $this->input->post('category'),
                        'testId' => $this->input->post('testId'),
                        'description' => $this->input->post('description'));
                if($this->db->where('id', $id)->update('sessions',$data)){                
                 $this->session->set_flashdata('message', lang('admin_record_updated_successfully'));                  
                }
                else{
                    $this->session->set_flashdata('error', lang('admin_error_adding_record')); 
                }
                redirect(base_url('admin/session'));
            
            }
            $this->set_title('Edit Session');
            $data = $this->includes;
            $content_data['question_lang'] = $this->SessionModel->get_all_question_lang();
            $content_data['test_list'] = $this->SessionModel->get_all_test();
            $content_data['session'] = $session;
            // $content_data = array('session' => $session);
            $data['content'] = $this->load->view('admin/session/update', $content_data, TRUE);
            $this->load->view($this->template, $data);
    }

    

    function session_list() {
        $list = $this->SessionModel->get_session();
        $data = array();
        $no = $_POST['start'];
        foreach ($list as $session) {
            $no++;
            $row = array();
            $row[] = $no;
            $row[] = ucfirst($session->name);
            $row[] = ucfirst($session->description);
            
            $row[] = $session->video_link;
            // $row[] = $session->assessment_link;
         
            $row[] = ucfirst($session->testId);
            $row[] = ucfirst($session->category);
           
            $btn = '';
            $btn    .= '<a href="' . base_url("admin/session/update/" . $session->id) . '" data-toggle="tooltip" title="'.lang("admin_copy_record").'" class="btn btn-primary btn-action mr-1"><i class="fas fa-pencil-alt"></i></a>

            <a href="' . base_url("admin/session/delete_video/" . $session->id) . '" data-toggle="tooltip"  title="'.lang("admin_delete_record").'" class="btn btn-danger btn-action mr-1 cat_delete"><i class="fas fa-trash"></i></a>';
            
            if($session->status == '0'){
                 $btn .= '<a href="' . base_url("admin/session/play_video/" . $session->id) . '" data-toggle="tooltip"  title="play video" class="btn btn-success btn-action mr-1 play_video"><i class="fas fa-play"></i></a>';
            }
            
            if($session->status == '1'){
                $btn .= '<a href="' . base_url("admin/session/pause_video/" . $session->id) . '" data-toggle="tooltip"  title="pause video" class="btn btn-secondary btn-action mr-1 pause_video"><i class="fas fa-pause"></i></a>';
            }
           
            
            $row[] = $btn;
            $data[] = $row;
        }
        $output = array("draw" => $_POST['draw'], "recordsTotal" => $this->SessionModel->count_all(), "recordsFiltered" => $this->SessionModel->count_filtered(), "data" => $data);
        //output to json format
        echo json_encode($output);
    }


    function play_video($id){
        action_not_permitted();
        $id_exits = $this->SessionModel->check_status($id,'0');
        if(!empty($id_exits)){
            $this->SessionModel->play_video($id);
        }
        $this->session->set_flashdata('message', lang('admin_record_updated_successfully'));
        redirect(base_url('admin/session'));  
    }

    function pause_video($id){
        action_not_permitted();
        $id_exits = $this->SessionModel->check_status($id,'1');
        if(!empty($id_exits)){
            $this->SessionModel->pause_video($id);
        }
        $this->session->set_flashdata('message', lang('admin_record_updated_successfully'));
        redirect(base_url('admin/session'));
    }

     function delete_video($id){
        action_not_permitted();
        $this->SessionModel->delete_video($id,'1');
        $this->session->set_flashdata('message', lang('admin_record_delete_successfully'));
        redirect(base_url('admin/session'));
    }

    
    
}
