<?php 
   $extra_sum = 0;
   if(!empty($data->priclist_detail)){
       $price_new_arr = json_decode($data->priclist_detail);
       if($price_new_arr != NULL){
           $price_new_arr = array_column($price_new_arr, 'value_field');
           $extra_sum = array_sum($price_new_arr);    
       }
   }

   $is_finance_display_filds = 1;
   if($user_role == 'KEC' ||  $user_role == 'Team leader'){
       $is_finance_display_filds = 0;    
   }

?>

@extends('template.app') 

@section('meta')
@endsection

@section('title')
    Make Deal
@endsection

@section('styles')
    <link href="{{ asset('assets/css/dropify.min.css') }}" rel="stylesheet">
    <link href="{{ asset('assets/css/sweetalert2.bundle.css') }}" rel="stylesheet">
    <link href="{{ asset('assets/css/select2.css') }}" rel="stylesheet">

    <style type="text/css">
        body {
            font-family: Arial, sans-serif;
        }
        table {
            margin-left: 45px;
            width: 100%;
            border-collapse: collapse;
            background-color: white;
            color: black;
        }
        th, td {
            border: 1px solid black;
            padding: 8px;
            text-align: left;
            line-height: 1.8;
        }
        th {
            background-color: #f2f2f2;
        }
        .right-align {
            text-align: right;
        }
        .final-deal {
            background-color: lightgray;
            font-weight: bold;
        }
        .light_gray{
            background-color: lightgray;
        }
        .text-center {
            text-align: center !important;
        }

        @media print {
            body {
                -webkit-print-color-adjust: exact;
                print-color-adjust: exact;
            }
            th, td {
                background-color: inherit !important;
            }
            .final-deal, .light_gray {
                background-color: lightgray !important;
                -webkit-print-color-adjust: exact;
                print-color-adjust: exact;
            }
            .card-body {
                margin-left: -75px;
            }
            .no-print {
                display: none !important;
            }
        }

        input[type="number"]::-webkit-inner-spin-button,
        input[type="number"]::-webkit-outer-spin-button {
            -webkit-appearance: none;
            margin: 0;
        }

        input[type="number"] {
            -moz-appearance: textfield; /* For Firefox */
        }

        .form-group {
            margin-bottom: 0rem!important;
        }

    </style>
