@extends('template.app')

@section('meta')
@endsection

@section('title')
    Dealer Invoice Create
@endsection

@section('styles')
@endsection

@section('content')
<div class="page-breadcrumb">
    <div class="row">
        <div class="col-7 align-self-center">
            <h4 class="page-title text-truncate text-dark font-weight-medium mb-1">Invoice Create</h4>
            <div class="d-flex align-items-center">
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb m-0 p-0">
                        <li class="breadcrumb-item"><a href="{{ route('dashboard') }}" class="text-muted">Dashboard</a></li>
                        <li class="breadcrumb-item"><a href="{{ route('car_varient') }}" class="text-muted">Dealer Invoice Create</a></li>
                    </ol>
                </nav>
            </div>
        </div>
    </div>
</div>
<div class="container-fluid">
    <div class="row">
        @if($data->inventory_id == NULL)
        <div class="col-12">
            <div class="alert alert-danger" role="alert">
              First assign car for this order
            </div>
        </div> 
        @else
        <div class="col-12">
            <div class="card">
                <div class="card-body">
                    <form action="{{ route('order.dealer_invoice_insert') }}" name="form" id="form" method="post" enctype="multipart/form-data">
                        @csrf
                        @method('POST')
                 
                        <input type="hidden" name="id" value="{{ $data->id }}">

                        <div class="row">
                            <div class="col-md-12 text-center">
                                <hr>
                                <h2>Dealer Vehicle Tax Invoice</h2>
                                <hr>
                            </div>
                            
                            <div class="form-group col-md-3">
                                <label class="control-label">From Branch</label>
                                <textarea name="branch_name" class="form-control">{!! $branch_nm->name !!}</textarea>                                
                                <span class="kt-form__help error branch_name"></span>
                            </div>
                            
                            <div class="form-group col-md-3">
                                <label class="control-label">Branch Address</label>
                                <textarea name="branch_address" class="form-control">{!! $branch_nm->address !!}</textarea>                                
                                <span class="kt-form__help error branch_address"></span>
                            </div>
                            
                            <div class="form-group col-sm-3">
                                <label for="name">GST No.</label>
                                <input type="text" class="form-control" disabled="disabled" value="24ABACS4515J1ZH">
                                <input type="hidden" name="dealer_gst" value="24ABACS4515J1ZH">
                            </div>      

                            <div class="form-group col-sm-3">
                                <label for="name">PAN No.</label>
                                <input type="text" class="form-control" disabled="disabled" value="ABACS4515J">
                                <input type="hidden" name="dealer_pan_number" value="ABACS4515J">
                            </div>  

                            <div class="form-group col-md-3">
                                <label class="control-label">Dealer Name</label>
                                <textarea name="bill_to" id="bill_to" class="form-control" placeholder="Enter Bill To">{!! $data->bill_to !!}</textarea>                                
                                <span class="kt-form__help error bill_to"></span>
                            </div>

                            <div class="form-group col-md-3">
                                <label class="control-label">Dealer Address</label>
                                <textarea name="address" id="address" class="form-control" placeholder="Enter Address">{!! $data->address !!}</textarea>                                
                                <span class="kt-form__help error address"></span>
                            </div>
                            
                            <input type="hidden" name="ship_to" value="{!! $data->bill_to !!}">
                            
                            <div class="form-group col-sm-3">
                                <label for="name">Dealer GST</label>
                                <input type="text" class="form-control" disabled="disabled" value="{!! $data->dealer_gst !!}">
                                <input type="hidden" name="dealer_gst" value="{!! $data->dealer_gst !!}">
                            </div>  
                            
                            <div class="form-group col-sm-3">
                                <label for="name">Dealer Phone No</label>
                                <input type="text" class="form-control" disabled="disabled" value="{{ $data->phone_no ?? '' }}">
                                <input type="hidden" name="phone_no" value="{{ isset($data->phone_no) ? $data->phone_no : '' }}">
                            </div>  

                            <div class="form-group col-sm-3">
                                <label for="name">Invoice No</label>
                                <input type="text" class="form-control" name="invoice_no" value="{!! $invoice_no !!}">
                            </div>   

                            <div class="form-group col-sm-3">
                                <label for="name">Invoice Date</label>
                                <input type="date" class="form-control" name="invoice_date" value="{{date('Y-m-d')}}">
                            </div>   
                            
                            <div class="form-group col-sm-3">
                                <label for="name">Place of Supply</label>
                                <select class="form-control" name="place_of_supply">
                                    <option value="">Select Place of Supply</option>
                                    @foreach($state as $key => $val)
                                        <option value="{!! $key !!}">{!! $val !!}</option>
                                    @endforeach
                                </select>
                            </div>   
                            
                            <div class="form-group col-md-3">
                                <label class="control-label">Remark</label>
                                <textarea name="remark" class="form-control">{!! $data->remark ?? '' !!}</textarea>                                
                                <span class="kt-form__help error remark"></span>
                            </div>

                            <div class="col-sm-12">
                                <div class="table-responsive">
                                  <table class="table">
                                    <thead>
                                      <tr>
                                        <th><b>PARTICULARS</b></th>
                                        <th><b>AMOUNT(Rs)</b></th>                                       
                                      </tr>
                                    </thead>
                                    <tbody> 
                                      <tr>
                                        <th>{{ isset($data->product_name) ? $data->product_name : '' }}</th>
                                        <td>{{ isset($data->on_road_price) ? $data->on_road_price : '' }}</td>
                                      </tr>
                                      <tr>
                                        <th>IGST ({{$gst_tax_percentage}}%)</th>
                                        <td>{{round($data->igst)}}</td>
                                      </tr>
                                      <tr>
                                        <th>CGST ({{$gst_tax_percentage/2}}%)</th>
                                        <td>{{round($data->cgst)}}</td>
                                      </tr>
                                      <tr>
                                        <th>SGST ({{$gst_tax_percentage/2}}%)</th>
                                        <td>{{round($data->sgst)}}</td>
                                      </tr>
                                      <tr>
                                        <th>Compensation Cess ({{$cess_tax_percentage}}%)</th>
                                        <td>{{round($data->compensation_cess)}}</td>
                                      </tr>
                                      <tr>
                                          <th>
                                            @if($data->discount_type == 'discount_plus')
                                                Discount ( + )
                                            @elseif($data->discount_type == 'discount_minus')
                                                Discount ( - )
                                            @else 
                                                Discount ( + / - )
                                            @endif
                                            </th>
                                        <td>{{round($data->discount)}}</td>
                                      </tr>
                                      @php
                                        $cgst = $data->cgst ?? 0;
                                        $sgst = $data->sgst ?? 0;
                                        $igst = $data->igst ?? 0;
                                        $purchase_price = $data->on_road_price ?? 0;
                                        $discount = $data->discount ?? 0;
                                        
                                        $final_total = 0;
                                        if($data->discount_type == 'discount_plus'){
                                            $final_total = $purchase_price + $cgst + $sgst + $igst + $discount;
                                        } else if($data->discount_type == 'discount_minus'){
                                            $final_total = $purchase_price - $discount + $cgst + $sgst + $igst;
                                        } else { 
                                            $final_total = $purchase_price + $cgst + $sgst + $igst;
                                        }
                                      @endphp
                                      <tr>
                                        <th><b>TOTAL</b></th>
                                        <td>{{round($final_total)}}</td>
                                      </tr>
                                      <tr>
                                        <th><b>TCS @1%</b></th>
                                       <td>{{$data->tcs_tax}}</td>
                                      </tr>
                                      <tr>
                                        <th><b>GRAND TOTAL</b></th>
                                        <td>{{round($data->grand_total)}}</td>
                                      </tr>
                                    </tbody>
                                  </table>
                                </div>
                            </div>

                            <input type="hidden" name="product_name" value="{{ isset($data->product_name) ? $data->product_name : '' }}">
                            <input type="hidden" name="on_road_price" value="{{ isset($data->on_road_price) ? $data->on_road_price : '' }}">
                            <input type="hidden" name="cgst" value="{{$data->cgst}}">
                            <input type="hidden" name="sgst" value="{{$data->sgst}}">
                            <input type="hidden" name="compensation_cess" value="{{$data->compensation_cess}}">
                            <input type="hidden" name="total" value="{{$data->total}}">
                            <input type="hidden" name="tcs_tax" value="{{$data->tcs_tax}}">
                            <input type="hidden" name="grand_total" value="{{$data->grand_total}}">
                            
                            <input type="hidden" name="vin_number" value="{{ isset($data->vin_number) ? $data->vin_number : '' }}">
                            <input type="hidden" name="chassis_number" value="{{ isset($data->chassis_number) ? $data->chassis_number : '' }}">
                            <input type="hidden" name="engine_number" value="{{ isset($data->engine_number) ? $data->engine_number : '' }}">
                            <input type="hidden" name="key_number" value="{{ isset($data->key_number) ? $data->key_number : '' }}"> 
                            <input type="hidden" name="hsn_code" value="{{ isset($data->hsn_code) ? $data->hsn_code : '' }}"> 

                            <div class="col-sm-12">
                                <h5>Vehicle Particulars</h5>
                                <div class="table-responsive">
                                  <table class="table">
                                    <tbody> 
                                      <tr>
                                        <th><b>Vin No</b></th>
                                        <th><b>Chassis No</b></th>
                                        <th><b>Engine No</b></th>
                                        <th><b>Key No</b></th>
                                        <th><b>HSN No</b></th>
                                      </tr>
                                      <tr>
                                        <td>{{ isset($data->vin_number) ? $data->vin_number : '' }}</td>
                                        <td>{{ isset($data->chassis_number) ? $data->chassis_number : '' }}</td>
                                        <td>{{ isset($data->engine_number) ? $data->engine_number : '' }}</td>
                                        <td>{{ isset($data->key_number) ? $data->key_number : '' }}</td>
                                        <td>{{ isset($data->hsn_code) ? $data->hsn_code : '' }}</td>
                                      </tr>
                                    </tbody>
                                  </table>
                                </div>
                            </div>
                        </div>
                        
                        <div class="form-group">
                            <button type="submit" class="btn waves-effect waves-light btn-rounded btn-outline-primary">Submit</button>
                            <a href="{{ route('order') }}" class="btn waves-effect waves-light btn-rounded btn-outline-secondary">Back</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        @endif
    </div>
