@extends('template.app')

@section('meta')
@endsection

@section('title')
    Assign Inventory
@endsection

@section('styles')
    <style>
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            border: 1px solid white!important;
            padding: 8px;
            text-align: center;
            line-height: 1.8;
        }
        .width_250{
            width: 250px;
        }
        
        .width_200{
            width: 200px;
        }
        
        .width_100{
            width: 100px;
        }
        
        .width_50{
            width: 50px;
        }
    </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">Inventories</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('inventory') }}" class="text-muted">Inventories</a></li>
                        <li class="breadcrumb-item text-muted active" aria-current="page">Assign</li>
                    </ol>
                </nav>
            </div>
        </div>
    </div>
</div>
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-body">
                    <form action="{{ route('inventory.accepted') }}" name="form" id="form" method="post" enctype="multipart/form-data">
                        @csrf
                        @method('POST')
                        
                        <input type="hidden" name="obf_id" id="obf_id" value="">
                        <input type="hidden" name="id" id="inventory_id" value="{{ $product['i_id'] }}">
                        
                        <table class="table tables" cellspacing="0">
                            <thead>
                                <tr>
                                    <th class="width_250">Model</th>
                                    <th class="width_250">VIN No.</th>
                                    <th class="width_250">Interior Color</th>
                                    <th class="width_250">Exterior Color</th>
                                    <th class="width_250">Manufacturing Year</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>{{ $product['car_name'] }}</td>
                                    <td>{{ $product['vin_number'] }}</td>
                                    <td>{{ $product['interior_color'] }}</td>
                                    <td>{{ $product['exterior_color'] }}</td>
                                    <td>{{ $product['manufacturing_year'] }}</td>
                                </tr>
                            </tbody>
                        </table>
                        
                        <table class="table tables">
                            <thead>
                                <tr>
                                    <th class="width_50">Customer ID</th>
                                    <th class="width_200">Booking Date</th>
                                    <th class="width_200">Customer Name</th>
                                    <th class="width_200">Location</th>
                                    <th class="width_200">On Road Price</th>
                                    <th class="width_200">Total Received Amount</th>
                                    <th class="width_200">Due Amount</th>
                                    <th class="width_100">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                @if(isset($data) && $data->isNotEmpty())
                                    @foreach($data as $row)
                                        <tr>
                                            <td>{{ $row->id }}</td>
                                            <td>{{ \Carbon\Carbon::parse($row->booking_date)->format('d-m-Y H:i:s') }}</td>
                                            <td>{{ $row->customer_name }}</td>
                                            <td>{{ $row->branch_name }}</td>
                                            <td>{{ $row->on_road_price }}</td>
                                            <td>{{ $row->received_amount }}</td>
                                            <td>{{ $row->due_amount }}</td>
                                            <td>
                                                <button type="button" class="btn btn-outline-primary allocate-btn" data-id="{{ $row->id }}">
                                                    Allocate
                                                </button>
                                            </td>
                                        </tr>
                                    @endforeach
                                @endif    
                            </tbody>
                        </table>
                        <div class="form-group">
                            <a href="{{ route('inventory') }}" class="btn waves-effect waves-light btn-rounded btn-outline-secondary">Back</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
    $(document).ready(function() {
        var form = $('#form');
        
        // Handle Allocate button click
        $('.allocate-btn').on('click', function () {
            var obfId = $(this).data('id');
            $('#obf_id').val(obfId); // set the selected customer ID
            form.submit();
        });
        
        form.submit(function(e) {
            e.preventDefault();
            
            $.ajax({
                url: form.attr('action'),
                type: form.attr('method'),
                data: new FormData($(this)[0]),
                dataType: 'json',
                async: false,
                processData: false,
                contentType: false,
                success: function(response) {
                    setTimeout(function () {
                        alert('Allocated successfully!');
                        window.location.href = "{{ route('inventory') }}";
                    }, 500);
                },
                error: function(json) {
                    if (json.status === 422) {
                        e.preventDefault();
                        var errors_ = json.responseJSON;
                        $.each(errors_.errors, function(key, value) {
                            $('.' + key).html(value);
                        });
                    }
                }
            });
        });
    });
</script>
@endsection