@extends('template.app')

@section('meta')
@endsection

@section('title')
Dealer Management
@endsection

@section('styles')
<style>
     .highlight {
        background-color: #ffff99; /* Change to desired highlight color */
    }
    #data-table.dataTable tbody td {
        padding: 0px !important;
        height: 20px !important;
        line-height: 35px !important;
        font-size: 15px !important;
        text-align: center;
    }
    #data-table th {
        text-align: center;
    }
    .form-group .custom_color {
        background-color: lightgrey !important;
    }
</style>
@endsection

@section('content')
<!-- Flatpickr CSS -->
<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">
<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">Dealer Management</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('dms') }}" class="text-muted">Dealer Management</a></li>
                        <li class="breadcrumb-item text-muted active" aria-current="page">List</li>
                    </ol>
                </nav>
            </div>
        </div>
        <div class="col-5 align-self-center">
            <div class="customize-input float-right">
            <a class="btn waves-effect waves-light btn-rounded btn-outline-primary pull-right" data-toggle="modal" data-target="#exampleModal" href="Javascript:void(0)">Import</a>
            <a class="btn waves-effect waves-light btn-rounded btn-outline-primary pull-right" href="{{ route('dealer.create') }}">Add New</a>
        </div>
        </div>
        <div class="col-sm-8"></div>
    </div>
</div>
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card" style="min-width:100% !important;">
                <div class="card-body">
                    <table class="table table-bordered data-table" id="data-table">
                        <thead>
                            <tr>
                                <th>No</th>
                                <th>Dealer Name</th>
                                <th>Dealer Email</th>
                                <th>Dealer Phone</th>
                                <th>Dealer Address</th>
                                <th>State</th>
                                <th>City</th>
                                <th>GST (%)</th>
                                <th>GST No</th>
                                <th>Remark</th>
                                <th>Action</th>
                            </tr>
                            <tr class="filterrow">
                                <th></th>
                                <th>
                                    <div class="search-box-container datatable-form-filter">
                                        <div class="form-group is-empty">
                                            <input type="text" name="dealer_name" value="{{ request()->get('dealer_name', '') }}" class="form-control text-capitalize custom_color" autocomplete="off" autofocus tabindex="1">
                                        </div>
                                    </div>
                                </th>
                                <th>
                                    <div class="search-box-container datatable-form-filter">
                                        <div class="form-group is-empty">
                                            <input type="text" name="dealer_email" value="{{ request()->get('dealer_email', '') }}" class="form-control text-capitalize custom_color" autocomplete="off" autofocus tabindex="2">
                                        </div>
                                    </div>
                                </th>
                                <th>
                                    <div class="search-box-container datatable-form-filter">
                                        <div class="form-group is-empty">
                                            <input type="text" name="dealer_mobile_no" value="{{ request()->get('dealer_mobile_no', '') }}" class="form-control text-capitalize custom_color" autocomplete="off" autofocus tabindex="3">
                                        </div>
                                    </div>
                                </th>
                                <th>
                                    <div class="search-box-container datatable-form-filter">
                                        <div class="form-group is-empty">
                                            <input type="text" name="dealer_address" value="{{ request()->get('dealer_address', '') }}" class="form-control text-capitalize custom_color" autocomplete="off" autofocus tabindex="4">
                                        </div>
                                    </div>
                                </th>
                                <th>
                                    <div class="search-box-container datatable-form-filter">
                                        <div class="form-group is-empty">
                                            <input type="text" name="state" value="{{ request()->get('state', '') }}" class="form-control text-capitalize custom_color" autocomplete="off" autofocus tabindex="1">
                                        </div>
                                    </div>
                                </th>
                                <th>
                                    <div class="search-box-container datatable-form-filter">
                                        <div class="form-group is-empty">
                                            <input type="text" name="city" value="{{ request()->get('city', '') }}" class="form-control text-capitalize custom_color" autocomplete="off" autofocus tabindex="2">
                                        </div>
                                    </div>
                                </th>
                                <th>
                                    <div class="search-box-container datatable-form-filter">
                                        <div class="form-group is-empty">
                                            <input type="text" name="gst" value="{{ request()->get('gst', '') }}" class="form-control text-capitalize custom_color" autocomplete="off" autofocus tabindex="3">
                                        </div>
                                    </div>
                                </th>
                                <th>
                                    <div class="search-box-container datatable-form-filter">
                                        <div class="form-group is-empty">
                                            <input type="text" name="gst_no" value="{{ request()->get('gst_no', '') }}" class="form-control text-capitalize custom_color" autocomplete="off" autofocus tabindex="3">
                                        </div>
                                    </div>
                                </th>
                                <th>
                                    <div class="search-box-container datatable-form-filter">
                                        <div class="form-group is-empty">
                                            <input type="text" name="remark" value="{{ request()->get('remark', '') }}" class="form-control text-capitalize custom_color" autocomplete="off" autofocus tabindex="4">
                                        </div>
                                    </div>
                                </th>
                                <th></th>
                            </tr>
                        </thead>
                    </table>
                </div>
            </div>
        </div>
        <div class="modal fade" id="exampleModal" role="dialog">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h3 class="modal-title">Import</h3>
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>

                    </div>
                    <form action="{{ route('dealer.import') }}" name="form" id="form" method="post" enctype="multipart/form-data">
                        @csrf
                        <div class="modal-body">
                            <div class="row">
                                <div class="form-group col-sm-12">
                                    <label for="first_name">Upload File Here</label>
                                    <input type="file" name="file" id="file" class="form-control" placeholder="Plese upload file here" required>
                                    <span class="kt-form__help error file"></span>
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="submit" class="btn btn-default">Submit!</button>
                            <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
                        </div>
                    </form>
                </div>
                 /.modal-content 
            </div>
             /.modal-dialog 
        </div>
        <!-- Modal -->
    </div>
