<?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');
    $this->load->model('Check_If_Voted');

    $electoralid = urldecode(trim($electoralid));
    $datas = $this->session->userdata('client');
    $datas = urldecode(trim($datas));

    // Check if user has already voted
    $hasVoted = $this->Check_If_Voted->check_vote($electoralid, $datas);
    if ($hasVoted) {
        $this->session->set_flashdata('voted', 'Already Voted');
        redirect(base_url().'index.php/Show_Voting_Data_Client/VotingTitlesDataClientVoted');
        return;
    }

    // Check voting date
    $isDateValid = $this->Check_If_Voted->check_date($electoralid);
    if (!$isDateValid) {
        $this->session->set_flashdata('voted', 'Last Date over');
        redirect(base_url().'index.php/Show_Voting_Data_Client/VotingTitlesDataClientVoted');
        return;
    }

    // Get candidate type and candidate data
    $this->db->where('ballot_name', $electoralid);
    $ballot = $this->db->get('ballot')->row();

    if (!$ballot) {
        $this->session->set_flashdata('error', 'Invalid Ballot');
        redirect(base_url().'index.php/Show_Voting_Data_Client/VotingTitlesDataClientVoted');
        return;
    }

    $candidate_type = $ballot->candidate_type;
    $all_data['data'] = $this->Voting_Titles_Admin_Data->Candidate_Data($electoralid);

    if (empty($all_data['data'])) {
        $this->session->set_flashdata('error', 'No Candidates Found');
        redirect(base_url().'index.php/Show_Voting_Data_Client/VotingTitlesDataClientVoted');
        return;
    }

    // Load appropriate view
    if ($candidate_type == 0) {
        $this->load->view('Voting_System_View_Client', $all_data);
    } else {
        $this->load->view('Multiple_Voting_System_View_Client', $all_data);
    }
}


}
?>