@extends('layouts.app')

@section('content')
    <div class="card card-outline-info">
        <div class="card-header">
            <h4 class="m-b-0 text-white">Hold Plot</h4>
        </div>
    <div class="card-body">
    {!! Form::open(['method' => 'POST','id'=> 'register_frm', 'data-parsley-validate'=>'', 'route' => ['admin.plot.dohold']]) !!}
        <div class="form-body control-group">
            <h3 class="card-title">Business Partner</h3>
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        {!! 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="{{ old('sponsor') }}" required="" style="margin-bottom: 0px; " />
                        <p class="help-block"></p>
                        @if($errors->has('sponsor_id'))
                            <p class="help-block error">
                                {{ $errors->first('sponsor_id') }}
                            </p>
                        @endif
                    </div>
                </div>
                <!--/span-->
                <div class="col-md-6">
                    <div class="form-group">
                        {!! Form::label('sponsor_name', 'Sponsor name*', ['class' => 'control-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">Select Property</h3>
            <div class="row">
              <div class="col-md-3">
                  <div class="form-group">
                      <label class="control-label" for="project_id">Project*</label>
                      {!! Form::select('project_id', $projects, [], ['class' => 'form-control form-control-normal']) !!}
                      @if($errors->has('project_id'))
                          <p class="help-block error alert">
                            {{ $errors->first('project_id') }}
                          </p>
                      @endif
                  </div>
              </div>
              <div class="col-md-3">
                  <div class="form-group">
                      <label class="control-label" for="sector_id">Sector*</label>
                      {!! Form::select('sector_id', [], [], ['class' => 'form-control form-control-normal']) !!}
                      @if($errors->has('sector_id'))
                          <p class="help-block error alert">
                            {{ $errors->first('sector_id') }}
                          </p>
                      @endif
                  </div>
              </div>
              <div class="col-md-3">
                  <div class="form-group">
                      <label class="control-label" for="block_id">Block*</label>
                      {!! Form::select('block_id', [], [], ['class' => 'form-control form-control-normal']) !!}
                      @if($errors->has('block_id'))
                          <p class="help-block error alert">
                            {{ $errors->first('block_id') }}
                          </p>
                      @endif
                  </div>
              </div>
              <div class="col-md-3">
                  <div class="form-group">
                      <label class="control-label" for="plot_id">Plot*</label>
                      {!! Form::select('plot_id', [], '[]', ['class' => 'form-control form-control-normal']) !!}
                      @if($errors->has('plot_id'))
                          <p class="help-block error alert">
                            {{ $errors->first('plot_id') }}
                          </p>
                      @endif
                  </div>
              </div>
              <!--/span-->


            </div>

    {!! Form::submit('Hold Plot', ['class' => 'btn btn-danger']) !!}
    {!! Form::close() !!}
    </div>
    </div>
<div class="card">
  <div class="card-body">
    <h4 class="card-title">Plots</h4>
      <div class="x_content">
        <table id="datatable-fixed-header" class="table table-striped table-bordered">
          <thead>
            <tr>
              <th>Business Partner ID</th>
              <th>Business Partner Name</th>
              <th>Title</th>
              <th>Project/Sector/Block</th>
              <th>Area</th>
              <th>Dimension</th>
              <th>PLC</th>
              <th>Holded Till</th>
              <th>Status</th>
              <!-- <th>Action</th> -->
            </tr>
          </thead>


          <tbody>
          @foreach ($plots as $plot)
            <tr>
              <td>{{$plot->username}}</td>
              <td>{{ $plot->full_name }}</td>
              <td>{{ $plot->name }}</td>
              <td>{{ $plot->projectName }}/{{ $plot->sectorName }}/{{ $plot->blockName }}</td>
              <td>{{ $plot->area }}</td>
              <td>{{ $plot->dimension }}</td>
              <td>{{ $plot->plcName }}</td>
              <td>{{ $plot->admin_status_date }}</td>
              <td>
                <form method="post" action="/admin/plots/plot_status_update/{{$plot->id}}" class="form-horizontal form-label-left">
                  @csrf
                  <select class="form-control col-md-7 col-xs-12" name="admin_status" onchange="this.form.submit()">
                    <option value="available" @if($plot->admin_status==NULL) selected="selected" @endif>Available</option>
                    <option value="onhold" @if($plot->admin_status=="onhold") selected="selected" @endif>On Hold</option>
                    <option value="registered" @if($plot->admin_status=="registered") selected="selected" @endif>Registered</option>
                  </select>
                </form>
              </td>
<!--               <td>
                <a type="button" class="btn btn-success" href="{{ url('/admin/plots'.'/'.$plot->id) }}">Edit</a>
              </td>
 -->            </tr>
          @endforeach
          </tbody>
        </table>
      </div>
    </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: 'yyyy-mm-dd'});

      $(document).ready(function() {
        jQuery('#datatable-fixed-header').DataTable({
          fixedHeader: true
        });

        $("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('/') }}/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 "{{ 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);
                
                $.toast({
                    heading: 'Amount calculated successfully!',
                    position: 'top-right',
                    loaderBg:'#ff6849',
                    icon: 'success',
                    hideAfter: 3500, 
                    stack: 6
                  });
              }
          });
        }
      }




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

      
</script>
@endsection
