<?php

namespace App\Http\Controllers;

use EF;
use Illuminate\Http\Request;
use App\User;
use Crypt;
use App\Commission;
use App\Booking;
use Spatie\Permission\Models\Role;

use App\Http\Requests\CommissionRequest;


class CommissionController extends Controller
{
    public function storeCommission(CommissionRequest $request)
    {
        try
        {

          $form_collect = $request->input();



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

          $form_collect['role_id']    = decrypt($form_collect['user_type']);
          $form_collect['user_id'] = decrypt($form_collect['user_id'] );
          $form_collect['role_id']    = decrypt($form_collect['user_type']);
          $form_collect['payment_type'] = "Commission";



          $commission = new Commission;

            $user = new User;

            $curr_user = $user->getUserById($form_collect['user_id']);

            if(isset($curr_user->id) && in_array($form_collect['role_id'],$curr_user->getCurrentUserRolesArrayWithColumn('id')))
            {
               $form_collect['role_id'] = $form_collect['role_id'];

               $curr_commission = $commission->storeCommission($form_collect);

            }
            else
            {
              parent::successMessage('Invalid User .');
            }




        if(isset($curr_commission->id))
        {

            $booking = $curr_commission->booking;

            if(isset($booking->id))
            {
                parent::setLogMessage($curr_commission->description.' side payments has been added against '.$booking->ms.' ('.$booking->plot->plot_id.' '.$booking->plot->type.') article.');
            }

            parent::successMessage('Side Payments has been created.',parent::getLogMessage());
        }

        }

        catch (DecryptException $e)
        {

        }

        return redirect()->back();
 

    }

    public function storeExtraCharges(CommissionRequest $request)
    {
        try
        {

          $form_collect = $request->input();




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

          $booking = new Booking;
          $commission = new Commission;

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

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

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

          if(isset($booking_detail->id))
          {
              $form_collect['user_id']     = $booking_detail->user->id;
              $form_collect['user_type']   = $booking_detail->user->type;
              $form_collect['percentage']  = NULL;

          if($form_collect['charges_type'] == 'Miscellaneous Charges')
          {
            $form_collect['payment_type'] = "Miscellaneous Charges";
          }
          elseif($form_collect['charges_type'] == 'Document Charges')
          {
            $document_charges = $commission->getBookingDocumentCharges($form_collect['booking_id']);
            if($document_charges > 0)
            {

                parent::errorMessage('Document Charges Already Exist');
                 return redirect()->back();
            }
            if($form_collect['amount'] != 5000)
            {

               parent::errorMessage('Invalid Amount For Documentation Charges');

                return redirect()->back();
            }
            else
            {
                $form_collect['payment_type'] = "Document Charges";
            }


          }

          $commission = new Commission;

          $curr_commission = $commission->storeCommission($form_collect);


        if(isset($curr_commission->id))
        {

            $booking = $curr_commission->booking;

            if(isset($booking->id))
            {
                parent::setLogMessage($curr_commission->description.' side payments has been created against '.$booking->ms.' ('.$booking->plot->plot_id.' '.$booking->plot->type.') article.');
            }

            parent::successMessage('Side payments has been created.',parent::getLogMessage());
        }
          }


        }

        catch (DecryptException $e)
        {

        }

        return redirect()->back();


    }
    public function updateCommission(CommissionRequest $request)
    {

        try
        {

          $form_collect = $request->input();


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

          $commission = new Commission;

          $curr_commission = $commission->updateCommission($form_collect,$id);

          if (isset($form_collect['role'])) {
            $form_collect['role'] = decrypt($form_collect['role']);
        }
     
          if(isset($curr_commission))
        {
            $booking = $curr_commission->booking;

            if(isset($booking->id))
            {
                parent::setLogMessage($curr_commission->description.' side payment has been updated against '.$booking->ms.' ('.$booking->plot->plot_id.' '.$booking->plot->type.') plot.');
            }

            parent::successMessage('Side Payment has been updated.',parent::getLogMessage());

        }

        }

        catch (DecryptException $e)
        {

        }

        return redirect()->back();

    }

