<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\TransferRequest;
use Illuminate\Contracts\Encryption\DecryptException;
use Crypt;
use App\Transfer;
use App\Commission;
use App\User;
use App\Branch;
use EF;

class TransferController extends Controller
{

    public function storeTransfer(TransferRequest $request)
    {

        try
        {
            $form_collect = $request->input();

            //DECRYPTING ENCRYPTED IDS
            $form_collect['user_id']          = decrypt($form_collect['user_id']);
            $form_collect['booking_id']       = decrypt($form_collect['booking_id']);
            $form_collect['bookinguser_id']   = decrypt($form_collect['bookinguser_id']);

            $transfer = new Transfer;

            if($form_collect['bookinguser_id'] !=  $form_collect['user_id']  )
            {
                $curr_transfer = $transfer->storeTransfer($form_collect);

                if(isset($curr_transfer->id))
                {
                    $booking   = $curr_transfer->booking;

                    $old_owner = $curr_transfer->getowner;
                    $new_owner = $booking->user;

                    // SET ERROR MESSAGE APPROVE BOOKING
                    parent::setLogMessage($booking->ms.' ('.$booking->plot->plot_id.'-'.$booking->plot->block_name.')'.EF::plotWhiteLabel()['module'].' has been transfered from '.$old_owner->name.' to '.$new_owner->name.'.');

                    parent::successMessage('Booking has been transfered.',parent::getLogMessage());
                    // END
                 }

            }
            else
            {
                parent::errorMessage('You cannot transfer '.EF::plotWhiteLabel()['module'].' to yourself.');
                 return redirect()->back();
            }


        }
        catch (DecryptException $e)
        {

        }

        return redirect('transferbooking');

    }


    public function updateTransfer(TransferRequest $request)
    {
        try
        {

          $form_collect = $request->input();


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

          $transfer = new Transfer;

          $curr_transfer = $transfer->updateTransfer($form_collect,$id);

        if(isset($curr_transfer->id))
        {
             parent::setLogMessage('Transfer '.$curr_transfer->ms.'  fee '.number_format($form_collect['fee']).' has been updated.');

            parent::successMessage('Transfer has been update.',parent::getLogMessage());
        }

        }

        catch (DecryptException $e)
        {

        }

        return redirect()->back();

    }

     public function transferedBooking()
    {


          $transfer = new Transfer;
          $branch = new Branch;

          $data = [

            'transferbooking_list'  =>  $transfer->getBranchWiseTransfer($branch->getBranchId()),

          ];

         return view('booking.view_transfer_booking',
            [

                'view_booking'  =>  view('components.booking.table_transferbooking',$data),
                'branch'                =>  $branch->getBranchList()

            ]

            );

    }

    public function approveTransfer($id)
    {
        parent::errorMessage('Transfered Booking not found.');

        try
        {
            $id = decrypt($id);

            $transfer = new Transfer;
            $user = new User;
            $commission = new Commission;


            $approved_transfer = $transfer->approveTransfer($id);

            if(isset($approved_transfer->id))
            {
                $booking = $approved_transfer->booking;
                $plot =  $booking->plot;
                $user  = $user->getUserById($booking->user_id);

                $form_collect['role_id']   = EF::getRoleIdByName('Customer');


                // $form_collect['payment_type'] = "Transfer" ;
                $form_collect['percentage']   = 100;
                $form_collect['user_type']    =  $user->type;
                $form_collect['user_id']      =  $user->id;
                $form_collect['booking_id']   =  $booking->id;
                $form_collect['amount']       =  $approved_transfer->transfer_fee;
                $form_collect['description']  =  $plot->plot_id.' '.$plot->block_name." Transfer Fee";
                $form_collect['payment_type'] = "Transfer";


           $com = new Commission;

           if ($approved_transfer->transfer_fee > 0)
           {
                if($com->storeCommission($form_collect))
                {
                    parent::setLogMessage('Transfer '.$approved_transfer->ms.' has been approved.');

                    parent::successMessage('Booking Transfered has been approved.',parent::getLogMessage());
                }
           }
           else
           {

                parent::setLogMessage('Transfer '.$approved_transfer->ms.' has been approved.');

                parent::successMessage('Transfer has been approved.',parent::getLogMessage());
           }



            }
        }
        catch (DecryptException $e)
        {

        }

        return redirect()->back();
    }


}
