<?php $__env->startSection('content'); ?>
    <div class="card card-outline-info">
        <div class="card-header">
            <h4 class="m-b-0 text-white">Create Deals</h4>
        </div>
    <div class="card-body">
    <?php echo Form::open(['method' => 'POST','id'=> 'register_frm', 'data-parsley-validate'=>'', 'route' => ['admin.deals.storePendingPlotDeal']]); ?>

        <div class="form-body control-group">
            <h3 class="card-title">Customer</h3>
            <div class="row">
                <div class="col-md-3">
                    <div class="form-group">
                        <label class="control-label" for="project_id">Customer Name</label>
                        <p class="help-block"><?php echo e($bookingWallet->customer_name); ?></p>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="form-group">
                        <label class="control-label" for="project_id">Customer Mobile Number</label>
                        <p class="help-block"><?php echo e($bookingWallet->customer_mobile); ?></p>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <p class="control-label" for="project_id">Find Customer Or <a href="<?php echo e(url('/')); ?>/admin/customers/createPendingDeal/<?php echo e($bookingWallet->id); ?>" class="btn btn-primary btn-sm">Click Here</a> to create new customer</p>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <?php echo Form::label('customer_id', 'Customer Id*', ['class' => 'control-label']); ?>

                        <input type="text" name="customer_id" id="customer-typeahead" class="form-control form-control-normal" placeholder="Customer's Username" value="<?php if(!is_null(Session::get('pendingDealCustUsername'))): ?><?php echo e(Session::get('pendingDealCustUsername')); ?><?php else: ?><?php echo e(old('customer')); ?><?php endif; ?>" required="" style="margin-bottom: 0px; " />
                        <p class="help-block"></p>
                        <?php if($errors->has('customer_id')): ?>
                            <p class="help-block error">
                                <?php echo e($errors->first('customer_id')); ?>

                            </p>
                        <?php endif; ?>
                    </div>
                </div>
                <!--/span-->
                <div class="col-md-6">
                    <div class="form-group">
                        <?php echo Form::label('customer_name', 'Customer name*', ['class' => 'control-label']); ?>

                        
                        <?php if(!is_null(Session::get('pendingDealCustName'))): ?>  
                        <?php echo Form::text('customer_name', Session::get('pendingDealCustName'), ['class' => 'form-control form-control-normal', 'id' => 'customer_name', 'placeholder' => '','disabled'=>'']); ?>

                        <?php else: ?>
                        <?php echo Form::text('customer_name', old('customer_name'), ['class' => 'form-control form-control-normal', 'id' => 'customer_name', 'placeholder' => '','disabled'=>'']); ?>

                        <?php endif; ?>
                        
                        <p class="help-block"></p>
                        <?php if($errors->has('customer_name')): ?>
                            <p class="help-block error">
                                <?php echo e($errors->first('customer_name')); ?>

                            </p>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            <h3 class="card-title">Business Partner</h3>
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <?php echo Form::label('sponsor_id', 'Business Partner Id*', ['class' => 'control-label']); ?>

                        <input type="text" name="sponsor_id" id="sponsor-typeahead" class="form-control form-control-normal" placeholder="Sponsor's Username" value="<?php echo e($associate->username); ?>" required="" style="margin-bottom: 0px; " />
                        <p class="help-block"></p>
                        <?php if($errors->has('sponsor_id')): ?>
                            <p class="help-block error">
                                <?php echo e($errors->first('sponsor_id')); ?>

                            </p>
                        <?php endif; ?>
                    </div>
                </div>
                <!--/span-->
                <div class="col-md-6">
                    <div class="form-group">
                        <?php echo Form::label('sponsor_name', 'Business Partner name*', ['class' => 'control-label']); ?>

                        <?php echo Form::text('sponsor_name', $associate->full_name, ['class' => 'form-control form-control-normal', 'id' => 'sponsor_name', 'placeholder' => '','disabled'=>'']); ?>

                        <p class="help-block"></p>
                        <?php if($errors->has('sponsor_name')): ?>
                            <p class="help-block error">
                                <?php echo e($errors->first('sponsor_name')); ?>

                            </p>
                        <?php endif; ?>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <?php echo Form::label('sponsor_name', 'From Business Partner ID*', ['class' => 'control-label']); ?>

                        
                        <p class="help-block"><?php echo e($from_user->username); ?></p>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <?php echo Form::label('sponsor_name', 'From Business Partner Name*', ['class' => 'control-label']); ?>

                        
                        <p class="help-block"><?php echo e($from_user->full_name); ?></p>
                    </div>
                </div>
            </div>
            <h3 class="card-title">Select Property</h3>
            <div class="row">
              <div class="col-md-3">
                  <div class="form-group">
                      <label class="control-label" for="project_id">Project*</label>
                      <p class="help-block"><?php echo e($plot->projectName); ?></p>
                  </div>
              </div>
              <div class="col-md-3">
                  <div class="form-group">
                      <label class="control-label" for="sector_id">Sector*</label>
                      <p class="help-block"><?php echo e($plot->sectorName); ?></p>
                  </div>
              </div>
              <div class="col-md-3">
                  <div class="form-group">
                      <label class="control-label" for="block_id">Block*</label>
                      <p class="help-block"><?php echo e($plot->blockName); ?></p>
                  </div>
              </div>
              <div class="col-md-3">
                  <div class="form-group">
                      <label class="control-label" for="plot_id">Plot*</label>
                      <p class="help-block"><?php echo e($plot->name); ?></p>
                  </div>
              </div>
              <div class="col-md-3">
                  <div class="form-group">
                      <label class="control-label" for="plot_id">PLC*</label>
                      <p class="help-block"><?php echo e($plot->plcName); ?></p>
                  </div>
              </div>
              <input type="hidden" name="project_id" value="<?php echo e($plot->projectId); ?>">
              <input type="hidden" name="sector_id" value="<?php echo e($plot->sectorId); ?>">
              <input type="hidden" name="block_id" value="<?php echo e($plot->blockId); ?>">
              <input type="hidden" name="plot_id" value="<?php echo e($plot->id); ?>">
              <!--/span-->
              <div class="col-md-3">
                <div class="form-group">
                    <label class="control-label" for="payment_plan">Payment Plan*</label>
                    <?php echo Form::select('payment_plan', [0=>'Full Payment', 1=>'EMI Payment'], 0, ['class' => 'form-control form-control-normal', 'id'=>'payment_plan']); ?>

                    <?php if($errors->has('payment_plan')): ?>
                        <p class="help-block error alert">
                          <?php echo e($errors->first('payment_plan')); ?>

                        </p>
                    <?php endif; ?>
                </div>
              </div>
              <div class="col-md-3" id="full_payment_plan_id">
                <div class="form-group">
                  <label class="control-label" for="full_payment_plan_id">Full Payment Plan*</label>
                  <?php echo Form::select('full_payment_plan_id', $fullPayments, '', ['class' => 'form-control form-control-normal', 'id' => 'full_payment_plan_id']); ?>

                  <?php if($errors->has('full_payment_plan_id')): ?>
                      <p class="help-block error alert">
                          <?php echo e($errors->first('full_payment_plan_id')); ?>

                      </p>
                  <?php endif; ?>
                </div>
              </div>
              <div class="col-md-3 emi_payment_plan_id" style="display: none;">
                <div class="form-group">
                  <label class="control-label" for="emi_payment_plan_id">EMI Payment Plan*</label>
                  <?php echo Form::select('emi_payment_plan_id', $emiPayments, '', ['class' => 'form-control form-control-normal', 'id' => 'emi_payment_plan_id']); ?>

                  <?php if($errors->has('emi_payment_plan_id')): ?>
                      <p class="help-block error alert">
                          <?php echo e($errors->first('emi_payment_plan_id')); ?>

                      </p>
                  <?php endif; ?>
                </div>
              </div>
              <div class="col-md-3 emi_payment_plan_id" style="display: none;">
                <div class="form-group">
                  <label class="control-label" for="emi_duration">Duration*</label>
                  <?php echo Form::select('emi_duration', $emi_duration, '', ['class' => 'form-control form-control-normal', 'id' => 'emi_duration']); ?>

                  <?php if($errors->has('emi_duration')): ?>
                      <p class="help-block error alert">
                          <?php echo e($errors->first('emi_duration')); ?>

                      </p>
                  <?php endif; ?>
                </div>
              </div>
            </div>
            <h3 class="card-title">Payment Overview</h3>
            <div class="row" id="full_payment">
                <div class="col-md-3">
                    <div class="form-group">
                        <label class="control-label" for="booking_amount">Booking Amount*</label>
                        <input type="text" id="booking_amount" name="booking_amount" required="required" class="form-control" value="">
                          <?php if($errors->has('booking_amount')): ?>
                              <p class="help-block">
                                  <strong><?php echo e($errors->first('booking_amount')); ?></strong>
                              </p>
                          <?php endif; ?>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="form-group">
                        <label class="control-label" for="allotment_amount">Allotment*</label>
                        <input type="text" id="allotment_amount" name="allotment_amount" required="required" class="form-control" value="">
                          <?php if($errors->has('allotment_amount')): ?>
                              <p class="help-block">
                                  <strong><?php echo e($errors->first('allotment_amount')); ?></strong>
                              </p>
                          <?php endif; ?>
                    </div>
                </div>
                <div class="col-md-3" id="registryAmount">
                    <div class="form-group">
                        <label class="control-label" for="registry_amount">Registry*</label>
                        <input type="text" id="registry_amount" name="registry_amount" required="required" class="form-control" value="0">
                          <?php if($errors->has('registry_amount')): ?>
                              <p class="help-block">
                                  <strong><?php echo e($errors->first('registry_amount')); ?></strong>
                              </p>
                          <?php endif; ?>
                    </div>
                </div>
                <div class="col-md-3" id="plc_amount">
                    <div class="form-group">
                        <label class="control-label" for="plc_amount">PLC Charge*</label>
                        <input type="text" id="plc_amount" name="plc_amount" required="required" class="form-control" value="0">
                          <?php if($errors->has('plc_amount')): ?>
                              <p class="help-block">
                                  <strong><?php echo e($errors->first('plc_amount')); ?></strong>
                              </p>
                          <?php endif; ?>
                    </div>
                </div>
            </div>

            <div class="row" id="emi_payment">
            </div>
            <input type="hidden" name="totalAmount" id="totalAmount" value="0">
            <input type="hidden" name="calc_emi_amount" id="calc_emi_amount" value="0">
            <input type="hidden" name="deal_id" value="<?php echo e($bookingWallet->source_id); ?>">
    <?php echo Form::submit('Proceed To register', ['class' => 'btn btn-danger']); ?>

    <?php echo Form::close(); ?>

    </div>
    </div>

