@extends('layouts.app')

@section('content')
    <div class="card card-outline-info">
        <div class="card-header">
            <h4 class="m-b-0 text-white">Dealer Registration</h4>
        </div>
    <div class="card-body">
    {!! Form::open(['method' => 'POST','id'=> 'register_frm', 'data-parsley-validate'=>'', 'route' => ['admin.dealers.store']]) !!}
        <div class="form-body control-group">
            <h4 class="card-title">Select Dealer Type And Sponsor Dealer</h4>
                <div class="row">
                  <div class="col-md-12">
                      <div class="form-group">
                      <label for="select_model" class="control-label">Dealer Type<span class="required">*</span></label>                  
                      <select name="dealer_type" class="form-control form-control-normal" id="dealer_type" required>
                          @foreach($dealer_types as $key=>$dealer_type)
                          <option value="{{$key}}">{{$dealer_type}}</option>
                          @endforeach
                      </select>
                      @if($errors->has('dealer_type'))
                          <p class="help-block error">
                              {{ $errors->first('dealer_type') }}
                          </p>
                      @endif
                    </div>
                  </div>
                  <div class="col-md-6 payOpt">
                    <div class="form-group">
                    <label for="dealer_username" class="control-label">Dealer Username<span class="required">*</span></label>                  
                          <input type="text" name="dealer_username" id="sponsor-typeahead" class="form-control form-control-normal" placeholder="Dealer's Username" value="{{ old('dealer_username') }}" style="margin-bottom: 0px; " />
                          <p class="help-block"></p>
                          @if($errors->has('dealer_username'))
                              <p class="help-block error">
                                  {{ $errors->first('dealer_username') }}
                              </p>
                          @endif
                      </div>
                  </div>
                  <div class="col-md-6 payOpt">
                    <div class="form-group">
                    <label for="select_model" class="control-label">Dealer Name<span class="required">*</span></label>
                          {!! Form::text('sponsor_name', old('sponsor_name'), ['class' => 'form-control form-control-normal', 'id' => 'sponsor_name', 'placeholder' => '','disabled'=>'']) !!}
                          <p class="help-block"></p>
                          @if($errors->has('sponsor_name'))
                              <p class="help-block error">
                                  {{ $errors->first('sponsor_name') }}
                              </p>
                          @endif
                      </div>
                  </div>

                </div>
           <h3 class="card-title">Personal Detail</h3>
            <div class="row">
                <div class="col-md-12">
                    <div class="form-group">
                        {!! Form::label('full_name', 'Full Name*', ['class' => 'control-label', 'required']) !!}
                        @if(isset($bookingWallet->dealer_name))
                        {!! Form::text('full_name', $bookingWallet->dealer_name, ['class' => 'form-control form-control-normal','id'=>'full_name', 'placeholder' => 'Full Name here']) !!}                        
                        @else
                        {!! Form::text('full_name', old('full_name'), ['class' => 'form-control form-control-normal','id'=>'full_name', 'placeholder' => 'Full Name here']) !!}
                        @endif
                        <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'), old('gender'), ['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']) !!}
                        @if(isset($bookingWallet->dealer_mobile))
                        {!! Form::number('mobileno', $bookingWallet->dealer_mobile, ['class' => 'form-control form-control-normal', 'id'=>'mobileno', 'placeholder' => 'Mobile No.', 'maxlength' => '10', 'required']) !!}
                        @else
                        {!! Form::number('mobileno', old('mobileno'), ['class' => 'form-control form-control-normal', 'id'=>'mobileno', 'placeholder' => 'Mobile No.', 'maxlength' => '10', 'required']) !!}                        
                        @endif
                        <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', old('email'), ['class' => 'form-control form-control-normal', 'id' => 'email', 'placeholder' => 'Email', 'required']) !!}
                    <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('aadhar', 'Aadhar*', ['class' => 'control-label']) !!}
                        {!! Form::number('aadhar', old('aadhar'), ['class' => 'form-control form-control-normal', 'placeholder' => 'Aadhar no.']) !!}
                        <p class="help-block">Only number no space</p>
                        @if($errors->has('aadhar'))
                            <p class="help-block error">
                                {{ $errors->first('aadhar') }}
                            </p>
                        @endif
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        {!! Form::label('panno', 'Pan no.*', ['class' => 'control-label']) !!}
                        {!! Form::text('panno', old('panno'), ['class' => 'form-control form-control-normal', 'placeholder' => 'Pan No.']) !!}
                        <p class="help-block">If you don't have PAN card please enter "N/A"</p>
                        @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('address', 'Address', ['class' => 'control-label']) !!}
                    {!! Form::textarea('address', old('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', old('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">
                    <label class="control-label" for="city_id">Select State</label>
                    <select name="state_id" id="state-dropdown" class="form-control">
                        @foreach($states as $state)
                        <option value="{{$state->id}}" @if($state->id==23) selected="selected" @endif>{{$state->name}}</option>
                        @endforeach
                    </select>
                    @if ($errors->has('state_id'))
                      <p class="help-block">
                          <strong>{{ $errors->first('state_id') }}</strong>
                      </p>
                    @endif
                </div>
              </div>
              <div class="col-md-4">
                <div class="form-group">
                    <label class="control-label" for="city_id">Select City</label>
                    <select name="city_id" id="city-dropdown" class="form-control">
                        @foreach($cities as $city)
                        <option value="{{$city->id}}">{{$city->name}}</option>
                        @endforeach
                    </select>
                    @if ($errors->has('city_id'))
                      <p class="help-block">
                          <strong>{{ $errors->first('city_id') }}</strong>
                      </p>
                    @endif
                </div>
            </div>


            </div>
            <div class="row">
                <div class="col-md-12">
                <div class="form-group">
                    {{ Form::checkbox('agreeterms', 1, null, ['id' => 'agreeterms', 'checked'=>true]) }}
                    {!! Form::label('agreeterms', 'By submitting this form I agree to the Terms and Conditions.', ['class' => 'control-label', 'style'=>'color:#455a64;']) !!}
                    <p class="help-block"></p>
                    @if($errors->has('agreeterms'))
                        <p class="help-block error">
                            {{ $errors->first('agreeterms') }}
                        </p>
                    @endif
                </div>
              </div>

            </div>
        </div>
        @if(isset($bookingWallet->deal_id))
        <input type="hidden" name="deal_id" value="{{$bookingWallet->deal_id}}">
        @endif
    {!! Form::button('Submit', ['class' => 'btn btn-primary', 'id' => "preview_btn", 'data-target' => "#exampleModal"]) !!}
    <div class="modal fade" tabindex="-1" role="dialog" id="exampleModal" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title">Modal title</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            <p>
              Are you sure? Please check Name, Email and Mobile no.
              These value can not be updated later!
              Passwords will be sent to email and to the mobile no. provided. Please keep it safe.
            </p>
            <p>
              <strong>Full Name : </strong><span id="fullName"></span> <br />
              <strong>Email : </strong><span id="Email"></span> <br />
              <strong>Mobile no : </strong><span id="mobileNo"></span> <br />
            </p>
          </div>
          <div class="modal-footer">
            {!! Form::submit('Proceed To register', ['class' => 'btn btn-danger']) !!}
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          </div>
        </div>
      </div>
    </div>
    {!! Form::close() !!}
    </div>
    </div>

@stop

@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>
      jQuery('.mydatepicker, #datepicker').datepicker({format: 'dd-mm-yyyy'});

      $(document).ready(function() {
        $('div.payOpt').hide();
        $('select#dealer_type').on('change', function() {
          if( this.value == 'mega' || this.value == ''){
            $('div.payOpt').hide();
          }else{
            $('div.payOpt').show();
          }
        });

        $('#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>');
                });
                }
            });
        });

        $("form#register_frm").not("[type=submit]").jqBootstrapValidation();
        $('#preview_btn').click(function(event) {
          $("#exampleModal").modal();
        });
        $( "input#email" ).change(function() {
          $( "span#Email" ).text($(this).val());
        });
        $( "input#full_name" ).change(function() {
          $( "span#fullName" ).text($(this).val());
        });
        $( "input#mobileno" ).change(function() {
          $( "span#mobileNo" ).text($(this).val());
        });
        $( "input#sponsor-typeahead" ).change(function() {
          $( "span#sponsorName" ).text($(this).val());
        });
      });

      var options = {

        url: function(phrase) {
          return "{{ url('/') }}/admin/dealers/getSponsors/" + phrase;
        },

        getValue: function(element) {
          return element.username;
        },

        template: {
            type: "description",
            fields: {
                description: "full_name"
            }
        },
        list: {
              onSelectItemEvent: function() {
                var data = $("#sponsor-typeahead").getSelectedItemData().full_name;
                $("input#sponsor_name").val(data).trigger("change");
              }
        },
        theme: "plate-dark",
        ajaxSettings: {
          dataType: "json",
          method: "GET",
          data: {
            dataType: "json"
          }
        },
        preparePostData: function(data) {
          data.phrase = $("#sponsor-typeahead").val();
          return data;
        },

        requestDelay: 400
      };

      $("#sponsor-typeahead").easyAutocomplete(options);
</script>
@endsection
