<?php

namespace App\Http\Controllers\Report;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class LaporanStiker extends Controller
{
    public function laporanStikerHarian()
    {
        return view('report.stiker.stiker-harian', [
            'filterSelect' => [
                'jenisLangganan' => DB::table('jenis_langganan')->get(),
                'jenisStiker'    => DB::table('stasiunkerja')->get(),
                'softsetting'    => DB::table('softseting')->first(),
            ]
        ]);
    }

    public function dataStiker(Request $request)
    {
        // 🔐 Validasi dasar
        $validated = $request->validate([
            'tanggalAwal'     => 'required|date',
            'tanggalAkhir'    => 'required|date',
            'jenis_langganan' => 'array|nullable',
            'jenis_stiker'    => 'array|nullable',
            'name'            => 'string|nullable',
            'no_pol'          => 'string|nullable',
        ]);

        $query = DB::table('transaksi_stiker as a')
            ->leftJoin('detail_transaksi_stiker as b', 'a.notrans', '=', 'b.notrans')
            ->leftJoin('jenis_langganan as c', 'a.jenis_langganan', '=', 'c.id')
            ->leftJoin('stasiunkerja as d', 'a.unit_kerja', '=', 'd.kode')
            ->leftJoin('pegawai as p', 'a.operator', '=', 'p.nomer')
            // ->leftJoin('tarif_stiker as ts','ts.id_mobil','=','a.jenis_mobil')
            ->leftJoin('tarif_stiker as ts', function ($join) {
                $join->on('ts.id_mobil', '=', 'b.jenis_mobil')
                    ->on('ts.jenis_langganan', '=', 'b.jenis_member');
            })
            ->select('a.*', 'b.*', 'c.nama as jenis_langganan','d.nama as stasiunkerja','p.nama as petugas','ts.tarif')
            ->whereBetween('a.tanggal', [
                $validated['tanggalAwal'] . ' 00:00:00',
                $validated['tanggalAkhir'] . ' 23:59:59'
            ])
            ->where('b.status', 1)

            // 🔹 Conditional filters
            ->when($request->name, fn ($q, $v) =>
                $q->where('a.nama', 'ILIKE', "%{$v}%")
            )
            ->when($request->no_pol, fn ($q, $v) =>
                $q->where('a.nopol', 'ILIKE', "%{$v}%")
            )
            ->when($request->jenis_langganan, fn ($q, $v) =>
                $q->whereIn('a.jenis_langganan', $v)
            )
            ->when($request->jenis_stiker, fn ($q, $v) =>
                $q->whereIn('a.unit_kerja', $v)
            )
            ->orderBy('a.nama');

        // 🪵 Debug (aktifkan hanya saat local)
        if (app()->isLocal()) {
            // Log::info('SQL Bindings', $query->getBindings());
            // Log::info('SQL Query', $query->toSql());
            // dd($query->toSql(), $query->getBindings());
        }

        return response()->json([
            'success' => true,
            'data' => $query->get(),
        ]);
    }
}

    // public function laporanStikerHarian() 
    // {
    //     $jenisLangganan = DB::table('jenis_langganan')->get();
    //     $jenisStiker = DB::table('stasiunkerja')->get();
    //     $filterSelect = [
    //         'jenisLangganan' => $jenisLangganan,
    //         'jenisStiker' => $jenisStiker
    //     ];

    //     return view ('report.stiker.stiker-harian', compact('filterSelect'));
    // }

    // public function dataStiker (Request $request)
    // {
    //     Log::info($request->jenis_langganan);
    //     $jenisLangganan = $request->jenis_langganan;
    //     $stasiunKerja = $request->jenis_stiker;
    //     $nama = $request->name;
    //     $nopol = $request->no_pol;
    //     $startDate = $request->tanggalAwal;
    //     $endDate  = $request->tanggalAKhir;
    //     Log::info($startDate);
    //     Log::info($endDate);
    //     $query = DB::table('mergetransaksistikerdetail as a')
    //     ->leftJoin('stasiunkerja as b', 'a.unit_kerja', '=', 'b.kode')
    //     ->whereBetween('a.tgl_edited', [
    //         $startDate . ' 00:00:00',
    //         $endDate . ' 23:59:59'])
    //      ->select(
    //         'a.*',
    //         'b.nama as jenis_langganan'
    //     );

    //     if (!empty($nama)) {
    //         $query->where('a.nama', 'LIKE', '%' . $nama . '%');
    //     }

    //     if (!empty($nopol)) {
    //         $query->where('a.nopol', 'LIKE', '%' . $nopol . '%');
    //     }

    //     if (!empty($jenisLangganan)) {
    //         $query->whereIn('a.jenis_langganan', $jenisLangganan);
    //     }

    //     if (!empty($stasiunKerja)) {
    //         $query->whereIn('a.unit_kerja', $stasiunKerja);
    //     }

    //     Log::info($query->getBindings());

    //     $data = $query->get();


    //     return response()->json([
    //         'success' => true,
    //         'data'  => $data,
    //     ]);
    // }
