@extends('layouts.master')

@section('title', 'Pricing')

@section('content')

    <div class="card">

        <div class="card-header">

            <div class="d-flex justify-content-between align-items-center">

                <h4 class="mb-0">
                    Pricing Configuration
                </h4>

                <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addPricingModal">

                    <i class="bi bi-plus-circle"></i>
                    Add Pricing

                </button>

            </div>

        </div>

        <div class="card-body">

            <div class="table-responsive">

                <table class="table table-bordered table-striped align-middle" id="pricingTable">

                    <thead class="table-light">

                        <tr>

                            <th width="60">
                                S/N
                            </th>

                            <th>
                                Cargo Type
                            </th>

                            <th>
                                Device Type
                            </th>

                            <th class="text-end">
                                Amount (TZS)
                            </th>

                            <th width="120">
                                Status
                            </th>

                            <th width="90" class="text-center">
                                Action
                            </th>

                        </tr>

                    </thead>

                    <tbody>

                        @foreach ($pricings as $pricing)
                            <tr>

                                <td>
                                    {{ $loop->iteration }}
                                </td>

                                <td>
                                    <span class="fw-bold">
                                        {{ $pricing->cargo_type }}
                                    </span>
                                </td>

                                <td>

                                    @if ($pricing->device_type == 'MASTER')
                                        <span class="badge bg-primary">
                                            MASTER
                                        </span>
                                    @else
                                        <span class="badge bg-info">
                                            SLAVE
                                        </span>
                                    @endif

                                </td>

                                <td class="text-end fw-bold">

                                    {{ number_format($pricing->amount, 2) }}

                                </td>

                                <td>

                                    @if ($pricing->status)
                                        <span class="badge bg-success">
                                            ACTIVE
                                        </span>
                                    @else
                                        <span class="badge bg-danger">
                                            DISABLED
                                        </span>
                                    @endif

                                </td>

                                <td class="text-center">

                                    <button class="btn btn-primary btn-sm edit-pricing" data-id="{{ $pricing->id }}"
                                        data-cargo_type="{{ $pricing->cargo_type }}"
                                        data-device_type="{{ $pricing->device_type }}" data-amount="{{ $pricing->amount }}"
                                        data-status="{{ $pricing->status }}" data-bs-toggle="modal"
                                        data-bs-target="#editPricingModal">

                                        <i class="bi bi-pencil"></i>

                                    </button>

                                </td>

                            </tr>
                        @endforeach

                    </tbody>

                </table>

            </div>

        </div>

    </div>

    @include('pricings.partials.add-pricing-modal')
    @include('pricings.partials.edit-pricing-modal')

@endsection

@push('scripts')
    <script>
        $(function() {

            $('#pricingTable').DataTable({

                order: [
                    [1, 'asc'],
                    [2, 'asc']
                ],

                pageLength: 25,

            });

        });

        $(document).on(
            'click',
            '.edit-pricing',
            function() {

                let id = $(this).data('id');

                $('#editPricingForm').attr(
                    'action',
                    '/pricings/' + id
                );

                $('#edit_cargo_type').val(
                    $(this).data('cargo_type')
                );

                $('#edit_device_type').val(
                    $(this).data('device_type')
                );

                $('#edit_amount').val(
                    $(this).data('amount')
                );

                $('#edit_status').val(
                    $(this).data('status')
                );

            }
        );
    </script>
@endpush
