<?php

namespace App\Http\Controllers\Admin;

use App\Http\Support\ValidationRule;
use Illuminate\Support\Facades\Hash;
use App\Http\Controllers\Controller;
use App\Models\Kegiatan;
use App\Models\KegiatanPeserta;
use App\Models\Konferensi;
use App\Models\Registrasi;
use App\Models\User;
use App\Models\VRegistrasi;
use Carbon\Carbon;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class AdminController extends Controller
{
    public function index() {
        return view('home');
    }

    public function index_user() {
        $user = User::get();
        $data = [
            'user' => $user,
        ];

        return view('admin.user.index', $data);
    }

    public function store_user(Request $request) {

        $rules = [
            'name' => 'required|string',
            'email' => 'required|email',
            'password' => 'string|min:8',
        ];

        $request->validate($rules, ValidationRule::getErrorMessage($rules));

        DB::beginTransaction();

        try{
            $password = Hash::make($request->password);
            $data = [
                'name' => strip_tags($request->name),
                'email' => strip_tags($request->email),
                'password' => $password
            ];

            User::query()->create($data);
            DB::commit();

            return redirect()->back()
            ->with('success', 'Data saved successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to save');
        }
    }

    public function update_user($id, Request $request){

        DB::beginTransaction();

        try{
            if($request->password){
                $password = Hash::make($request->password);
            }
            else{
                $user = User::query()->where('id', $id)->first();
                $password = $user->password;
            }

            $data = [
                'name' => strip_tags($request->name),
                'email' => strip_tags($request->email),
                'password' => $password
            ];
            User::query()->where('id', $id)->update($data);
            DB::commit();

            return redirect()->back()
                ->with('success', 'Data saved successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to update');
        }
    }

    public function delete_user($id){
        DB::beginTransaction();

        try{
            User::find($id)->delete();
            DB::commit();

            return redirect()->back()
                ->with('success', 'Data deleted successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to delete');
        }
    }

    public function index_kegiatan() {
        $kegiatan = Kegiatan::get();
        $data = [
            'kegiatan' => $kegiatan
        ];

        return view('admin.kegiatan.index', $data);
    }

    public function store_kegiatan(Request $request) {
        $rules = [
            'nama' => 'required|string',
            'harga' => 'required|numeric'
        ];

        $request->validate($rules, ValidationRule::getErrorMessage($rules));

        DB::beginTransaction();

        try{
            $data = [
                'nama' => strip_tags($request->nama),
                'harga' => $request->harga
            ];
            Kegiatan::query()->create($data);
            DB::commit();

            return redirect()->back()
                ->with('success', 'Data saved successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to create');
        }
    }

    public function update_kegiatan($id, Request $request) {
        $rules = [
            'nama' => 'required|string',
            'harga' => 'required|numeric'
        ];

        $request->validate($rules, ValidationRule::getErrorMessage($rules));

        DB::beginTransaction();

        try{
            $data = [
                'nama' => $request->nama,
                'harga' => $request->harga
            ];

            Kegiatan::query()->where('id', $id)->update($data);
            DB::commit();

            return redirect()->back()
                ->with('success', 'Data saved successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to update');
        }
    }

    public function delete_kegiatan($id) {
        DB::beginTransaction();
        try{
            Kegiatan::find($id)->delete();
            DB::commit();

            return redirect()->back()
                ->with('success', 'Data deleted successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to delete');
        }
    }

    public function index_konferensi() {
        $konferensi = Konferensi::get();
        $data = [
            'konferensi' => $konferensi
        ];

        return view('admin.konferensi.index', $data);
    }

    public function store_konferensi(Request $request) {
        $rules = [
            'nama' => 'required|string',
            'harga' => 'required|numeric'
        ];

        $request->validate($rules, ValidationRule::getErrorMessage($rules));

        DB::beginTransaction();

        try{
            $data = [
                'nama' => strip_tags($request->nama),
                'harga' => $request->harga
            ];
            Konferensi::query()->create($data);
            DB::commit();

            return redirect()->back()
                ->with('success', 'Data saved successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to update');
        }
    }

    public function update_konferensi($id, Request $request) {
        $rules = [
            'nama' => 'required|string',
            'harga' => 'required|numeric'
        ];

        $request->validate($rules, ValidationRule::getErrorMessage($rules));

        DB::beginTransaction();

        try{
            $data = [
                'nama' => $request->nama,
                'harga' => $request->harga
            ];

            Konferensi::query()->where('id', $id)->update($data);
            DB::commit();

            return redirect()->back()
                ->with('success', 'Data saved successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to update');
        }
    }

    public function delete_konferensi($id) {
        DB::beginTransaction();
        try{
            Konferensi::find($id)->delete();
            DB::commit();

            return redirect()->back()
                ->with('success', 'Data deleted successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to delete');
        }
    }

    public function index_peserta(){
        // $peserta = Registrasi::whereHas('pkRegistrasiPeserta')->get();
        $peserta = VRegistrasi::orderby('created_at', 'DESC')->get();
        $data = [
            'peserta' => $peserta
        ];

        return view('admin.peserta.index', $data);
    }

    // public function delete_peserta($id) {
    //     DB::beginTransaction();
    //     try{
    //         KegiatanPeserta::where('id_registrasi',$id)->delete();
    //         Registrasi::where('id',$id)->delete();

    //         DB::commit();

    //         return redirect()->back()
    //             ->with('success', 'Data deleted successfully');
    //     }
    //     catch(Exception $e){
    //         Log::error($e);
    //         DB::rollBack();

    //         return redirect()->back()
    //             ->with('error', 'Data failed to delete');
    //     }
    // }

    public function download_peserta()
    {
        date_default_timezone_set("Asia/Jakarta");
        $data['peserta']    =  VRegistrasi::orderby('created_at','DESC')->get();
        $data['nama_file'] = 'daftar peserta konaspi'." ".date('Y-m-d');

        return view('admin.peserta.excel', $data);
    }

    public function index_pembayaran(){
        $pembayaranVa = DB::connection('va')->table('neosidata.v_pembayaran_konaspi')->orderBy('va')->get();

        foreach ($pembayaranVa as $key => $value) {
            $nomorVa[] = $value->va;
            $konaspi = Registrasi::where('nomor_va', $value->va)->first();
            $date = Carbon::createFromFormat('dmy', $value->tgl)->format('Y-m-d');

            $data[$key] = [
                'konaspi' => $konaspi,
                'va' => $value,
                'tglbayar' => $date
            ];
        }

        return view('admin.pembayaran.index', compact('data'));
    }

    public function download_data_pembayaran()
    {
        date_default_timezone_set("Asia/Jakarta");
        $pembayaranVa = DB::connection('va')->table('neosidata.v_pembayaran_konaspi')->orderBy('va')->get();

        foreach ($pembayaranVa as $key => $value) {
            $nomorVa[] = $value->va;
            $konaspi = Registrasi::where('nomor_va', $value->va)->first();
            $date = Carbon::createFromFormat('dmy', $value->tgl)->format('Y-m-d');

            $data[$key] = [
                'konaspi' => $konaspi,
                'va' => $value,
                'tglbayar' => $date
            ];
        }

        return view('admin.pembayaran.excel', compact('data'));
    }
}