<?php $__env->stopSection(); ?>

<?php $__env->startSection('javascript'); ?>
    <!-- Date picker plugins css -->
    <link href="<?php echo e(url('assets')); ?>/plugins/bootstrap-datepicker/bootstrap-datepicker.min.css" rel="stylesheet" type="text/css" />
    <!-- Daterange picker plugins css -->
    <link href="<?php echo e(url('assets')); ?>/plugins/timepicker/bootstrap-timepicker.min.css" rel="stylesheet">
    <link href="<?php echo e(url('assets')); ?>/plugins/bootstrap-daterangepicker/daterangepicker.css" rel="stylesheet">
    <!-- Date Picker Plugin JavaScript -->
    <script src="<?php echo e(url('assets')); ?>/plugins/bootstrap-datepicker/bootstrap-datepicker.min.js"></script>
    <!-- Date range Plugin JavaScript -->
    <script src="<?php echo e(url('assets')); ?>/plugins/timepicker/bootstrap-timepicker.min.js"></script>
    <script src="<?php echo e(url('assets')); ?>/plugins/bootstrap-daterangepicker/daterangepicker.js"></script>
    <script src="<?php echo e(url('assets')); ?>/js/jqBootstrapValidation.js"></script>
    <link href="<?php echo e(url('assets')); ?>/plugins/EasyAutocomplete/easy-autocomplete.min.css" rel="stylesheet">
    <script src="<?php echo e(url('assets')); ?>/plugins/EasyAutocomplete/jquery.easy-autocomplete.min.js"></script>
    <script src="<?php echo e(url('assets')); ?>/js/typeahead.bundle.js"></script>
    <!-- Parsley -->
    <script>
      jQuery('.mydatepicker, #datepicker').datepicker({format: 'yyyy-mm-dd'});

      $(document).ready(function() {
        $("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() {
          $( "input#sponsorName" ).text($(this).val());
        });
      });

      var options = {

        url: function(phrase) {
          return "<?php echo e(url('/')); ?>/users/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);
      var options = {

        url: function(phrase) {
          return "<?php echo e(url('/')); ?>/admin/deals/getCustomersName/" + phrase;
        },

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

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

        requestDelay: 400
      };

      $("#customer-typeahead").easyAutocomplete(options);

      $("select[name='payment_plan']").change(function(){
        var payment_type = $(this).val();
        if(payment_type == 0){
          $('div#full_payment_plan_id').show();
          $('div.emi_payment_plan_id').hide();
        }else{
          $('div#full_payment_plan_id').hide();
          $('div.emi_payment_plan_id').show();
        }
      });
      
      $("select[name='full_payment_plan_id']").change(function(){
          var project_id = $("input[name='project_id']").val();
          var sector_id = $("input[name='sector_id']").val();
          var block_id = $("input[name='block_id']").val();
          var plot_id = $("input[name='plot_id']").val();

          var full_payment_plan_id = $(this).val();
          var payment_plan_type = $("select[name='payment_plan']").val();
          var token = $("input[name='_token']").val();
        $.toast({
            heading: 'Please wait!',
            text: 'Please wait till server is  calculating amount.',
            position: 'top-right',
            loaderBg:'#ff6849',
            icon: 'info',
            hideAfter: 3500, 
            stack: 6
          });
        if(project_id == '' || sector_id == '' || block_id == '' || plot_id == ''){
          swal({   
            title: "Please select Project, Sector, Block, Plot first!",   
            text: "Will close in 2 sec",   
            timer: 2000,   
            showConfirmButton: false 
          });
        }else{
          $.ajax({
              url: "<?php echo route('admin.deals.calculate-plan-amount') ?>",
              method: 'POST',
              data: {
                project_id:project_id,
                sector_id:sector_id,
                block_id:block_id,
                plot_id:plot_id,
                payment_plan_type:payment_plan_type,
                payment_plan_id:full_payment_plan_id,
                _token:token
              },
              success: function(data) {
                console.log(data);
                $('div#registryAmount').show();
                $('div.emiFields').remove();
                $("input#booking_amount").val(data.bookingAmount);
                $("input#allotment_amount").val(data.allotmentAmount);
                $("input#registry_amount").val(data.registryAmount);
                $("input#totalAmount").val(data.totalAmount);
                $("input#plc_amount").val(data.plc_amount);
                
                $.toast({
                    heading: 'Amount calculated successfully!',
                    position: 'top-right',
                    loaderBg:'#ff6849',
                    icon: 'success',
                    hideAfter: 3500, 
                    stack: 6
                  });
              }
          });
        }
      });

      $("select[name='emi_duration']").change(function(){
        var project_id = $("input[name='project_id']").val();
        var sector_id = $("input[name='sector_id']").val();
        var block_id = $("input[name='block_id']").val();
        var plot_id = $("input[name='plot_id']").val();
        var emi_duration = $(this).val();
        var payment_plan_type = $("select[name='payment_plan']").val();
        var emi_payment_plan_id = $("select[name='emi_payment_plan_id']").val();
        $.toast({
            heading: 'Please wait!',
            text: 'Please wait till server is  calculating amount.',
            position: 'top-right',
            loaderBg:'#ff6849',
            icon: 'info',
            hideAfter: 3500, 
            stack: 6
          });
        if(emi_payment_plan_id == '' || project_id == '' || sector_id == '' || block_id == '' || plot_id == ''){
          swal({   
            title: "Please select Project, Sector, Block, Plot, EMI Payment Plan first!",   
            text: "Will close in 2 sec",   
            timer: 2000,   
            showConfirmButton: false 
          });
        }else{
          var token = $("input[name='_token']").val();
          $.ajax({
              url: "<?php echo route('admin.deals.calculate-plan-amount') ?>",
              method: 'POST',
              data: {
                project_id:project_id,
                sector_id:sector_id,
                block_id:block_id,
                plot_id:plot_id,
                payment_plan_type:payment_plan_type,
                payment_plan_id:emi_payment_plan_id,
                emi_duration:emi_duration,
                _token:token
              },
              success: function(data) {
                console.log(data);
                $("input#booking_amount").val(data.bookingAmount);
                $("input#allotment_amount").val(data.allotmentAmount);
                $("input#totalAmount").val(data.totalAmount);
                $("input#calc_emi_amount").val(data.emiAmount);
                $("input#plc_amount").val(data.plc_amount);
                
                addEMIFields(data.emiAmount, emi_duration);
                $.toast({
                    heading: 'Amount calculated successfully!',
                    position: 'top-right',
                    loaderBg:'#ff6849',
                    icon: 'success',
                    hideAfter: 3500, 
                    stack: 6
                  });
              }
          });
        }
      });

      function addEMIFields(amount, emi_duration){
        $('div#registryAmount').hide();
        $('div.emiFields').remove();
        var htmlEMI = '';
        htmlEMI = htmlEMI +  '<div class="col-md-3 emiFields">'
                      + '<div class="form-group">'
                      + '    <label class="control-label" for="emi_start_date">EMI Start Date*</label>'
                      + '    <input type="text" id="emi_start_date" name="emi_start_date" required="required" class="form-control mydatepicker" value="">'
                      + '</div>'
                      + '</div>';
        for(i=0; i<emi_duration; i++){
            htmlEMI = htmlEMI +  '<div class="col-md-3 emiFields">'
                          + '<div class="form-group">'
                          + '    <label class="control-label" for="emi['+ i +']">EMI ' + (i+1) + '*</label>'
                          + '    <input type="text" id="emi_'+ i +'" name="emi['+ i +']" required="required" class="form-control" value="' + amount + '">'
                          + '</div>'
                          + '</div>';
        }
        $('div#full_payment').append(htmlEMI);
        jQuery('.mydatepicker, #datepicker').datepicker({format: 'yyyy-mm-dd'});
      }

      // $("select[name='project_id']").change(function(){
      //     var project_id = $(this).val();
      //     var token = $("input[name='_token']").val();
      //     $.ajax({
      //         url: "<?php echo route('admin.select-ajax') ?>",
      //         method: 'POST',
      //         data: {project_id:project_id, _token:token},
      //         success: function(data) {
      //           $("select[name='sector_id'").html('');
      //           $("select[name='sector_id'").html(data.options);
      //         }
      //     });
      // });

      // $("select[name='sector_id']").change(function(){
      //     var sector_id = $(this).val();
      //     var token = $("input[name='_token']").val();
      //     $.ajax({
      //         url: "<?php echo route('admin.select-ajax') ?>",
      //         method: 'POST',
      //         data: {sector_id:sector_id, _token:token},
      //         success: function(data) {
      //           $("select[name='block_id'").html('');
      //           $("select[name='block_id'").html(data.options);
      //         }
      //     });
      // });

      // $("select[name='block_id']").change(function(){
      //     var block_id = $(this).val();
      //     var token = $("input[name='_token']").val();
      //     $.ajax({
      //         url: "<?php echo route('admin.select-ajax') ?>",
      //         method: 'POST',
      //         data: {block_id:block_id, _token:token},
      //         success: function(data) {
      //           $("select[name='plot_id'").html('');
      //           $("select[name='plot_id'").html(data.options);
      //         }
      //     });
      // });

</script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH G:\xampp\htdocs\yespropmart2.loc\resources\views/admin/deals/createPendingplotdeals.blade.php ENDPATH**/ ?>