@extends('layout')
@section('content')
    <!-- begin::main content -->
    <main class="main-content">
        <div class="container">
            <!-- begin::page header -->
            <div class="page-header">
                <h3>Quotes List</h3>
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="{{ asset('dashboard') }}">{{ __('message.dashboard') }}</a></li>
                        <li class="breadcrumb-item active" aria-current="page">{{ __('message.list') }}</li>
                    </ol>
                </nav>
            </div>
            <!-- end::page header -->
            <div class="card">

                <div class="card-header">
                <x-flashMessage />
                    <div class="row mt-3">
                        <div class="col-md-5">
                            <form action="{{asset('account/quotes')}}" method="get">
                                
                                <div class="company-field-box">
                                        <div>
                                            <div class="input-group">
                                                <div class="input-group-prepend">
                                                    <button type="button" class="btn border input-group-text filter-company-btn px-4">
                                                        <i class="ti ti-filter fa-lg"></id></i>
                                                    </button>
                                                </div>
                                                    
                                                    <input type="search" value="{{isset($_GET['search']) ? $_GET['search'] : null}}" name="search" placeholder="Quotes Number" class="form-control border-right-0 seach-input">
                                                    <button class="btn border border-left-0" type="submit" style="margin-left:-5px;">
                                                        <i class="ti ti-search fa-lg"></i>
                                                    </button>
                                                    <a class="btn btn-white border" href="{{asset('account/quotes')}}"><i class="ti ti-reload fa-lg"></i></a>
                                                    
                                            </div>
                                            <div class="filter-form-box d-none">
                                                <div class="diamond"></div>
                                                <form method="get" action="{{asset('account/quotes')}}">
                                                    <div class="form-group">
                                                        <label>Quotation Date</label>
                                                        <input value="{{isset($_GET['quotation_date']) ? $_GET['quotation_date'] : null}}" type="date" name="quotation_date" class="form-control" >
                                                    </div>
                                                    
                                                            <div class="form-group ">
                                                                <label>PO Number</label>
                                                                <input type="text" name="po_number" value="{{isset($_GET['po_number']) ? $_GET['po_number'] : null}}" class="form-control" >
                                                            </div>

                                                          
                                                                                                        
                                                    
                                                    <div class="row">
                                                        <div class="form-group col-6">
                                                            <label>Start Date</label>
                                                            <input type="date" name="start_date" value="{{isset($_GET['start_date']) ? $_GET['start_date'] : null}}"  class="form-control" id="start_date">
                                                        </div>
                                                        
                                                        <div class="form-group col-6">
                                                            <label>End Date</label>
                                                            <input type="date" name="end_date" value="{{isset($_GET['end_date']) ? $_GET['end_date'] : null}}" class="form-control" id="end_date">
                                                        </div>
                                                        
                                                    </div>
                                                    <div class="row">
                                                        <div class="form-group col-12 d-flex justify-content-end">
                                                            <button type="submit" class="btn btn-success mr-5 px-4 apply-button">Apply </button>
                                                        
                                                            <a href="{{asset('account/quotes')}}" class="btn btn-primary px-3 text-white">Clear</a>
                                                        </div>
                                                    </div>
                                                </form>
                                            </div>
                                        </div>
                                </div>
                            </form>
                        </div>
                        <div class="col-md-4"></div>
                        <div class="col-md-3 d-flex justify-content-end">
                            <div>
                            
                                <a data-toggle="tooltip" data-original-title="Add Quotes"
                                    href="{{ asset('account/add-quotes') }}" class="btn m-r-10 btn-outline-secondary pull-left">
                                    <i class="ti ti-plus fa-lg"></i>
                                </a>
                                <button data-toggle="tooltip" data-original-title="Delete All"
                                     class="btn m-r-10 btn-outline-secondary pull-left delete_all_invoice">
                                    <i class="ti ti-trash fa-lg"></i>
                                </button>
                                <button title="Export Quotes"
                                    data-toggle="modal" data-target="#invoiceModal"
                                     class="btn m-r-10 btn-outline-secondary pull-left">
                                    <i class="fa fa-download fa-lg"></i>
                                </button>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="card-body">

                    @if (isset($quotes))

                        <div class="table-responsive">
                            <table class="table table-striped table-bordered">
                                <thead>
                                    <tr>
                                        <th>
                                            <input type="checkbox" id="checkAll" />
                                        </th>
                                        <th class="wrap">S.NO</th>
                                        <th>QUOTATION NO</th>
                                        <th>QUOTATION DATE</th>
                                        <th>PO NUMBER</th>                                                                             
                                        <th>VALID DATE</th>
                                        <th>CLIENT</th>
                                        <th>TOTAL</th>
                                        <th>STATUS</th>
                                        <th>ACTION</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @if (!empty($quotes))
                                        @foreach ($quotes as $key=>$value)
                                       
                                           <tr>
                                                <td>
                                                    <input type="checkbox" class="check_invoice" value="{{$value->id}}" />
                                                </td>
                                                <td>{{++$key}}</td>
                                                <td>
                                                    {{$value->quotation_number}}
                                                </td>
                                                <td>
                                                    {{$value->quotation_date}}
                                                </td>
                                                <td>{{$value->po_number}}</td>
                                                <td>{{$value->valid_date}}</td>
                                
                                                <td>{{$value->customer_name}}</td>
                                                <td>₹ {{$value->grand_total}}</td>
                                                <td>
                                                    <select class="form-control change_quote_status" quotes_id="{{$value->id}}">
                                                        <option value="pending" {{$value->status == "pending" ? "selected" : ''}}>Pending</option>
                                                        <option value="reject" {{$value->status == "reject" ? "selected" : ''}}>Reject</option>
                                                        <option value="accept" {{$value->status == "accept" ? "selected" : ''}}>Accept</option>
                                                    </select>
                                                </td>
                                                
                                                <td>
                                                    
                                              
                                                <div class="btn-group">
                                                        <!-- <a class="btn btn-sm btn-outline-info" href="{{asset('account/get-invoice/'.$value->id)}}"><i class="fa fa-eye"></i></a> -->
                                                        <a class="btn btn-sm btn-outline-info" href="{{asset('account/edit-quotes/'.$value->id)}}"><i class="fa fa-pencil"></i></a>
                                                        
                                                        <button class="btn btn-sm btn-outline-danger reg-delete-btn" reg_id="{{$value->id}}" data-toggle="tooltip"
                                                            data-original-title="Delete Invoice"  ><i
                                                                class="ti ti-trash fa-lg"></i>
                                                        </button>
                                                        
                                                    </div>
                                                </td>
                                           </tr>
                                        @endforeach
                                    @else
                                        <h1>No Record Found!!</h1>
                                    @endif
                                </tbody>
                            </table>
                        </div>

                        
                        <div class="mt-3 d-flex justify-content-center">
                        {{ $quotes->links('pagination::bootstrap-4') }} 
                        </div>
                        
                    @endif
                </div>
            </div>
        </div>

        <!-- The Modal -->
        <div class="modal" id="invoiceModal">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">

                <!-- Modal Header -->
                <div class="modal-header bg-success rounded-0">
                    <h4 class="modal-title">Export Document</h4>
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                </div>

                <!-- Modal body -->
                <div class="modal-body">
                    <p><strong>Please select the document period.</strong></p>
                    <form action="{{ asset('account/export-quotes') }}" method="get">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label>Start Date</label>
                                    <input type="date" name="start_date" class="form-control">
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label>End Date</label>
                                    <input type="date" name="start_date" class="form-control">
                                </div>
                            </div>
                        </div>
                        
                        <div class="d-flex justify-content-end">
                            <button type="submit" class="btn btn-success">Export</button>
                        </div>
                    </form>
                </div>

                

                </div>
            </div>
        </div>



    </main>
    <!-- end::main content -->