</div>
@endsection

@section('scripts')
<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://code.jquery.com/jquery-3.6.0.min.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 type="text/javascript">-->
<!--        var datatable;-->
        
<!--        $(document).ready(function() {-->
<!--        if ($('#data-table')) {-->
<!--            datatable = $('#data-table').DataTable({-->
<!--                processing: true,-->
<!--                serverSide: true,-->
<!--                "pageLength": 25,-->
<!--                "iDisplayLength": 25,-->
<!--                "responsive": true,-->
<!--                "aaSorting": [],-->
<!--                "ajax": {-->
<!--                    "url": "{{ route('dealer') }}",-->
<!--                    "type": "POST",-->
<!--                    "dataType": "json",-->
<!--                    "data": {-->
<!--                        _token: "{{csrf_token()}}"-->
<!--                    }-->
<!--                },-->
<!--                "columnDefs": [{-->
<!--                    "orderable": true,-->
<!--                }],-->
<!--                columns: [{-->
<!--                    data: 'DT_RowIndex',-->
<!--                    name: 'DT_RowIndex'-->
<!--                    },-->
<!--                    {-->
<!--                        data: 'dealer_name',-->
<!--                        name: 'dealer_name'-->
<!--                    },-->
<!--                    {-->
<!--                        data: 'dealer_email',-->
<!--                        name: 'dealer_email'-->
<!--                    },-->
<!--                    {-->
<!--                        data: 'dealer_mobile_no',-->
<!--                        name: 'dealer_mobile_no'-->
<!--                    },-->
<!--                    {-->
<!--                        data: 'dealer_address',-->
<!--                        name: 'dealer_address'-->
<!--                    },-->
<!--                    {-->
<!--                        data: 'state',-->
<!--                        name: 'state'-->
<!--                    },-->
<!--                    {-->
<!--                        data: 'city',-->
<!--                        name: 'city'-->
<!--                    },-->
<!--                    {-->
<!--                        data: 'gst',-->
<!--                        name: 'gst'-->
<!--                    },-->
<!--                    {-->
<!--                        data: 'remark',-->
<!--                        name: 'remark'-->
<!--                    },-->
<!--                    {-->
<!--                        data: 'action',-->
<!--                        name: 'action',-->
<!--                        orderable: false,-->
<!--                    },-->
<!--                ]-->
<!--            });-->

<!--        }-->
        <!--// Highlight row on mouseover-->
<!--            $('#data-table tbody').on('mouseover', 'tr', function() {-->
<!--                $(this).addClass('highlight');-->
<!--            });-->

            <!--// Remove highlight on mouseout-->
<!--            $('#data-table tbody').on('mouseout', 'tr', function() {-->
<!--                $(this).removeClass('highlight');-->
<!--            });-->
<!--    });-->
<!--</script>-->
 <script type="text/javascript">
        window.change_status = function(object) {
            var id = $(object).data("id");
            var status = $(object).data("status");
    
            if (confirm('Are you sure?')) {
                $.ajax({
                    "url": "{!! route('dealer.change.status') !!}",
                    "dataType": "json",
                    "type": "POST",
                    "data": {
                        id: id,
                        status: status,
                        _token: "{{ csrf_token() }}"
                    },
                    success: function(response) {
                        if (response.code == 200) {
                            window.LaravelDataTables["dataTableBuilder"].ajax.reload();
                            toastr.success('Status changed successfully', 'Success');
                        } else {
                            toastr.error('Failed to change status', 'Error');
                        }
                    }
                });
            }
        }
    
        $(document).ready(function() {
            (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": ""
                    },
                    scrollY: '400px',
                    scrollCollapse: true,
                    paging: true,
                    searching: false,
                    bSortCellsTop : true,
                    ajax: {
                        url: '{!! route("dealer") !!}',
                        data: function (d) {
                            d.dealer_name = jQuery(".datatable-form-filter input[name='dealer_name']").val();
                            d.dealer_email = jQuery(".datatable-form-filter input[name='dealer_email']").val();
                            d.dealer_mobile_no = jQuery(".datatable-form-filter input[name='dealer_mobile_no']").val();
                            d.dealer_address = jQuery(".datatable-form-filter input[name='dealer_address']").val();
                            d.state = jQuery(".datatable-form-filter input[name='state']").val();
                            d.city = jQuery(".datatable-form-filter input[name='city']").val();
                            d.gst = jQuery(".datatable-form-filter input[name='gst']").val();
                            d.gst_no = jQuery(".datatable-form-filter input[name='gst_no']").val();
                            d.remark = jQuery(".datatable-form-filter input[name='remark']").val();
                        },
                        error : function(d) {}
                    },
                    columns: [
                        {data: 'DT_RowIndex', name: 'DT_RowIndex',class:'text-center DT_RowIndex',title: 'No.', orderable: false, width:100,default: true},
                        {data: 'dealer_name', name: 'dealer_name',class:'text-center dealer_name',title: 'Dealer Name', orderable: false,default: true},
                        {data: 'dealer_email', name: 'dealer_email',class:'text-center dealer_email',title: 'Dealer Email', orderable: false,default: true},
                        {data: 'dealer_mobile_no', name: 'dealer_mobile_no',class:'text-center dealer_mobile_no',title: 'Dealer Phone', orderable: false,default: true},
                        {data: 'dealer_address', name: 'dealer_address',class:'text-center dealer_address',title: 'Dealer Address', orderable: false,default: true},
                        {data: 'state', name: 'state',class:'text-center state',title: 'State', orderable: false,default: true},
                        {data: 'city', name: 'city',class:'text-center city',title: 'City', orderable: false,default: true},
                        {data: 'gst', name: 'gst',class:'text-center gst',title: 'GST (%)', orderable: false,default: true},
                        {data: 'gst_no', name: 'gst_no',class:'text-center gst_no',title: 'GST No', orderable: false,default: true},
                        {data: 'remark', name: 'remark',class:'text-center remark',title: 'Remark', orderable: false,default: true},
                        // {data: 'status', name: 'status',class:'text-center status',title: 'Status', orderable: false,default: true},
                        {data: 'action', name: 'action',title: 'Action', orderable: false, searchable: false, width:50,class:'text-center action_column',default: true}
                    ],
                    lengthMenu: [
                        [25, 50,100, -1 ],['25', '50','100', 'Show all' ]
                    ],
                    order: [],
                    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);
        });  
        
        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