<?php

namespace App\Http\Controllers\md;

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

class FlatController extends Controller
{
    public function allFlats() {
        // $products = DB::table('products')
        // ->join('locations', 'products.location_id', '=', 'locations.id')
        // ->select('products.name', 'locations.name as location_name', 'products.quantity')
        // ->get();


        $allFlats = DB::table('flats')
    ->join('product_template', 'flats.product_template_id', '=', 'product_template.id')
    ->select('product_template.name as product_name', 'flats.flat_number', 'flats.available')
    ->get();
        return view('md.flats.all_flat', compact('allFlats'));
    }

    public function addFlats() {
        $products = DB::table('product_template')->get();
        return view('md.flats.add_flat', compact('products'));
    }

    public function storeFlats(Request $request) {
        try {
            $flatLocation = new Flat();
            $flatLocation->product_template_id = $request->product_template_id;
            $flatLocation->flat_number = $request->flat_number;
            $flatLocation->available = $request->available;
            $flatLocation->save();
            return redirect()->route('all.flat')->with('success', 'Flat Added Successfully');

        } catch (\Throwable $th) {
          return $th;
        }

    }
}
