<?php
/*ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);*/
ini_set('memory_limit', '2048M');
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Archanai_namelist_report extends REF_Controller {
    public function __construct() {
        parent::__construct();
		$this->load->helper(array('form', 'url'));
        $this->load->library('upload');
        $this->load->model('reports/Archanai_namelist_report_model', '', TRUE);
        $this->uri_key_index = 0;
    }
    
    public function index() {
        $data = array(); 

        if (!empty($this->session->flashdata('success'))) 
            $data['success'] = $this->session->flashdata('success');
        else 
            $data['error'] = $this->session->flashdata('error');

        
        $data['category'] = $this->Archanai_namelist_report_model->category();
        $data['category_items'] = array();
        $data['type'] = '';
        // echo "<pre>"; print_r($data); exit;
        if((!empty($this->input->post('submit'))) && ($this->input->post('submit') == 'Submit' || $this->input->post('submit') == 'Print Data' || $this->input->post('submit') == 'Pdf Data' || $this->input->post('submit') == 'Excel Data')) {
		    $start_date 		= $this->input->post('start_date');
            $end_date 			= $this->input->post('end_date');
			$category_id	 		= $this->input->post('category_id');
            $item_id			= $this->input->post('item_id');
            if(!empty($category_id))  {
                $data['category_items'] = $this->Archanai_namelist_report_model->get_items($category_id);
            }
            if(!empty($item_id))  {
                $item = $this->Archanai_namelist_report_model->get_by_item($item_id);
                $data['type'] = $item->name;
            }
        }else{
			$start_date 		= '';
            $end_date 			= '';
			$category_id	 		= '';
            $item_id			= '';
		}		
       		
        $data['start_date'] 	= $start_date;
        $data['end_date'] 		= $end_date;
		$data['category_id'] 		= $category_id;
        $data['item_id'] 	= $item_id;
		
		$data['results'] = $this->Archanai_namelist_report_model->get_data($start_date,$end_date,$category_id,$item_id);
		// echo '<pre>'; print_r($data['results']); exit;
		$data['pdftype'] 		= 'Pdf Data';	
		$data['exceltype'] 		= 'Excel Data';
		
		$actions 				= $this->actions();
        $data['form_action'] 	= 'archanai_namelist_report';
        $data['title']			= !empty($data['type']) ? $data['type'] : 'Archanai Name List Report';
        
      if($this->input->post('submit') == 'Print Data' ){
        $body 				= array();
        $results = $data['results'];

        $jj = 0;
        foreach($results as $key=>$row) {
             $name = (!empty($row['bar_name_1'])) ? '<span class="lang-tamil">' . $row['bar_name_1'] . '</span>' : 'N/A';
             $star = $row['star_name1'];
             $name1 = (!empty($row['bar_name_2'])) ? '<span class="lang-tamil">' . $row['bar_name_2'] . '</span>' : 'N/A';
             $star1 = $row['star_name2'];
             $name2 = (!empty($row['bar_name_3'])) ? '<span class="lang-tamil">' . $row['bar_name_3'] . '</span>' : 'N/A';
             $star2 = $row['star_name3'];
             
             if(!empty($row['bar_name_1']) || !empty($row['bar_name_2']) || !empty($row['bar_name_3'])) {             
                $sl = ++$jj;
                $body[] = array($sl, $name, $star, $name1, $star1, $name2, $star2); 
             }
        }

            $data['dataType'] = $this->input->post('submit');
            $footer_cnt	= '<div style="width: 100%; float: right;">
                <div style="float: left; clear: both; width:33.3%;">
                    <div>&nbsp;</div>
                    <div>&nbsp;</div>
                    <div>&nbsp;</div>
                    <div><b style="font-size: 15px;">Verified By</b></div>
                    <div>&nbsp;</div>
                    <div>&nbsp;</div>
                    <div>&nbsp;</div>
                    <div>-----------------------------------------</div>
            </div>
            <div style="float: left; width:33.3%;">
                    <div>&nbsp;</div>
                    <div>&nbsp;</div>
                    <div>&nbsp;</div>
                    <div><b style="font-size: 15px;">Approved By</b></div>
                    <div>&nbsp;</div>
                    <div>&nbsp;</div>
                    <div>&nbsp;</div>
                    <div>-----------------------------------------</div>
            </div>
            <div style="float: left; width:33.3%;">
                    <div>&nbsp;</div>
                    <div>&nbsp;</div>
                    <div>&nbsp;</div>
                    <div><b style="font-size: 15px;">Confirmed By</b></div>
                    <div>&nbsp;</div>
                    <div>&nbsp;</div>
                    <div>&nbsp;</div>
                    <div>-----------------------------------------</div>
            </div>
        </div>';

        $args = array(
            'type'      => '10', // Default = 1 // Normal (Double print), '2' => Kattalai Archanai,   '3' = Hall Booking | Wedding Hall, '4' => 'Madapalli', '5' => Archanai, '6' => Common
            'rec_no'    => '',
            'title'     => 'Archanai Name List Report',
            'start_date' => $start_date,
            'end_date' => $end_date,
            'amount'    => array(''),
            'copy'      => FALSE,
            //'footer_txt'=> $footer_cnt,

            'data'      => array(
            // Type = 6
            'tables' => array(
                #'head'  => array('Receipt No', 'Name', 'Quantity', 'Unit Price',(($general->gst > 0) ? ('Before GST', 'GST Amount', 'Total') : 'Total')),
                'head'  => array('S.No', 'Name', 'Star', 'Name1', 'Star1','Name2', 'Star2'),
                'body'  => $body,
            ),

            // 'sub_tables' => array(
            //     'head'  => array(''),
            //     'body'  => $sub_body,
            //     'total'  => $sub_total
            // ),
            ),
        );

        // echo '<pre>';print_r($args);die;
        print_rec($args);
        redirect('archanai_namelist_report','refresh');
            // if( IS_KSK ) :
            //     $html = $this->load->view('reports/archanai_namelist/pdf', $data, TRUE);
            //     print_rec($html);
            //     redirect('archanai_namelist_report','refresh');
            // else :
            //     $this->render($data, 'reports/archanai_namelist/pdf');
            // endif;
        } else if ($this->input->post('submit') == 'Pdf Data') {
            $data['dataType']	= $this->input->post('submit');
			$user_data 			= get_session_userdata();
			
            if (IS_KSK) :
                $html = $this->load->view('reports/archanai_namelist/pdf', $data, TRUE);
                $args = array();
                if (!is_array($args)) :
                    $is_html = TRUE;
                    $args = array('raw_html' => $args, 'copy' => FALSE);
                endif;
                $type = !empty($args['type']) ? $args['type'] : '1';
                define('IS_SHORT', ($type == '5' || $args['short'] ? TRUE : FALSE));
                define('WIDTH', IS_SHORT ? '287' : '1000'); //'562');
                // $args['is_copy'] = ($type == '1' && !isset($args['copy'])) || $args['copy'] ? TRUE : FALSE;
                $args['is_copy'] = (( $type == '12' || $type == '13' || $type == '14' || $type == '15' || $type == '16' || $type == '17') && !isset($args['copy'])) || $args['copy'] ? TRUE : FALSE;
                $args['sepr'] = '&nbsp;/&nbsp;';
                $q = $this->db->get_where(TBL_SETTINGS_BASIC);
                $args['settings'] = $q->row_array();
                $settings=$args['settings'];
                $q = $this->db->get_where(TBL_SETTINGS_DESIGN);
                $args['settings'] = array_merge($args['settings'], $q->row_array());
//                print_r($args);
               // $header = $this->load->view('templates/print/pdfheader', $args, TRUE);
                $logo_url = !empty($args['settings']['logo']) && file_exists(DIR_COMPANY_LOGO. $args['settings']['logo']) ? DIR_COMPANY_LOGO . $args['settings']['logo'] : 'assets/images/logo.png';
              // $htmlall ='<div class="clear" style="clear:both;height:9em"></div>';
				$search_date=$date;               
                $htmlall ='<table width="100%" style="font-family:Verdana, Geneva, sans-serif">';
                $htmlall .='<tr>';
                $htmlall .=' <td width="5%"><div style="min-width: 115px;">';
                $htmlall .='<img src="'.$logo_url.'" style="width:100px" />';
                $htmlall .='</div></td>';
                $htmlall .=' <td width="80%" align="center">';
                $htmlall .=' <table border="0" width="100%">';
                $htmlall .=' <tr>';
                $htmlall .=' <td align="center" style="font-size:14px;">';
                $htmlall .=$settings['temple_name'];
                $htmlall .='</tr>';
                $htmlall .='</table>';
                $htmlall .='</td>';
                $htmlall .='<tr><td colspan="3"><hr></td></tr>';
                $htmlall .='</table>';
                $htmlall .='<table width="100%" border="0" width="100%" cellpadding="5" style="font-family:Verdana, Geneva, sans-serif">';
                $htmlall .='<tr>';
                $htmlall .='<td width="25%">&nbsp;</td>';
                $htmlall .='<td align="center" width="50%" style="font-weight:bold; font-size:14px;">';
                $htmlall .=$data['type'];
                $htmlall .='<td width="25%" style="font-size:13px;">';               
				$htmlall .='Printed On:';
                $htmlall .=$user_data['name']."-".date("Y-m-d H:i:s");
                $htmlall .='</td>';
                $htmlall .=' </tr>';
                $htmlall .=' </table>';     
				
                $html = $this->load->view('reports/archanai_namelist/pdf', $data, TRUE);
                echo $htmlall; exit;
                $filename = "archanai_namelist_report_pdf" . date("Y-m-d");
                $pdfFilePath = date('d-m-Y').".pdf";
                $this->load->library('m_pdf');
                $pdf = $this->m_pdf->load();
                $pdf->SetAutoFont(AUTOFONT_ALL);
				//                $pdf->AddPage("L");
				 $pdf->AddPage('L', // L - landscape, P - portrait
				'', '', '', '',
				5, // margin_left
				5, // margin right
				5, // margin top
				5, // margin bottom
				3, // margin header
				3);
            	unlink(FCPATH.'uploads/archanai_namelist_pdf/');
                $pdf->setFooter('{PAGENO}');
              	$pdf->WriteHTML($html);
               	$pdf->Output(FCPATH.'uploads/archanai_namelist_pdf/'.$pdfFilePath, "F");
               	$_SESSION['pdf_devotee']="success";
               	$_SESSION['pdf_devotee_file']=$pdfFilePath;
                redirect("archanai_namelist_report","refresh");
                
               //exit(0);
            else :
                $this->render($data, 'reports/archanai_namelist/pdf');
            endif;
        } elseif(((!empty($this->input->post('submit'))) && ($this->input->post('submit') == 'Excel Data'))) {
		    $this->load->view('reports/archanai_namelist/excel', $data);
        } else {
			$data['service_menus'] = [];//$this->devotee_details_report_model->list_service_menus();
            $this->render($data, 'reports/archanai_namelist/list');
        }
    }
	
	public function get_item_list(){
		$cat_id 		= $_POST['catId'];
		$item_list 	= $this->Archanai_namelist_report_model->get_items($cat_id);
		$html			= '<option value="">Select Sub Category</option>';		
		if($item_list){
			foreach($item_list as $item){
				$html .= '<option value="'.$item['id'].'">'.$item['name'].'</option>';
			}
		}		
		echo $html;
	}
   
} 
?>