<?php

// namespace App\Http\Controllers;

// use Illuminate\Http\Request;
// use Illuminate\Support\Facades\Storage;
// use App\Models\PurchaseInvoice;

// class PurchaseInvoiceController extends Controller
// {
//     public function index()
//     {
//         $invoices = PurchaseInvoice::latest()->get();
//         return view('purchase_invoice.index', compact('invoices'));
//     }

//     /**
//      * Show the form for creating a new purchase invoice.
//      */
//     public function create()
//     {
//         return view('purchase_invoices.form', [
//             'action' => route('purchase-invoice.store'),
//             'method' => 'POST',
//             'car' => null, // Optional - for old data reuse if needed
//         ]);
//     }

//     /**
//      * Store a newly created purchase invoice in storage.
//      */
//     public function store(Request $request)
//     {
//         $validated = $request->validate([
//             'make' => 'required|string|max:255',
//             'model' => 'required|string|max:255',
//             'registration_no' => 'nullable|string|max:255',
//             'purchase_date' => 'required|date',
//             'purchase_price' => 'required|numeric|min:0',
//             'vendor_name' => 'nullable|string|max:255',
//             'invoice_file' => 'nullable|file|mimes:pdf,jpg,jpeg,png|max:2048',
//         ]);

//         // Handle file upload
//         if ($request->hasFile('invoice_file')) {
//             $path = $request->file('invoice_file')->store('purchase_invoices');
//             $validated['invoice_file_path'] = $path;
//         }

//         // Create the invoice
//         PurchaseInvoice::create($validated);

//         return redirect()->route('purchase-invoice.create')->with('success', 'Purchase invoice submitted successfully.');
//     }
// }




namespace App\Http\Controllers;

use App\Models\PurchaseInvoice;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class PurchaseInvoiceController extends Controller
{
    public function index()
    {
        // $cars = Car::where('status', 1)->orderBy('id', 'desc')->get();

        $invoices = PurchaseInvoice::where('status', 1)->orderBy('id', 'desc')->get();
        return view('purchase_invoice.index', compact('invoices'));

    }

    public function create()
    {
        return view('purchase_invoice.form', [
            'invoice' => null,
            'action' => route('purchase-invoice.store'),
            'method' => 'POST',
        ]);
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'make' => 'required|string|max:255',
            'model' => 'required|string|max:255',
            'varient' => 'required|string|max:255',
            'registration_no' => 'nullable|string|max:100',
            'purchase_date' => 'required|date',
            'purchase_price' => 'required|numeric',
            'vendor_name' => 'nullable|string|max:255',
            'invoice_file_path' => 'nullable|file|mimes:pdf,jpg,jpeg,png',
        ]);

        if ($request->hasFile('invoice_file_path')) {
            $validated['invoice_file_path'] = $request->file('invoice_file_path')->store('invoices', 'public');
        }

        PurchaseInvoice::create($validated);

        return redirect()->route('purchase-invoice.index')->with('success', 'Invoice added successfully.');
    }

    public function edit(PurchaseInvoice $purchase_invoice)
    {
        return view('purchase_invoice.form', [
            'invoice' => $purchase_invoice,
            'action' => route('purchase-invoice.update', $purchase_invoice),
            'method' => 'PUT',
        ]);
    }

    public function update(Request $request, PurchaseInvoice $purchase_invoice)
    {
        $validated = $request->validate([
            'make' => 'required|string|max:255',  
            'model' => 'required|string|max:255',
            'varient' => 'required|string|max:255',
            'registration_no' => 'nullable|string|max:100',
            'purchase_date' => 'required|date',
            'purchase_price' => 'required|numeric',
            'vendor_name' => 'nullable|string|max:255',

            'seller_cnic' => 'nullable|string|max:255',
            'seller_phone' => 'nullable|string|max:255',
            'seller_address' => 'nullable|string|max:255',

            'invoice_file_path' => 'nullable|file|mimes:pdf,jpg,jpeg,png',
        ]);

        if ($request->hasFile('invoice_file_path')) {
            // Optional: Delete old file
            if ($purchase_invoice->invoice_file_path) {
                Storage::disk('public')->delete($purchase_invoice->invoice_file_path);
            }

            $validated['invoice_file_path'] = $request->file('invoice_file_path')->store('invoices', 'public');
        }

        $purchase_invoice->update($validated);

        return redirect()->route('purchase-invoice.index')->with('success', 'Invoice updated successfully.');
    }

    public function destroy(PurchaseInvoice $purchase_invoice)
    {
        if ($purchase_invoice->invoice_file_path) {
            Storage::disk('public')->delete($purchase_invoice->invoice_file_path);
        }

        $purchase_invoice->delete();

        return redirect()->route('purchase-invoice.index')->with('success', 'Invoice deleted successfully.');
    }

    // print purchase invoice
    public function print($id)
    {
        $invoice = PurchaseInvoice::findOrFail($id);
    
        return view('purchase_invoice.print', compact('invoice'));
    }
    


    // public function report($id)
    // {
    //     $invoice = PurchaseInvoice::findOrFail($id);

    //     // Update status to 0
    //     $invoice->status = 0;
    //     $invoice->save();

    //     // Redirect to Car Inspection form with the invoice ID
    //     return redirect()->route('cars.create', ['invoice' => $invoice->id]);
    // }



}
