<?php

namespace App\Http\Controllers\Backend;

use App\Helpers\CoreConstants;
use App\Http\Controllers\Controller;
use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class PurchesController extends Controller
{
    public function create(){
        $data['PageTitle'] = 'Add Purches';
        $data['parentPurchesShowClass'] = 'show';
        $data['allPurchesActiveClass'] = 'active';
        return view('backend.purches.create')->with($data);
    }
    public function searchresult(Request $request)
    {
        Log::info($request->all());
        if ($request->has('term')) {
            $term = $request->input('term');
            $product = Product::where('name', 'LIKE', '%' . $term . '%')->select('id', 'name', 'stock', 'price','product_code')->get();

            $Product_array = [];

            foreach ($product as $item) {
                $Product_array[] = [
                    'id' => $item->id,
                    'label' => $item->name, // Sanitize the name
                    'value' => $item->name,
                    'stock' => $item->stock,
                    'price' => $item->price,
                    'product_code' => $item->product_code,
                ];
            }

            return response()->json($Product_array);
        }
    }
}
