<?php

namespace Modules\Travel\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Brian2694\Toastr\Facades\Toastr;
use Modules\Travel\Entities\TravelPlan;
use Modules\Travel\Http\Requests\TravelPlanRequest;
use Modules\Travel\Repositories\TravelPlanRepository;

class TravelPlanController extends Controller
{
    protected $travelPlanRepo;

    public function __construct()
    {
        $this->travelPlanRepo = new TravelPlanRepository;
    }

    public function index(Request $request)
    {
        try {
            if ($request->ajax()) {
                return $this->travelPlanRepo->table($request);
            }
            $data['class']  = 'travel_plan_datatable';
            $data['fields'] = $this->travelPlanRepo->fields();
            $data['table']  = route('travel.plans.index');
            $data['url_id'] = 'travel_plan_datatable_url';
            $data['title'] = _trans('common.Travel Plan List');

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

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

    public function store(TravelPlanRequest $request)
    {
        try {

            $this->travelPlanRepo->storeTravelPlan($request);

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

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

    public function show($id)
    {
        $data['title'] = _trans('common.Travel Plan Details');
        $data['travelPlan'] = TravelPlan::findOrFail($id);
        return view('travel::travel-plans.show', compact('data'));
    }

    public function edit($id)
    {
        $data['title']      = _trans('common.Travel Plan Edit');
        $data['travelPlan'] = TravelPlan::findOrFail($id);

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

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

            $this->travelPlanRepo->updateTravelPlan($request, $id);

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

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

    public function approveOrReject(Request $request, $id)
    {
        try {

            $this->travelPlanRepo->approveOrReject($request, $id);

            $message = _trans('response.Travel Plan has been approved successfully');

            if ($request->is_rejected) {
                $message = _trans('response.Travel Plan has been rejected successfully');
            }

            Toastr::success($message, 'Success');
            return redirect()->route('travel.plans.index');

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

    public function destroy($id)
    {
        try {

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

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

        } catch (\Throwable $th) {
            Toastr::error(_trans('response.You can not delete this Plan'), 'Error');
            return back();
        } 
    }

    public function generateTravelPlanSignature($id)
    {
        try {
            $travelPlan = TravelPlan::findOrFail($id);

            $signature = $travelPlan->signature;

            if (!$signature) {
                return null;
            }

            $image = preg_replace('#^data:image/\w+;base64,#i', '', $signature);
            
            $image = base64_decode($image);

            return \Illuminate\Support\Facades\Response::make($image, 200, [
                'Content-Type' => 'image/png',
                'Content-Disposition' => 'inline; filename="signature.png"'
            ]);
        } catch (\Throwable $th) {
            return null;
        }
    }
}
