@extends('layout')
@section('content')
    <!-- begin::main content -->
    <main class="main-content">
        <div class="container">
            <!-- begin::page header -->
            <div class="page-header">
                <h3>Order 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">
            <x-flashMessage />
                <div class="card-header">
                    
                    <div class="row mt-3">
                        <div class="col-md-5">
                            <form action="{{asset('account/purchase-order')}}" 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="Order 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/purchase-order')}}"><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/purchase-order')}}">
                                                    <div class="form-group">
                                                        <label>Vendor</label>
                                                        <input value="{{isset($_GET['vendor']) ? $_GET['vendor'] : null}}" type="text" name="vendor" 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/purchase-order')}}" 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 Order"
                                    href="{{ asset('account/add-purchase-order') }}" 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" id="deleteVendors">
                                    <i class="ti ti-trash fa-lg"></i>
                                </button>
                                <button title="Export Delivery Challan"
                                    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($orders))

                        <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>ORDER NO.</th>
                                        <th>DATE</th>
                                        <th>VENDOR</th>
    
                                        <th>TOTAL AMOUNT</th> 
                                        <th>STATUS</th>                                                                          
                                        <th>ACTION</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @if (!empty($orders))
                                        @foreach ($orders as $key=>$value)
                                       
                                           <tr>
                                                <td>
                                                    <input type="checkbox" class="vendor_check" value="{{$value->id}}" />
                                                </td>
                                                <td>{{++$key}}</td>
                                                <td>
                                                    {{$value->po_number}}
                                                </td>
                                                <td>
                                                    {{$value->po_date}}
                                                </td>
                                                <td>
                                                    {{$value->customer_name}}
                                                </td>
                                                
                                                <td>
                                                    {{$value->grand_total}}
                                                </td>
                                                <td>
                                                    <select class="p-1 border purchase_order_status" order_id="{{$value->id}}">
                                                        <option value="draft" {{$value->status == "draft" ? 'selected' : ''}}>Draft</option>
                                                        <option value="pending" {{$value->status == "pending" ? 'selected' : ''}}>Pending</option>
                                                        <option value="reject" {{$value->status == "reject" ? 'selected' : ''}}>Reject</option>
                                                        <option value="approved" {{$value->status == "approved" ? 'selected' : ''}}>Approved</option>
                                                        <option value="completed" {{$value->status == "completed" ? 'selected' : ''}}>Completed</option>
                                                    </select>
                                                    
                                                </td>
                                                <td>
                                                    
                                              
                                                <div class="btn-group">
                                                        <a class="btn btn-sm btn-outline-info" href="{{asset('account/edit-purchase-order/'.$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 Bank"  ><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">
                        {{ $orders->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-purchase-order') }}" 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() {
                $('.vendor_check:checkbox').not(this).prop('checked', this.checked);
            });
            
            $('#deleteVendors').click(function() {
                
                    var all_ids = [];
                    $('.vendor_check:checked').each(function(i) {
                        all_ids[i] = $(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": all_ids
                            },
                            url: "{{asset('account/delete-multiple-purchase-order')}}",
                            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();
                            }
                        });
                       
                    }
                });
                
                //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-purchase-order')}}",
                            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();
                            }
                        });
                       
                    }
                });
                
            });

            $(".purchase_order_status").on("change",function(){
                var status = $(this).val();
                var order_id = $(this).attr("order_id");
                $.ajaxSetup({
                            headers: {
                                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                            }
                        });
                $.ajax({
                    type: "post",
                    data: {
                        "status": status,
                        "order_id": order_id
                    },
                    url: "{{asset('account/purchase-order-status')}}",
                    dataType: "html",
                    success: function(result) 
                    {
                        toastr.success("Purchase order change successfully !");
                    },
                    error: function(error,xhr,status)
                    {
                        toastr.error("Failed !");
                    }
                });
            });
            
            
        });
    </script>
@endpush