</div>
@endsection

@section('scripts')
<script>
    $("#percentage").keyup(function(e){
        $('.percentage').html('');
        if ($(this).val() > 99){
            $('.percentage').html('');
            $('.percentage').html('Enter Value Between 0 to 100!');
            $(this).val();
            $(this).val(100);
        }else if($(this).val() <= 99){
            $('.percentage').html('');
        }
    });

</script>

<script>
    $(document).ready(function() {
        var form = $('#form');
        $('.kt-form__help').html('');
        form.submit(function(e) {
            $('.help-block').html('');
            $('.m-form__help').html('');
            $.ajax({
                url: form.attr('action'),
                type: form.attr('method'),
                data: new FormData($(this)[0]),
                dataType: 'json',
                async: false,
                processData: false,
                contentType: false,
                success: function(json) {
                    return true;
                },
                error: function(json) {
                    if (json.status === 422) {
                        e.preventDefault();
                        var errors_ = json.responseJSON;
                        $('.kt-form__help').html('');
                        $.each(errors_.errors, function(key, value) {
                            $('.' + key).html(value);
                        });
                    }
                }
            });
        });
    });

    $('#bill_to_1').on('click', function(){
        $('#bill_to').val($('#customer_name').val());
        $('#bill_to_2').prop('checked',false);
    });

    $('#bill_to_2').on('click', function(){
        $('#bill_to').val($('#company_name').val());
        $('#bill_to_1').prop('checked',false);
    });

    $('#address_ch_1').on('click', function(){
        $('#address').val($('#address_1').val());
        $('#address_ch_2').prop('checked',false);
    });

    $('#address_ch_2').on('click', function(){
        $('#address').val($('#address_2').val());
        $('#address_ch_1').prop('checked',false);
    });

    $('#address_ch_1_s').on('click', function(){
        $('#ship_to').val($('#address_1').val());
        $('#address_ch_2_s').prop('checked',false);
    });

    $('#address_ch_2_s').on('click', function(){
        $('#ship_to').val($('#address_2').val());
        $('#address_ch_1_s').prop('checked',false);
    });
</script>
@endsection