<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\UserRequest;
use App\User;
use Crypt;
use App\Nominee;
use App\Booking;
use EF;




class NomineeController extends Controller
{
    public function storeNominee(UserRequest $request)
    {
        try
        {

            $branch_label = EF::branchWhiteLabel();

            $form_collect = $request->input();
            // dd($form_collect);
            $form_collect['designation'] = NULL;
            $form_collect['password']    = NULL;
            $form_collect['type']        = "Customer";
            $form_collect['booking_id']  = decrypt( $form_collect['booking_id']);
            $form_collect['relation_id'] = decrypt( $form_collect['relation_id']);

            if(isset($form_collect['user_id'])){
                $form_collect['user_id'] = decrypt( $form_collect['user_id']);
            }
            // $form_collect[$branch_label['module']]    = decrypt( $form_collect[$branch_label['module']]);
            // BY DEFAULT BRANCH ATTACH WITH ALL USER RED SUN ITS ID === 1
            $form_collect[$branch_label['module']] = 1;
            $form_collect['user_image']  = $request->user_image;

            $form_collect['designation_id']        = 0;
            $form_collect['employee_type']         = NULL;
            $form_collect['salary_mode']           = NULL;
            $form_collect['bank_branch']           = NULL;
            $form_collect['bank_name']             = NULL;
            $form_collect['account_no']            = NULL;
            $form_collect['employee_form']         = NULL;
            $form_collect['blood_group']           = NULL;
            $form_collect['vendor_type']           = 0;







            $nominee = new Nominee;

            $curr_user  = $nominee->storeNominee($form_collect,$branch_label['module']);

            $booking = new Booking;

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



            if(isset($curr_user->id) && isset($booking->id))
            {

               parent::setLogMessage($form_collect['nok_type'].' "'.$curr_user->name.'" has been created '.$booking->ms.' ('.$booking->plot->plot_id.' '.$booking->plot->block_name.') plot.');

               parent::successMessage('Nominee/ Co Owner has been created.',parent::getLogMessage());
             }
        }
         catch (DecryptException $e)
        {

        }



       return back();

    }

    //UPDATE NOMINEE
     public function updateNominee(UserRequest $request)
    {
        try
        {
            $form_collect = $request->input();

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

            $form_collect['user_image']  = $request->user_image;

            $form_collect['type'] =   'Customer';
            $form_collect['designation_id']        = 0;
            $form_collect['employee_type']         = NULL;
            $form_collect['salary_mode']           = NULL;
            $form_collect['bank_branch']           = NULL;
            $form_collect['bank_name']             = NULL;
            $form_collect['account_no']            = NULL;
            $form_collect['employee_form']         = NULL;
            $form_collect['blood_group']           = NULL;
            $form_collect['vendor_type']           = 0;

            $branch_label = EF::branchWhiteLabel();



            // $form_collect[$branch_label['module']] = decrypt($form_collect[$branch_label['module']]);

            // BY DEFAULT BRANCH ATTACH WITH ALL USER RED SUN ITS ID === 1
            $form_collect[$branch_label['module']] = 1;

           $nominee = new Nominee;

           $curr_nominee = $nominee->updateNominee($form_collect,$branch_label['module']);




            if(isset($curr_nominee->id))
            {
               $curr_user = $curr_nominee->user;

               $booking = new Booking;

               $booking = $booking->getBookingById($curr_nominee->booking_id);

               if(isset($curr_user->id) && isset($booking->id))
               {
                    parent::setLogMessage($curr_nominee->nok_type.' "'.$curr_user->name.'" has been updated '.$booking->ms.' ('.$booking->plot->plot_id.' '.$booking->plot->block_name.') plot.');

                    parent::successMessage('Nominee/ Co Owner has been updated.',parent::getLogMessage());
               }


             }
        }
        catch (DecryptException $e)
        {

        }

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

