<?php
namespace App\Http\Controllers\Admin;
use App\Models\HouseType;
use App\Models\SqftProperty;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class SqftPropertyController extends Controller
{     
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
         if($request->ajax()){
             return datatables()->of(SqftProperty::select('id', 'name', 'status', 'ordering', 'type_of_property'))
            ->addColumn('actions', function($row){
                return '<span class="action-links"><a class="uk-text-primary uk-edit" href="'.url('admin/sqft-property/'.$row->id.'/edit').'" uk-tooltip="Edit Sqft Proerty"><i class="fa-solid fa-pen-to-square"></i></a>  <a data-id="'.$row->id.'" class="uk-text-danger delete-sqft-property" href="javascript:void(0)" uk-tooltip="Delete Sqft Property"><i class="fa-solid fa-trash"></i></a></span>';
            })
            ->addColumn('type_of_property', function($row){
                $typeOfProperty = json_decode($row->type_of_property);
                $typeOfProperty = implode(", ",$typeOfProperty);
                return $typeOfProperty;
            })
            ->addColumn('status', function($row){
                if($row->status == '1'){ 
                    $status = '<a href="javascript:void(0)" data-id="'.$row->id.'" class="uk-label uk-label-success active">Active</a>';
                }else{
                    $status = '<a href="javascript:void(0)" data-id="'.$row->id.'" class="uk-label uk-label-danger inactive">Inactive</a>';
                }
                return $status;
            })
            ->rawColumns(['actions','status', 'type_of_property'])->toJson();
        }
        else{
            $this->bladeContent['title']  = 'Sqft Property'; 
            $this->bladeContent['active'] = 'sqft-property';
            return view('admin.sqft_property.index',$this->bladeContent);
        }
    }
    
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        $this->bladeContent['title']  = 'Add Sqft Property';
        $this->bladeContent['active'] = 'sqft-property';
        return view ('admin.sqft_property.create', $this->bladeContent);
    }
    
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
     public function store(Request $request)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        
        $sqft_prperty           = new SqftProperty();
        $sqft_prperty->name     = request('name');
        $sqft_prperty->ordering = request('ordering');
        $sqft_prperty->save();
        return redirect('admin/sqft-property')->with('success','Sqft property successfully created.');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Users  $users
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request)
    {
        // $request->session()->put('name',$request->input('name'));
        // return view('task')->with('name',$request->session()->get('name'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Users  $users
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        $sqft_prperty                         = SqftProperty::select('id', 'name', 'ordering', 'type_of_property')->where('id', $id)->first();
        $this->bladeContent['title']          = 'Edit Sqft Property';
        $this->bladeContent['active']         = 'sqft-property';
        $this->bladeContent['data']           =  $sqft_prperty;
        $this->bladeContent['typeOfProperty'] = HouseType::select('name')->orderBy('ordering', 'asc')->get();
        return view('admin.sqft_property.edit', $this->bladeContent);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Users  $users
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request,$id)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        
        if(request('type_of_property')){
            $property = json_encode($request->input('type_of_property'));
        }else{
            $property = NULL;
        }
        
        $sqft_prperty                   = SqftProperty::select('id', 'name', 'ordering', 'type_of_property')->where('id', $id)->first();
        $sqft_prperty->name             = request('name');
        $sqft_prperty->ordering         = request('ordering');  
        $sqft_prperty->type_of_property = $property;
        $sqft_prperty->save();
        return redirect()->back()->with('success','Sqft property successfully updated.');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Users  $users
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        SqftProperty::where('id',$id)->delete();
        return response()->json([
            'success' => 1,
            'message' => 'Data successfully deleted.'
        ]);
    } 
    
    public function status(Request $request)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        $id = request('id');
        if($request->type == 'active'){
            $value = '1';
        }else{
            $value = '0';
        }
       SqftProperty::where('id', $id)->update(array('status' => $value));
        return response()->json([
        'success' => 1,
        'message' => 'status changed.'
      ]);
    } 
}