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

class Add_Ballot_Controller extends CI_Controller {

	public function Add_Ballot_View()
	{
		$this->load->model('Add_Zone_Model');
        
        if($this->Add_Zone_Model->Show_Zones())
            {   
                $all_data['data']=$this->Add_Zone_Model->Show_Zones();
                        /*echo " "."admin ".$data[0]->name." ".$data[0]->email." ".$data[0]->type;
                        echo " "."admin ".$data[1]->name." ".$data[1]->email." ".$data[1]->type;*/

                $this->load->view('Add_Ballot_View',$all_data);
               
                     //echo " ".$all_data[0]->id;
            }
         else
         {
             echo "not inserted";
         }
    }
    
    public function Add_Ballot_Data()
	{
		$this->load->library('form_validation');
        $this->form_validation->set_rules('zone', 'zone', 'required');
        $this->form_validation->set_rules('position', 'Ballet Name', 'required|is_unique[ballot.ballot_name]');
        $this->form_validation->set_rules('date', 'Date of Voting', 'required');

        if($this->form_validation->run())
        {
            $this->load->model('Add_Ballot_Model');   
            
            $zone=$this->input->post('zone');
            $position=$this->input->post('position');
            $date=$this->input->post('date');

            $edit_data['zone']=$zone;
            $edit_data['ballot_name']=$position;
            $edit_data['date']=$date;
            $edit_data['total_votes']=0;
//print_r($this->Add_Ballot_Model->Insert_Ballot_Data($edit_data));die();

            if($this->Add_Ballot_Model->Insert_Ballot_Data($edit_data)) 
            {    
                $this->load->model('Show_All_Data_Model'); 

                if($this->Show_All_Data_Model->Fetch_Client_Data_Zone($zone))
                    {
                        $all_data=$this->Show_All_Data_Model->Fetch_Client_Data_Zone($zone);
        
                        $i=0;
        
                        //print_r($all_data);
        
                        foreach($all_data as $particular_client)
                        {
                            $client_id=$particular_client->cid;
                            
                            $data3['client_id']= $client_id;
                            $data3['ballot_name']= $position;
                            $data3['vote']= 0;
        
                            $this->load->model('Add_Client_To_Ballots');
       // print_r($position);die();
                            if($this->Add_Client_To_Ballots->Insert_Data($data3))
                            {   
                                $i= $i+1;
                            }
                        }

                        $this->session->set_flashdata('ballot_added','Ballot Added');
                        redirect(base_url().'index.php/Add_Ballot_Controller/Add_Ballot_View');
                    }
                    else
                    {
                        //echo "No clients found for this zone";
                        $this->session->set_flashdata('ballot_added','Ballot Added');
                        redirect(base_url().'index.php/Add_Ballot_Controller/Add_Ballot_View');
                    }

            }

            else
            {
                $this->session->set_flashdata('error','insert falied');
                redirect(base_url().'index.php/Add_Ballot_Controller/Add_Ballot_View');
            }
        }

        else
        {
            $this->session->set_flashdata('error',validation_errors());
            redirect(base_url().'index.php/Add_Ballot_Controller/Add_Ballot_View');
        }
    }
    
}
