<?php

namespace Modules\Travel\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Brian2694\Toastr\Facades\Toastr;
use Modules\Travel\Entities\TravelWorkflow;
use Modules\Travel\Http\Requests\TravelWorkflowRequest;
use Modules\Travel\Repositories\TravelWorkflowRepository;

class TravelWorkflowController extends Controller
{
    protected $travelWorkflowRepo;

    public function __construct()
    {
        $this->travelWorkflowRepo = new TravelWorkflowRepository;
    }

    public function index(Request $request)
    {
        try {
            if ($request->ajax()) {
                return $this->travelWorkflowRepo->table($request);
            }
            $data['class']  = 'travel_workflow_datatable';
            $data['fields'] = $this->travelWorkflowRepo->fields();
            $data['table']  = route('travel.workflows.index');
            $data['url_id'] = 'travel_workflow_datatable_url';

            if (env('TRAVEL')) {
                $data['title'] = _trans('common.Daily Work Report');
            } else {
                $data['title'] = _trans('common.Travel Workflow List');
            }

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

    public function create()
    {
        if (env('TRAVEL')) {
            $data['title'] = _trans('common.Daily Work Report Create');
        } else {
            $data['title'] = _trans('common.Travel Workflow Create');
        }

        return view('travel::travel-workflows.create', compact('data'));
    }

    public function store(TravelWorkflowRequest $request)
    {
        try {

            $this->travelWorkflowRepo->storeTravelWorkflow($request);

            Toastr::success(_trans('response.Travel Workflow has been created successfully'), 'Success');
            return redirect()->route('travel.workflows.index');

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

    public function show($id)
    {
        if (env('TRAVEL')) {
            $data['title'] = _trans('common.Daily Work Report Details');
        } else {
            $data['title'] = _trans('common.Travel Workflow Details');
        }

        $data['travelWorkflow'] = TravelWorkflow::find($id);
        return view('travel::travel-workflows.show', compact('data'));
    }

    public function edit($id)
    {
        if (env('TRAVEL')) {
            $data['title'] = _trans('common.Daily Work Report Edit');
        } else {
            $data['title'] = _trans('common.Travel Workflow Edit');
        }

        $data['travelWorkflow'] = TravelWorkflow::find($id);

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

    public function update(TravelWorkflowRequest $request, $id)
    {
        try {

            $this->travelWorkflowRepo->updateTravelWorkflow($request, $id);

            Toastr::success(_trans('response.Travel Workflow has been updated successfully'), 'Success');
            return redirect()->route('travel.workflows.index');

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

    public function destroy($id)
    {
        try {

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

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

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