<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Voters;
use App\Pemilih;
use App\Exports\PemilihExport;
use App\Imports\PemilihImport;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;

class VotersController extends Controller
{
    //
     public function __construct()
    {
        $this->middleware('auth');
    }
    public function index()
    {
    	$pemilih = Pemilih::all();

    	return view('dashboard/voter/voters',['pemilih' => $pemilih ]);
    }
    public function tambah()
    {
    	    	return view('dashboard/voter/tambah');
    }
	/** TAMPILKAN MENU HAPUS */
	public function hapus()
	{
		return view('dashboard/voter/hapus');
	}
    public function store(Request $data)
    {
    	$jumlah = $data->jumlah;
		// dd($jumlah);
    	for ($i=0; $i < $jumlah ; $i++) {
    		# code...
    		$karakter = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    		// $string   = '';
            $nis = $i;
            $nama = 'pemilih '. $i;
            $kelas = '-';
    		for ($x=0; $x < 2000 ; $x++)
            {

    			// $pos = rand(0, strlen($karakter)-1);
    			// $string = $karakter[$pos];
                $length = 5;
                $string =  substr(str_shuffle(str_repeat($karakter, 5)), 0, $length);


    		}

            $token = strtoupper($string);
    			/** CEK TOKEN SUDAH TERDAFTAR ATAU BELUM*/
    			$cek = Voters::find($token);
    			if(empty($cek))
                {
		    		Voters::create([
		    			'nis' => $nis,
		    			'nama' => $nama,
		    			'kelas' => $kelas,
		    			'token' => $token,
		    			'status'   => "Belum Voting" ]);
		        }

    	}

    	 return redirect('/admin/voters');
	}
	//hapustoken
	public function delete(Request $data)
	{
		
		$jumlah = $data->input('jumlah');
		$kriteria = $data->input('kriteria');
		

		if($kriteria == "0"){
			
			DB::table('kandidat')->update([
				'jumlahsuara' => 0
			]);
			DB::table('pemilih')->delete();
			return redirect('/admin/voters');
		}
		elseif($kriteria == "Sudah Voting"){
			
			DB::table('kandidat')->update([
				'jumlahsuara' => 0
			]);
			DB::table('pemilih')->where('status','Sudah Voting')->delete();
			return redirect('/admin/voters');
		}
		elseif($kriteria == "Belum Voting"){
			DB::table('pemilih')->where('status','Belum Voting')->delete();
			return redirect('/admin/voters');
		}
	}

	public function export_excel()
	{
		return Excel::download(new PemilihExport, 'DataPemilih.xlsx');
	}

    public function import_excel(Request $request)
	{

		$validasi = $request->validate([
            'file' => 'required'
        ]);
        Excel::import(new PemilihImport, $validasi['file']);
        return redirect('/admin/voters');
	}

	public function download(Request $request)
    {
        $filename = 'formatexcelvoters\formatexcelvoters.xlsx';
        $path = public_path('storage\formatexcelvoters\formatexcelvoters.xlsx');
		// dd($path);
        return response()->download($path);
    }
}
