<?php

namespace App\Http\Controllers\Admin;

use App\Models\Property;
use Illuminate\Http\Request;
use App\Models\Propertycategory;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Session;
use Yajra\DataTables\Facades\DataTables;
use Illuminate\Support\Facades\Validator;

class Property_controller extends Controller
{
    // show update modal details
    public function edit_propertycategories($id)
    {
        $category=Propertycategory::find($id);

        // dd($category);die();
        if(! $category)
        {
            abort(404);
        }
        return $category;
    }
    // add and edit a property category
    public function storepropertycat(Request $request)
    {
        if($request->propertycat_id)
        {
            $category=Propertycategory::find($request->propertycat_id);

            // dd($category);die();
            if(!$category)
            {
                abort(404);
            }else{
                $request->validate([
                    'property_category'=>'required'
                ]);
        
                $category->update([
                    'propertycat_title'=>$request->property_category,
                ]);
        
                return response()->json([
                    'success'=>'Property Category Has Been Updated Successfully'
                ],201);
            }
        }else{
            $data=$request->all();
            // dd($data);die();
            $request->validate([
                'property_category'=>'required'
            ]);

            Propertycategory::create([
                'propertycat_title'=>$data['property_category']
            ]);

            return response()->json([
                'success'=>'Property Category Has Been Saved In the DB'
            ],201);
        }
    }

    // get property categories
    public function get_propertycategories(Request $request)
    {
        $categories=Propertycategory::select('id','propertycat_title','status');

        if($request->ajax()){
            return DataTables::of ($categories)
            ->addColumn ('action',function($row){
                return 
                    '<a href="#" class="btn btn-success editbutton" data-id="'.$row->id.'">Edit</a>
                     <a href="#" id="deletepropertycat" class="btn btn-danger" data-id="'.$row->id.'">Delete</a>';
            })
            ->rawColumns(['action'])
            ->make(true);
        }
    }

    // delete a property category
    public function delete_propertycategories($id)
    {
        $category=Propertycategory::find($id);

        if($category)
        {
            $category->delete();
            return response()->json([
                'success'=>200,
                'message'=>'Property Category Has Been Deleted Successfully'
            ]);
        }else{
    
            return response()->json([
                'success'=>404,
                'message'=>'Property Category Not Found'
            ]);
        }
    }

    // show all properties and their details
    public function properties()
    { 
        $properties=Property::select('id','property_name','Sold','property_price','propertycat_id');
        Session::put('page','properties');
        return view('Admin.Properties.addproperty',compact('properties'));
    }
}
