<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\Agenda;
use App\Models\Kategori;
use App\Models\UserAgenda;
use Exception;
use Illuminate\Console\View\Components\Alert;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;

class AgendaController extends Controller
{
    public function index(Request $request)
    {

        // $ngajar = DB::connection('siakad')->table('akt_ajar_dosen as a')
        //     ->select('k.id_kls','m.nm_mk', 'j.int_hari1', 'j.int_hari2', 'j.jam11', 'j.jam12')
        //     ->leftJoin('kelas_kuliah as k', 'k.id_kls', '=', 'a.id_kls')
        //     ->leftJoin('kelas_jadwal as j', 'j.id_kls', '=', 'a.id_kls')
        //     ->leftJoin('matkul as m', 'm.id_mk', '=', 'k.id_mk')
        //     ->leftJoin('reg_ptk as r', 'r.id_reg_ptk', '=', 'a.id_reg_ptk')

        //     ->leftJoin('sdm as s', 's.id_sdm', '=', 'r.id_sdm')
        //     ->where('s.nip', $nip)
        //     ->get();

        //     dd($ngajar);
        if ($request->ajax()) {

            $tahun = '2023';
            $nip = '197410082006041002';

// * CEK SURAT KELUAR
            $sql = "SELECT
            tr_surat_keluar.id as id,
            tr_surat_keluar.perihal as title,
            tr_surat_keluar.tglmulai as start,
            tr_surat_keluar.tglselesai as end,
            tr_surat_keluar.jam_mulai,
            tr_surat_keluar.jam_selesai 
        FROM
            tr_surat_keluar
            LEFT JOIN tr_penerima_surat_keluar AS a ON a.idsurat = tr_surat_keluar.id 
        WHERE
            a.nip = '199405072022032014'
            AND tr_surat_keluar.tglmulai >= '$request->start'
            AND tr_surat_keluar.tglselesai <= '$request->end'
            AND UPPER(tr_surat_keluar.perihal :: TEXT) LIKE '%UNDANGAN%' ";

            $keluar = DB::connection('eoffice')->select($sql);

            $getData = array();
            $i = 0;
            foreach ($keluar as $item) {
                if ($item->jam_mulai !== null ) {
                    $date = $item->start . ' ' . $item->jam_mulai;

                    $newDate = date('Y-m-d H:i:s', strtotime($date));

                    $start = $newDate;
                } else {
                    $date = $item->start . ' 08:00:00';

                    $newDate = date('Y-m-d H:i:s', strtotime($date));

                    $start = $newDate;
                }

                if ($item->jam_selesai !== null and $item->jam_mulai < $item->jam_selesai) {
                    $date = $item->start . ' ' . $item->jam_selesai;

                    $newDate = date('Y-m-d H:i:s', strtotime($date));

                    $end = $newDate;
                } else {
                    $date = $item->start . ' 16:00:00';

                    $newDate = date('Y-m-d H:i:s', strtotime($date));

                    $end = $newDate;
                }                
                
                $getData[$i++] = [
                    'id' => $item->id,
                    'title' => $item->title,
                    'start' => $start,
                    'end' => $end,
            ];                
            }

// * CEK SURAT MASUK
            $sql = "SELECT
            tr_surat_masuk.id as id,
            tr_surat_masuk.perihal as title,
            tr_surat_masuk.tglmulai as start,
            tr_surat_masuk.tglselesai as end,
            tr_surat_masuk.jam_mulai,
            tr_surat_masuk.jam_selesai 
        FROM
            tr_surat_masuk
            LEFT JOIN tr_penerima_surat_masuk AS a ON a.idsurat = tr_surat_masuk.id 
        WHERE
            a.nip = '199405072022032014'
            AND tr_surat_masuk.tglmulai >= '$request->start'
            AND tr_surat_masuk.tglselesai <= '$request->end'
            AND UPPER(tr_surat_masuk.perihal :: TEXT) LIKE '%UNDANGAN%' ";

            $masuk = DB::connection('eoffice')->select($sql);
            
            $i = 0;
            foreach ($masuk as $item) {
                if ($item->jam_mulai !== null ) {
                    $date = $item->start . ' ' . $item->jam_mulai;

                    $newDate = date('Y-m-d H:i:s', strtotime($date));

                    $start = $newDate;
                } else {
                    $date = $item->start . ' 08:00:00';

                    $newDate = date('Y-m-d H:i:s', strtotime($date));

                    $start = $newDate;
                }

                if ($item->jam_selesai !== null and $item->jam_mulai < $item->jam_selesai) {
                    $date = $item->start . ' ' . $item->jam_selesai;

                    $newDate = date('Y-m-d H:i:s', strtotime($date));

                    $end = $newDate;
                } else {
                    $date = $item->start . ' 16:00:00';

                    $newDate = date('Y-m-d H:i:s', strtotime($date));

                    $end = $newDate;
                }                
                
                $getData[$i++] = [
                    'id' => $item->id,
                    'title' => $item->title,
                    'start' => $start,
                    'end' => $end,
            ];                
            }

            return response()->json($getData);
        }
        // $data = UserAgenda::all();
        // return $data;
        return view('user.index');
    }

    public function surat_masuk(Request $request)
    {
        $nip = '199309052019032017';
        $tahun = '2023';

        // $data = UserAgenda::whereDate('start', '>=', $request->start)
        //                ->whereDate('end', '<=', $request->end)
        //                ->get(['id', 'title', 'start', 'end']);

        $surat = DB::connection('eoffice')->table('tr_surat_masuk')
            ->select('tr_surat_masuk.id', 'tr_surat_masuk.perihal as title', 'tr_surat_masuk.tglmulai as start', 'tr_surat_masuk.tglmulai as end', 'tr_surat_masuk.jam_mulai', 'tr_surat_masuk.jam_selesai')
            ->leftJoin('tr_penerima_surat_masuk as a', 'a.idsurat', '=', 'tr_surat_masuk.id')
            // ->where('a.nip', $nip)
            ->where('tr_surat_masuk.perihal', 'like', '%undangan%')
            // ->where('tr_surat_masuk.tahun', $tahun)
            ->get(['id', 'title', 'start', 'end']);
        // dd($surat);
    }

    public function create()
    {
        $kategori = Kategori::query()->get();
        return view('user.create')->with('data', $kategori);
    }

    public function store(Request $request)
    {
        // dd($request);
        $data = [];

        DB::beginTransaction();

        // Validator::make($request->all(), [
        //     'nama' => 'required|string',
        //     'tanggal' => 'required',
        //     'waktu' => 'required',
        //     'lokasi' => 'string',
        //     'link_zoom' => 'required|string',
        //     'id_zoom' => 'required|string',
        //     'passcode_zoom' => 'required|string',
        // ]);

        try {
            // $swalData = [
            //     'type' => 'success',
            //     'title' => 'Agenda'
            // ];
            $data = $request->except(['_method', '_token']);
            $data['id'] =
                $data['idunit'] = '1526d530-d992-49ba-b663-3ed703b96a0f'; //nanti bakal diambil dari session user
            $data['statusacara'] = 0;

            Agenda::query()->create($data);

            DB::commit();

            return back()->with('notify-swal', );

            alert()->flash('Data berhasil ditambahkan', 'success');
        } catch (Exception $e) {
            dd($e);
            Log::debug('Tambah Agenda Error');
            Log::error($e);

            DB::rollBack();
        }
    }
}
