<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\DocumentRequest;
use App\Document;
use App\Booking;

class DocumentController extends Controller
{
   

public function storeDocument(DocumentRequest $request)
    {
    	try
    	{


    	$form_collect = $request->input();


    	$form_collect['booking_id']  = decrypt($form_collect['booking_id']);

    	$form_collect['user_id'] = decrypt($form_collect['user_id']);
        $form_collect['document'] = ($request->document);


    	$document = new Document;

        $booking_obj = new Booking;

         $booking = $booking_obj->getBookingById($form_collect['booking_id']);

    	if($document->storeDocument($form_collect))
    	{
                 parent::setLogMessage($document->description.' document has been uploaded against '.$booking->ms.' ('.$booking->plot->plot_id.' '.$booking->plot->type.') plot.');

              parent::successMessage('Document has been uploaded.');
    	}	

    	}
    	
    	catch (DecryptException $e) 
        {
            
        }

        return redirect()->back();

    }

    public function removeDocument($id)
    { 
        parent::errorMessage('Document not found.');

        try 
        {
            $id = decrypt($id);

            $document = new Document;

            if($document->removeDocument($id))
            {
                parent::successMessage('Document has been removed.',parent::getLogMessage());
            } 
        } 
        catch (DecryptException $e) 
        {
            
        }

        return redirect()->back();
    }


      

}
