<?php

namespace Modules\Travel\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Brian2694\Toastr\Facades\Toastr;
use Modules\Travel\Entities\TravelMeeting;
use Modules\Travel\Http\Requests\TravelPlanRequest;
use Modules\Travel\Http\Requests\TravelMeetingRequest;
use Modules\Travel\Repositories\TravelMeetingRepository;

class TravelMeetingController extends Controller
{
    protected $travelMeetingRepo;

    public function __construct()
    {
        $this->travelMeetingRepo = new TravelMeetingRepository;
    }

    public function index(Request $request, $plan_id)
    {
        try {
            if ($request->ajax()) {
                return $this->travelMeetingRepo->table($request, $plan_id);
            }
            $data['class']  = 'travel_plan_meeting_datatable';
            $data['fields'] = $this->travelMeetingRepo->fields();
            $data['table']  = route('travel.plans.meetings.index', $plan_id);
            $data['url_id'] = 'travel_plan_meeting_datatable_url';
            $data['title'] = _trans('common.Travel Meeting List');
            $data['plan_id'] = $plan_id;

            return view('travel::travel-meetings.index', compact('data'));
        } catch (\Throwable $th) {
            Toastr::error(_trans('response.Something went wrong!'), 'Error');
            return redirect()->back();
        }
    }

    public function create($plan_id)
    {
        $data['title']   = _trans('common.Travel Meeting Create');
        $data['plan_id'] = $plan_id;
        return view('travel::travel-meetings.create', compact('data'));
    }

    public function store(TravelMeetingRequest $request, $plan_id)
    {
        try {

            $this->travelMeetingRepo->storeTravelMeeting($request, $plan_id);

            Toastr::success(_trans('response.Travel Meeting has been created successfully'), 'Success');
            return redirect()->route('travel.plans.meetings.index', $plan_id);

        } catch (\Throwable $th) {
            Toastr::error(_trans('response.Something went wrong!'), 'Error');
            return back();
        }        
    }

    public function show($plan_id, $id)
    {
        $data['title'] = _trans('common.Travel Meeting Details');
        $data['travelMeeting'] = TravelMeeting::findOrFail($id);
        return view('travel::travel-meetings.show', compact('data'));
    }

    public function edit($plan_id, $id)
    {
        $data['title']          = _trans('common.Travel Meeting Edit');
        $data['travelMeeting']  = TravelMeeting::findOrFail($id);

        return view('travel::travel-meetings.edit', compact('data'));
    }

    public function update(TravelMeetingRequest $request, $plan_id, $id)
    {
        try {

            $travelMeeting = $this->travelMeetingRepo->updateTravelMeeting($request, $id);

            Toastr::success(_trans('response.Travel Meeting has been updated successfully'), 'Success');
            return redirect()->route('travel.plans.meetings.index', $travelMeeting->travel_plan_id);

        } catch (\Throwable $th) {
            Toastr::error(_trans('response.Something went wrong!'), 'Error');
            return back();
        }   
    }

    public function destroy($id)
    {
        try {

            $this->travelMeetingRepo->delete($id);

            Toastr::success(_trans('response.Travel Meeting has been deleted successfully'), 'Success');
            return redirect()->back();

        } catch (\Throwable $th) {
            Toastr::error(_trans('response.Something went wrong!'), 'Error');
            return back();
        } 
    }
}
