<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use App\User;
use App\YesPin;
use App\EvEvSolarModels;
use App\Http\Requests\StoreUsersRequest;
use App\Http\Requests\StoreBinUsersRequest;
use App\Http\Requests\UpdateUsersRequest;
use Yajra\Datatables\Datatables;
use Mail;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Validator;
use Image;

class LeadController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $user = Auth::user();
        $states = DB::table('states')->get();
        $ev_ev_solar_model_ids = \App\EvEvSolarModels::where('type', 'SOLAR')->get();
        return view('leads.add_lead', array(
            'user' => $user,
            'states'=> $states,
            'ev_ev_solar_model_ids' => $ev_ev_solar_model_ids
       ));
    }

    //doAddLead
    //'pending','approved','loanApproved','loanDeclined','cancelled'
    public function doAddLead(Request $request){
        $user = Auth::user();
        if(isset($request->full_name)){


            $validator = Validator::make($request->all(), [
                'placement' => 'required|in:l,r',
                'full_name' => 'required',
                'gender' => 'required',
                'mobileno' => 'required',
                'email' => 'required|email',
                'panno' => 'required',
                'dob' => 'required',
                'bank_name' => 'required',
                'econn_no' => 'required',
                'solar_conn_type' => 'required',
                'ev_ev_solar_model_id' => 'required',
                'payment_reference' => 'required',
                'state' => 'required|exists:states,id',
                'city' => 'required|exists:cities,id',
                'property_type' => 'required',
                'payment_type' => 'required|in:loan,self',
                'aadhar_front' => 'required|image|mimes:jpeg,png,jpg,gif,JPEG,JPG|max:16000',
                'aadhar_back' => 'required|image|mimes:jpeg,png,jpg,gif,JPEG,JPG|max:16000',
                'pan_image' => 'required|image|mimes:jpeg,png,jpg,gif,JPEG,JPG|max:16000',
                'elect_bill_image' => 'required|mimes:pdf|max:10000',
                'pass_cheque_book' => 'required|image|mimes:jpeg,png,jpg,gif,JPEG,JPG|max:16000',
                'payment_proof' => 'required|image|mimes:jpeg,png,jpg,gif,JPEG,JPG|max:16000',
            ]);

            if ($validator->fails()) {
                return Redirect::back()
                            ->withErrors($validator)
                            ->withInput();
            }else{
                $validateKycUserMobPan = $this->validateKycUserMobPan($request);
                $solar_model = \App\EvEvSolarModels::where('id', $request->ev_ev_solar_model_id)->first();
                
                if($validateKycUserMobPan->valid == false)
                {   
                    return Redirect::back()
                                ->with('errormessage', $validateKycUserMobPan->message)
                                ->withInput();
                }
                
                $solar_model = \App\EvEvSolarModels::where('id', $request->ev_ev_solar_model_id)->first();
                if($request->payment_type=='loan'){
                    if($request->loan_amount > $solar_model->amount){
                        return Redirect::back()
                                ->with('errormessage', "Loan amount must be less than or equal to ". $solar_model->amount)
                                ->withInput();
                    }
                }
                
                if($validateKycUserMobPan->valid == true)
                {
                    //aadhar_front
                    $image = $request->file('aadhar_front');
                    $file_name    = $image->getClientOriginalName();
                    $image_resize = Image::make($image->getRealPath());
                    $image_resize->resize(1024, 1024, function ($constraint) {
                        $constraint->aspectRatio();
                        $constraint->upsize();
                    });
                    $aadhar_front = $user->username.'_aadhar_front_'.time().'.'.$image->getClientOriginalExtension();
                    $image_resize->save(public_path('/uploads/proofs/' .$aadhar_front));

                    //aadhar_back
                    $image = $request->file('aadhar_back');
                    $file_name    = $image->getClientOriginalName();
                    $image_resize = Image::make($image->getRealPath());
                    $image_resize->resize(1024, 1024, function ($constraint) {
                        $constraint->aspectRatio();
                        $constraint->upsize();
                    });
                    $aadhar_back = $user->username.'_aadhar_back_'.time().'.'.$image->getClientOriginalExtension();
                    $image_resize->save(public_path('/uploads/proofs/' .$aadhar_back));

                    //pan_image
                    $image = $request->file('pan_image');
                    $file_name    = $image->getClientOriginalName();
                    $image_resize = Image::make($image->getRealPath());
                    $image_resize->resize(1024, 1024, function ($constraint) {
                        $constraint->aspectRatio();
                        $constraint->upsize();
                    });
                    $pan_image = $user->username.'_pan_image_'.time().'.'.$image->getClientOriginalExtension();
                    $image_resize->save(public_path('/uploads/proofs/' .$pan_image));

                    //elect_bill_image
                    $image = $request->file('elect_bill_image');
                    $file_name    = $image->getClientOriginalName();
                    $elect_bill_image = $user->username.'_elect_bill_image_'.time().'.'.$image->getClientOriginalExtension();
                    $image->store(public_path('/uploads/proofs/' .$elect_bill_image));

                    //pass_cheque_book
                    $image = $request->file('pass_cheque_book');
                    $file_name    = $image->getClientOriginalName();
                    $image_resize = Image::make($image->getRealPath());
                    $image_resize->resize(1024, 1024, function ($constraint) {
                        $constraint->aspectRatio();
                        $constraint->upsize();
                    });
                    $pass_cheque_book = $user->username.'_pass_cheque_book_'.time().'.'.$image->getClientOriginalExtension();
                    $image_resize->save(public_path('/uploads/proofs/' .$pass_cheque_book));


                    //payment_proof
                    $image = $request->file('payment_proof');
                    $file_name    = $image->getClientOriginalName();
                    $image_resize = Image::make($image->getRealPath());
                    $image_resize->resize(1024, 1024, function ($constraint) {
                        $constraint->aspectRatio();
                        $constraint->upsize();
                    });
                    $payment_proof = $user->username.'_payment_proof_'.time().'.'.$image->getClientOriginalExtension();
                    $image_resize->save(public_path('/uploads/proofs/' .$payment_proof));
                    
                    $city = \App\City::where('id', $request->city)->first();
                    $state = \App\State::where('id', $request->state)->first();
                    $maxId = \App\EvCustomer::max('id')+1;
                    $ack_no = "JS-".strtoupper(date('M').date('y'))."-".str_pad($maxId, 8, '0', STR_PAD_LEFT);

                    if($request->payment_type=='loan'){
                        $amount = $solar_model->amount;
                        $balance_amount = $solar_model->amount;
                        $loan_amount = $request->loan_amount;
                        $paid_amount = 0;
                    }else{
                        $amount = $solar_model->amount;
                        $balance_amount = $solar_model->amount;
                        $loan_amount = 0;
                        $paid_amount = 0;
                    }

                    $UserActivation = \App\EvCustomer::create([
                                        'full_name' => $request->full_name,
                                        'email' => $request->email,
                                        'username' => $user->username,
                                        'sponsor' => $user->username,
                                        'sponsor_id' => $user->id,
                                        'introducer' => $user->username,
                                        'introducer_id' => $user->id,
                                        'placement' => $request->placement,
                                        'country' => 'India',
                                        'mobileno' => $request->mobileno,
                                        'ipaddress' => $request->ip(),
                                        'gender' => $request->gender,
                                        'panno' => $request->panno,
                                        'dob' => $request->dob,
                                        'state' => $state->id,
                                        'city' => $city->id,
                                        'city_name' => $city->name,
                                        'state_name' => $state->name,
                                        'address' => $request->address,
                                        'pincode' => $request->pincode,
                                        'bank_name' => $request->bank_name,
                                        'pan_image' => $pan_image,
                                        'pass_cheque_book' => $pass_cheque_book,
                                        'econn_no' => $request->econn_no,
                                        'solar_conn_type' => $request->solar_conn_type,
                                        'ev_ev_solar_model_id' => $request->ev_ev_solar_model_id,
                                        'aadhar_front' => $aadhar_front,
                                        'aadhar_back' => $aadhar_back,
                                        'elect_bill_image' => $elect_bill_image,
                                        'status' => 0,
                                        'status_name'=>'pending',
                                        'payment_proof' => $payment_proof,
                                        'payment_reference' => $request->payment_reference,
                                        'sponsor_comment' => $request->sponsor_comment,
                                        'ack_number' => $ack_no,
                                        'property_type' => $request->property_type,
                                        'payment_type' => $request->payment_type,
                                        'amount' => $amount,
                                        'balance_amount' => $balance_amount,
                                        'loan_amount' => $loan_amount,
                                        'paid_amount' => $paid_amount,
                                        'monthly_elecbill' => $request->monthly_elecbill,
                                        'lead_type' => 'New'
                                      ]);
                    //return Redirect::to('/users/billSentReport')->with('message','Bill submitted successfully!');
                    return Redirect::back()->with('message','Lead Created successfully!');
                }
            }
        }
        return Redirect::back()->with('message','Something went wrong!');
    }
    
    public function bookMySolar()
    {
        $user = Auth::user();
        $states = DB::table('states')->get();
        $ev_ev_solar_model_ids = \App\EvEvSolarModels::where('type', 'SOLAR')->get();
        return view('leads.book_my_solar', array(
            'user' => $user,
            'states'=> $states,
            'ev_ev_solar_model_ids' => $ev_ev_solar_model_ids
       ));
    }

    //doAddLead
    public function doBookMySolar(Request $request){
        $user = Auth::user();
        if(isset($request->bank_name)){

            $validator = Validator::make($request->all(), [
                'bank_name' => 'required',
                'econn_no' => 'required',
                'solar_conn_type' => 'required',
                'ev_ev_solar_model_id' => 'required',
                'payment_reference' => 'required',
                'property_type' => 'required',
                'payment_type' => 'required|in:loan,self',
                'aadhar_front' => 'required|image|mimes:jpeg,png,jpg,gif,JPEG,JPG|max:16000',
                'aadhar_back' => 'required|image|mimes:jpeg,png,jpg,gif,JPEG,JPG|max:16000',
                'pan_image' => 'required|image|mimes:jpeg,png,jpg,gif,JPEG,JPG|max:16000',
                'elect_bill_image' => 'required|mimes:pdf|max:10000',
                'pass_cheque_book' => 'required|image|mimes:jpeg,png,jpg,gif,JPEG,JPG|max:16000',
                'payment_proof' => 'required|image|mimes:jpeg,png,jpg,gif,JPEG,JPG|max:16000',
            ]);

            if ($validator->fails()) {
                return Redirect::back()
                            ->withErrors($validator)
                            ->withInput();
            }else{
                $solar_model = \App\EvEvSolarModels::where('id', $request->ev_ev_solar_model_id)->first();
                if($request->payment_type=='loan'){
                    if($request->loan_amount > $solar_model->amount){
                        return Redirect::back()
                                ->with('errormessage', "Loan amount must be less than or equal to ". $solar_model->amount)
                                ->withInput();
                    }
                }

                //aadhar_front
                $image = $request->file('aadhar_front');
                $file_name    = $image->getClientOriginalName();
                $image_resize = Image::make($image->getRealPath());
                $image_resize->resize(1024, 1024, function ($constraint) {
                    $constraint->aspectRatio();
                    $constraint->upsize();
                });
                $aadhar_front = $user->username.'_aadhar_front_'.time().'.'.$image->getClientOriginalExtension();
                $image_resize->save(public_path('/uploads/proofs/' .$aadhar_front));

                //aadhar_back
                $image = $request->file('aadhar_back');
                $file_name    = $image->getClientOriginalName();
                $image_resize = Image::make($image->getRealPath());
                $image_resize->resize(1024, 1024, function ($constraint) {
                    $constraint->aspectRatio();
                    $constraint->upsize();
                });
                $aadhar_back = $user->username.'_aadhar_back_'.time().'.'.$image->getClientOriginalExtension();
                $image_resize->save(public_path('/uploads/proofs/' .$aadhar_back));

                //pan_image
                $image = $request->file('pan_image');
                $file_name    = $image->getClientOriginalName();
                $image_resize = Image::make($image->getRealPath());
                $image_resize->resize(1024, 1024, function ($constraint) {
                    $constraint->aspectRatio();
                    $constraint->upsize();
                });
                $pan_image = $user->username.'_pan_image_'.time().'.'.$image->getClientOriginalExtension();
                $image_resize->save(public_path('/uploads/proofs/' .$pan_image));

                //elect_bill_image
                $image = $request->file('elect_bill_image');
                $file_name    = $image->getClientOriginalName();
                $elect_bill_image = $user->username.'_elect_bill_image_'.time().'.'.$image->getClientOriginalExtension();
                $image->store(public_path('/uploads/proofs/' .$elect_bill_image));

                //pass_cheque_book
                $image = $request->file('pass_cheque_book');
                $file_name    = $image->getClientOriginalName();
                $image_resize = Image::make($image->getRealPath());
                $image_resize->resize(1024, 1024, function ($constraint) {
                    $constraint->aspectRatio();
                    $constraint->upsize();
                });
                $pass_cheque_book = $user->username.'_pass_cheque_book_'.time().'.'.$image->getClientOriginalExtension();
                $image_resize->save(public_path('/uploads/proofs/' .$pass_cheque_book));


                //payment_proof
                $image = $request->file('payment_proof');
                $file_name    = $image->getClientOriginalName();
                $image_resize = Image::make($image->getRealPath());
                $image_resize->resize(1024, 1024, function ($constraint) {
                    $constraint->aspectRatio();
                    $constraint->upsize();
                });
                $payment_proof = $user->username.'_payment_proof_'.time().'.'.$image->getClientOriginalExtension();
                $image_resize->save(public_path('/uploads/proofs/' .$payment_proof));
                
                $maxId = \App\EvCustomer::max('id')+1;
                $ack_no = "JS-".strtoupper(date('M').date('y'))."-".str_pad($maxId, 8, '0', STR_PAD_LEFT);

                if($request->payment_type=='loan'){
                    $amount = $solar_model->amount;
                    $balance_amount = $solar_model->amount;
                    $loan_amount = $request->loan_amount;
                    $paid_amount = 0;
                }else{
                    $amount = $solar_model->amount;
                    $balance_amount = $solar_model->amount;
                    $loan_amount = 0;
                    $paid_amount = 0;
                }


                $UserActivation = \App\EvCustomer::create([
                                    'full_name' => $user->full_name,
                                    'email' => $user->email,
                                    'username' => $user->username,
                                    'sponsor' => $user->sponsor,
                                    'sponsor_id' => $user->sponsor_id,
                                    'introducer' => $user->username,
                                    'introducer_id' => $user->id,
                                    'placement' => 'l',
                                    'country' => 'India',
                                    'mobileno' => $user->mobileno,
                                    'ipaddress' => $request->ip(),
                                    'gender' => $user->gender,
                                    'panno' => $user->panno,
                                    'dob' => $user->dob,
                                    'state' => $user->state,
                                    'city' => $user->city,
                                    'city_name' => $user->city_name,
                                    'state_name' => $user->state_name,
                                    'address' => $user->address,
                                    'pincode' => $user->pincode,
                                    'bank_name' => $request->bank_name,
                                    'pan_image' => $pan_image,
                                    'pass_cheque_book' => $pass_cheque_book,
                                    'econn_no' => $request->econn_no,
                                    'solar_conn_type' => $request->solar_conn_type,
                                    'ev_ev_solar_model_id' => $request->ev_ev_solar_model_id,
                                    'aadhar_front' => $aadhar_front,
                                    'aadhar_back' => $aadhar_back,
                                    'elect_bill_image' => $elect_bill_image,
                                    'status' => 0,
                                    'status_name'=>'pending',
                                    'payment_proof' => $payment_proof,
                                    'payment_reference' => $request->payment_reference,
                                    'sponsor_comment' => $request->sponsor_comment,
                                    'ack_number' => $ack_no,
                                    'property_type' => $request->property_type,
                                    'payment_type' => $request->payment_type,
                                    'amount' => $amount,
                                    'balance_amount' => $balance_amount,
                                    'loan_amount' => $loan_amount,
                                    'paid_amount' => $paid_amount,
                                    'monthly_elecbill' => $request->monthly_elecbill,
                                    'lead_type' => 'Self'
                                  ]);
                //return Redirect::to('/users/billSentReport')->with('message','Bill submitted successfully!');
                return Redirect::back()->with('message','Solar Booked Created successfully!');

            }
        }
        return Redirect::back()->with('message','Something went wrong!');
    }
    
    public function editLead($id)
    {
        $user = Auth::user();
        $states = DB::table('states')->get();
        $ev_ev_solar_model_ids = \App\EvEvSolarModels::where('type', 'SOLAR')->get();
        $customer = \App\EvCustomer::where([['id', '=',$id], ['introducer_id', '=',$user->id]])->first();
        
        if(isset($customer->id)){
            return view('leads.edit_lead', array(
                'user' => $user,
                'states'=> $states,
                'ev_ev_solar_model_ids' => $ev_ev_solar_model_ids,
                'customer' => $customer
            ));
        }else{
            dd("Permission denied.");
        }
    }

    //doAddLead
    //'pending','approved','loanApproved','loanDeclined','cancelled'
    public function doEditLead($id, Request $request){
        $user = Auth::user();
        $customer = \App\EvCustomer::where([['id', '=',$id], ['id', '=',$id]])->first();
        
        if(isset($request->bank_name) && isset($customer->id)){

            if($customer->lead_type=='Self'){
                $to_validate = [
                    'econn_no' => 'required',
                    'solar_conn_type' => 'required',
                    'ev_ev_solar_model_id' => 'required',
                    'payment_reference' => 'required',
                    'property_type' => 'required',
                'payment_type' => 'required|in:loan,self',
                ];
            }else{
                $to_validate = [
                    'placement' => 'required|in:l,r',
                    'full_name' => 'required',
                    'gender' => 'required',
                    'dob' => 'required',
                    'bank_name' => 'required',
                    'state' => 'required|exists:states,id',
                    'city' => 'required|exists:cities,id',
                    'econn_no' => 'required',
                    'solar_conn_type' => 'required',
                    'ev_ev_solar_model_id' => 'required',
                    'payment_reference' => 'required',
                    'property_type' => 'required',
                    'payment_type' => 'required|in:loan,self',
                ];
            }


            $validator = Validator::make($request->all(), $to_validate);

            if ($validator->fails()) {
                return Redirect::back()
                            ->withErrors($validator)
                            ->withInput();
            }else{

                if($customer->lead_type=='New'){
                    $validateKycUserMobPan = $this->validateKycUserMobPan($request);
                            
                    if($validateKycUserMobPan->valid == false)
                    {   
                        return Redirect::back()
                                    ->with('errormessage', $validateKycUserMobPan->message)
                                    ->withInput();
                    }
                }
                
                $solar_model = \App\EvEvSolarModels::where('id', $request->ev_ev_solar_model_id)->first();
                if($request->payment_type=='loan'){
                    if($request->loan_amount > $solar_model->amount){
                        return Redirect::back()
                                ->with('errormessage', "Loan amount must be less than or equal to ". $solar_model->amount)
                                ->withInput();
                    }
                }
                
                if(true)
                {
                    //aadhar_front
                    if($request->file('aadhar_front')){
                        @unlink(public_path('/uploads/proofs/'.$customer->aadhar_front));

                        $image = $request->file('aadhar_front');
                        $file_name    = $image->getClientOriginalName();
                        $image_resize = Image::make($image->getRealPath());
                        $image_resize->resize(1024, 1024, function ($constraint) {
                            $constraint->aspectRatio();
                            $constraint->upsize();
                        });
                        $aadhar_front = $user->username.'_aadhar_front_'.time().'.'.$image->getClientOriginalExtension();
                        $image_resize->save(public_path('/uploads/proofs/' .$aadhar_front));
                    }else{
                        $aadhar_front = $customer->aadhar_front;
                    }
                    
                    //aadhar_back
                    if($request->file('aadhar_back')){
                        @unlink(public_path('/uploads/proofs/'.$customer->aadhar_back));

                        $image = $request->file('aadhar_back');
                        $file_name    = $image->getClientOriginalName();
                        $image_resize = Image::make($image->getRealPath());
                        $image_resize->resize(1024, 1024, function ($constraint) {
                            $constraint->aspectRatio();
                            $constraint->upsize();
                        });
                        $aadhar_back = $user->username.'_aadhar_back_'.time().'.'.$image->getClientOriginalExtension();
                        $image_resize->save(public_path('/uploads/proofs/' .$aadhar_back));
                    }else{
                        $aadhar_back = $customer->aadhar_back;
                    }

                    //pan_image
                    if($request->file('pan_image')){
                        @unlink(public_path('/uploads/proofs/'.$customer->pan_image));
                        $image = $request->file('pan_image');
                        $file_name    = $image->getClientOriginalName();
                        $image_resize = Image::make($image->getRealPath());
                        $image_resize->resize(1024, 1024, function ($constraint) {
                            $constraint->aspectRatio();
                            $constraint->upsize();
                        });
                        $pan_image = $user->username.'_pan_image_'.time().'.'.$image->getClientOriginalExtension();
                        $image_resize->save(public_path('/uploads/proofs/' .$pan_image));
                    }else{
                        $pan_image = $customer->pan_image;
                    }

                    //elect_bill_image
                    if($request->file('elect_bill_image')){
                        @unlink(public_path('/uploads/proofs/'.$customer->elect_bill_image));

                        $image = $request->file('elect_bill_image');
                        $file_name    = $image->getClientOriginalName();
                        $elect_bill_image = $user->username.'_elect_bill_image_'.time().'.'.$image->getClientOriginalExtension();
                        $image->store(public_path('/uploads/proofs/' .$elect_bill_image));
                    }else{
                        $elect_bill_image = $customer->elect_bill_image;
                    }

                    //pass_cheque_book
                    if($request->file('pass_cheque_book')){
                        @unlink(public_path('/uploads/proofs/'.$customer->pass_cheque_book));

                        $image = $request->file('pass_cheque_book');
                        $file_name    = $image->getClientOriginalName();
                        $image_resize = Image::make($image->getRealPath());
                        $image_resize->resize(1024, 1024, function ($constraint) {
                            $constraint->aspectRatio();
                            $constraint->upsize();
                        });
                        $pass_cheque_book = $user->username.'_pass_cheque_book_'.time().'.'.$image->getClientOriginalExtension();
                        $image_resize->save(public_path('/uploads/proofs/' .$pass_cheque_book));
                    }else{
                        $pass_cheque_book = $customer->pass_cheque_book;
                    }


                    //payment_proof
                    if($request->file('payment_proof')){
                        @unlink(public_path('/uploads/proofs/'.$customer->payment_proof));

                        $image = $request->file('payment_proof');
                        $file_name    = $image->getClientOriginalName();
                        $image_resize = Image::make($image->getRealPath());
                        $image_resize->resize(1024, 1024, function ($constraint) {
                            $constraint->aspectRatio();
                            $constraint->upsize();
                        });
                        $payment_proof = $user->username.'_payment_proof_'.time().'.'.$image->getClientOriginalExtension();
                        $image_resize->save(public_path('/uploads/proofs/' .$payment_proof));
                    }else{
                        $payment_proof = $customer->payment_proof;
                    }
                    

                    if($request->payment_type=='loan'){
                        $amount = $solar_model->amount;
                        $balance_amount = $solar_model->amount;
                        $loan_amount = $request->loan_amount;
                        $paid_amount = 0;
                    }else{
                        $amount = $solar_model->amount;
                        $balance_amount = $solar_model->amount;
                        $loan_amount = 0;
                        $paid_amount = 0;
                    }

                    if($customer->lead_type=='Self'){
                        $to_update = [
                                        'bank_name' => $request->bank_name,
                                        'pan_image' => $pan_image,
                                        'pass_cheque_book' => $pass_cheque_book,
                                        'econn_no' => $request->econn_no,
                                        'solar_conn_type' => $request->solar_conn_type,
                                        'ev_ev_solar_model_id' => $request->ev_ev_solar_model_id,
                                        'aadhar_front' => $aadhar_front,
                                        'aadhar_back' => $aadhar_back,
                                        'elect_bill_image' => $elect_bill_image,
                                        'payment_proof' => $payment_proof,
                                        'payment_reference' => $request->payment_reference,
                                        'sponsor_comment' => $request->sponsor_comment,
                                        'property_type' => $request->property_type,
                                        'payment_type' => $request->payment_type,
                                        'amount' => $amount,
                                        'balance_amount' => $balance_amount,
                                        'loan_amount' => $loan_amount,
                                        'paid_amount' => $paid_amount,
                                        'monthly_elecbill' => $request->monthly_elecbill
                                      ];

                    }else{
                        $city = \App\City::where('id', $request->city)->first();
                        $state = \App\State::where('id', $request->state)->first();

                        $to_update = [
                                        'full_name' => $request->full_name,
                                        'placement' => $request->placement,
                                        'gender' => $request->gender,
                                        'dob' => $request->dob,
                                        'state' => $state->id,
                                        'city' => $city->id,
                                        'city_name' => $city->name,
                                        'state_name' => $state->name,
                                        'address' => $request->address,
                                        'pincode' => $request->pincode,
                                        'bank_name' => $request->bank_name,
                                        'pan_image' => $pan_image,
                                        'pass_cheque_book' => $pass_cheque_book,
                                        'econn_no' => $request->econn_no,
                                        'solar_conn_type' => $request->solar_conn_type,
                                        'ev_ev_solar_model_id' => $request->ev_ev_solar_model_id,
                                        'aadhar_front' => $aadhar_front,
                                        'aadhar_back' => $aadhar_back,
                                        'elect_bill_image' => $elect_bill_image,
                                        'payment_proof' => $payment_proof,
                                        'payment_reference' => $request->payment_reference,
                                        'sponsor_comment' => $request->sponsor_comment,
                                        'property_type' => $request->property_type,
                                        'payment_type' => $request->payment_type,
                                        'amount' => $amount,
                                        'balance_amount' => $balance_amount,
                                        'loan_amount' => $loan_amount,
                                        'paid_amount' => $paid_amount,
                                        'monthly_elecbill' => $request->monthly_elecbill
                                      ];
                    }

                    $UserActivation = \App\EvCustomer::where('id', $customer->id)->update($to_update);
                    //return Redirect::to('/users/billSentReport')->with('message','Bill submitted successfully!');
                    return Redirect::back()->with('message','Lead Updated successfully!');
                }
            }
        }
        return Redirect::back()->with('errormessage','Something went wrong!');
    }


    protected function validateKycUserMobPan($request){
        $user = Auth::user();
        $validator = new \stdClass();
        $validator->valid = true;
        $validator->message = "";
        

        $mobCnt = \App\User::where([['panno', '=',$request->panno]])->count();
        //$panCnt = User::where('panno', $request->mobileno)->count();

        if($mobCnt > 0){
            $validator->valid = false;
            $validator->message = 'This PAN no. is used 1 times. More than 1 times not allowed.';
        }
        $mobCnt = \App\User::where([['mobileno', '=', $request->mobileno]])->count();
        //$panCnt = User::where('panno', $request->mobileno)->count();

        if($mobCnt > 0){
            $validator->valid = false;
            $validator->message = 'This Mobile no. is used 1 times. More than 1 times not allowed.';
        }
        
        $mobCnt = \App\User::where([['email', '=',$request->email]])->count();
        //$panCnt = User::where('panno', $request->mobileno)->count();

        if($mobCnt > 0){
            $validator->valid = false;
            $validator->message = 'This email is already in use.';
        }
        return $validator;
    }


    public function myLeadsList($type)
     {
        $user = Auth::user();
        return view('leads.myLeadsList', compact('user', 'type'));
     
     }


    public function myLeadsListAnyData($type, Request $request)
    {
        $user = Auth::user();
        //$user_gen = \App\BinaryUser::where('user_id', '=', $user->id)->first();
        DB::statement(DB::raw('set @rownum=0'));
        $users = \App\EvCustomer::select([
            DB::raw('@rownum  := @rownum  + 1 AS rownum'),
            'ev_customers.*',
            'evm.title',
            'evm.kw',
            'u2.username as s_username',
            'u2.full_name as s_full_name',
        ])
        ->join('users as u2', 'u2.id', '=', 'ev_customers.sponsor_id')
        ->join('ev_ev_solar_models as evm', 'evm.id', '=', 'ev_customers.ev_ev_solar_model_id')
        ->where([['ev_customers.introducer_id', '=', $user->id], ['ev_customers.status_name', '=', $type]])
        ->orderby('ev_customers.created_at', 'desc');
        
        $datatables = Datatables::of($users)->addColumn('action',         
         
         ' @if($status==0)'
         .'<a class="btn btn-warning" href="{{ url("/")."/editLead/".$id }}">'
         .'Edit'
         .'</a>'
         .'<br />'
         .'<br />'
         .'<a class="btn btn-danger" href="{{ url("/")."/deleteLead/".$id }}">'
         .'Delete'
         .'</a>'
         .'@endif'         
        );
}
}