<?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.store']]); ?>

        <div class="form-body control-group">
            <h3 class="card-title">Business Partner & Customers</h3>
            <div class="row">
                <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($customerSelected!=null): ?><?php echo e($customerSelected->username); ?><?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($customerSelected!=null): ?>
                        <?php echo Form::text('customer_name', $customerSelected->full_name, ['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>
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <?php echo Form::label('sponsor_id', 'Sponsor 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(old('sponsor')); ?>" 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', 'Sponsor name*', ['class' => 'control-label']); ?>

                        <?php echo Form::text('sponsor_name', old('sponsor_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>
            <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>
                      <?php echo Form::select('project_id', $projects, [], ['class' => 'form-control form-control-normal']); ?>

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

                          </p>
                      <?php endif; ?>
                  </div>
              </div>
              <div class="col-md-3">
                  <div class="form-group">
                      <label class="control-label" for="sector_id">Sector*</label>
                      <?php echo Form::select('sector_id', [], [], ['class' => 'form-control form-control-normal']); ?>

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

                          </p>
                      <?php endif; ?>
                  </div>
              </div>
              <div class="col-md-3">
                  <div class="form-group">
                      <label class="control-label" for="block_id">Block*</label>
                      <?php echo Form::select('block_id', [], [], ['class' => 'form-control form-control-normal']); ?>

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

                          </p>
                      <?php endif; ?>
                  </div>
              </div>
              <div class="col-md-3">
                  <div class="form-group">
                      <label class="control-label" for="plot_id">Plot*</label>
                      <?php echo Form::select('plot_id', [], '[]', ['class' => 'form-control form-control-normal']); ?>

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

                          </p>
                      <?php endif; ?>
                  </div>
              </div>
              <!--/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, ['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 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 class="col-md-3" id="developer_charge">
                    <div class="form-group">
                        <label class="control-label" for="developer_charge">Development Charge*</label>
                        <input type="text" id="developer_charge" name="developer_charge" required="required" class="form-control" value="0">
                          <?php if($errors->has('developer_charge')): ?>
                              <p class="help-block">
                                  <strong><?php echo e($errors->first('developer_charge')); ?></strong>
                              </p>
                          <?php endif; ?>
                    </div>
                </div>
            </div>

            <div class="row" id="emi_payment">
            </div>

            <div class="form-group">
              <div class="col-md-6 col-sm-6 col-xs-12">
              <input type="checkbox" name="redeem_coupon" class="" id="redeem_coupon" value="1" />
              <label class="control-label col-md-3 col-sm-3 col-xs-12" for="redeem_coupon">Apply Gift wallet
              </label>
               <?php if($errors->has('redeem_coupon')): ?>
                    <span class="help-block">
                        <strong><?php echo e($errors->first('redeem_coupon')); ?></strong>
                    </span>
                <?php endif; ?>
              </div>
            </div>
            <div class="form-group">
              <div class="col-md-6 col-sm-6 col-xs-12">
              <input type="checkbox" name="discard_plc" class="" id="discard_plc" value="1" />
              <label class="control-label col-md-3 col-sm-3 col-xs-12" for="discard_plc">Discard PLC
              </label>
               <?php if($errors->has('discard_plc')): ?>
                    <span class="help-block">
                        <strong><?php echo e($errors->first('discard_plc')); ?></strong>
                    </span>
                <?php endif; ?>
              </div>
            </div>
            <input type="hidden" name="totalAmount" id="totalAmount" value="0">
            <input type="hidden" name="calc_emi_amount" id="calc_emi_amount" value="0">
    <?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() {
          $( "span#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();
      //   }
      // });
      
      function up_full_payment_plan_id(){
          var project_id = $("select[name='project_id']").val();
          var sector_id = $("select[name='sector_id']").val();
          var block_id = $("select[name='block_id']").val();
          var plot_id = $("select[name='plot_id']").val();

          var full_payment_plan_id = $("select[name='payment_plan']").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 5 sec",   
            timer: 5000,   
            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);
                $("input#developer_charge").val(data.developer_charge);
                
                $.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 = $("select[name='project_id']").val();
        var sector_id = $("select[name='sector_id']").val();
        var block_id = $("select[name='block_id']").val();
        var plot_id = $("select[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='payment_plan']").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);
                $("input#developer_charge").val(data.developer_charge);

                
                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);
              }
          });
      });
      $("select[name='plot_id']").change(function(){
          var project_id = $("select[name='project_id']").val();
          var token = $("input[name='_token']").val();
          $.ajax({
              url: "<?php echo route('admin.select-ajax') ?>",
              method: 'POST',
              data: {plot_id:project_id, _token:token},
              success: function(data) {
                $("select[name='payment_plan'").html('');
                $("select[name='payment_plan'").html(data.options);
              }
          });
      });
      
      $("select[name='payment_plan']").change(function(){
          var payment_plan = $("select[name='payment_plan']").val();
          var token = $("input[name='_token']").val();
          $.ajax({
              url: "<?php echo route('admin.select-ajax') ?>",
              method: 'POST',
              data: {payment_plan:payment_plan, _token:token},
              success: function(data) {
                var payment_type = data.options.type;
                if(payment_type == 0){
                  up_full_payment_plan_id();
                  //$('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();
                }

              }
          });
      });
      
</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/create.blade.php ENDPATH**/ ?>