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

class Show_Voting_Data_Client extends CI_Controller {

    function __construct()
    {
        parent::__construct();
    }

    function VotingTitlesDataClient($username)
    {
        $username=trim($username);
        $username=urldecode($username);
        $zone=" ";
        $this->load->model('Voting_Titles_Admin_Data');
       // print_r($this->Voting_Titles_Admin_Data->Fetch_Data($zone));die();
        
        if($this->Voting_Titles_Admin_Data->Fetch_Data_User($username))
            {   
                $zone=$this->Voting_Titles_Admin_Data->Fetch_Data_User($username);

                if($this->Voting_Titles_Admin_Data->Fetch_Data($zone))
                {   
                    $all_data['data']=$this->Voting_Titles_Admin_Data->Fetch_Data($zone);
                   
                    if(isset($all_data['data'])){
                      $this->load->view('Ballot_View_Client',$all_data);  
                    }
                    
                }
                else
                {
                    echo "no";
                }
            }
        else
        {
            echo "no";
        }    
    }

    function VotingTitlesDataClientVoted()
    {
        $datas=$this->session->userdata('client');
        $datas = urldecode($datas);
        $datas=trim($datas);

        $this->load->model('Voting_Titles_Admin_Data');
        //$this->session->set_flashdata('voted','Already Voted');

        if($this->Voting_Titles_Admin_Data->Fetch_Data_User($datas))
            {   
                $zone=$this->Voting_Titles_Admin_Data->Fetch_Data_User($datas);

                if($this->Voting_Titles_Admin_Data->Fetch_Data($zone))
                {   
                    $all_data['data']=$this->Voting_Titles_Admin_Data->Fetch_Data($zone);
                    $all_data['datas']=$datas;
                    if(isset($all_data['data'])){
                    $this->load->view('Ballot_View_Client',$all_data);
                    }
                }
                else
                {
                    echo "no";
                }
            }

        else
        {

        }
    }

    function VotingCandidatesDataClient($electoralid)
    {
        $this->load->model('Voting_Titles_Admin_Data');
        
        $electoralid=urldecode($electoralid);
        $electoralid=trim($electoralid);

        $datas=$this->session->userdata('client');
        $datas = urldecode($datas);
        $datas=trim($datas);
//print_r(($electoralid));die();
        if($this->Voting_Titles_Admin_Data->Candidate_Data($electoralid))
            {   
                $this->load->model('Check_If_Voted');
          
                $check=$this->Check_If_Voted->check_vote($electoralid,$datas);
                
                if(!($check))
                {
                   $array = array('ballot_name' => $electoralid);
                   
       
                   $query = $this->db->select('date')->from('ballot')->where($array)->get();
                   $query->result()[0]->date;
                  //print_r($query->result()[0]->date);die();
                 // print_r(date("Y-m-d"));die();
                   // $this->Check_If_Voted->check_date($electoralid);
                    if($this->Check_If_Voted->check_date($electoralid))
                    {
                        $this->load->model('Voting_Titles_Admin_Data');

                        $all_data['data']=$this->Voting_Titles_Admin_Data->Candidate_Data($electoralid);
                            /*echo " "."admin ".$data[0]->name." ".$data[0]->email." ".$data[0]->type;
                            echo " "."admin ".$data[1]->name." ".$data[1]->email." ".$data[1]->type;*/
// print_r($all_data['data']);die();
                        $this->load->view('Voting_System_View_Client',$all_data);
                            //echo " ".$all_data[0]->id;
                    }
                    else
                    {
                        $this->session->set_flashdata('voted','Last Date over');
                        redirect(base_url().'index.php/Show_Voting_Data_Client/VotingTitlesDataClientVoted');
                    }
                }

                else
                {
                    $this->session->set_flashdata('voted','Already Voted');
                    redirect(base_url().'index.php/Show_Voting_Data_Client/VotingTitlesDataClientVoted'); 
                }
                
            }
        
        else
        {
            $all_data['data']=null;
            $this->session->set_flashdata('error'.'No Candidates found');
            $this->load->view('Voting_System_View_Client',$all_data);

        }    
    }

}
?>