<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Kandidat;
use App\Voting;
use App\Pemilih;
use Illuminate\Support\Facades\DB;
class VotingController extends Controller
{
    //
    public function index(Request $data)
    {
        if($data->session()->get('token')){
            //Jika Ada Session
                $kandidat = Kandidat::all();
                return view('voting.index',[
                    'kandidat' =>$kandidat
                ]);
        }else{
            // jika tidak ada session
            return redirect('/votinglogin');
        }

    }
    public function simpansuara($idkandidat,Request $data)
    {
        $token = $data->session()->get('token');
        // $status_vote = Pemilih::where('token',$token)->first()->status;
        if (isset($token)) {
            $nama_kandidat = Kandidat::whereId($idkandidat)->first()->nama;
            $jumlah = DB::table('kandidat')->where('id',$idkandidat)->get();
            foreach ($jumlah as $key) {
                $jumlahsuara = $key->jumlahsuara;
            }

            DB::table('kandidat')->where('id',$idkandidat)->update([
                'jumlahsuara' => $jumlahsuara+1
            ]);

            DB::table('pemilih')->where('token',$data->session()->get('token'))->update([
                'status' => 'Sudah Voting'
            ]);

            $caripemilih = DB::table('pemilih')->where('token',$data->session()->get('token'))->first();
            $idpemilih = $caripemilih->id;
            // dd($idpemilih);
            Pemilih::where('id', $idpemilih)->update([
                'kandidat_dipilih' => $nama_kandidat,
            ]);
            return redirect ('/logoutvoting');
        }
        else
        {
            return redirect ('/logoutvoting');
        }
        
    }
    public function apisimpansuara(Request $data)
    {
        $idkandidat = $data->idkandidat;
        $token      = $data->token;
        $jumlah = DB::table('kandidat')->where('id',$idkandidat)->get();
        $voter = DB::table('pemilih')->where('token',$token)->get();
        foreach($voter as $v) {
            $statusvoter = $v->status;
        }
        if($statusvoter == 'Belum Voting'){
        foreach ($jumlah as $key) {
             $jumlahsuara = $key->jumlahsuara;
        }
        DB::table('kandidat')->where('id',$idkandidat)->update([
            'jumlahsuara' => $jumlahsuara+1
        ]);
        DB::table('pemilih')->where('token',$token)->update([
            'status' => 'Sudah Voting'
        ]);
        $success = '1';
        $msg = 'Berhasil';
        }else{
            $success = '0';
            $msg = 'Gagal';
        }
        return response()->json([
                    'success' => $success,
                    'message' => $msg
                ]);
    }

    public function logoutvoting(Request $data){
        $data->session()->forget('token');
        return view('voting.logout');
    }

    // public function daftartoken()
    // {
    //     $token = DB::table('pemilih')->where('status','2')->get();
    //     foreach($token as $tkn) {
    //         echo $tkn->token;
    //         echo '<br>';
    //         echo '<br>';
    //     }
    // }

}
