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

class TermsConditionsController 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(TermCondition::select('id', 'type', 'title'))
            ->addColumn('actions', function($row){
                return '<span class="action-links"><a class="uk-text-primary uk-edit" href="'.url('admin/terms-conditions/'.$row->id.'/edit').'" uk-tooltip="Edit"><i class="fa-solid fa-pen-to-square"></i></a></span>';
            })
            ->rawColumns(['actions'])->toJson();
        }
        else{
            $this->bladeContent['title']  = 'Terms Conditions'; 
            $this->bladeContent['active'] = 'terms-conditions';
            return view('admin.terms_conditions.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 Term Condition';
        $this->bladeContent['active'] = 'terms-conditions';
        return view ('admin.terms_conditions.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('/');
        }
        
        $termCondtion              = new TermCondition();
        $termCondtion->type        = request('type');
        $termCondtion->description = request('description');
        $termCondtion->save();
        
        return redirect('admin/terms-conditions')->with('success','Term condition 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('/');
        }
        $termCondtion                 = TermCondition::select('id', 'title', 'type', 'description')->where('id', $id)->first();
        $this->bladeContent['title']  = 'Edit Term Condition';
        $this->bladeContent['active'] = 'terms-conditions';
        $this->bladeContent['data']   =  $termCondtion;
        return view('admin.terms_conditions.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('/');
        }
        
        $termCondtion              = TermCondition::select('id', 'title', 'type', 'description')->where('id', $id)->first();
        $termCondtion->title        = request('title');
        $termCondtion->type        = request('type');
        $termCondtion->description = request('description');
        $termCondtion->save();
        return redirect()->back()->with('success','Term condition successfully updated.');
    }

}