<?php

namespace App\Http\Controllers;

use App\Models\KartuKeluarga;
use App\Models\Kelahiran;
use App\Models\Kematian;
use App\Models\Pendatang;
use App\Models\Penduduk;
use App\Models\Pindah;
use App\Models\SuratUtama;
use Barryvdh\DomPDF\Facade\Pdf;
use Carbon\Carbon;
use Illuminate\Http\Request;

class LaporanController extends Controller
{
    public function exportPendudukPdf(Request $request)
    {
        $startDate = Carbon::parse($request->start_date)->startOfDay();
        $endDate = Carbon::parse($request->end_date)->endOfDay();

        $records = Penduduk::query()
            ->whereBetween('created_at', [$startDate, $endDate])
            ->get();

        $data = [
            'records' => $records,
            'start_date' => $startDate,
            'end_date' => $endDate,
        ];

        $pdf = Pdf::loadView('pdf.laporan_penduduk', $data);

        return $pdf->download('laporan-penduduk-' . now()->format('Y-m-d') . '.pdf');
    }

      public function exportKeluargaPdf(Request $request)
    {
        $startDate = Carbon::parse($request->start_date)->startOfDay();
        $endDate = Carbon::parse($request->end_date)->endOfDay();

        $records = KartuKeluarga::query()
            ->with('kepalaKeluarga')
            ->withCount(['anggotaKeluarga as jumlah_anak' => function ($query) {
                $query->where('hubungan', 'Anak'); // Pastikan 'Anak' sesuai dengan data di database
            }])
            ->whereBetween('created_at', [$startDate, $endDate])
            ->get();

        $data = [
            'records' => $records,
            'start_date' => $startDate,
            'end_date' => $endDate,
        ];

        $pdf = Pdf::loadView('pdf.laporan_keluarga', $data);

        return $pdf->download('laporan-kartu-keluarga-' . now()->format('Y-m-d') . '.pdf');
    }

    public function exportKelahiranPdf(Request $request)
    {
        $startDate = Carbon::parse($request->start_date)->startOfDay();
        $endDate = Carbon::parse($request->end_date)->endOfDay();

        $records = Kelahiran::query()
            ->with(['penduduk', 'anggotaKeluarga.kartuKeluarga'])
            ->whereBetween('created_at', [$startDate, $endDate])
            ->get();

        $data = [
            'records' => $records,
            'start_date' => $startDate,
            'end_date' => $endDate,
        ];

        $pdf = Pdf::loadView('pdf.laporan_kelahiran', $data);

        return $pdf->download('laporan-kelahiran-' . now()->format('Y-m-d') . '.pdf');
    }

    public function exportKematianPdf(Request $request)
    {
        $startDate = Carbon::parse($request->start_date)->startOfDay();
        $endDate = Carbon::parse($request->end_date)->endOfDay();

        $records = Kematian::query()
            ->with('penduduk')
            ->whereBetween('tanggal_kematian', [$startDate, $endDate])
            ->get();

        $data = [
            'records' => $records,
            'start_date' => $startDate,
            'end_date' => $endDate,
        ];

        $pdf = Pdf::loadView('pdf.laporan_kematian', $data);

        return $pdf->download('laporan-kematian-' . now()->format('Y-m-d') . '.pdf');
    }

    public function exportPendatangPdf(Request $request)
    {
        $startDate = Carbon::parse($request->start_date)->startOfDay();
        $endDate = Carbon::parse($request->end_date)->endOfDay();

        $records = Pendatang::query()
            ->with('penduduk')
            ->whereBetween('tanggal_datang', [$startDate, $endDate])
            ->get();

        $data = [
            'records' => $records,
            'start_date' => $startDate,
            'end_date' => $endDate,
        ];

        $pdf = Pdf::loadView('pdf.laporan_pendatang', $data);

        return $pdf->download('laporan-pendatang-' . now()->format('Y-m-d') . '.pdf');
    }

    public function exportPindahPdf(Request $request)
    {
        $startDate = Carbon::parse($request->start_date)->startOfDay();
        $endDate = Carbon::parse($request->end_date)->endOfDay();

        $records = Pindah::query()
            ->with('penduduk')
            ->whereBetween('tanggal_pindah', [$startDate, $endDate])
            ->get();

        $data = [
            'records' => $records,
            'start_date' => $startDate,
            'end_date' => $endDate,
        ];

        $pdf = Pdf::loadView('pdf.laporan_pindah', $data);

        return $pdf->download('laporan-pindah-' . now()->format('Y-m-d') . '.pdf');
    }

   public function exportSuratPdf(Request $request)
    {
        $startDate = Carbon::parse($request->start_date)->startOfDay();
        $endDate = Carbon::parse($request->end_date)->endOfDay();
        $tipeSurat = $request->tipe_surat;

        $query = SuratUtama::query();

        // Tambahkan kondisi ini untuk tidak memfilter jika 'semua_surat' dipilih
        if ($tipeSurat !== 'semua_surat') {
            $query->where('id_tipe_surat', $tipeSurat);
        }
        
        $records = $query->whereBetween('tanggal_pengajuan', [$startDate, $endDate])->get();

        // Pastikan juga array ini mencakup 'semua_surat'
        $tipeSuratOptions = [
            'semua_surat' => 'Semua Surat',
            'surat_kelahiran' => 'Surat Kelahiran',
            'surat_kematian' => 'Surat Kematian',
            'surat_keterangan_ahli_waris' => 'Surat Keterangan Ahli Waris',
            'surat_keterangan_domisili' => 'Surat Keterangan Domisili',
            'surat_keterangan_kehilangan' => 'Surat Keterangan Kehilangan',
            'surat_keterangan_skck' => 'Surat Keterangan SKCK',
            'surat_keterangan_usaha' => 'Surat Keterangan Usaha',
            'surat_pendatang' => 'Surat Keterangan Pendatang',
            'surat_pengantar_perkawinan' => 'Surat Pengantar Perkawinan',
            'surat_permohonan' => 'Surat Permohonan',
            'surat_pindah' => 'Surat Keterangan Pindah',
            'surat_sktm' => 'Surat Keterangan Tidak Mampu (SKTM)',
        ];

        $data = [
            'records' => $records,
            'start_date' => $startDate,
            'end_date' => $endDate,
            'tipe_surat' => $tipeSurat,
            'tipe_surat_label' => $tipeSuratOptions[$tipeSurat] ?? 'Surat Umum',
            'tipeSuratOptions' => $tipeSuratOptions, // Kirim juga array ini ke view PDF
        ];

        $viewName = 'pdf.laporan_surat';
        
        $pdf = Pdf::loadView($viewName, $data);

        return $pdf->download('laporan-surat-' . $tipeSurat . '-' . now()->format('Y-m-d') . '.pdf');
    }

}