@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">Make Deal</h4>
                <div class="d-flex align-items-center">
                    <nav aria-label="breadcrumb" class="d-flex justify-content-between align-items-center">
                        <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('obf') }}" class="text-muted">OBF</a></li>
                            <li class="breadcrumb-item text-muted active" aria-current="page">Reverse Calculation</li>
                        </ol>
                    </nav>
                </div>
            </div>

            <div class="col-5 align-self-center">
                <div class="customize-input float-right">
                    <button onclick="printDiv('content-to-print')" style="background-color: green; color: white; margin-left: 30px; border: none; cursor: pointer; border-radius: 5px;">
                        <i class="fa fa-print"></i> Print Content
                    </button>
                </div>
            </div>
        </div>    
    
        <div class="container-fluid" id="content-to-print">
            <div class="row">
                <div class="col-12">
                    <div class="card">
                        <div class="card-body">
                            <div class="col-md-offset-3">
                                <form role="form" enctype="multipart/form-data" id="make-deal">
                                    <input type="hidden" name="id" id="hidden_obf_id" value="{{ $data->id }}">
                                    <input type="hidden" name="customer_type_new" id="hidden_customer_type_new" value="{{ $data->customer_type_new }}">
                                    <input type="hidden" name="rto_tax_indevidual" id="hidden_rto_tax_indevidual" value="{{ $rto_data->rto_tax_individual }}">
                                    <input type="hidden" name="rto_tax_company" id="hidden_rto_tax_company" value="{{ $rto_data->rto_tax_company }}">
                                    <input type="hidden" name="rto_amount" id="hidden_rto_amount" value="{{ $rto_data->totalcharges }}">
                                    <input type="hidden" name="start_value_1" id="hidden_start_value_1" value="{{ $branch->start_value1 }}">
                                    <input type="hidden" name="end_value_1" id="hidden_end_value_1" value="{{ $branch->end_value1 }}">
                                    <input type="hidden" name="tax_percentage_1" id="hidden_tax_percentage_1" value="{{ $branch->tax_percentage1 }}">
                                    <input type="hidden" name="start_value_2" id="hidden_start_value_2" value="{{ $branch->start_value2 }}">
                                    <input type="hidden" name="end_value_2" id="hidden_end_value_2" value="{{ $branch->end_value2 }}">
                                    <input type="hidden" name="tax_percentage_2" id="hidden_tax_percentage_2" value="{{ $branch->tax_percentage2 }}">
                                    <input type="hidden" name="municipal_tax_applicable" id="hidden_municipal_tax_applicable" value="{{$data->municipal_tax_applicable }}">
                                    <input type="hidden" name="basic_purchase_price" id="hidden_basic_purchase_price" value="{{$data->purchase_price }}">
                                    <input type="hidden" name="registration_tax_id" id="hidden_registration_tax_id" value="{{$data->registration_tax_id }}">
                                    <input type="hidden" name="insurance_id" id="hidden_insurance_id" value="{{$data->insurance_id }}">
                                    <input type="hidden" name="rto_custome_handling_charges" id="hidden_rto_custome_handling_charges" value="{{$data->rto_custome_handling_charges }}">
                                    
                                    <div class="row">
                                        <!-- <div class="col-lg-12 "> -->
                                            <div class="col-md-12">
                                                <table>
                                                    <tr>
                                                        <td colspan="6" class="text-center"><b>Reverse Calculation</b></td>
                                                    </tr>
                                                    <tr>
                                                        <td><b>Booking Date</b></td>
                                                        <td colspan="2">{!! \Carbon\Carbon::parse($data->booking_date)->format('d-m-Y') !!}</td>
                                                        <td><b>KEC</b></td>
                                                        <td colspan="2">{!! $data->sales_person_name !!}</td>
                                                    </tr>
                                                    <tr>
                                                        <td><b>Customer Name</b></td>
                                                        <td colspan="2">{!! $data->customer_name !!}</td>
                                                        <td><b>TL</b></td>
                                                        <td colspan="2">{!! strtoupper($data->team_name) !!}</td>
                                                    </tr>
                                                    <tr>
                                                        <td><b>Location (Branch)</b></td>
                                                        <td colspan="5">{!! strtoupper($data->branch_name) !!}</td>
                                                    </tr>
                                                    <tr>
                                                        <td><b>Vin No.</b></td>
                                                        <td colspan="5">{!! $data->vin_number !!}</td>
                                                    </tr>
                                                    <tr class="light_gray">
                                                        <td class="text-center"><b>{!! $data->car_model_name !!}</b></td>
                                                        <td class="text-center"><b>Purchase Price</b></td>
                                                        <td class="text-center"><b>Aging Days</b></td>
                                                        <td class="text-center"><b>Remark</b></td>
                                                        <td class="text-center"><b>Ex Showroom Price</b></td>
                                                        <td class="text-center"><b>Deal</b></td>
                                                    </tr>
                                                    <tr>
                                                        <td><b>{!! $data->car_varient_name !!}</b></td>
                                                        <td class="right-align purchase_price">{!! $data->purchase_price !!}</td>
                                                        <td class="right-align">{!! $data->aging_days !!}</td>
                                                        <td class="right-align">{!! $data->remark !!}</td>
                                                        <td class="right-align ex_showroom_price">{!! $price_list_name->basic !!}</td>
                                                        <td class="right-align">
                                                            <div class="form-group">
                                                                <input type="number" class="form-control text-right ex_showroom_price_deal" name="ex_showroom_price_deal" id="ex_showroom_price_deal" placeholder="Enter Ex Showroom Amount" value="{{ $data->deal_amt ?? '' }}">
                                                            </div>
                                                            @if($data->deal_amt < $data->purchase_price)
                                                                <span class="no-print" id="error-message" style="color: red; font-size: 12px;">Your Deal Price is Less Than Purchase Price</span>
                                                            @endif
                                                        </td>
                                                        
                                                    </tr>
                                                    <tr>
                                                        <input type="hidden" name="gst_tax" id="hidden_gst_tax" value="{{ intval($price_list_name->gst_percentage) }}">
                                                        <td><b>GST @ {!! $price_list_name->gst_percentage !!}%</b></td>
                                                        <td class="right-align">
                                                            <div class="form-group">
                                                                <input type="number" class="form-control text-right purchase_gst_tax_deal" name="purchase_gst_tax_deal" id="purchase_gst_tax_deal" placeholder="Enter Purchase GST Tax Amount" value="{{ $data->purchase_gst_tax_deal ?? '' }}">
                                                            </div>
                                                        </td>
                                                        <td class="right-align gst_price" colspan="3">{!! $price_list_name->gst_amount !!}</td>
                                                        <td class="right-align">
                                                            <div class="form-group">
                                                                <input type="number" class="form-control text-right gst_tax_deal" name="gst_tax_deal" id="gst_tax_deal" placeholder="Enter GST Tax Amount" value="{{ $data->gst_tax_deal ?? '' }}">
                                                            </div>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <input type="hidden" name="cess_tax" id="hidden_cess_tax" value="{{ rtrim($price_list_name->cess_percentage, '.0') }}">
                                                        <td><b>Cess @ {!! $price_list_name->cess_percentage !!}%</b></td>
                                                        <td class="right-align cess_price" colspan="4">{!! $price_list_name->cess_amount !!}</td>
                                                        <td class="right-align">
                                                            <div class="form-group">
                                                                <input type="number" class="form-control text-right cess_tax_deal" name="cess_tax_deal" id="cess_tax_deal" placeholder="Enter Cess Tax Amount" value="{{ $data->cess_tax_deal ?? '' }}">
                                                            </div>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <input type="hidden" name="tcs_tax_amt" id="hidden_tcs_tax_amt" value="{{ $data->tcs_tax_id }}">
                                                        <input type="hidden" name="tcs_tax" id="hidden_tcs_tax" value="1">
                                                        <td><b>TCS @ 1%</b></td>
                                                        <td class="right-align tcs_price" colspan="4">{!! $data->tcs_tax_id !!}</td>
                                                        <td class="right-align">
                                                            <div class="form-group">
                                                                <input type="number" class="form-control text-right tcs_tax_deal" name="tcs_tax_deal" id="tcs_tax_deal" placeholder="Enter TCS Tax Amount" value="{{ $data->tcs_tax_deal ?? '' }}">
                                                            </div>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td><b>Municipal Tax</b> ({{ ucwords(str_replace('_', '-', $data->municipal_tax_type)) }})</td>
                                                        <td class="right-align municipal_price" colspan="4">{!! $data->municipal_tax_id !!}</td>
                                                        <td class="right-align">
                                                            <div class="form-group">
                                                                <input type="number" class="form-control text-right municipal_tax_deal" name="municipal_tax_deal" id="municipal_tax_deal" placeholder="Enter Municipal Tax Amount" value="{{ $data->municipal_tax_deal ?? '' }}">
                                                            </div>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td><b>Municipal Custom Handling Charges</b></td>
                                                        <td class="right-align municipal_custom_handling_charges" colspan="4">{!! $data->municipal_custom_handling_charges !!}</td>
                                                        <td class="right-align">
                                                            <div class="form-group">
                                                                <input type="number" class="form-control text-right municipal_custom_handling_charges_deal" name="municipal_custom_handling_charges_deal" id="municipal_custom_handling_charges_deal" placeholder="Enter Municipal Custom Handling Charges" value="{{ $data->municipal_custom_handling_charges_deal ?? '' }}">
                                                            </div>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td><b>RTO</b></td>
                                                        <td class="right-align rto_price" colspan="4">{!! $data->registration_tax_id !!}</td>
                                                        <td class="right-align">
                                                            <div class="form-group">
                                                                <input type="number" class="form-control text-right rto_deal" name="rto_deal" id="rto_deal" placeholder="Enter RTO Amount"  value="{{ $data->rto_deal ?? '' }}">
                                                            </div>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td><b>RTO Custom Handling Charges</b></td>
                                                        <td class="right-align rto_custom_handling_charges_price" colspan="4">{!! $data->rto_custom_handling_charges ?? 0 !!}</td>
                                                        <td class="right-align">
                                                            <div class="form-group">
                                                                <input type="number" class="form-control text-right rto_custom_handling_charges_deal" name="rto_custom_handling_charges_deal" id="rto_custom_handling_charges_deal" placeholder="Enter Insurance Amount"  value="{{ $data->rto_cutom_handling_charges_deal ?? '' }}">
                                                            </div>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td><b>INSURANCE</b></td>
                                                        <td class="right-align insurance_price" colspan="4">{!! $data->insurance_id ?? 0 !!}</td>
                                                        <td class="right-align">
                                                            <div class="form-group">
                                                                <input type="number" class="form-control text-right insurance_deal" name="insurance_deal" id="insurance_deal" placeholder="Enter Insurance Amount"  value="{{ $data->insurance_deal ?? '' }}">
                                                            </div>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td><b>Fastag</b></td>
                                                        <td class="right-align fastage_price" colspan="4">{!! $data->fasttag_amount_new ?? 0 !!}</td>
                                                        <td class="right-align fastage_amt_deal">{!! $data->fasttag_amount_new ?? 0 !!}</td>
                                                    </tr>
                                                    <tr>
                                                        <td><b>EW</b></td>
                                                        <td class="right-align extended_warranty" colspan="4">{!! $data->extand_warranties_amount ?? 0!!}</td>
                                                        <td class="right-align ew_deal">{!! $data->extand_warranties_amount ?? 0 !!}</td>
                                                    </tr>
                                                    <tr>
                                                        <td><b>Accessories</b></td>
                                                        <td class="right-align accessories_price" colspan="4">{!! $accessory_sum ?? 0 !!}</td>
                                                        <td class="right-align accessories_deal">{!! $accessory_sum ?? 0 !!}</td>
                                                    </tr>
                                                    <tr>
                                                        <td><b>Other Accessories</b></td>
                                                        <td class="right-align other_price" colspan="4">{!! $data->other_accesssories_prise ?? 0 !!}</td>
                                                        <td class="right-align other_deal">{!! $data->other_accesssories_prise ?? 0 !!}</td>
                                                    </tr>
                                                    <tr>
                                                        @if(isset($data->rsa_type) && !empty($data->rsa_type))
                                                            <td><b>RSA</b></td>
                                                            <td class="right-align rsa_price" colspan="4">{!! $data->rsa_amount ?? 0 !!}</td>
                                                            <td class="right-align rsa_deal">{!! $data->rsa_amount ?? 0 !!}</td> 
                                                        @elseif(isset($data->ccs_type) && !empty($data->ccs_type))
                                                            <td><b>CCS</b></td>
                                                            <td class="right-align rsa_price" colspan="4">{!! $data->ccs_amount ?? 0 !!}</td>
                                                            <td class="right-align rsa_deal">{!! $data->ccs_amount ?? 0 !!}</td> 
                                                        @endif
                                                    </tr>
                                                    <tr>
                                                        <td><b>MCP</b></td>
                                                        <td class="right-align mcp_price" colspan="4">{!! $data->mcp_amount ?? 0 !!}</td>
                                                        <td class="right-align mcp_deal">{!! $data->mcp_amount ?? 0 !!}</td>
                                                    </tr>
                                                    <tr>
                                                        <td colspan="6"> </td>
                                                    </tr>
                                                    <tr class="light_gray">
                                                        <td><b>Total On Road Price</b></td>
                                                        <td class="right-align on_road_price" colspan="4"></td>
                                                        <td class="right-align on_road_price_deal"></td>
                                                    </tr>
                                                    <tr class="final-deal">
                                                        <td><b>Accepted Deal</b></td>
                                                        <td class="right-align" colspan="4">
                                                            <div class="form-group">
                                                                <input type="number" class="form-control text-right" name="final_deal_amt" id="final_deal_amt" placeholder="Enter Final Deal Amount" value="{{ $data->final_deal_amt ?? $data->final_on_road_price_after_way_off }}">
                                                            </div>
                                                        </td>
                                                        <td class="right-align final_deal_deal">{!! $data->final_deal_amt ?? '' !!}</td>
                                                    </tr>
                                                    <tr>
                                                        <td><b>Difference</b></td>
                                                        <td class="right-align diff_amount" colspan="4"></td>
                                                        <td class="right-align diff_amount_deal"></td>
                                                    </tr>
                                                </table>
                                                
                                                <table class="mt-7">
                                                    <tr>
                                                        <td><b>Remark</b></td>
                                                        <td class="right-align" colspan="5">
                                                            <div class="form-group">
                                                                <input type="text" class="form-control text-center" name="remark" id="remark" placeholder="Enter Remark" value="{{ $data->remark ?? '' }}">
                                                            </div>
                                                        </td>
                                                    </tr>
                                                </table>
                                            </div> 
                                            <div class="col-md-12 text-center mt-5 no-print">
                                                <button class="btn btn-outline-primary nextBtn btn-lg pull-right deal_create" type="button">submit</button>
                                            </div>
                                        <!-- </div> -->
                                    </div>           
                                </form>
                            </div>    
                        </div>
                    </div>
                </div>
            </div>   
        </div>
    </div>   
 
