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

class c_exports extends CI_Controller {
	function __construct(){
		parent::__construct();		
		$this->load->model('m_masters');
        $this->load->helper(array('url','html','form'));
        $this->load->library('session');
		ini_set('max_execution_time', 0); 
	}
	
	public function reports()
	{
		require_once APPPATH . "/third_party/PHPExcel.php";
		$object= new PHPExcel();
		$object_writer = PHPExcel_IOFactory::createWriter($object, 'Excel5');
	    header('Content-Disposition: attachment;filename="gablo.xls"');
		header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

		$errmsg = "";
     	
     	$datefrom = $this->input->post("datefrom");
     	$dateto = $this->input->post("dateto");
     	$reporttype = $this->input->post("reporttype");

     	if($datefrom == "")
     	{
     		$errmsg = $errmsg . "<br>". "Please fill date from!";
     	}
     	if($dateto == "")
     	{
     		$errmsg = $errmsg . "<br>". "Please fill date to!";
     	}
     	if ($reporttype == "blank")
     	{
     		$errmsg = $errmsg . "<br>". "Please fill Report Type!";
     	}
     	if ($errmsg == "")
     	{
	     	$datefrom = date('Y-m-d', strtotime($datefrom));
	     	$dateto = date('Y-m-d', strtotime($dateto));
	     	
	     	if($reporttype == "rawreport")
	       	{
	       		$table_columns = array("PhoneNo", "Username","Response","Table Data", "Creator Operator Group","Creator", "Upload Date");
	      		$column = 0;
	      		// $querystring = "select phoneno,username,response,tabledata,creatoroperatorgroup,creator,uploaddate from work where uploaddate > '" .$datefrom . " 00:00:00' and uploaddate < '" .$dateto ." 23:59:59'";
	     		$work = $this->m_masters->reportrawreport($datefrom,$dateto)->result();

	     		
		       	$object->setActiveSheetIndex(0);
		       	$object->getDefaultStyle()->getNumberFormat()->setFormatCode(
		        PHPExcel_Style_NumberFormat::FORMAT_TEXT
	    		);
			     foreach($table_columns as $field){

			        $object->getActiveSheet()->setCellValueByColumnAndRow($column, 1, $field);

			        $column++;
			    }
		      	$excel_row = 2;
		      	foreach($work as $row){

		        $object->getActiveSheet()->setCellValueByColumnAndRow(0, $excel_row,$row->phoneno);
		        $object->getActiveSheet()->setCellValueByColumnAndRow(1, $excel_row, $row->username);
		        $object->getActiveSheet()->setCellValueByColumnAndRow(2, $excel_row, $row->response);
		        $object->getActiveSheet()->setCellValueByColumnAndRow(3, $excel_row, $row->tabledata);
		        $object->getActiveSheet()->setCellValueByColumnAndRow(4, $excel_row, $row->creatoroperatorgroup);
		        $object->getActiveSheet()->setCellValueByColumnAndRow(5, $excel_row, $row->creator);
		        $object->getActiveSheet()->setCellValueByColumnAndRow(6, $excel_row, $row->uploaddate);
		        $excel_row++;
	      		}
	      	}

	      	if($reporttype == "calloperator")
	       	{
	       		$table_columns = array("Operator", "Operator Total Call","Operator Total Call Distinct","Group Total Call", "Group Total Call Distinct","User Created", "Interested", "Not Active", "Not Picked up","Not Interested","Invalid Number");
	      		
	      		$column = 0;
	     		$work = $this->m_masters->reportcalloperator($datefrom,$dateto)->result();
		       	$object->setActiveSheetIndex(0);
		       	$object->getDefaultStyle()->getNumberFormat()->setFormatCode(
		        PHPExcel_Style_NumberFormat::FORMAT_TEXT
	    		);
			     foreach($table_columns as $field){

			        $object->getActiveSheet()->setCellValueByColumnAndRow($column, 1, $field);

			        $column++;
			    }
		      	$excel_row = 2;
		      	foreach($work as $row){

		        $object->getActiveSheet()->setCellValueByColumnAndRow(0, $excel_row,$row->Creator);
		        $object->getActiveSheet()->setCellValueByColumnAndRow(1, $excel_row, $row->total_call_operator);
		        $object->getActiveSheet()->setCellValueByColumnAndRow(2, $excel_row, $row->total_call_operator_distinct);
		        $object->getActiveSheet()->setCellValueByColumnAndRow(3, $excel_row, $row->total_group_call);
		        $object->getActiveSheet()->setCellValueByColumnAndRow(4, $excel_row, $row->total_group_call_distinct);
		        $object->getActiveSheet()->setCellValueByColumnAndRow(5, $excel_row, $row->UserCreated);
		        $object->getActiveSheet()->setCellValueByColumnAndRow(6, $excel_row, $row->interested);
		        $object->getActiveSheet()->setCellValueByColumnAndRow(7, $excel_row,$row->not_active);
		        $object->getActiveSheet()->setCellValueByColumnAndRow(8, $excel_row, $row->not_picked_up);
		        $object->getActiveSheet()->setCellValueByColumnAndRow(9, $excel_row, $row->not_interested);
		        $object->getActiveSheet()->setCellValueByColumnAndRow(10, $excel_row, $row->invalid_number);
		        $excel_row++;
	      		}
	      	}
	      	
		        

	      
	      // $object_writer->save('C://Users/DELL/Downloads/'.$excelname);
	      $object_writer->save('php://output');
	      $errmsg = "Data Exported";

	    }
	     echo $errmsg;
	    // echo "asdf";
	}


}