@extends('layouts.app')

@section('content')
<div class="card card-outline-info">
  <div class="card-header">
      <h4 class="m-b-0 text-white">Create Lead - New User Customer</h4>
  </div>
  <div class="card-body">
    <div class="x_content">
      <form id="demo-form2" action="{{ url('/admin/doUpdateLeadStatus/'.$customer->id) }}" method="post" enctype="multipart/form-data" data-parsley-validate class="form-horizontal form-label-left">
        {{ csrf_field() }}
            @if($customer->lead_type=='New')
            <h3 class="card-title">Personal Detail</h3>
            <div class="row">
                <div class="col-md-4">
                    <div class="form-group">
                      {!! Form::label('placement', 'Tracking No.*', ['class' => 'control-label']) !!}
                      <input type="text" class="form-control" value="{{$customer->ack_number}}" id="copyInput" readonly>  
                      <!-- Copy Button with Icon -->
                      <button class="btn btn-outline-secondary" type="button" id="button-copy" onclick="copyText()">
                        <i class="fa fa-clipboard"></i> <!-- Bootstrap Icon -->
                      </button>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-4">
                    <div class="form-group">
                        {!! Form::label('placement', 'Placement*', ['class' => 'control-label']) !!}
                        {!! Form::select('placement',array('l'=>'Left', 'r'=>'Right'), $customer->placement, ['class' => 'form-control form-control-normal', 'required', 'placeholder' => 'Select']) !!}
                        <p class="help-block"></p>
                        @if($errors->has('placement'))
                            <p class="help-block error">
                                {{ $errors->first('placement') }}
                            </p>
                        @endif
                    </div>
                </div>
                <div class="col-md-8">
                    <div class="form-group">
                        {!! Form::label('full_name', 'Full Name*', ['class' => 'control-label', 'required']) !!}
                        {!! Form::text('full_name', $customer->full_name, ['class' => 'form-control form-control-normal','id'=>'full_name', 'placeholder' => 'Full Name here']) !!}
                        <p class="help-block"></p>
                        @if($errors->has('full_name'))
                            <p class="help-block error">
                                {{ $errors->first('full_name') }}
                            </p>
                        @endif
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        {!! Form::label('gender', 'Gender*', ['class' => 'control-label']) !!}
                        {!! Form::select('gender', array('Male', 'Female'), !empty($customer->gender) ? $customer->gender : 0, ['class' => 'form-control form-control-normal', 'required', 'placeholder' => 'Select']) !!}
                        <p class="help-block"></p>
                        @if($errors->has('gender'))
                            <p class="help-block error">
                                {{ $errors->first('gender') }}
                            </p>
                        @endif
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        {!! Form::label('mobileno', 'Mobile no.*', ['class' => 'control-label']) !!}
                        {!! Form::number('mobileno', $customer->mobileno, ['class' => 'form-control form-control-normal', 'id'=>'mobileno', 'placeholder' => 'Mobile No.', 'maxlength' => '10', 'required', 'disabled']) !!}
                        <p class="help-block">Do not enter 0 or +91 before the mobile number</p>
                        @if($errors->has('mobileno'))
                            <p class="help-block error">
                                {{ $errors->first('mobileno') }}
                            </p>
                        @endif
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                    {!! Form::label('email', 'Email*', ['class' => 'control-label']) !!}
                    {!! Form::email('email', $customer->email, ['class' => 'form-control form-control-normal', 'id' => 'email', 'placeholder' => 'Email', 'required', 'disabled']) !!}
                    <p class="help-block"></p>
                    @if($errors->has('email'))
                        <p class="help-block">
                            {{ $errors->first('email') }}
                        </p>
                    @endif
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        {!! Form::label('panno', 'Pan no.*', ['class' => 'control-label']) !!}
                        {!! Form::text('panno', $customer->panno, ['class' => 'form-control form-control-normal', 'placeholder' => 'Pan No.', 'disabled']) !!}
                        @if($errors->has('panno'))
                            <p class="help-block error">
                                {{ $errors->first('panno') }}
                            </p>
                        @endif
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        {!! Form::label('dob', 'Date of Birth*', ['class' => 'control-label']) !!}
                        {!! Form::text('dob', $customer->dob, ['class' => 'form-control form-control-normal mydatepicker', 'placeholder' => 'Date of birth']) !!}

                        <p class="help-block"></p>
                        @if($errors->has('dob'))
                            <p class="help-block error">
                                {{ $errors->first('dob') }}
                            </p>
                        @endif
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                    {!! Form::label('address', 'Address', ['class' => 'control-label']) !!}
                    {!! Form::textarea('address', $customer->address, ['class' => 'form-control form-control-normal', 'placeholder' => 'Address', 'rows'=> '2']) !!}


                    <p class="help-block"></p>
                    @if($errors->has('address'))
                        <p class="help-block">
                            {{ $errors->first('address') }}
                        </p>
                    @endif
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                    {!! Form::label('pincode', 'Pincode', ['class' => 'control-label']) !!}
                    {!! Form::text('pincode', $customer->pincode, ['class' => 'form-control form-control-normal', 'placeholder' => 'pincode']) !!}


                    <p class="help-block"></p>
                    @if($errors->has('pincode'))
                        <p class="help-block">
                            {{ $errors->first('pincode') }}
                        </p>
                    @endif
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                    {!! Form::label('state', 'State*', ['class' => 'control-label']) !!}

                    <select class="form-control form-control-normal" name="state" id="state-dropdown" required="">
                        <option value="">Select State</option>
                        @foreach ($states as $state) 
                        <option value="{{$state->id}}" @if ($customer->state==$state->id) selected  @endif>
                        {{$state->name}}
                        </option>
                        @endforeach
                    </select>
                    <p class="help-block"></p>
                    @if($errors->has('state'))
                        <p class="help-block">
                            {{ $errors->first('state') }}
                        </p>
                    @endif
                    </div>
                </div>
                @php
                    if(!empty($customer->state)){
                        $cities = \App\City::where('state_id', $customer->state)->get();
                    }
                @endphp
                <div class="col-md-4">
                    <div class="form-group">
                    {!! Form::label('city', 'City*', ['class' => 'control-label']) !!}
                    <select class="form-control form-control-normal" name="city" id="city-dropdown" required="">
                        @if(isset($cities))
                            @foreach($cities as $city)
                                <option value="{{$city->id}}" {{ $customer->city == $city->id ? 'selected' : '' }}>{{$city->name}}</option>
                            @endforeach
                        @endif
                    </select>
                    <p class="help-block"></p>
                    @if($errors->has('city'))
                        <p class="help-block">
                            {{ $errors->first('city') }}
                        </p>
                    @endif
                    </div>
                </div>                
            </div>
            @endif
            <h3 class="card-title">Solar And Connection</h3>
            <div class="row">
                <div class="col-md-4">
                    <div class="form-group">
                        {!! Form::label('bank_name', 'Bank Name', ['class' => 'control-label']) !!}
                        {!! Form::text('bank_name', $customer->bank_name, ['class' => 'form-control form-control-normal', 'placeholder' => 'Bank Name']) !!}

                        <p class="help-block">Nationalised Bank Only</p>
                        @if($errors->has('bank_name'))
                            <p class="help-block error">
                                {{ $errors->first('bank_name') }}
                            </p>
                        @endif
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        {!! Form::label('econn_no', 'Monthly Avg Electric Bill Amount*', ['class' => 'control-label']) !!}
                        {!! Form::text('econn_no', $customer->econn_no, ['class' => 'form-control form-control-normal', 'placeholder' => 'Monthly Avg Electric Bill Amount', 'required']) !!}
                        <p class="help-block"></p>
                        @if($errors->has('econn_no'))
                            <p class="help-block error">
                                {{ $errors->first('econn_no') }}
                            </p>
                        @endif
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        {!! Form::label('solar_conn_type', 'Solar Connection Type*', ['class' => 'control-label']) !!}
                        {!! Form::select('solar_conn_type', array('Off-Grid'=>'Off-Grid', 'On-Grid'=>'On-Grid', 'Hybrid'=>'Hybrid'), !empty($customer->solar_conn_type) ? $customer->solar_conn_type : 'Off-Grid', ['class' => 'form-control form-control-normal', 'required', 'placeholder' => 'Select']) !!}
                        <p class="help-block"></p>
                        @if($errors->has('solar_conn_type'))
                            <p class="help-block error">
                                {{ $errors->first('solar_conn_type') }}
                            </p>
                        @endif
                    </div>
                </div>
                <div class="col-md-4">
                      <div class="form-group">
                      <label for="ev_ev_solar_model_id" class="control-label">Select KW<span class="required">*</span></label>                  
                      <select name="ev_ev_solar_model_id" class="form-control form-control-normal" id="ev_ev_solar_model_id" required>
                          @foreach($ev_ev_solar_model_ids as $ev_ev_solar_model_id)
                          <option value="{{$ev_ev_solar_model_id->id}}" amount="{{$ev_ev_solar_model_id->amount}}" {{ $customer->ev_ev_solar_model_id == $ev_ev_solar_model_id->id ? 'selected' : '' }}>{{$ev_ev_solar_model_id->title}}</option>
                          @endforeach
                      </select>
                      @if($errors->has('ev_ev_solar_model_id'))
                          <p class="help-block error">
                              {{ $errors->first('ev_ev_solar_model_id') }}
                          </p>
                      @endif
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        {!! Form::label('property_type', 'Property Type*', ['class' => 'control-label']) !!}
                        {!! Form::select('property_type', array('Residential'=>'Residential', 'Commercial'=>'Commercial', 'Industrial'=>'Industrial', 'Government'=>'Government'), !empty($customer->property_type) ? $customer->property_type : 'Residential', ['class' => 'form-control form-control-normal', 'required', 'placeholder' => 'Select']) !!}
                        <p class="help-block"></p>
                        @if($errors->has('property_type'))
                            <p class="help-block error">
                                {{ $errors->first('property_type') }}
                            </p>
                        @endif
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        {!! Form::label('monthly_elecbill', 'Electricity Connection No.', ['class' => 'control-label']) !!}
                        {!! Form::number('monthly_elecbill', $customer->monthly_elecbill, ['class' => 'form-control form-control-normal', 'placeholder' => 'Electricity Connection No.']) !!}
                        <p class="help-block"></p>
                        @if($errors->has('monthly_elecbill'))
                            <p class="help-block error">
                                {{ $errors->first('monthly_elecbill') }}
                            </p>
                        @endif
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        {!! Form::label('payment_type', 'Payment Type*', ['class' => 'control-label']) !!}
                        {!! Form::select('payment_type', array('loan'=>'Loan', 'self'=>'Self'), !empty($customer->payment_type) ? $customer->payment_type : 'loan', ['class' => 'form-control form-control-normal', 'required', 'placeholder' => 'Select', 'id'=>'payment_type']) !!}
                        <p class="help-block"></p>
                        @if($errors->has('payment_type'))
                            <p class="help-block error">
                                {{ $errors->first('payment_type') }}
                            </p>
                        @endif
                    </div>
                </div>
                <div class="col-md-4" id="loan_amount_con">
                    <div class="form-group">
                        {!! Form::label('loan_amount', 'Loan Amount', ['class' => 'control-label']) !!}
                        {!! Form::number('loan_amount', $customer->loan_amount, ['class' => 'form-control form-control-normal loan_amount', 'placeholder' => 'Loan Amount']) !!}
                        <p class="help-block"></p>
                        @if($errors->has('loan_amount'))
                            <p class="help-block error">
                                {{ $errors->first('loan_amount') }}
                            </p>
                        @endif
                    </div>
                </div>
            </div>
            <h3 class="card-title">Upload Documents</h3>
            <div class="row">
                <div class="col-md-4">
                    <div class="form-group">
                        {!! Form::label('aadhar_front', 'Aadhaar Front*', ['class' => 'control-label']) !!}
                        <p class="help-block"><a href="{{url('/uploads/proofs/'.$customer->aadhar_front)}}" target="_blank"><img src="{{url('/uploads/proofs/'.$customer->aadhar_front)}}" style="width: 200px;"></a></p>
                        @if($errors->has('aadhar_front'))
                            <p class="help-block error">
                                {{ $errors->first('aadhar_front') }}
                            </p>
                        @endif
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        {!! Form::label('aadhar_back', 'Aadhaar Back*', ['class' => 'control-label']) !!}
                        <p class="help-block"><a href="{{url('/uploads/proofs/'.$customer->aadhar_back)}}" target="_blank"><img src="{{url('/uploads/proofs/'.$customer->aadhar_back)}}" style="width: 200px;"></a></p>
                        @if($errors->has('aadhar_back'))
                            <p class="help-block error">
                                {{ $errors->first('aadhar_back') }}
                            </p>
                        @endif
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        {!! Form::label('pan_image', 'Pan Card*', ['class' => 'control-label']) !!}
                        <p class="help-block"><a href="{{url('/uploads/proofs/'.$customer->pan_image)}}" target="_blank"><img src="{{url('/uploads/proofs/'.$customer->pan_image)}}" style="width: 200px;"></a></p>
                        @if($errors->has('pan_image'))
                            <p class="help-block error">
                                {{ $errors->first('pan_image') }}
                            </p>
                        @endif
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        {!! Form::label('elect_bill_image', 'Electricity Bill*', ['class' => 'control-label']) !!}
                        <p class="help-block"><a href="{{url('/uploads/proofs/'.$customer->elect_bill_image)}}" target="_blank"><img src="{{url('/uploads/proofs/'.$customer->elect_bill_image)}}" style="width: 200px;"></a></p>
                        @if($errors->has('elect_bill_image'))
                            <p class="help-block error">
                                {{ $errors->first('elect_bill_image') }}
                            </p>
                        @endif
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        {!! Form::label('pass_cheque_book', 'Bank Passbook or Cheque*', ['class' => 'control-label']) !!}
                        <p class="help-block"><a href="{{url('/uploads/proofs/'.$customer->pass_cheque_book)}}" target="_blank"><img src="{{url('/uploads/proofs/'.$customer->pass_cheque_book)}}" style="width: 200px;"></a></p>
                        @if($errors->has('pass_cheque_book'))
                            <p class="help-block error">
                                {{ $errors->first('pass_cheque_book') }}
                            </p>
                        @endif
                    </div>
                </div>
            </div>
            <h3 class="card-title">Payment</h3>
            <div class="row">
                <div class="col-md-6 col-sm-12 col-xs-12">
                    <img src="{{url('/MerchantQRCode.png')}}" style="width: 537px;">
                </div>
                <div class="col-md-6 col-sm-6 col-xs-12">
                    <div class="row">
                        
                        <div class="col-md-12">
                            <div class="form-group">
                                {!! Form::label('payment_proof', 'Screenshot of UPI payment', ['class' => 'control-label']) !!}
                                <p class="help-block"><a href="{{url('/uploads/proofs/'.$customer->payment_proof)}}" target="_blank"><img src="{{url('/uploads/proofs/'.$customer->payment_proof)}}" style="width: 200px;"></a></p>
                                @if($errors->has('payment_proof'))
                                    <p class="help-block error">
                                        {{ $errors->first('payment_proof') }}
                                    </p>
                                @endif
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                {!! Form::label('payment_reference', 'Payment Reference/UTR No.*', ['class' => 'control-label']) !!}
                                {!! Form::text('payment_reference', $customer->payment_reference, ['class' => 'form-control form-control-normal', 'placeholder' => 'Payment Reference/UTR No.', 'required']) !!}
                                <p class="help-block"></p>
                                @if($errors->has('payment_reference'))
                                    <p class="help-block error">
                                        {{ $errors->first('payment_reference') }}
                                    </p>
                                @endif
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                {!! Form::label('sponsor_comment', 'Any Comment Optional', ['class' => 'control-label']) !!}
                                {!! Form::text('sponsor_comment', $customer->sponsor_comment, ['class' => 'form-control form-control-normal', 'placeholder' => 'Any Comment Optional']) !!}
                                <p class="help-block"></p>
                                @if($errors->has('sponsor_comment'))
                                    <p class="help-block error">
                                        {{ $errors->first('sponsor_comment') }}
                                    </p>
                                @endif
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            @if($customer->status==0 || $customer->status==1)
            <h3 class="card-title">Payment</h3>
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        {!! Form::label('status', 'Status*', ['class' => 'control-label']) !!}
                        @if($customer->status==0)
                        {!! Form::select('status', array(1=>'approved', 4=>'cancelled'), !empty($customer->status) ? $customer->status : '', ['class' => 'form-control form-control-normal', 'required', 'placeholder' => 'Select']) !!}
                        @endif
                        @if($customer->status==1 && $customer->payment_type=='loan')
                        {!! Form::select('status', array(2=>'loanApproved', 3=>'loanDeclined'), !empty($customer->status) ? $customer->status : '', ['class' => 'form-control form-control-normal', 'required', 'placeholder' => 'Select']) !!}
                        @endif
                        <p class="help-block"></p>
                        @if($errors->has('status'))
                            <p class="help-block error">
                                {{ $errors->first('status') }}
                            </p>
                        @endif
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        {!! Form::label('admin_comment', 'Admin Comment*', ['class' => 'control-label']) !!}
                        {!! Form::text('admin_comment', $customer->admin_comment, ['class' => 'form-control form-control-normal', 'placeholder' => 'Admin Comment', 'required']) !!}
                        <p class="help-block"></p>
                        @if($errors->has('admin_comment'))
                            <p class="help-block error">
                                {{ $errors->first('admin_comment') }}
                            </p>
                        @endif
                    </div>
                </div>
            </div>
            @endif
        </div>
        <div class="ln_solid"></div>
        <div class="form-group">
          <div class="col-md-12 col-sm-12 col-xs-12 col-md-offset-3 text-center">
            <button type="submit" class="btn btn-lg btn-success">Submit</button>
          </div>
        </div>
      </form>
    </div>
  </div>