    public function updateExtraCharges(CommissionRequest $request)
    {

        try
        {

          $form_collect = $request->input();



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

          $commission = new Commission;

          $form_collect['percentage'] = NULL;
          if($form_collect['charges_type'] == 'Document Charges' && $form_collect['updateamount'] != 5000 )
          {

               parent::errorMessage('Invalid Amount For Documentation Charges');

                return redirect()->back();

          }


          $curr_commission = $commission->updateCommission($form_collect,$id);

        if(isset($curr_commission))
        {
            $booking = $curr_commission->booking;

            if(isset($booking->id))
            {
                parent::setLogMessage($curr_commission->description.' side payment has been updated against '.$booking->ms.' ('.$booking->plot->plot_id.' '.$booking->plot->type.') article.');
            }

            parent::successMessage($form_collect['charges_type'].' has been updated.',parent::getLogMessage());

        }

        }

        catch (DecryptException $e)
        {

        }

        return redirect()->back();

    }




    public function removeCommission($id)
    {
        parent::errorMessage('Side Payment not found.');

        try
        {
            $id = decrypt($id);

            $commission = new Commission;

            $curr_commission = $commission->removeCommission($id);

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

                if(isset($booking->id))
                {
                    parent::setLogMessage($curr_commission->description.' side payment has been removed against '.$booking->ms.' ('.$booking->plot->plot_id.' '.$booking->plot->type.') plot.');
                }

                parent::successMessage('Side payment has been removed.',parent::getLogMessage());

            }
        }
        catch (DecryptException $e)
        {

        }

        return redirect()->back();
    }

     public function getComission($id){
        $id=Crypt::decrypt($id);
        $commission = new Commission();
        $get_commission=$commission->getBookingCommissions($id);


        $view = '<option value="'.Crypt::encrypt(0).'">Select Commission</option>';
        foreach($get_commission as $rows){

        $view .= '<option value="'.Crypt::encrypt($rows->id).'">'.$rows->description.'</option>';
        }


         return $view;
    }

    public function getCommissionAmount($id)
    {
         if(\Request::ajax())
    {
         try
       {

        $id = Crypt::decrypt($id);
        $comission= new Commission();

        $current_commission = $comission->getCommissionById($id);

        $user = $current_commission->user;

        $amount=$current_commission->amount - $current_commission->received_amount;

       $data = array();
       $data['amount'] = $amount;

       $data['user_view'] = '<option value="'.Crypt::encrypt($user->id).'">'.ucfirst($user->name).' '.ucfirst($user->designation).'</option>';

       $data['type'] = $user->type;

// =======
//        $data['type'] = $user->userRoles()->first()->pluck('name');


//     //    $data['user_view'] = '<option value="'.Crypt::encrypt($user->id).'">'.ucfirst($user->name).' '.ucfirst($user->designation).'</option>';
//     //    $data['type'] = $user->type;
       
// >>>>>>> origin/dashboard-ajaxbased
       return $data;
   }
   catch (DecryptException $e)
       {

       }
    }
       return redirect()->back();
}

      public function massInsertCommission($salesperson,$dealer,$booking_id)
     {

        $commission = new Commission;

        foreach ($salesperson as $rows)
        {
            try
            {
                $data = [

                    'user_id'           =>  decrypt($rows),
                    'booking_id'        =>  $booking_id,
                    'user_type'         => 'Employee',
                    'payment_type'      => 'Commission',
                    'description'       => 'Employee Commission',
                    'percentage'        => 0,
                    'amount'            => 0,
                ];

                $commission->storeCommission($data);

            }
            catch (DecryptException $e)
            {

            }


        }

        foreach ($dealer as $rows)
        {

            try
            {
                $data = [

                    'user_id'           =>  decrypt($rows),
                    'booking_id'        =>  $booking_id,
                    'user_type'         => 'Dealer',
                    'payment_type'      => 'Commission',
                    'description'       => 'Dealer Commission',
                    'percentage'        => 0,
                    'amount'            => 0,

                ];

                $commission->storeCommission($data);


            }
            catch (DecryptException $e)
            {

            }

        }



        return 0;
    }

}
