@extends('template.app')

@section('meta') @endsection

@section('title') Insurance Report @endsection

@section('styles')
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
    <link href="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/css/select2.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.datatables.net/1.13.7/css/jquery.dataTables.min.css">
    <style>
        .table-responsive {
            overflow-x: auto;
            max-width: 100%;
        }
        
        .dataTables_scrollHeadInner,
        .dataTables_scrollBody table {
            width: 100% !important;
        }
        
        .dataTables_scrollHead {
            overflow: hidden;
        }
        .dataTables_scrollBody {
            max-height: 400px; 
            overflow-y: auto;
        }
        
        
        #data-table {
            width: 100% !important;
            border-collapse: collapse;
        }
        
        #data-table thead th {
            white-space: nowrap;
        }
       
        #data-table_wrapper {
            width: 100%;
            overflow-x: auto;
        }
        
        td, th {
            border: 1px solid #dddddd;
            text-align: left;
            padding: 8px;
        }
        
        tr:nth-child(even) {
            background-color: #313762;
        }
        .daterangepicker {
            background-color: #fff !important;
            color: #000 !important;
            border: 1px solid #ccc;
            z-index: 9999;
        }
        
        .daterangepicker .calendar-table,
        .daterangepicker td,
        .daterangepicker th {
            background-color: #fff !important;
            color: #000 !important;
        }
        
        .daterangepicker td.active,
        .daterangepicker td.active:hover {
            background-color: #007bff !important;
            color: #fff !important;
        }
        
        .daterangepicker .drp-buttons .btn {
            background-color: #007bff;
            color: #fff;
        }
        
        .daterangepicker .drp-buttons .btn:hover {
            background-color: #0056b3;
        }
        
        .width_150 {
            width: 150px;
        }
        .width_200 {
            width: 200px;
        }
        .width_250 {
            width: 250px;
        }
        .width_300 {
            width: 300px;
        }
        
        tr:nth-child(even) {
         background-color: rgba(0,0,0,0.24) !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">Insurance Report</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('allReport') }}" class="text-muted">Mis Report</a></li>
                            <li class="breadcrumb-item text-muted active" aria-current="page">Insurance Report</li>
                        </ol>
                    </nav>
                </div>
            </div>

            <div class="col-5 align-self-center">
                <div class="customize-input float-right">
                    <button class="btn waves-effect waves-light btn-rounded btn-outline-primary pull-right" type="button" data-toggle="collapse" data-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
                        Export
                    </button>
                </div>
            </div>

            <div class="col-sm-8"></div>

            <div class="col-sm-4">
                <div class="col-md-12 align-self-center collapse" id="collapseExample">
                    <div class="customize-input float-right">
                        <form action="{{ route('insuranceExport') }}" method="get">
                            @csrf
                            <div class="row">
                                <div class="form-group col-md-6">
                                    <input type="text" name="filter_booking_date" class="form-control text-capitalize" autocomplete="off" tabindex="5" id="filter_booking_date" placeholder="DD-MM-YYYY to DD-MM-YYYY">
                                </div>
            
                                <div class="form-group col-md-4">
                                    <select name="slug" class="form-control" required>
                                        <option value="">Select Status</option>
                                        <option value="all">All</option>
                                        <option value="invoice">Invoice Date</option>
                                    </select>
                                </div>
                                <div class="form-group col-md-6">
                                    <button class="btn btn-primary" type="submit">Submit</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="container-fluid">
       <div class="row">
            <div class="col-12">
                <div class="card">
                    <div class="card-body table-responsive" style="text-align: center;">
                        <table class="table table-bordered data-table tables" id="data-table"> 
                            <thead>
                                <tr>
                                    <th>No</th>
                                    <th>Location</th>
                                    <th>Customer Name</th>
                                    <th>Model</th>
                                    <th>Variant</th>
                                    <th>Vin Number</th>
                                    <th>Invoice Date</th>
                                    <th>Invoice No.</th>
                                    <th>Ex. Sr. Price</th>
                                    <th>ERP Insurance Price</th>
                                    <th>Insurance Company Name</th>
                                    <th>Insurance Type</th>
                                    <th>Policy No.</th>
                                    <th>Date</th>
                                    <th>Net OD Premium</th>
                                    <th>Gross Premium Paid</th>
                                    <th>Insu. Dep. Status</th>
                                </tr>

                                <tr class="filterrow">

                                    <th></th>

                                    <!-- Branch -->
                                    <th>
                                        <div class="search-box-container datatable-form-filter width_300">
                                            <div class="form-group is-empty">
                                                <input type="text" name="branch" id="branch" value="{{ request()->get('branch', '') }}" class="form-control text-capitalize custom_color" id="branch" autocomplete="off" autofocus tabindex="1">
                                            </div>
                                        </div>
                                    </th>

                                    <!-- Customer Name -->
                                    <th>
                                        <div class="search-box-container datatable-form-filter width_300">
                                            <div class="form-group is-empty">
                                                <input type="text" name="customer_name" id="customer_name" value="{{ request()->get('customer_name', '') }}" class="form-control text-capitalize custom_color" id="customer_name" autocomplete="off" autofocus tabindex="2">
                                            </div>
                                        </div>
                                    </th>

                                    <!-- Model -->
                                    <th>
                                        <div class="search-box-container datatable-form-filter width_300">
                                            <div class="form-group is-empty">
                                                <input type="text" name="model" id="model" value="{{ request()->get('model', '') }}" class="form-control text-capitalize custom_color" id="model" autocomplete="off" autofocus tabindex="3">
                                            </div>
                                        </div>
                                    </th>
                                    
                                    <!-- Varient -->
                                    <th>
                                        <div class="search-box-container datatable-form-filter width_300">
                                            <div class="form-group is-empty">
                                                <input type="text" name="car_varient_name" id="car_varient_name" value="{{ request()->get('car_varient_name', '') }}" class="form-control text-capitalize custom_color" id="car_varient_name" autocomplete="off" autofocus tabindex="4">
                                            </div>
                                        </div>
                                    </th>

                                    <!-- Vin Number -->
                                    <th>
                                        <div class="search-box-container datatable-form-filter width_300">
                                            <div class="form-group is-empty">
                                                <input type="text" name="vin_number" id="vin_number" value="{{ request()->get('vin_number', '') }}" class="form-control text-capitalize custom_color" id="vin_number" autocomplete="off" autofocus tabindex="5">
                                            </div>
                                        </div>
                                    </th>

                                    <!-- Invoice Date -->
                                    <th>
                                        <div class="search-box-container datatable-form-filter width_300">
                                            <div class="form-group is-empty">
                                                <input type="text" name="invoice_date" id="invoice_date" value="{{ request()->get('invoice_date', '') }}" class="form-control text-capitalize custom_color" id="invoice_date" autocomplete="off" autofocus tabindex="6">
                                            </div>
                                        </div>
                                    </th>

                                    <!-- Invoice No. -->
                                    <th>
                                        <div class="search-box-container datatable-form-filter width_300">
                                            <div class="form-group is-empty">
                                                <input type="text" name="invoice_no" id="invoice_no" value="{{ request()->get('invoice_no', '') }}" class="form-control text-capitalize custom_color" id="invoice_no" autocomplete="off" autofocus tabindex="7">
                                            </div>
                                        </div>
                                    </th>

                                    <!-- Ex. Sh. Price -->
                                    <th>
                                        <div class="search-box-container datatable-form-filter width_300">
                                            <div class="form-group is-empty">
                                                <input type="text" name="ex_showroom_price" id="ex_showroom_price" value="{{ request()->get('ex_showroom_price', '') }}" class="form-control text-capitalize custom_color" id="ex_showroom_price" autocomplete="off" autofocus tabindex="8">
                                            </div>
                                        </div>
                                    </th>

                                    <!-- ERP Insurance Price -->
                                    <th>
                                        <div class="search-box-container datatable-form-filter width_300">
                                            <div class="form-group is-empty">
                                                <input type="text" name="insurance_id" id="insurance_id" value="{{ request()->get('insurance_id', '') }}" class="form-control text-capitalize custom_color" id="insurance_id" autocomplete="off" autofocus tabindex="9">
                                            </div>
                                        </div>
                                    </th>

                                    <!-- Insurance Company Name -->
                                    <th>
                                        <div class="search-box-container datatable-form-filter width_300">
                                            <div class="form-group is-empty">
                                                <input type="text" name="insurance_name" id="insurance_name" value="{{ request()->get('insurance_name', '') }}" class="form-control text-capitalize custom_color" id="insurance_name" autocomplete="off" autofocus tabindex="10">
                                            </div>
                                        </div>
                                    </th>

                                    <!-- Insurance Type -->
                                    <th>
                                        <div class="search-box-container datatable-form-filter width_300">
                                            <div class="form-group is-empty">
                                                <input type="text" name="insurance_type" id="insurance_type" value="{{ request()->get('insurance_type', '') }}" class="form-control text-capitalize custom_color" id="insurance_type" autocomplete="off" autofocus tabindex="11">
                                            </div>
                                        </div>
                                    </th>

                                    <!-- Policy No. -->
                                    <th>
                                        <div class="search-box-container datatable-form-filter width_300">
                                            <div class="form-group is-empty">
                                                <input type="text" name="policy_number" id="policy_number" value="{{ request()->get('policy_number', '') }}" class="form-control text-capitalize custom_color" id="policy_number" autocomplete="off" autofocus tabindex="12">
                                            </div>
                                        </div>
                                    </th>

                                    <!-- Insurance Date -->
                                    <th>
                                        <div class="search-box-container datatable-form-filter width_300">
                                            <div class="form-group is-empty">
                                                <input type="text" name="date_of_insurance" id="date_of_insurance" value="{{ request()->get('date_of_insurance', '') }}" class="form-control text-capitalize custom_color" id="date_of_insurance" autocomplete="off" autofocus tabindex="13">
                                            </div>
                                        </div>
                                    </th>

                                    <!-- OD Premium -->
                                    <th>
                                        <div class="search-box-container datatable-form-filter width_300">
                                            <div class="form-group is-empty">
                                                <input type="text" name="own_damage_premium" id="own_damage_premium" value="{{ request()->get('own_damage_premium', '') }}" class="form-control text-capitalize custom_color" id="own_damage_premium" autocomplete="off" autofocus tabindex="14">
                                            </div>
                                        </div>
                                    </th>

                                    <!-- Gross Premium -->
                                    <th>
                                        <div class="search-box-container datatable-form-filter width_300">
                                            <div class="form-group is-empty">
                                                <input type="text" name="gross_premium_paid" id="gross_premium_paid" value="{{ request()->get('gross_premium_paid', '') }}" class="form-control text-capitalize custom_color" id="gross_premium_paid" autocomplete="off" autofocus tabindex="15">
                                            </div>
                                        </div>
                                    </th>

                                    <!-- Status -->
                                    <th>
                                        <div class="search-box-container datatable-form-filter width_300">
                                            <div class="form-group is-empty">
                                                <input type="text" name="status" id="status" value="{{ request()->get('status', '') }}" class="form-control text-capitalize custom_color" id="status" autocomplete="off" autofocus tabindex="16">
                                            </div>
                                        </div>
                                    </th>
                                </tr>
                            </thead>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>        
@endsection



@section('scripts')

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap5.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
    <script src="https://cdn.datatables.net/1.13.7/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/js/select2.min.js"></script>
    <script src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" />
    <script src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>

    <script type="text/javascript">
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
    </script>

    <script type="text/javascript">
        $(document).ready(function() {
            $('#invoice_date,#date_of_insurance,#filter_booking_date').daterangepicker({
                autoUpdateInput: false,
                locale: {
                    format: 'DD-MM-YYYY',
                    cancelLabel: 'Clear'
                }
            });
            
            $('#invoice_date,#date_of_insurance,#filter_booking_date').on('apply.daterangepicker', function(ev, picker) {
                $(this).val(picker.startDate.format('DD-MM-YYYY') + ' to ' + picker.endDate.format('DD-MM-YYYY'));
                $('#data-table').DataTable().draw();
            });
            
            $('#invoice_date,#date_of_insurance,#filter_booking_date').on('cancel.daterangepicker', function(ev, picker) {
                $(this).val('');
                $('#data-table').DataTable().draw();
            });

            $('.js-example-basic-single').select2();

            var tableColumns = [
                {data: null, title: 'No.', class: 'text-center', orderable: false, width: 100,
                    render: function (data, type, row, meta) {
                        return meta.row + meta.settings._iDisplayStart + 1;
                    }
                },
                {data: 'branch', name: 'branch', class: 'text-center', title: 'Location', orderable: true,default: true},
                {data: 'customer_name', name: 'customer_name', class: 'text-center', title: 'Customer Name', orderable: true,default: true},
                {data: 'model', name: 'model', class: 'text-center', title: 'Model', orderable: true,default: true},
                {data: 'car_varient_name', name: 'car_varient_name', class: 'text-center', title: 'Variant', orderable: true,default: true},
                {data: 'vin_number', name: 'vin_number', class: 'text-center', title: 'Vin Number', orderable: true,default: true},
                {data: 'invoice_date', name: 'invoice_date', class: 'text-center', title: 'Invoice Date', orderable: true,default: true},
                {data: 'invoice_no', name: 'invoice_no', class: 'text-center', title: 'Invoice No.', orderable: true,default: true},
                {data: 'ex_showroom_price', name: 'ex_showroom_price', class: 'text-center', title: 'Ex. Sr. Price', orderable: true,default: true},
                {data: 'insurance_id', name: 'insurance_id', class: 'text-center', title: 'ERP Insurance Price', orderable: true,default: true},
                {data: 'insurance_name', name: 'insurance_name', class: 'text-center', title: 'Insurance Company Name', orderable: true,default: true},
                {data: 'insurance_type', name: 'insurance_type', class: 'text-center', title: 'Insurance Type', orderable: true,default: true},
                {data: 'policy_number', name: 'policy_number', class: 'text-center', title: 'Policy No.', orderable: true,default: true},
                {data: 'date_of_insurance', name: 'date_of_insurance', class: 'text-center', title: 'Date', orderable: true,default: true},
                {data: 'own_damage_premium', name: 'own_damage_premium', class: 'text-center', title: 'Net OD Premium', orderable: true,default: true},
                {data: 'gross_premium_paid', name: 'gross_premium_paid', class: 'text-center', title: 'Gross Premium Paid', orderable: true,default: true},
                {data: 'status', name: 'status', class: 'text-center', title: 'Insu. Dep. Status', orderable: true,default: true},
            ];

            (function(window, $) {
                window.LaravelDataTables = window.LaravelDataTables || {};
                window.LaravelDataTables["dataTableBuilder"] = $("#data-table").DataTable({
                    processing: true,
                    serverSide: true,
                    language: {
                        "info": "_START_ - _END_ of _TOTAL_",
                        "lengthMenu": "_MENU_ ",
                        "infoFiltered": ""
                    },
                    scrollCollapse: true,
                    paging: true,
                    searching: false,
                    bSortCellsTop : true,
                    ajax: {
                        url: '{!! route("InsuranceFetchReport") !!}',
                        method: "POST",
                        data: function (d) {
                            d.branch = jQuery(".datatable-form-filter input[name='branch']").val();
                            d.customer_name = jQuery(".datatable-form-filter input[name='customer_name']").val();
                            d.model = jQuery(".datatable-form-filter input[name='model']").val();
                            d.car_varient_name = jQuery(".datatable-form-filter input[name='car_varient_name']").val();
                            d.vin_number = jQuery(".datatable-form-filter input[name='vin_number']").val();
                            d.invoice_date = jQuery(".datatable-form-filter input[name='invoice_date']").val();
                            d.invoice_no = jQuery(".datatable-form-filter input[name='invoice_no']").val();
                            d.ex_showroom_price = jQuery(".datatable-form-filter input[name='ex_showroom_price']").val();
                            d.insurance_id = jQuery(".datatable-form-filter input[name='insurance_id']").val();
                            d.insurance_name = jQuery(".datatable-form-filter input[name='insurance_name']").val();
                            d.insurance_type = jQuery(".datatable-form-filter input[name='insurance_type']").val();
                            d.policy_number = jQuery(".datatable-form-filter input[name='policy_number']").val();
                            d.date_of_insurance = jQuery(".datatable-form-filter input[name='date_of_insurance']").val();
                            d.own_damage_premium = jQuery(".datatable-form-filter input[name='own_damage_premium']").val();
                            d.gross_premium_paid = jQuery(".datatable-form-filter input[name='gross_premium_paid']").val();
                            d.status = jQuery(".datatable-form-filter input[name='status']").val();
                        },
                        error : function(d) {}
                    },
                    columns: tableColumns,
                    lengthMenu: [
                        [25, 50,100, -1 ],['25', '50','100', 'Show all' ]
                    ],
                    order: [ [0,'DESC'] ],
                    responsive: true,
                    colReorder:{
                        enable: false,
                    },
                    dom: '<"top"f>rt<"bottom"ipl><"clear">',
                    fnInitComplete: function(settings, json) {
                        jQuery('table.table input:checkbox').map(function() {
                            if(jQuery(this).prop("checked") != true){
                                var column = table.column( jQuery(this).attr('data-column') );
                                column.visible(false);
                            }
                        });
                    },
                    pageLength: 25,
                });
            })(window, jQuery);

            $("#min").flatpickr({ onSelect: function () { window.LaravelDataTables["dataTableBuilder"].draw(); }, changeMonth: true, changeYear: true });

            $(document).on("change", "#min", function () {
                window.LaravelDataTables["dataTableBuilder"].draw();
            })  

            jQuery('.datatable-form-filter input').on('keyup', function (e) {
                if(jQuery(this).val().length >= 3 || e.keyCode == 13 || !jQuery(this).val()) {
                    window.LaravelDataTables["dataTableBuilder"].draw();
                    e.preventDefault();
                }
            });
        
            jQuery('.datatable-form-filter select').on('change', function (e) {
                window.LaravelDataTables["dataTableBuilder"].draw();
                e.preventDefault();
            });
        
            jQuery('input:checkbox[name="name_sort_asc"],input:checkbox[name="name_sort_desc"]').on('click', function (e) {
                window.LaravelDataTables["dataTableBuilder"].draw();
                e.preventDefault();
            });
        }); 
    </script>
@endsection