<?php

namespace App\Http\Controllers;

use App\Country;
use App\Customer;
use App\DepositorBank;
use App\DNFBP;
use Illuminate\Http\Request;
use App\CustomerNominee;
use Illuminate\Support\Facades\Validator;

class NomineeController extends Controller
{
    //
    public function index(){

      $nominee = CustomerNominee::orderBy('id', 'desc')->get();
      $customer = Customer::all();
      activity('View')->log('Nominee list.');

      return view('nominee.index', compact('nominee','customer'));
    }
    public function create(){

 
        $customer=  Customer::all();
        activity('Open')->log('Nominee create Page');
        return view('nominee.add',compact('customer'));
    }
    public function store(Request $request){

        
        
    $validatedData = $request->validate([
        'customer_id' => 'required',
        'nominee_name' => 'required|regex:/^[a-zA-Z\s,.-]+$/u',
        'nominee_cnic' => 'required',

        'nominee_so_do_wo' => 'required|regex:/^[a-zA-Z\s,-.]+$/u',
        "nominee_father_type" => "required",
        "nominee_passport_no" => "nullable|string|max:100",
        "nominee_email" => "nullable|email",
        "nominee_mobile_number" => "nullable|string|max:12",
        "nominee_phone_number" => "nullable|string|max:12",
        "nominee_res_number" => "nullable|string|max:12",
        "nominee_relation" => "required",
        "nominee_address" => "required"


         
  
 
  

    ]);
        if ($request->nominee_cnic) 
        {
             $dnfbp_check_point_nominee = DNFBP::where('cnic' , $request->nominee_cnic)->first();

                if ($dnfbp_check_point_nominee == true) 
            {
                return redirect()
                ->back()
                ->withInput()
                ->with([
                    'message' => 'This Member cnic is found in list of DNFBP. You cant add this nominee record.',
                    'alert-type' => 'error',
                ]);
            }
        }
        
   $nominee=   new  CustomerNominee;
   $nominee->customer_id=$request->customer_id;
   $nominee->nominee_name=$request->nominee_name;
   $nominee->nominee_so_do_wo=$request->nominee_so_do_wo;
   $nominee->nominee_cnic=$request->nominee_cnic;
   $nominee->nominee_email=$request->nominee_email;
   $nominee->nominee_relation=$request->nominee_relation;
   $nominee->so_do_wo_flag=$request->nominee_father_type;
//    $nominee->nominee_contact_number=$request->;
   $nominee->nominee_passport_no=$request->nominee_passport_no;
   $nominee->nominee_phone_number=$request->nominee_phone_number;
   $nominee->nominee_res_number=$request->nominee_res_number;
   $nominee->nominee_mobile_number=$request->nominee_mobile_number;
   $nominee->save();

   activity('Create')->log('Nominee ' . $nominee->nominee_name);

   return redirect()
                 
                   ->back()
                   ->with([
                       'message' => 'Nominee created successfully!',
                       'alert-type' => 'info'
   ]);

    }

    public function updateTable(Request $request) {
       
        if($request->nomi != 0 && $request->cus != 0) {
            $nomi = $request->nomi;
           $cus= $request->cus;
            $data['nominee'] = CustomerNominee::where([['id',$nomi]])->whereHas('customer', function($query) use ($cus) {
                    $query->where('id',$cus);
                })->get();
        }
        else if($request->cus != 0) {
            $cus = $request->cus;
            $data['nominee'] = CustomerNominee::whereHas('customer', function($query) use ($cus) {
                    
                        $query->where('id', $cus);
                    
                })->get();
        }
        else if($request->nomi != 0) {
            $data['nominee'] = CustomerNominee::where([['id',$request->nomi]])->get();
        }

        return view('nominee.update-table',$data);
    }

    public function edit($id){
        $nominee = CustomerNominee::where('id',$id)->first();
        $customer = Customer::all();

        activity('Open')->log('Edit Nominee Page');
        return view('nominee.edit',compact('customer','nominee'));

    }
    public function update(Request $request){
        $validatedData = $request->validate([
            'customer_id' => 'required',
            'nominee_name' => 'required|regex:/^[a-zA-Z\s,.-]+$/u',
            'nominee_cnic' => 'required',
    
            'nominee_so_do_wo' => 'required|regex:/^[a-zA-Z\s,-.]+$/u',
            "nominee_father_type" => "required",
            "nominee_passport_no" => "nullable|string|max:100",
            "nominee_email" => "nullable|email",
            "nominee_mobile_number" => "nullable|string|max:12",
            "nominee_phone_number" => "nullable|string|max:12",
            "nominee_res_number" => "nullable|string|max:12",
            "nominee_relation" => "required",
            "nominee_address" => "required",
            'id'=>'required'
    
    
             
      
     
      
    
        ]);
            if ($request->nominee_cnic) 
            {
                 $dnfbp_check_point_nominee = DNFBP::where('cnic' , $request->nominee_cnic)->first();
    
                    if ($dnfbp_check_point_nominee == true) 
                {
                    return redirect()
                    ->back()
                    ->withInput()
                    ->with([
                        'message' => 'This Member cnic is found in list of DNFBP. You cant add this nominee record.',
                        'alert-type' => 'error',
                    ]);
                }
            }
        $nominee = CustomerNominee::where('id',$request->id)->first();
        $nominee->customer_id=$request->customer_id;
        $nominee->nominee_name=$request->nominee_name;
        $nominee->nominee_so_do_wo=$request->nominee_so_do_wo;
        $nominee->nominee_cnic=$request->nominee_cnic;
        $nominee->nominee_email=$request->nominee_email;
        $nominee->nominee_relation=$request->nominee_relation;
        $nominee->so_do_wo_flag=$request->nominee_father_type;
     //    $nominee->nominee_contact_number=$request->;
        $nominee->nominee_passport_no=$request->nominee_passport_no;
        $nominee->nominee_phone_number=$request->nominee_phone_number;
        $nominee->nominee_res_number=$request->nominee_res_number;
        $nominee->nominee_mobile_number=$request->nominee_mobile_number;
        $nominee->nominee_address=$request->nominee_address;
        $nominee->save();
     
        activity('Edit')->log('Nominee ' . $nominee->nominee_name);
     
        return redirect()
                      
                        ->back()
                        ->with([
                            'message' => 'Nominee edited successfully!',
                            'alert-type' => 'info'
        ]);
    }

    public function detailNominee($id){

        $nominee = CustomerNominee::where('id',$id)->first();


           activity('View')->log('Nominee Profile '.$nominee->nominee_name);
        return view('nominee.detail',compact('nominee'));
    }

    public function deleteNominee(Request $request)
    {

        $nominee = CustomerNominee::where('id',$request->id)->first();  
        $nominee->is_deleted = true;
        $nominee->save();
        activity('Delete')->log('delete the Nominee info '.$nominee->nominee_name .'-'. $nominee->nominee_cnic);         
        return redirect()->back()  ->with([
            'message' => 'Nominee deleted successfully!',
            'alert-type' => 'info'
           ]);

    }
}
