<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Imports\BukuImport;
use App\Models\Buku;
use App\Models\Darjah;
use App\Models\JenisSekolah;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class BukuController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        $bukus = Buku::orderBy('kod_buku')->get();
        $statDarjah = $bukus->groupBy('darjah')->map->count();
        $jumlahTotal = $bukus->count();
        $darjahs = Darjah::orderBy('kod_darjah')->get();
        $jenisSekolahs = JenisSekolah::orderBy('kod_jenis')->get();
        return view('admin.buku.index', compact('bukus', 'statDarjah', 'jumlahTotal', 'darjahs', 'jenisSekolahs'));
    }

    public function store(Request $request)
    {
        $request->validate([
            'kod_buku'       => 'required|string|max:50|unique:buku,kod_buku',
            'judul'          => 'required|string|max:255',
            'darjah'         => 'required|string|max:100',
            'jenis_sekolah'  => 'nullable|string|max:20',
        ]);

        Buku::create([
            'kod_buku'       => strtoupper($request->kod_buku),
            'judul'          => $request->judul,
            'darjah'         => $request->darjah,
            'jenis_sekolah'  => $request->jenis_sekolah,
        ]);

        return redirect()->route('admin.buku.index')->with('success', 'Buku berjaya didaftarkan.');
    }

    public function update(Request $request, Buku $buku)
    {
        $request->validate([
            'judul'          => 'required|string|max:255',
            'darjah'         => 'required|string|max:100',
            'jenis_sekolah'  => 'nullable|string|max:20',
        ]);

        $buku->update([
            'judul'          => $request->judul,
            'darjah'         => $request->darjah,
            'jenis_sekolah'  => $request->jenis_sekolah,
        ]);

        return redirect()->route('admin.buku.index')->with('success', 'Buku berjaya dikemaskini.');
    }

    public function destroy(Buku $buku)
    {
        $buku->delete();
        return redirect()->route('admin.buku.index')->with('success', 'Buku berjaya dipadam.');
    }

    public function importPukal(Request $request)
    {
        $request->validate([
            'fail_excel' => 'required|file|mimes:xlsx,xls,csv|max:5120',
        ]);

        Excel::import(new BukuImport, $request->file('fail_excel'));

        return redirect()->route('admin.buku.index')->with('success', 'Daftar pukal buku berjaya diimport.');
    }

    public function templateExcel()
    {
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();

        $sheet->fromArray(['Bil', 'Kod Buku', 'Judul', 'Darjah'], null, 'A1');
        $sheet->fromArray([1, 'BK001', 'Bahasa Melayu Jilid 1', 'Darjah 1'], null, 'A2');
        $sheet->fromArray([2, 'BK002', 'Matematik Tahun 2', 'Darjah 2'], null, 'A3');

        $writer = new Xlsx($spreadsheet);

        return response()->streamDownload(function () use ($writer) {
            $writer->save('php://output');
        }, 'template_buku.xlsx', [
            'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        ]);
    }
}
