<?php

namespace App\Http\Controllers;

use App\Models\OmsDealer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;

class OmsDealerController extends Controller
{
    public function index()
    {
        $dealers = OmsDealer::latest()->get();
        return view('pages.oms_dealers.index', compact('dealers'));
    }

    public function create()
    {
        return view('pages.oms_dealers.create');
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'sale_point' => 'required|string|max:255',
            'dealer_name' => 'required|string|max:255',
            'word_number' => 'required|string|max:255',
            'mobile_number' => 'required|string|unique:oms_dealers,mobile_number|max:20',
            'institute_name' => 'required|string|max:255',
            'image' => 'nullable|image|mimes:jpeg,png,jpg|max:2048',
        ]);

        if ($request->hasFile('image')) {
            $imageName = time() . '.' . $request->image->extension();
            $request->image->move(public_path('uploads/dealer_image'), $imageName);
            $validated['image'] = $imageName;
        }

        OmsDealer::create($validated);
        return redirect()->route('oms_dealers.index')->with('success', 'Dealer created successfully.');
    }

    public function edit($id)
    {
        $dealer = OmsDealer::findOrFail($id);
        return view('pages.oms_dealers.edit', compact('dealer'));
    }

    public function update(Request $request, $id)
    {
        $dealer = OmsDealer::findOrFail($id);

        $validated = $request->validate([
            'sale_point' => 'required|string|max:255',
            'dealer_name' => 'required|string|max:255',
            'word_number' => 'required|string|max:255',
            'mobile_number' => 'required|string|max:20|unique:oms_dealers,mobile_number,' . $dealer->id,
            'institute_name' => 'required|string|max:255',
            'image' => 'nullable|image|mimes:jpeg,png,jpg|max:2048',
        ]);

        if ($request->hasFile('image')) {
            // Delete old image
            if ($dealer->image && File::exists(public_path('uploads/dealer_image/' . $dealer->image))) {
                File::delete(public_path('uploads/dealer_image/' . $dealer->image));
            }
            $imageName = time() . '.' . $request->image->extension();
            $request->image->move(public_path('uploads/dealer_image'), $imageName);
            $validated['image'] = $imageName;
        }

        $dealer->update($validated);
        return redirect()->route('oms_dealers.index')->with('success', 'Dealer updated successfully.');
    }

    public function destroy($id)
    {
        $dealer = OmsDealer::findOrFail($id);
        if ($dealer->image && File::exists(public_path('uploads/dealer_image/' . $dealer->image))) {
            File::delete(public_path('uploads/dealer_image/' . $dealer->image));
        }
        $dealer->delete();
        return back()->with('success', 'Dealer deleted successfully.');
    }

    public function print($id)
    {
        $dealer = OmsDealer::findOrFail($id);
        // Prepare data for QR Code
        $qrData = "Sale Point: {$dealer->sale_point}\n";
        $qrData .= "Dealer: {$dealer->dealer_name}\n";
        $qrData .= "Word: {$dealer->word_number}\n";
        $qrData .= "Mobile: {$dealer->mobile_number}\n";
        $qrData .= "Institute: {$dealer->institute_name}";

        return view('pages.oms_dealers.print', compact('dealer', 'qrData'));
    }
}