<?php

namespace App\Http\Controllers;

use App\Exports\ProsesExport;
use App\Models\Confidence;
use App\Models\EclatCalculation;
use App\Models\Itemset2;
use App\Models\Itemset3;
use App\Models\Proses;
use Barryvdh\DomPDF\Facade\Pdf;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class HasilController extends Controller
{
    public function index()
    {
        $proses = Proses::orderByDesc('id')->get();
        $count = Proses::count();
        return view('layouts.pages.hasil', compact('proses', 'count'));
    }

    public function show($id)
    {
        $proses = Proses::findOrFail($id);
        $itemset2 =
            Confidence::where('itemset', '2-item')->where('proses_id', $id)->get();
        $itemset3 =
            Confidence::where('itemset', '3-item')->where('proses_id', $id)->get();

        return view('layouts.pages.detail', compact('proses', 'itemset2', 'itemset3'));
    }

    public function generatePDF($id)
    {
        $proses = Proses::findOrFail($id);
        $itemset2 =
            Confidence::where('itemset', '2-item')->where('proses_id', $id)->get();
        $itemset3 =
            Confidence::where('itemset', '3-item')->where('proses_id', $id)->get();
        $rules =
            Confidence::where('proses_id', $id)->where('keterangan', 'Lolos')->get();

        $pdf = Pdf::loadView('layouts.pages.pdf', compact('proses', 'itemset2', 'itemset3', 'rules'));

        $timestamp = Carbon::now()->format('Ymd_His');
        $fileName = 'eclat_' . $timestamp . '.pdf';

        return $pdf->download($fileName);
    }
    public function exportExcel($id)
    {
        $timestamp = Carbon::now()->format('Ymd_His');
        $fileName = 'eclat_' . $timestamp . '.xlsx';

        return Excel::download(new ProsesExport($id), $fileName);
    }
}
