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

class Add_Client_Contoller extends CI_Controller {

	public function Add_Client_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_Client_View',$all_data);
               
                     //echo " ".$all_data[0]->id;
            }
         else
         {
             echo "not inserted";
         }   

    }
    
    public function ClientByAdmin()
	{
		$this->load->library('form_validation');
        $this->form_validation->set_rules('zone', 'zone', 'required');
        $this->form_validation->set_rules('id', 'MemberShip Id', 'required|is_unique[users.cid]');
        $this->form_validation->set_rules('email', 'email', 'required|valid_email|is_unique[users.email]');
        $this->form_validation->set_rules('mobile', 'mobile', 'required|is_unique[users.mobile]');
        $this->form_validation->set_rules('name', 'name', 'required');
        $this->form_validation->set_rules('place', 'place', 'required');
        $this->form_validation->set_rules('type', 'type', 'required');

        if($this->form_validation->run())
        {
            $zone=$this->input->post('zone');
            $id=$this->input->post('id');
            $name=$this->input->post('name');
            $email=$this->input->post('email');
            $mobile=$this->input->post('mobile');
            $place=$this->input->post('place');
            $type=$this->input->post('type');

            $edit_data['email']=$email;
            $edit_data['zone']=$zone;
            $edit_data['cid']=$id;
            $edit_data['name']=$name;
            $edit_data['mobile']=$mobile;
            $edit_data['place']=$place;
            $edit_data['type']=$type;

            $data2['username']=$id;
            $data2['type']=$type;

            if($type=="admin" || $type=="super_admin")
            {
                $this->load->model('Add_Client_Model');

                $pwd=$this->input->post('pwd');

                $data3["username"]=$id;
                $data3["type"]=$type;
                $data3["password"]=$pwd;

                if($this->Add_Client_Model->Insert_Admin($data3))
                   {
                        $this->load->model('Add_Client_Model');
                        if($this->Add_Client_Model->Insert_Client_Data($edit_data))
                        {
                                if($this->Add_Client_Model->insert_data_list($data2))
                                {
                                    $this->session->set_flashdata('admin_inserted','Added Successfully');
                                    //redirect(base_url().'index.php/Add_Client_Contoller/Add_Client_View'); 
                                     $this->load->model('Add_Zone_Model');
                                     $all_data['data']=$this->Add_Zone_Model->Show_Zones();
                                    $this->load->view('Add_Client_View',$all_data);
                                }
                                else
                                {
                                        //echo "Not Inserted";
                                    $this->session->set_flashdata('admin_inserted','Not Inserted');
                                    //redirect(base_url().'index.php/Add_Client_Contoller/Add_Client_View');
                                     $this->load->model('Add_Zone_Model');
                                     $all_data['data']=$this->Add_Zone_Model->Show_Zones();
                                    $this->load->view('Add_Client_View',$all_data);
                                }
                                    
                        }
                         else
                            {
                                //echo "Not Inserted";
                                $this->session->set_flashdata('client_inserted','Data not Inserted');
                                //redirect(base_url().'index.php/Add_Client_Contoller/Add_Client_View');
                                $this->load->model('Add_Zone_Model');
                                $all_data['data']=$this->Add_Zone_Model->Show_Zones();
                                 $this->load->view('Add_Client_View',$all_data);
                            }
                   }
                   else
                   {
                        //echo "Not Inserted";
                        $this->session->set_flashdata('admin_inserted','Data not Inserted');
                        //redirect(base_url().'index.php/Add_Client_Contoller/Add_Client_View');
                         $this->load->model('Add_Zone_Model');
                         $all_data['data']=$this->Add_Zone_Model->Show_Zones();
                        $this->load->view('Add_Client_View',$all_data);
                   }

            }

            $this->load->model('Add_Client_Model');
            if($this->Add_Client_Model->Insert_Client_Data($edit_data))
                {
                   if($this->Add_Client_Model->insert_data_list($data2))
                   {
                        /*$this->session->set_flashdata('client_inserted','Client Added Successfully');
                        redirect(base_url().'index.php/Add_Client_Contoller/Add_Client_View'); */ 

                        $this->load->model('Voting_Titles_Admin_Data');

                        $i=0;

                        if($this->Voting_Titles_Admin_Data->Fetch_Data($zone))
                        {
                            $all_data=$this->Voting_Titles_Admin_Data->Fetch_Data($zone);
                            //print_r($all_data);

                            foreach($all_data as $particular_ballot)
                            {
                                $client_id=$particular_ballot->id;
                                
                                $data3['client_id']= $id;
                                $data3['ballot_name']= $particular_ballot->ballot_name;
                                $data3['vote']= 0;
            
                                $this->load->model('Add_Client_To_Ballots');
            
                                if($this->Add_Client_To_Ballots->Insert_Data($data3))
                                {   
                                    $i= $i+1;
                                }
                            }

                            $this->session->set_flashdata('client_inserted','Client Added Successfully');
                            //redirect(base_url().'index.php/Add_Client_Contoller/Add_Client_View');
                             $this->load->model('Add_Zone_Model');
                             $all_data['data']=$this->Add_Zone_Model->Show_Zones();
                             $this->load->view('Add_Client_View',$all_data);
                        }
                        else
                        {
                            //echo "nooooooooooooooo";
                            $this->session->set_flashdata('client_inserted','Client Added Successfully');
                            //redirect(base_url().'index.php/Add_Client_Contoller/Add_Client_View');
                               $this->load->model('Add_Zone_Model');
                                $all_data['data']=$this->Add_Zone_Model->Show_Zones();
                             $this->load->view('Add_Client_View',$all_data);
                        }
                   }    
                   else
                   {
                        //echo "Not Inserted";
                        $this->session->set_flashdata('client_inserted','Client Added Successfully');
                        //redirect(base_url().'index.php/Add_Client_Contoller/Add_Client_View');
                         $this->load->model('Add_Zone_Model');
                         $all_data['data']=$this->Add_Zone_Model->Show_Zones();
                         $this->load->view('Add_Client_View',$all_data);
                   }
                    
                }
                else
                {
                    //echo "Not Inserted";
                    $this->session->set_flashdata('client_inserted','Client Added Successfully');
                   // redirect(base_url().'index.php/Add_Client_Contoller/Add_Client_View');
                        $this->load->model('Add_Zone_Model');
                        $all_data['data']=$this->Add_Zone_Model->Show_Zones();
                        $this->load->view('Add_Client_View',$all_data);
                }
        }

        else
        {
            $this->session->set_flashdata('error',validation_errors());
            //redirect(base_url().'index.php/Add_Client_Contoller/Add_Client_View');
             $this->load->model('Add_Zone_Model');
             $all_data['data']=$this->Add_Zone_Model->Show_Zones();
             $this->load->view('Add_Client_View',$all_data);
        }
	}
}