@endsection
@push('footer-script')
    <!-- begin::dataTable -->
    <script src="{{ asset('public/admin/js/examples/datatable.js') }}"></script>
    <!-- end::dataTable -->
    <script>
        $(document).ready(function() {
            $(".filter-company-btn").click(function(){
            
                if($(".filter-form-box").hasClass("d-none"))
                {
                    $(".filter-form-box").removeClass("d-none");
                }
                else
                {
                    $(".filter-form-box").addClass("d-none");
                }
            });

            $(".reset_password_btn").click(function(){
                var user_id = $(this).attr("user_id");
                $("#user_unique_id").val(user_id);
            });

            $("#checkAll").click(function() {
                $('.check_invoice:checkbox').not(this).prop('checked', this.checked);
            });
            /*
            $('#deleteAll').click(function() {
                if (confirm("Are you sure you want to delete Users? ")) {
                    var val = [];
                    $('.deleteall:checked').each(function(i) {
                        val[i] = $(this).val();
                    });

                    if (val.length > 0) {
                        $.ajax({
                            method: "POST",
                            headers: {
                                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                            },
                            url: "{{ asset('delete-multiple-users') }}",
                            data: {
                                "ids": val
                            }
                        }).done(function(data) {
                            if (data) {
                                location.reload();
                            } else {
                                alert("Something went wrong!, Try again");
                            }
                        });
                    }
                }
                //alert({{ asset('admin/delete-multiple-users') }});
            });*/
            
            $(".reg-delete-btn").click(function(){
                var id = $(this).attr("reg_id");
               
                swal({
                    title: 'Are you sure?',
                    text: 'Once deleted, you will not be recover!',
                    icon: 'warning',
                    dangerMode: true,
                    buttons: ["Cancel", "Yes!"],
                }).then(function(value) {
                    if (value) {
                        $.ajaxSetup({
                            headers: {
                                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                            }
                        });
                        $.ajax({
                            type: "post",
                            data: {
                                "id": id
                            },
                            url: "{{asset('account/delete-quotes')}}",
                            dataType: "html",
                            success: function(result) 
                            {
                                swal("success!", "Successfully deleted!", "success");
                                window.location.reload();
                            },
                            error: function(error,xhr,status)
                            {
                                swal("failed!", "failed to delete!", "error");
                               window.location.reload();
                            }
                        });
                       
                    }
                });
                
            });
            $(".delete_all_invoice").click(function() {
                var URL = "{{ asset('account/multiple-delete-quotes') }}";
                var parent_ids = [];
                $('.check_invoice:checkbox:checked').each(function() {
                    parent_ids.push($(this).val());
                });
                swal({
                    title: 'Are you sure?',
                    text: 'Once deleted, you will not be recover!',
                    icon: 'warning',
                    dangerMode: true,
                    buttons: ["Cancel", "Yes!"],
                }).then(function(value) {
                    if (value) {
                        $.ajaxSetup({
                            headers: {
                                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                            }
                        });
                        $.ajax({
                            type: "post",
                            data: {
                                "ids": parent_ids
                            },
                            url: URL,
                            dataType: "html",
                            success: function(result) {
                                        
                                swal("success!", "Successfully deleted!", "success");
                                window.location.reload();
                            },
                            error: function(xhr)
                            {
                                swal("failed!", "failed !", "error");
                                window.location.reload();
                            }
                        });
                    }
                });
            });

            $(".change_quote_status").on("change",function(){
                var status = $(this).val();
                var id = $(this).attr("quotes_id");

                        $.ajax({
                            type: "post",
                            headers: {
                                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                            },
                            data: {
                                id,
                                status
                            },
                            url: "{{asset('account/quotes-status-change')}}",
                            dataType: "html",
                            success: function(result) 
                            {
                                toastr.success("Quations status changed !");
                            },
                            error: function(error,xhr,status)
                            {
                                toastr.error("Failed !");
                            }
                        });
            });

            $(".purpose-btn").click(function(){
                
                $("#purposeModal").modal();
            });
            
        });
    </script>
@endpush
