<?php

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

class ProductListController extends Controller
{
  public function allProducts(){
    $products = DB::table('product_template')->get();
    return view('sales.products.all-products', compact('products'));
  }

//   public function availableFlatsonProducts($id){
//     $allProductID = DB::table('product_template')->where('id', $id)->first();
//     $allFlats = DB::table('flats')->where('prodct_template_id', $allProductID)
//     ->join('product_template', 'flats.product_template_id', '=', 'product_template.id')
//     ->select('product_template.name as product_name', 'flats.flat_number', 'flats.available')
//     ->get();
//     return response()->json( $allFlats);
//   }

  public function availableFlatsonProducts($id){

    $allProduct = DB::table('product_template')->where('id', $id)->first();

    if ($allProduct) {
        $allFlats = DB::table('flats')
            ->where('product_template_id', $allProduct->id)
            ->where('available', '>', 0) // Use the id property of $allProduct
            ->join('product_template', 'flats.product_template_id', '=', 'product_template.id')
            ->select('product_template.name as product_name', 'flats.flat_number', 'flats.available')
            ->get();
            return response()->json(['status' => 200, 'flats' => $allFlats]);
    } else {
        // Handle the case where the product with the given ID is not found.
        return response()->json(['error' => 'Product not found'], 404);
    }
}

}