</div>
<!-- /page content -->
@endsection
@section('javascript')
    <!-- Date picker plugins css -->
    <link href="{{ url('assets') }}/plugins/bootstrap-datepicker/bootstrap-datepicker.min.css" rel="stylesheet" type="text/css" />
    <!-- Daterange picker plugins css -->
    <link href="{{ url('assets') }}/plugins/timepicker/bootstrap-timepicker.min.css" rel="stylesheet">
    <link href="{{ url('assets') }}/plugins/bootstrap-daterangepicker/daterangepicker.css" rel="stylesheet">
    <!-- Date Picker Plugin JavaScript -->
    <script src="{{ url('assets') }}/plugins/bootstrap-datepicker/bootstrap-datepicker.min.js"></script>
    <!-- Date range Plugin JavaScript -->
    <script src="{{ url('assets') }}/plugins/timepicker/bootstrap-timepicker.min.js"></script>
    <script src="{{ url('assets') }}/plugins/bootstrap-daterangepicker/daterangepicker.js"></script>
    <script src="{{ url('assets') }}/js/jqBootstrapValidation.js"></script>
    <link href="{{ url('assets') }}/plugins/EasyAutocomplete/easy-autocomplete.min.css" rel="stylesheet">
    <script src="{{ url('assets') }}/plugins/EasyAutocomplete/jquery.easy-autocomplete.min.js"></script>
    <script src="{{ url('assets') }}/js/typeahead.bundle.js"></script>
    <!-- Parsley -->
    <script>
    $(document).ready(function() {
      jQuery('.mydatepicker, #datepicker').datepicker({format: 'dd-mm-yyyy'});
      $('#state-dropdown').on('change', function() {
            var state_id = this.value;
            $("#city-dropdown").html('');
            $.ajax({
                url:"{{url('get-cities-by-state')}}",
                type: "POST",
                data: {
                state_id: state_id,
                _token: '{{csrf_token()}}' 
                },
                dataType : 'json',
                success: function(result){
                $('#city-dropdown').html('<option value="">Select City</option>'); 
                $.each(result.cities,function(key,value){
                $("#city-dropdown").append('<option value="'+value.id+'">'+value.name+'</option>');
                });
                }
            });
        });
    });

    payment_type
    loan_amount

    $(document).ready(function() {
        $('#payment_type').on('change', function() {
            var selectedValue = $(this).val();
            if(selectedValue=="self"){
                $("#loan_amount_con").hide();
            }else{
                $("#loan_amount_con").show();
            }            
        });

        $('#ev_ev_solar_model_id').on('change', function() {
            var selectedValue = $(this).find(':selected').attr('amount');            
            $("input#loan_amount").val(selectedValue);
            
        });
        const form = document.getElementById('demo-form2');

        form.addEventListener('submit', function(event) {
          const amount = $('#ev_ev_solar_model_id').find(':selected').attr('amount');
          
          if ($("input#loan_amount").val() > amount && $('#payment_type').val()=='loan') {
            // Prevent the form from submitting
            event.preventDefault();
            alert('Loan Amount must be less or equal to '+amount);
          }
        });
    });
    
    function copyText() {
      var copyText = document.getElementById("copyInput");
      navigator.clipboard.writeText(copyText.value).then(() => {
        alert("Text copied: " + copyText.value);
      });
    }
    </script>
@endsection