<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Report_controller 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('reports_custome_script.js');
        // load the language files
        // load the category model
        $this->load->model('ReportModel');
        $this->load->model('UsersModel');
        $this->load->model('pdf_model');
        //load the form validation
        $this->load->library('form_validation');
        // set constants
        define('REFERRER', "referrer");
        define('THIS_URL', base_url('admin/reports'));
        define('DEFAULT_LIMIT', 10);
        define('DEFAULT_OFFSET', 0);
        define('DEFAULT_SORT', "last_name");
        define('DEFAULT_DIR', "asc");
    }

    function index() {
        $this->add_css_theme('sweetalert.css')->add_js_theme('sweetalert-dev.js')->add_js_theme('bootstrap-notify.min.js')->set_title("Report List");
        $data = $this->includes;
        $content_data = array('states' => $this->UsersModel->get_all_states(),'facilities' => $this->UsersModel->get_all_facilities(),'districts' => $this->UsersModel->get_all_districts(),'quizes' => $this->UsersModel->get_all_quizes());
        $data['content'] = $this->load->view('admin/reports/list', $content_data, TRUE);
        $this->load->view($this->template, $data);
    }


    public function reportsAjax()
	{
		$data = $_POST;
        $columns = [];
        $page = $data['draw'];
        $limit = $data['length'];
        $offset = $data['start'];
        $searchVal = $data['search']['value'];
        $sortColIndex = $data['order'][0]['column'];
        $sortBy = $data['order'][0]['dir'];
        $s_id = $data['s_id'];
        $d_id=$data['d_id'];
        $f_id=$data['f_id'];
        $q_id=$data['q_id'];

        $Total = $this->ReportModel->reportsCount($searchVal, $s_id, $d_id, $f_id, $q_id);


        if ($Total) {
            $result = $this->ReportModel->reportsAjax($searchVal, $sortColIndex, $sortBy, $limit, $offset, $s_id, $d_id, $f_id, $q_id);
            $i = $offset;
            foreach ($result as $key => $item) {
                $offset++;
                $row = [];
                // print_r($item);exit;
                array_push($row, $offset);
                array_push($row,ucwords($item['first_name']. ' '. $item['last_name']));
                array_push($row,$item['mobile']);
                array_push($row,$item['facility_name']);
                array_push($row,$item['state']);
                array_push($row,$item['district']);
                array_push($row,$item['quize_name']);
                array_push($row,$item['total_attemp']);
                array_push($row,$item['correct']);
                array_push($row,$item['total_attemp'] - $item['correct']);
                array_push($row,$item['correct']);
                                
                
                $action = "";
                // print_r($item);exit;
                $action .= '<a href="' . base_url("admin/report/summary/". $item['participant_id']) . '" data-toggle="tooltip"  title="'.lang("quiz_summary").'" class="btn btn-primary btn-action mr-1"><i class="fas fa-eye"></i></a>';
                $action .= '<a href="' . base_url("admin/pdf/". $item['participant_id']) . '" data-toggle="tooltip" title="Download Certifucate" class="btn btn-success btn-action" target="_blank"><i class="fas fa-print"></i></a>';
                
                array_push($row, $action);
                $columns[] = $row;
            }
        }
        $response = [
            'draw' => $page,
            'data' => $columns,
            'recordsTotal' => $Total,
            'recordsFiltered' => $Total,
            '$limit' => $limit,
            '$offset' => $offset,
        ];
        echo json_encode($response);
	}

    function pdf($id) {
        //echo $id;exit;
        //$data = $this->core_model->pdfData($date);
        $data = array();
        $this->pdf_model->downloadCertificate($data);
        
    }



}