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

class Admin_Edit extends CI_Controller {
    
    public function Admin_Data($username)
	{
        $this->load->model('Show_Data_Model');

        if($this->Show_Data_Model->User_data($username))
            {   
                if($this->Show_Data_Model->LoginData($username))
                {
                    $all_data['Ldata']=$this->Show_Data_Model->LoginData($username);
                }
                $all_data['data']=$this->Show_Data_Model->User_data($username);

                $this->load->view('Edit_Details_Admin',$all_data);
            }

    }
    
   
    public function Update_Data()
	{
        $this->load->library('form_validation');
        $this->form_validation->set_rules('zone', 'zone', 'required');
        $this->form_validation->set_rules('type', 'type', 'required');
        $this->form_validation->set_rules('mobile', 'mobile', 'required');
        $this->form_validation->set_rules('place', 'place', 'required');
        $this->form_validation->set_rules('name', 'name', 'required');
		$this->form_validation->set_rules('email', 'email', 'valid_email');
    	// $this->form_validation->set_message('check_user_email', 'Email must be unique');
	//	$this->form_validation->set_rules('mid', 'MemberShip Id', 'is_unique[users.cid]');

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

        $edit_data=array();

        // if (!empty($username)) {
        //     $edit_data['cid'] = $username;
        // }

        if (!empty($type)) {
            $edit_data['type'] = $type;
        }

        if (!empty($name)) {
            $edit_data['name'] = $name;
        }

        if (!empty($email)) {
            $edit_data['email'] = $email;
        }

        if (!empty($mobile)) {
            $edit_data['mobile'] = $mobile;
        }

        if (!empty($place)) {
            $edit_data['place'] = $place;
        }
       
        if (!empty($zone)) {
            $edit_data['zone'] = $zone;
        }

        $this->load->model('Admin_Edit_Model');
        
         if(($this->Admin_Edit_Model->Edit_Data_Client_Admin($edit_data,$this->uri->segment('3'))) )
            {   
                $this->session->set_flashdata('updated_data','data is updated');
                redirect(base_url().'index.php/Admin_Edit/Admin_Data/'.$username);
               
            }

            else
            {
                $this->session->set_flashdata('updated_data','no data updated');
                redirect(base_url().'index.php/Admin_Edit/Admin_Data/'.$username); 
            }
		}
			else
        {
           $this->session->set_flashdata('error',validation_errors());
                redirect(base_url().'index.php/Admin_Edit/Admin_Data/'.$username); 
		   
        }

    }
    
    public function Delete_Data($username)
	{
        $this->load->model('Admin_Edit_Model');

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

        if($this->Show_Data_Model->Admins_data($username))
            {   
                $all_data=$this->Show_Data_Model->Admins_data($username);

                if($this->Admin_Edit_Model->Delete_Data_Client_Administrator($username))
                {     
                    if($this->Admin_Edit_Model->Delete_Data_Client_Admin($username))
                    {    
                        if($this->Admin_Edit_Model->Delete_Data_Client_Admin_User($username))
                        {   
                            if($this->Admin_Edit_Model->Delete_Data_Client_Admin_In_Ballot($username))
                            {     
                               
                                $this->session->set_flashdata('deleted_client','Data Deleted');
                                 $this->load->model('Show_All_Data_Model');
                        $all_data['data']=$this->Show_All_Data_Model->Fetch_Data();
                        
                                 $this->load->view('welcome_message',$all_data);
                            }  
                        }        
                    }     
                }

            }

//print_r($this->Admin_Edit_Model->Delete_Data_Client_Admin_In_Ballot($username));die();
        if($this->Admin_Edit_Model->Delete_Data_Client_Admin($username))
            {    
                 if($this->Admin_Edit_Model->Delete_Data_Client_Admin_User($this->uri->segment('3')))
                 {   
                    if($this->Admin_Edit_Model->Delete_Data_Client_Admin_In_Ballot($username))
                    {     
                        $this->session->set_flashdata('deleted_client','Data Deleted');
                        $this->load->model('Show_All_Data_Model');
                        $all_data['data']=$this->Show_All_Data_Model->Fetch_Data();
                        $this->load->view('welcome_message',$all_data);
                    }  else{
                        $this->session->set_flashdata('deleted_client','Data Deleted');
                        $this->load->model('Show_All_Data_Model');
                        $all_data['data']=$this->Show_All_Data_Model->Fetch_Data();
                        $this->load->view('welcome_message',$all_data);
                    }
                } else{
                    $this->session->set_flashdata('deleted_client','Data Deleted');
                        $this->load->model('Show_All_Data_Model');
                        $all_data['data']=$this->Show_All_Data_Model->Fetch_Data();
                        $this->load->view('welcome_message',$all_data);
                }       
            }else{
                $this->session->set_flashdata('deleted_client','Data Deleted');
                        $this->load->model('Show_All_Data_Model');
                        $all_data['data']=$this->Show_All_Data_Model->Fetch_Data();
                        $this->load->view('welcome_message',$all_data);
            }
    }


    public function Admin_Data_Candidate($username)
	{
        $this->load->model('Show_Data_Model');

        if($this->Show_Data_Model->Candidate_data($username))
            {   
                $all_data['data']=$this->Show_Data_Model->Candidate_data($username);
                $this->load->view('Edit_Details_Candidate_Admin',$all_data);
            }

    }

    public function Update_Data_Candidate()
	{
        $participant_name=$this->input->post('participant_name');
        $place=$this->input->post('place');
        $vote=$this->input->post('vote');
        $username=$this->uri->segment('3');
        $username=trim($username);
        $username=urldecode($username);
        $edit_data=array();

        if (!empty($participant_name)) {
            $edit_data['participant_name'] = $participant_name;
        }

        if (!empty($place)) {
            $edit_data['place'] = $place;
        }

        if (!empty($vote)) {
            $edit_data['vote'] = $vote;
        }

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

        if(($this->Admin_Edit_Model->Edit_Data_Client_Candidate($edit_data,$this->uri->segment('3'))) )
            {   
                $this->session->set_flashdata('updated_data','data is updated');
                redirect(base_url().'index.php/Admin_Edit/Admin_Data_Candidate/'.$username);
               
            }

            else
            {
                //echo $edit_data;
                $this->session->set_flashdata('updated_data','no data updated');
                redirect(base_url().'index.php/Admin_Edit/Admin_Data_Candidate/'.$username); 
            }

    }
}
