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

class Holly_day extends CI_Controller {

	function __construct() 
	{
        error_reporting(0);
        parent::__construct();
        $this->load->model('CommonModel');
        $this->load->helper('Security');
        $this->load->library('form_validation');

       
    }

	public function index()
	{ 
		if(isAdmin())
		{ 
			$data['recordSet']=$this->db->get('holly_day')->result_array();
			$data['content'] = 'Holly_day/Listing';
			$this->load->view('layout_home', $data);
		}
	}
	public function cat_edit_details()
	{ 
		if(isAdmin())
		{ 
			$input_data = $this->input->post();
            $input_data = $this->security->xss_clean($input_data);
			$cat_details=$this->db->where('h_id',$input_data['h_id'])->get('holly_day')->result_array();
		?>
         <div class="form-group">
          <div class="row">
              <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                  <label>Date</label>
                  <input type="date" name="date" id="date" class="form-control" required="" value="<?php echo $cat_details[0]['date']; ?>"  aria-required="true">
                  <br>
              </div>
          </div>
          </div>

          <div class="form-group">
          <div class="row">
              <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                  <label>Purpose</label>
                  <input type="text" name="purpose" id="purpose" class="form-control" required="" value="<?php echo $cat_details[0]['purpose']; ?>"  aria-required="true">
                  <input type="hidden" name="h_id" id="h_id" class="form-control" required="" value="<?php echo $cat_details[0]['h_id']; ?>"  aria-required="true">
                  <br>
              </div>
          </div>
          </div>

		<?php
		}
	}

	public function add()
	{
		if(isAdmin())
		{
			$data['content'] = 'Holly_day/Add';
			$this->load->view('layout_home', $data);
		}
	}

	public function save()
	{
		if(isAdmin())
		{
			$input_data = $this->input->post();
            $input_data = $this->security->xss_clean($input_data);
            $duplicate_data=$this->db->where('purpose', $input_data['purpose'])->get('holly_day')->result_array();
            if(empty($duplicate_data))
            {
            
        	$insert_data['purpose'] = $input_data['purpose'];
        	$insert_data['date'] = date('Y-m-d', strtotime($input_data['date']));
        	
            $this->db->insert('holly_day',$insert_data);
            echo 'success';
        	} else{
        		echo 'failed';
        	}
		}
	}

	public function Edit_data()
	{
	    if(isAdmin())
	    {  
	        	$input_data = $this->input->post();
	            $input_data = $this->security->xss_clean($input_data);
	            $duplicate_data=$this->db->where('purpose', $input_data['purpose'])->where('h_id !=', $input_data['h_id'])->get('holly_day')->result_array();
	            if(empty($duplicate_data))
	            {
	            $edit_data['purpose'] = $input_data['purpose'];
	            $edit_data['date'] = date('Y-m-d',strtotime($input_data['date']));
	        	
	            $this->db->where('h_id',$input_data['h_id']);
	            $this->db->update('holly_day',$edit_data);
	            echo 'success';
	        	} else{
	        		echo 'failed';
	        	}
	    }      
	}
	public function Delete()
    {
        if(isAdmin())
        {
        	$input_data = $this->input->post();
        	$this->db->where('h_id', $input_data['id']);
			$this->db->delete('holly_day');
        }
    }
    public function export()
	{
		if(isAdmin())
		{ 
			$project_array=$this->db->get('holly_day')->result_array();
			header("Content-type: application/csv");
		    header("Content-Disposition: attachment; filename=\"Holly_day-".date('d-m-Y').".csv\"");
		    header("Pragma: no-cache");
		    header("Expires: 0");

		     $handle = fopen('php://output', 'w');
			$excel_heding=array();
			array_push($excel_heding, 'SL', 'Purpose', 'Date');
			fputcsv($handle, $excel_heding);
			foreach ($project_array as $key => $project_data) {
			$export_req = array();
			
			array_push($export_req, $key+1, $project_data['purpose'],  $project_data['date']);

			//print_r($call_det);die;
			fputcsv($handle, $export_req);
			} 
			 fclose($handle);
		     exit;
		
		}
	}
	public function import(){

        $data = array();

        $memData = array();

                $insertCount = $updateCount = $rowCount = $notAddCount = 0;

                if(is_uploaded_file($_FILES['import_data']['tmp_name'])){

                    $this->load->library('CSVReader');

                    $csvData = $this->csvreader->parse_csv($_FILES['import_data']['tmp_name']);

                    if(!empty($csvData)){

                        foreach($csvData as $row){ 
                        	$rowCount++;
                        	$duplicate_data=$this->db->where('purpose', $row['Purpose'])->get('holly_day')->result_array();
				            if(empty($duplicate_data))
				            {
							$insert_data['purpose'] = $row['Purpose'];
							$insert_data['date'] = date('Y-m-d', strtotime($row['Date']));
							
							$this->db->insert('holly_day',$insert_data);	
							}

                          $insertCount++;  

                        }
                        $notAddCount = ($rowCount - ($insertCount + $updateCount));
                    }
                }

    }

}