<?php
    defined('BASEPATH') or exit('No Direct Script is Allowed');
    class DashboardController extends CI_Controller
	    {
		    function __construct(){
                parent::__construct();
                    if(!$this->session->id){
                    redirect(site_url('admin/login'));
                }
            }
            
            public function index(){
                $data['servies'] = $this->db->count_all_results('tbl_package');
                $data['industreies'] = $this->db->count_all_results('tbl_industries');
                $data['banner'] = $this->db->count_all_results('tbl_banner');
                $data['testimonial'] = $this->db->count_all_results('tbl_testimonial');
                $data['partners'] = $this->db->count_all_results('tbl_partners');
                $data['blog'] = $this->db->count_all_results('tbl_blog');
                $data['contact'] = $this->db->count_all_results('tbl_contact');
                $data['enquiry'] = $this->db->count_all_results('tbl_enquiry');
               
                
                $data['contacts'] = $this->db->order_by('id','DESC')->limit(10)->get('tbl_contact')->result();
                    $this->load->view('backend/global/header');
                    $this->load->view('backend/global/menu');
                    $this->load->view('backend/dashboard',$data);
                    $this->load->view('backend/global/footer');
                    
            } 
            
            
            public function profile(){
                
                $admin_id = $this->session->id;
                
                $data['profile'] = $this->db->where('adm_id',$admin_id)->get('tbl_admin')->row();
                
                $this->load->view('backend/global/header');
                $this->load->view('backend/global/menu');
                $this->load->view('backend/profile',$data);
                $this->load->view('backend/global/footer');
            } 
            
             public function profile_update(){
                 
                 $data = array(
						'adm_name' => strtoupper($this->input->post('adm_name')),
						'adm_email' => $this->input->post('adm_email'),
						'adm_phone' => strtoupper($this->input->post('adm_phone')),
						'adm_address' => $this->input->post('adm_address'),
						'adm_facebook' => strtoupper($this->input->post('adm_facebook')),
						'adm_instagram' => $this->input->post('adm_instagram'),
						'adm_linkdin' => strtoupper($this->input->post('adm_linkdin')),
						'adm_twiitter' => $this->input->post('adm_twiitter'),
					);
				        $this->db->where('adm_id', $this->input->post('adm_id'));
						$this->db->update('tbl_admin', $data);
							if($data){
								echo json_encode(['msg' => 'success']);
								exit;
							}else{
								echo json_encode(['msg' => 'error']);
								exit;
							}
                
            }
            
           
            
            
            public function password(){
            	$this->db->where('adm_id', $this->input->post('adm_id'));
            	$this->db->where('adm_password', $this->input->post('oldpassword'));
            	$admin  = $this->db->get('tbl_admin');
		            if($admin->num_rows() > 0){
                	        $data = array(
                			    'adm_password' => $this->input->post('newpassword'),
                				
                				);
                		    $this->db->where('adm_id', $this->input->post('adm_id'));
						    $this->db->update('tbl_admin', $data);
						    
						    
                			echo json_encode(['msg' => 'success']);
                			exit;
                	}else{
                			echo json_encode(['msg' => 'oldpassword']);
                			exit;
                		}
		    }
		    
		    public function import(){
		        
               $this->load->view('backend/global/header');
                $this->load->view('backend/global/menu');
                $this->load->view('backend/import');
                $this->load->view('backend/global/footer');
		    }
		    
	    public function import_action(){
                $csvMimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain');
	    
        	    if(!empty($_FILES['importFile']['name']) && in_array($_FILES['importFile']['type'], $csvMimes)){
        		    	if(is_uploaded_file($_FILES['importFile']['tmp_name'])){
        		        	$csvFile = fopen($_FILES['importFile']['tmp_name'], 'r');
        		        	fgetcsv($csvFile);
        		        	while(($line = fgetcsv($csvFile)) !== FALSE){
        		        	    
        		        	    $state = $line[0];
        		        	    $city = $line[1];
        		        	    $area = $line[2];
        		        	    $pincode = $line[3];
        		        	
        		        	
        		        	 //   $cat_test_name = $line[0];
        		        	 //   $cat_test_department = $line[1];
        		        	 //   $cat_test_sample_type = $line[2];
        		        	 //   $cat_test_vial = $line[3];
        		        	 //   $cat_test_color = $line[4];
        		        	 //   $cat_test_TAT = $line[5];
        		        	 //   $cat_test_MRP = $line[6];
        		        	 //   $cat_test_CROP = $line[7];
        		        	 //   $cat_test_requisites = $line[9];
        		        	 //   $cat_test_location_type = $line[10];
        		        	 //   $cat_test_to = $line[11];
        		        	   
        		        	  
        		        	   
        		        	    $data = array(
        		        	        'lc_state' => $state,
        		        	        'lc_city' => $city,
        		        	        'lc_area' => $area,
        		        	        'lc_pincode' => $pincode,
        		        	        
        		        	        
        		        	        
        		        	     //   'cat_test_name' => $cat_test_name,
        		        	     //   'cat_test_department' => $cat_test_department,
        		        	     //   'cat_test_sample_type' => $cat_test_sample_type,
        		        	     //   'cat_test_vial' => $cat_test_vial,
        		        	     //   'cat_test_color' => $cat_test_color,
        		        	     //   'cat_test_TAT' => $cat_test_TAT,
        		        	     //   'cat_test_MRP' => $cat_test_MRP,
        		        	     //   'cat_test_CROP' => $cat_test_CROP,
        		        	     //   'cat_test_requisites' => $cat_test_requisites,
        		        	     //   'cat_test_location_type' => $cat_test_location_type,
        		        	     //   'cat_test_to' => $cat_test_to,
        		        	    );
        		        	    $this->db->insert('location',$data);
        		        	}
        		        	echo json_encode(['msg' => 'Uploaded successfully']);
        		    	}else{
        		    	    echo json_encode(['msg' => 'File Could not be uploaded for some technical issue! please try after some time']);
        		    	}
        	    }else{
        	        echo json_encode(['msg' => 'Invalid File Type! Please Upload a CSV File Only']);
        	    }
		    }
            
        public function export_team() {
                        $filename = "staff_" . date('Ymd_His') . ".xls";
                    
                        // Set headers to make browser download as Excel
                        header("Content-Type: application/vnd.ms-excel");
                        header("Content-Disposition: attachment; filename=\"$filename\"");
                        header("Content-Transfer-Encoding: BINARY");
                        header("Pragma: no-cache");
                        header("Expires: 0");
                    
                        // Fetch data
                        $this->db->select('
                            mbr_name,
                            mbr_email,
                            mbr_phone,
                            mbr_designation,
                            mbr_type,
                            mbr_address,
                            mbr_pincode,
                            mbr_age,
                            mbr_gender,
                            mbr_fname,
                            mbr_mname,
                            mbr_sname,
                            mbr_aadhar_number,
                            mbr_bank_name,
                            mbr_bank_account,
                            mbr_holder_name,
                            mbr_bank_ifsc,
                            mbr_branch_name,
                            mbr_cunsultation,
                            mbr_hra,
                            mbr_allawnces,
                            mbr_bonus,
                            mbr_convenyence,
                            mbr_pf,
                            mbr_fixed_gross,
                            mbr_esic,
                            mbr_pli,
                            mbr_ctc,
                            mbr_photo,
                            mbr_idproof,
                            mbr_resume,
                            mbr_apointment,
                            mbr_nda
                        ');
                        $usersData = $this->db->get('tbl_member')->result_array();
                    
                        // Start Excel table
                        echo "<table border='1'>";
                        echo "<tr>
                            <th>SR</th>
                            <th>NAME</th>
                            <th>EMAIL</th>
                            <th>PHONE</th>
                            <th>DESIGNATION</th>
                            <th>TYPE</th>
                            <th>ADDRESS</th>
                            <th>PINCODE</th>
                            <th>DOB</th>
                            <th>GENDER</th>
                            <th>FATHER_NAME</th>
                            <th>MOTHER_NAME</th>
                            <th>SPOUSE_NAME</th>
                            <th>AADHAR_NUMBER</th>
                            <th>BANK_NAME</th>
                            <th>ACCOUNT_NUMBER</th>
                            <th>ACCOUNT_HOLDER_NAME</th>
                            <th>BANK_IFSC</th>
                            <th>BANK_BRANCH_NAME</th>
                            <th>CONSULTATION</th>
                            <th>HRA</th>
                            <th>SPECIAL_ALLOWANCE</th>
                            <th>BONUS</th>
                            <th>CONVEYANCE</th>
                            <th>PF</th>
                            <th>FIXED_GROSS</th>
                            <th>ESIC</th>
                            <th>PLI</th>
                            <th>CTC</th>
                            <th>PHOTO</th>
                            <th>ID_PROOF</th>
                            <th>RESUME</th>
                            <th>APPOINTMENT_LETTER</th>
                            <th>NDA</th>
                        </tr>";
                    
                        $sr = 1;
                        foreach ($usersData as $user) {
                            
                            $bank = $user['mbr_bank_account'];
                            $bank_text = (string)$bank; // convert into a string
                            $arr = str_split($bank_text, "4"); // break string in 3 character sets
                            $bank_text_new_text = implode("-", $arr);  // implode array with comma
                            
                            $aadhar = $user['mbr_aadhar_number'];
                            $aadhar_text = (string)$bank; // convert into a string
                            $arr = str_split($aadhar_text, "4"); // break string in 3 character sets
                            $aadhar_text_new_text = implode("-", $arr);  // implode array with comma
                            
                            $mobile = $user['mbr_phone'];
                            $mobile_text = (string)$mobile; // convert into a string
                            $arr = str_split($mobile_text, "5"); // break string in 3 character sets
                            $mobile_text_new_text = implode("-", $arr);  // implode array with comma
                            
                            
                            echo "<tr>";
                            echo "<td>" . $sr++ . "</td>";
                            echo "<td>" . $user['mbr_name'] . "</td>";
                            echo "<td>" . $user['mbr_email'] . "</td>";
                            echo "<td>" . $mobile_text_new_text . "</td>";
                            echo "<td>" . $user['mbr_designation'] . "</td>";
                            echo "<td>" . $user['mbr_type'] . "</td>";
                            echo "<td>" . $user['mbr_address'] . "</td>";
                            echo "<td>" . $user['mbr_pincode'] . "</td>";
                            echo "<td>" . $user['mbr_age'] . "</td>";
                            echo "<td>" . $user['mbr_gender'] . "</td>";
                            echo "<td>" . $user['mbr_fname'] . "</td>";
                            echo "<td>" . $user['mbr_mname'] . "</td>";
                            echo "<td>" . $user['mbr_sname'] . "</td>";
                            echo "<td>" . $aadhar_text_new_text . "</td>";
                            echo "<td>" . $user['mbr_bank_name'] . "</td>";
                            echo "<td>" . $bank_text_new_text . "</td>";
                            echo "<td>" . $user['mbr_holder_name'] . "</td>";
                            echo "<td>" . $user['mbr_bank_ifsc'] . "</td>";
                            echo "<td>" . $user['mbr_branch_name'] . "</td>";
                            echo "<td>" . $user['mbr_cunsultation'] . "</td>";
                            echo "<td>" . $user['mbr_hra'] . "</td>";
                            echo "<td>" . $user['mbr_allawnces'] . "</td>";
                            echo "<td>" . $user['mbr_bonus'] . "</td>";
                            echo "<td>" . $user['mbr_convenyence'] . "</td>";
                            echo "<td>" . $user['mbr_pf'] . "</td>";
                            echo "<td>" . $user['mbr_fixed_gross'] . "</td>";
                            echo "<td>" . $user['mbr_esic'] . "</td>";
                            echo "<td>" . $user['mbr_pli'] . "</td>";
                            echo "<td>" . $user['mbr_ctc'] . "</td>";
                            echo "<td>" . "https://www.surewelldiagnostic.com/uploads/member/".$user['mbr_photo'] . "</td>";
                            echo "<td>" . "https://www.surewelldiagnostic.com/uploads/member/".$user['mbr_idproof'] . "</td>";
                            echo "<td>" . "https://www.surewelldiagnostic.com/uploads/member/".$user['mbr_resume'] . "</td>";
                            echo "<td>" . "https://www.surewelldiagnostic.com/uploads/member/".$user['mbr_apointment'] . "</td>";
                            echo "<td>" . "https://www.surewelldiagnostic.com/uploads/member/".$user['mbr_nda'] . "</td>";
                            echo "</tr>";
                        }
                    
                        echo "</table>";
                        exit;
                    }

    	
    

        
            
	}