@endsection    

@section('scripts')
    <script>
        
        jQuery(document).ready(function(){
            //For GST Calculation For Purchase Price
            var purchase_price = parseFloat(jQuery('.purchase_price').text().replace(/,/g, '')) || 0;
            var hidden_gst_tax = parseFloat(jQuery('#hidden_gst_tax').val()) || 0;
            var purchase_gst_calculation = purchase_price * hidden_gst_tax / 100;
            purchase_gst_calculation = Math.round(purchase_gst_calculation);
            jQuery('#purchase_gst_tax_deal').val(purchase_gst_calculation);

            //For Get Total On Road Price With Extended Warranty
            var ex_showroom_price = parseFloat(jQuery('.ex_showroom_price').text().replace(/,/g, '')) || 0;
            var gst_price = parseFloat(jQuery('.gst_price').text().replace(/,/g, '')) || 0;
            var cess_price = parseFloat(jQuery('.cess_price').text().replace(/,/g, '')) || 0;
            var tcs_price = parseFloat(jQuery('.tcs_price').text().replace(/,/g, '')) || 0;
            var municipal_price = parseFloat(jQuery('.municipal_price').text().replace(/,/g, '')) || 0;
            var municipal_custom_handling_charges = parseFloat(jQuery('.municipal_custom_handling_charges').text().replace(/,/g, '')) || 0;
            var rto_price = parseFloat(jQuery('.rto_price').text().replace(/,/g, '')) || 0;
            var rto_custom_handling_charges_price = parseFloat(jQuery('.rto_custom_handling_charges_price').text().replace(/,/g, '')) || 0;
            var insurance_price = parseFloat(jQuery('.insurance_price').text().replace(/,/g, '')) || 0;
            var fastage_price = parseFloat(jQuery('.fastage_price').text().replace(/,/g, '')) || 0;
            var extended_warranty = parseFloat(jQuery('.extended_warranty').text().replace(/,/g, '')) || 0;
            var accessories_price = parseFloat(jQuery('.accessories_price').text().replace(/,/g, '')) || 0;
            var rsa_price = parseFloat(jQuery('.rsa_price').text().replace(/,/g, '')) || 0;
            var mcp_price = parseFloat(jQuery('.mcp_price').text().replace(/,/g, '')) || 0;
            var other_price = parseFloat(jQuery('.other_price').text().replace(/,/g, '')) || 0;

            var final_on_road_price = ex_showroom_price + gst_price + cess_price + tcs_price + municipal_price + municipal_custom_handling_charges + rto_price + rto_custom_handling_charges_price + insurance_price + fastage_price + extended_warranty + accessories_price + rsa_price + mcp_price + other_price;
            jQuery('.on_road_price').text(final_on_road_price.toLocaleString());


            //For Get Edit Time Difference Show
            var on_road_price = parseFloat(jQuery('.on_road_price').text().replace(/,/g, '')) || 0;
            var final_deal_amt = parseFloat(jQuery('#final_deal_amt').val()) || 0;
            var difference = on_road_price - final_deal_amt;
            jQuery('.diff_amount').text(difference.toLocaleString());

            jQuery('.final_deal_deal').text(final_deal_amt);

            var on_road_price_deal = parseFloat(jQuery('.on_road_price_deal').text().replace(/,/g, '')) || 0;
            var final_deal_deal = parseFloat(jQuery('.final_deal_deal').text().replace(/,/g, '')) || 0;
            var deal_difference = on_road_price_deal - final_deal_deal;
            jQuery('.diff_amount_deal').text(deal_difference.toLocaleString());
        });    

        // For Showing Difference on Data Input
        jQuery('#final_deal_amt').on('input', function(){
            var on_road_price = parseFloat(jQuery('.on_road_price').text().replace(/,/g, '')) || 0;
            var final_deal_amt = parseFloat(jQuery('#final_deal_amt').val()) || 0;
            var difference = on_road_price - final_deal_amt;
            jQuery('.diff_amount').text(difference.toLocaleString());

            jQuery('.final_deal_deal').text(final_deal_amt);

            var on_road_price_deal = parseFloat(jQuery('.on_road_price_deal').text().replace(/,/g, '')) || 0;
            var final_deal_deal = parseFloat(jQuery('.final_deal_deal').text().replace(/,/g, '')) || 0;
            var deal_difference = on_road_price_deal - final_deal_deal;
            jQuery('.diff_amount_deal').text(deal_difference.toLocaleString());
        });


        // ******   For Deal Side Calculation Start   ****** //


        //For Deal Side Get Total Amount && Show Difference
        var ex_showroom_price_deal = parseFloat(jQuery('.ex_showroom_price_deal').val()) || 0;
        var gst_tax_deal = parseFloat(jQuery('.gst_tax_deal').val()) || 0;
        var cess_tax_deal = parseFloat(jQuery('.cess_tax_deal').val()) || 0;
        var tcs_tax_deal = parseFloat(jQuery('.tcs_tax_deal').val()) || 0;
        var municipal_tax_deal = parseFloat(jQuery('.municipal_tax_deal').val()) || 0;
        var municipal_custom_handling_charges_deal = parseFloat(jQuery('.municipal_custom_handling_charges_deal').val()) || 0;
        var rto_deal = parseFloat(jQuery('.rto_deal').val()) || 0;
        var rto_custom_handling_charges_deal = parseFloat(jQuery('.rto_custom_handling_charges_deal').val()) || 0;
        var insurance_deal = parseFloat(jQuery('.insurance_deal').val()) || 0;
        var fastage_amt_deal = parseFloat(jQuery('.fastage_amt_deal').text().replace(/,/g, '')) || 0;
        var ew_deal = parseFloat(jQuery('.ew_deal').text().replace(/,/g, '')) || 0;
        var accessories_deal = parseFloat(jQuery('.accessories_deal').text().replace(/,/g, '')) || 0;
        var rsa_deal = parseFloat(jQuery('.rsa_deal').text().replace(/,/g, '')) || 0;
        var mcp_deal = parseFloat(jQuery('.mcp_deal').text().replace(/,/g, '')) || 0;
        var other_deal = parseFloat(jQuery('.other_deal').text().replace(/,/g, '')) || 0;

        var on_road_price_before = ex_showroom_price_deal + gst_tax_deal + cess_tax_deal + tcs_tax_deal + municipal_tax_deal + municipal_custom_handling_charges_deal + rto_deal + rto_custom_handling_charges_deal + insurance_deal + fastage_amt_deal + ew_deal + accessories_deal + rsa_deal + mcp_deal + other_deal;
        jQuery('.on_road_price_deal').text(on_road_price_before.toLocaleString());

        //For Get Data of Insurance in TextBox
        var insurance_deal_value = jQuery('#insurance_deal').val().trim();
        if (insurance_deal_value === '') {
            var insurance_id = parseFloat(jQuery('#hidden_insurance_id').val()) || 0;
            jQuery('#insurance_deal').val(insurance_id);
        }

        // For Get Calculation Based on Deal Side Ex Showrrom Amount Input
        jQuery('.ex_showroom_price_deal').on('input', function(){
            var ex_showroom_price_deal = parseFloat(jQuery('.ex_showroom_price_deal').val()) || 0;
            var purchase_price = parseFloat(jQuery('.purchase_price').text().replace(/,/g, '')) || 0;
            var purchase_gst_tax_deal = parseFloat(jQuery('.purchase_gst_tax_deal').val()) || 0;
            
            
            if(ex_showroom_price_deal < purchase_price){
                var with_gst_ex_showroom_price = purchase_price + purchase_gst_tax_deal;
                if (jQuery('#error-message').length === 0) {
                    jQuery(this).after('<span id="error-message" style="color: red; font-size: 12px;">Your Deal Price is Less Than Purchase Price</span>');
                }
            } else {
                var with_gst_ex_showroom_price = ex_showroom_price_deal + gst_amount + cess_tax_deal;
                jQuery('#error-message').remove();
            }
            var ex_showroom_price_deal = parseFloat(jQuery('.ex_showroom_price_deal').val()) || 0;
            var purchase_price = parseFloat(jQuery('.purchase_price').text().replace(/,/g, '')) || 0;
            var purchase_gst_tax_deal = parseFloat(jQuery('.purchase_gst_tax_deal').val()) || 0;

            // For GST Calculation
            var hidden_gst_tax = parseFloat(jQuery('#hidden_gst_tax').val()) || 0;
            var gst_calculation = ex_showroom_price_deal * hidden_gst_tax / 100;
            gst_calculation = Math.round(gst_calculation);
            jQuery('#gst_tax_deal').val(gst_calculation);

            // For Cess Calculation
            var hidden_cess_tax = parseFloat(jQuery('#hidden_cess_tax').val()) || 0;
            var cess_calculation = ex_showroom_price_deal * hidden_cess_tax / 100;
            cess_calculation = Math.round(cess_calculation);
            jQuery('#cess_tax_deal').val(cess_calculation);

            // For TCS Calculation
            var hidden_tcs_tax = parseFloat(jQuery('#hidden_tcs_tax').val()) || 0;
            var hidden_tcs_tax_amt = parseFloat(jQuery('#hidden_tcs_tax_amt').val()) || 0;
            var ex_showroom_deal_amount_total = ex_showroom_price_deal + gst_calculation + cess_calculation;
            tcs_calculation = 0;
            if (hidden_tcs_tax_amt > 0) {
                if (ex_showroom_deal_amount_total > 1000000) {
                    var tcs_calculation = ex_showroom_deal_amount_total * hidden_tcs_tax / 100;
                }
            }    
            tcs_calculation = Math.round(tcs_calculation);
            jQuery('#tcs_tax_deal').val(tcs_calculation);

            // For RTO Calculation
            var hidden_customer_type_new = parseFloat(jQuery('#hidden_customer_type_new').val()) || 0;
            var rto_tax_indevidual = parseFloat(jQuery('#hidden_rto_tax_indevidual').val()) || 0;
            var rto_tax_company = parseFloat(jQuery('#hidden_rto_tax_company').val()) || 0;
            var rto_amount = parseFloat(jQuery('#hidden_rto_amount').val()) || 0;
            var hidden_registration_tax_id = parseFloat(jQuery('#hidden_registration_tax_id').val()) || 0;
            var hidden_rto_custome_handling_charges = parseFloat(jQuery('#hidden_rto_custome_handling_charges').val()) || 0;
            
            if (hidden_registration_tax_id > 0){
                if(hidden_customer_type_new == 1 || customer_type_new == 2){ // For Individual Customer Type
                    registration_tax = ((ex_showroom_price_deal*rto_tax_indevidual)/100)+parseInt(rto_amount);
            
                    if(isNaN(registration_tax)) {
                        registration_tax = 0;
                    }

                    if(ex_showroom_price_deal == 0 || ex_showroom_price_deal == '') {
                        registration_tax = 0;
                    }

                    $('#rto_deal').val(Math.round(registration_tax));

                } else { // For Corporate Customer Type
                    registration_tax = ((ex_showroom_price_deal)/100*rto_tax_company)+parseInt(rto_amount);
                            
                    if(isNaN(registration_tax)) {
                        registration_tax = 0;
                    }

                    if(ex_showroom_price_deal == 0 || ex_showroom_price_deal == '') {
                        registration_tax = 0;
                    }

                    $('#rto_deal').val(Math.round(registration_tax));
                }
            }    

            // For Municipal Tax Calculation
            var hidden_municipal_tax_applicable = jQuery('#hidden_municipal_tax_applicable').val();
            if(hidden_municipal_tax_applicable == 1) {
                var start_value1 = parseFloat(jQuery('#hidden_start_value_1').val()) || 0;
                var end_value1 = parseFloat(jQuery('#hidden_end_value_1').val()) || 0;
                var tax_percentage1 = parseFloat(jQuery('#hidden_tax_percentage_1').val()) || 0;
                var start_value2 = parseFloat(jQuery('#hidden_start_value_2').val()) || 0;
                var end_value2 = parseFloat(jQuery('#hidden_end_value_2').val()) || 0;
                var tax_percentage2 = parseFloat(jQuery('#hidden_tax_percentage_2').val()) || 0;
                var gst_amount = parseFloat(jQuery('#gst_tax_deal').val()) || 0;
                var cess_tax_deal = parseFloat(jQuery('#cess_tax_deal').val()) || 0;

                var branch_percentage = 0;
                if(ex_showroom_price_deal < purchase_price){
                    var with_gst_ex_showroom_price = purchase_price + purchase_gst_tax_deal;
                    if (jQuery('#error-message').length === 0) {
                        jQuery(this).after('<span id="error-message" style="color: red; font-size: 12px;">Your Deal Price is Less Than Purchase Price</span>');
                    }
                } else {
                    var with_gst_ex_showroom_price = ex_showroom_price_deal + gst_amount + cess_tax_deal;
                    jQuery('#error-message').remove();
                }
                
                if (with_gst_ex_showroom_price >= start_value1 && with_gst_ex_showroom_price <= end_value1) {
                    branch_percentage = tax_percentage1;
                } else if (with_gst_ex_showroom_price >= start_value2 && with_gst_ex_showroom_price <= end_value2) {
                    branch_percentage = tax_percentage2;
                }

                var municipal_tax = ((ex_showroom_price_deal + gst_amount) * branch_percentage) / 100;

                if (isNaN(municipal_tax) || ex_showroom_price_deal == 0) {
                    municipal_tax = 0;
                }

                jQuery('#municipal_tax_deal').val(Math.round(municipal_tax));
            } else {
                jQuery('#municipal_tax_deal').val(0);
            }
            

            // For Getting Total Of Deal Side All Calculation
            var ex_showroom_price_deal = parseFloat(jQuery('.ex_showroom_price_deal').val()) || 0;
            var gst_tax_deal = parseFloat(jQuery('.gst_tax_deal').val()) || 0;
            var cess_tax_deal = parseFloat(jQuery('.cess_tax_deal').val()) || 0;
            var tcs_tax_deal = parseFloat(jQuery('.tcs_tax_deal').val()) || 0;
            var municipal_tax_deal = parseFloat(jQuery('.municipal_tax_deal').val()) || 0;
            var municipal_custom_handling_charges_deal = parseFloat(jQuery('.municipal_custom_handling_charges_deal').val()) || 0;
            var rto_deal = parseFloat(jQuery('.rto_deal').val()) || 0;
            var rto_custom_handling_charges_deal = parseFloat(jQuery('.rto_custom_handling_charges_deal').val()) || 0;
            var insurance_deal = parseFloat(jQuery('.insurance_deal').val()) || 0;
            var fastage_amt_deal = parseFloat(jQuery('.fastage_amt_deal').text().replace(/,/g, '')) || 0;
            var ew_deal = parseFloat(jQuery('.ew_deal').text().replace(/,/g, '')) || 0;
            var accessories_deal = parseFloat(jQuery('.accessories_deal').text().replace(/,/g, '')) || 0;
            var rsa_deal = parseFloat(jQuery('.rsa_deal').text().replace(/,/g, '')) || 0;
            var mcp_deal = parseFloat(jQuery('.mcp_deal').text().replace(/,/g, '')) || 0;
            var other_deal = parseFloat(jQuery('.other_deal').text().replace(/,/g, '')) || 0;

            var on_road_price = ex_showroom_price_deal + gst_tax_deal + cess_tax_deal + tcs_tax_deal + municipal_tax_deal + municipal_custom_handling_charges_deal + rto_deal + rto_custom_handling_charges_deal + insurance_deal + fastage_amt_deal + ew_deal + accessories_deal + rsa_deal + mcp_deal + other_deal;
            jQuery('.on_road_price_deal').text(on_road_price.toLocaleString());

            //For Deal Side Showing Difference
            var on_road_price_deal = parseFloat(jQuery('.on_road_price_deal').text().replace(/,/g, '')) || 0;
            var final_deal_deal = parseFloat(jQuery('.final_deal_deal').text().replace(/,/g, '')) || 0;
            var deal_difference = on_road_price_deal - final_deal_deal;
            jQuery('.diff_amount_deal').text(deal_difference.toLocaleString());
        });

        //For Deal Side Input Change Time Show Difference
        jQuery('.ex_showroom_price_deal, #gst_tax_deal, #cess_tax_deal, #tcs_tax_deal, #municipal_tax_deal, #municipal_custom_handling_charges_deal, #rto_deal,#rto_custom_handling_charges_deal, #insurance_deal').on('input', function(){
            var ex_showroom_price_deal = parseFloat(jQuery('.ex_showroom_price_deal').val()) || 0;
            var gst_tax_deal = parseFloat(jQuery('.gst_tax_deal').val()) || 0;
            var cess_tax_deal = parseFloat(jQuery('.cess_tax_deal').val()) || 0;
            var tcs_tax_deal = parseFloat(jQuery('.tcs_tax_deal').val()) || 0;
            var municipal_tax_deal = parseFloat(jQuery('.municipal_tax_deal').val()) || 0;
            var municipal_custom_handling_charges_deal = parseFloat(jQuery('.municipal_custom_handling_charges_deal').val()) || 0;
            var rto_deal = parseFloat(jQuery('.rto_deal').val()) || 0;
            var rto_custom_handling_charges_deal = parseFloat(jQuery('.rto_custom_handling_charges_deal').val()) || 0;
            var insurance_deal = parseFloat(jQuery('.insurance_deal').val()) || 0;
            var fastage_amt_deal = parseFloat(jQuery('.fastage_amt_deal').text().replace(/,/g, '')) || 0;
            var ew_deal = parseFloat(jQuery('.ew_deal').text().replace(/,/g, '')) || 0;
            var accessories_deal = parseFloat(jQuery('.accessories_deal').text().replace(/,/g, '')) || 0;
            var rsa_deal = parseFloat(jQuery('.rsa_deal').text().replace(/,/g, '')) || 0;
            var mcp_deal = parseFloat(jQuery('.mcp_deal').text().replace(/,/g, '')) || 0;
            var other_deal = parseFloat(jQuery('.other_deal').text().replace(/,/g, '')) || 0;

            var on_road_price = ex_showroom_price_deal + gst_tax_deal + cess_tax_deal + tcs_tax_deal + municipal_tax_deal + municipal_custom_handling_charges_deal + rto_deal + rto_custom_handling_charges_deal + insurance_deal + fastage_amt_deal + ew_deal + accessories_deal + rsa_deal + mcp_deal + other_deal;
            jQuery('.on_road_price_deal').text(on_road_price.toLocaleString());
            
            var on_road_price_deal = parseFloat(jQuery('.on_road_price_deal').text().replace(/,/g, '')) || 0;
            var final_deal_deal = parseFloat(jQuery('.final_deal_deal').text().replace(/,/g, '')) || 0;
            var deal_difference = on_road_price_deal - final_deal_deal;
            jQuery('.diff_amount_deal').text(deal_difference.toLocaleString());
        })

        function printDiv(divId) {
            // Get the content of the div
            var content = document.getElementById(divId).innerHTML;
            
            // Open a new window
            var printWindow = window.open('', '', 'height=600,width=800');
            
            // Get all stylesheets from the current document
            var stylesheets = document.querySelectorAll('link[rel="stylesheet"], style');

            // Start writing HTML
            printWindow.document.write('<html><head><title>Print Content</title>');

            // Append existing styles
            stylesheets.forEach(function(style) {
                if (style.tagName === 'LINK') {
                    printWindow.document.write('<link rel="stylesheet" href="' + style.href + '" type="text/css">');
                } else {
                    printWindow.document.write('<style>' + style.innerHTML + '</style>');
                }
            });

            printWindow.document.write('</head><body>');
            printWindow.document.write(content);
            printWindow.document.write('</body></html>');

            // Close the document to ensure styles are applied
            printWindow.document.close();

            // Wait for the content to load before printing
            printWindow.onload = function () {
                printWindow.print();
                printWindow.close();
            };
        }

        jQuery('.deal_create').on('click', function() {
            var id = $("#hidden_obf_id").val();
            var purchase_gst_tax_deal = $("#purchase_gst_tax_deal").val();
            var final_deal_amt = $("#final_deal_amt").val();
            var ex_showroom_price_deal = $("#ex_showroom_price_deal").val();
            var gst_tax_deal = $("#gst_tax_deal").val();
            var cess_tax_deal = $("#cess_tax_deal").val();
            var tcs_tax_deal = $("#tcs_tax_deal").val();
            var municipal_tax_deal = $("#municipal_tax_deal").val();
            var municipal_custom_handling_charges_deal = $("#municipal_custom_handling_charges_deal").val();
            var rto_deal = $("#rto_deal").val();
            var rto_custom_handling_charges_deal = $("#rto_custom_handling_charges_deal").val();
            var insurance_deal = $("#insurance_deal").val();
            var remark = $("#remark").val();

            var data = {
                id: id,
                purchase_gst_tax_deal: purchase_gst_tax_deal,
                final_deal_amt: final_deal_amt,
                ex_showroom_price_deal: ex_showroom_price_deal,
                gst_tax_deal: gst_tax_deal,
                cess_tax_deal: cess_tax_deal,
                tcs_tax_deal: tcs_tax_deal,
                municipal_tax_deal: municipal_tax_deal,
                municipal_custom_handling_charges_deal: municipal_custom_handling_charges_deal,
                rto_deal: rto_deal,
                rto_custom_handling_charges_deal: rto_custom_handling_charges_deal,
                insurance_deal: insurance_deal,
                remark: remark,
            };

            $.ajax({
                url: "{{ url('obf/Make-deal/create') }}",
                type: "POST",
                dataType: "json",
                data: {
                    "_token": "{{ csrf_token() }}",
                    "id": id,
                    "purchase_gst_tax_deal": purchase_gst_tax_deal,
                    "final_deal_amt": final_deal_amt,
                    "ex_showroom_price_deal": ex_showroom_price_deal,
                    "gst_tax_deal": gst_tax_deal,
                    "cess_tax_deal": cess_tax_deal,
                    "tcs_tax_deal": tcs_tax_deal,
                    "municipal_tax_deal": municipal_tax_deal,
                    "municipal_custom_handling_charges_deal": municipal_custom_handling_charges_deal,
                    "rto_deal": rto_deal,
                    "rto_custom_handling_charges_deal": rto_custom_handling_charges_deal,
                    "insurance_deal": insurance_deal,
                    "remark": remark,
                },
                success: function(response) {
                    if (response.code === 200) {  // Check for response code 200
                        toastr.success("Deal created successfully!");
                        setTimeout(function() {
                            location.reload();  // Reload the page after a short delay
                        }, 1000);
                    } else {
                        toastr.error("Failed to create deal. Please try again.");
                    }
                },
                error: function(xhr, status, error) {
                    toastr.error("An error occurred: " + error);
                }
            });
        });
    </script>
@endsection    
