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

class Import_ctrl extends user_index {
 
	
	public function __construct(){ 
		parent::__construct();
		$data = array();
		$this->load->model('admin_model');	
	}
	
	/*----------------- load import page -----------------*/
	public function import_details(){	
		$data['sdata'] 				= array();
		$data['subview'] 			= 'import/import_details';
		$data['active_cls'] 		= 'imppo_cls';
		$data['sdata']['ul_list'] 	= $this->admin_model->get_result_array('users_list');				
		$this->load->view('common/sidepanel',$data);
	}
	
	public function import_customer_details() {
		$this->load->library('excel');        
        $path 						= 'public_html/import_files/'; 
        $config['upload_path'] 		= $path;
        $config['allowed_types'] 	= 'xlsx|xls';
        $config['remove_spaces'] 	= TRUE;
        // $this->upload->initialize($config);
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('csv_file')) {
                $error = array('error' => $this->upload->display_errors());
        } else {
            $data = array('upload_data' => $this->upload->data());
        }
        
        if (!empty($data['upload_data']['file_name'])) {
            $import_xls_file = $data['upload_data']['file_name'];
            } else {
            $import_xls_file = 0;
        }
        $inputFileName 		= $path . $import_xls_file;
        try {
        	$inputFileType 	= PHPExcel_IOFactory::identify($inputFileName);
        	$objReader 		= PHPExcel_IOFactory::createReader($inputFileType);
            $objPHPExcel 	= $objReader->load($inputFileName);
        } catch (Exception $e) {
            die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME)
                    . '": ' . $e->getMessage());
        }
        $allDataInSheet 	= $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
            
        $arrayCount 		= count($allDataInSheet);
        $flag 				= 0;
        // $makeArray = array('First_Name' => 'First_Name', 'Last_Name' => 'Last_Name', 'Email' => 'Email', 'DOB' => 'DOB', 'Contact_NO' => 'Contact_NO');
        $makeArray 			= array();
        $SheetDataKey 		= array();
        foreach ($allDataInSheet as $in_sheet_key=>$in_sheet_res_row) {
			if($in_sheet_key=='1'){
				$makeArray 	= $in_sheet_res_row;
			}else{
				$sheet_d_r = array();
				foreach ($in_sheet_res_row as $key => $value) {
					$key_val = 'cl_'.$makeArray[$key];
					$sheet_d_r[$key_val] = $value;	
				}
				$SheetDataKey[] = $sheet_d_r;
			}
		}
		if(sizeof($SheetDataKey)!=0){
			foreach($SheetDataKey as $d_sheet_key=>$d_sheet_res){
				$res_d = $d_sheet_res;					
				$this->db->insert('customer_list', $res_d); 		
				$id = $this->db->insert_id();
				$res_u['cl_code'] =  'CU-'.$id;
				$this->admin_model->update_data('customer_list','cl_id',$id,$res_u);
			}
			$this->session->set_flashdata('tos_success', 'Customer excel uploaded.');
			redirect('import_ctrl/import_details');
		}else{
			$this->session->set_flashdata('tos_error', 'Please upload correct excel');
			redirect('import_ctrl/import_details');				
		}				
	}
	

	public function import_transporter_details() {
		$this->load->library('excel');        
        $path 						= 'public_html/import_files/'; 
        $config['upload_path'] 		= $path;
        $config['allowed_types'] 	= 'xlsx|xls';
        $config['remove_spaces'] 	= TRUE;
        // $this->upload->initialize($config);
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('csv_file')) {
                $error = array('error' => $this->upload->display_errors());
        } else {
            $data = array('upload_data' => $this->upload->data());
        }
        
        if (!empty($data['upload_data']['file_name'])) {
            $import_xls_file = $data['upload_data']['file_name'];
            } else {
            $import_xls_file = 0;
        }
        $inputFileName 		= $path . $import_xls_file;
        try {
        	$inputFileType 	= PHPExcel_IOFactory::identify($inputFileName);
        	$objReader 		= PHPExcel_IOFactory::createReader($inputFileType);
            $objPHPExcel 	= $objReader->load($inputFileName);
        } catch (Exception $e) {
            die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME)
                    . '": ' . $e->getMessage());
        }
        $allDataInSheet 	= $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
            
        $arrayCount 		= count($allDataInSheet);
        $flag 				= 0;
        // $makeArray = array('First_Name' => 'First_Name', 'Last_Name' => 'Last_Name', 'Email' => 'Email', 'DOB' => 'DOB', 'Contact_NO' => 'Contact_NO');
        $makeArray 			= array();
        $SheetDataKey 		= array();
        foreach ($allDataInSheet as $in_sheet_key=>$in_sheet_res_row) {
			if($in_sheet_key=='1'){
				$makeArray 	= $in_sheet_res_row;
			}else{
				$sheet_d_r = array();
				foreach ($in_sheet_res_row as $key => $value) {
					$key_val = 'tl_'.$makeArray[$key];
					$sheet_d_r[$key_val] = $value;	
				}
				$SheetDataKey[] = $sheet_d_r;
			}
		}
		if(sizeof($SheetDataKey)!=0){
			foreach($SheetDataKey as $d_sheet_key=>$d_sheet_res){
				$res_d = $d_sheet_res;					
				$this->db->insert('transporter_list', $res_d); 		
				$id = $this->db->insert_id();
				$res_u['tl_code'] =  'TR-'.$id;
				$this->admin_model->update_data('transporter_list','tl_id',$id,$res_u);
			}
			$this->session->set_flashdata('tos_success', 'Customer excel uploaded.');
			redirect('import_ctrl/import_details');
		}else{
			$this->session->set_flashdata('tos_error', 'Please upload correct excel');
			redirect('import_ctrl/import_details');				
		}				
	}
	



	
	

	
}
