@extends('admin.layouts.app')

@section('title', 'ثبت حواله داخلی')

@section('content')
<div class="card">
    <div class="card-header">
        <h5 class="mb-0">ثبت حواله داخلی</h5>
    </div>
    <form method="POST" action="{{ route('admin.bills.store-internal') }}" enctype="multipart/form-data">
        @csrf
        <div class="card-body">
            <div class="row g-3">
                <div class="col-md-4">
                    <label class="form-label">بار</label>
                    <select name="load_id" class="form-select" required>
                        @foreach($loads as $load)
                            <option value="{{ $load->id }}">#{{ $load->id }} - {{ $load->title }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-4"><label class="form-label">شماره حواله</label><input class="form-control" name="dispatch_order_number" required></div>
                <div class="col-md-4"><label class="form-label">تاریخ صدور</label><input type="datetime-local" class="form-control" name="issue_date" required></div>
                <div class="col-md-3"><label class="form-label">زمان صدور</label><input class="form-control" name="issue_time" value="{{ now()->format('H:i') }}" required></div>
                <div class="col-md-3"><label class="form-label">شروع حمل</label><input type="datetime-local" class="form-control" name="transport_start_date" required></div>
                <div class="col-md-3"><label class="form-label">تحویل</label><input type="datetime-local" class="form-control" name="delivery_date" required></div>
                <div class="col-md-3"><label class="form-label">نوع حمل</label><select class="form-select" name="transport_type" required><option value="domestic">داخلی</option><option value="export">صادرات</option><option value="import">واردات</option><option value="transit">ترانزیت</option></select></div>
                <div class="col-md-4"><label class="form-label">نوع بار</label><select class="form-select" name="cargo_kind" required><option value="general">عمومی</option><option value="dangerous">خطرناک</option><option value="perishable">فاسدشدنی</option><option value="oil">نفتی</option></select></div>
                <div class="col-md-4"><label class="form-label">نوع سرویس</label><select class="form-select" name="service_type" required><option value="retail">خرده بار</option><option value="full_truck">دربستی</option><option value="combined">ترکیبی</option></select></div>
                <div class="col-md-4"><label class="form-label">شرکت حمل</label><select class="form-select" name="freight_company_id"><option value="">--</option>@foreach($companies as $company)<option value="{{ $company->id }}">{{ $company->company_name }}</option>@endforeach</select></div>

                <div class="col-12"><hr><h6>شرکت حمل</h6></div>
                <div class="col-md-3"><input class="form-control" name="company_name" placeholder="نام شرکت" required></div>
                <div class="col-md-3"><input class="form-control" name="company_national_id" placeholder="شناسه ملی" required></div>
                <div class="col-md-3"><input class="form-control" name="company_economic_code" placeholder="کد اقتصادی"></div>
                <div class="col-md-3"><input class="form-control" name="company_phone" placeholder="شماره تماس"></div>
                <div class="col-md-6"><input class="form-control" name="company_ceo_name" placeholder="نام مدیرعامل"></div>

                <div class="col-12"><hr><h6>راننده</h6></div>
                <div class="col-md-3"><input class="form-control" name="first_driver_name" placeholder="نام راننده اول" required></div>
                <div class="col-md-3"><input class="form-control" name="first_driver_national_code" placeholder="کد ملی راننده اول" required></div>
                <div class="col-md-3"><input class="form-control" name="first_driver_mobile" placeholder="موبایل راننده اول" required></div>
                <div class="col-md-3"><input class="form-control" name="first_driver_account_number" placeholder="شماره حساب"></div>
                <div class="col-md-6">
                    <label class="form-label">عکس راننده اول</label>
                    <input type="file" class="form-control" name="first_driver_photo" accept="image/*">
                    <small class="text-muted">فرمت مجاز: jpg/png/webp - حداکثر 3 مگابایت</small>
                </div>
                <div class="col-md-4"><input class="form-control" name="second_driver_name" placeholder="نام راننده دوم"></div>
                <div class="col-md-4"><input class="form-control" name="second_driver_national_code" placeholder="کد ملی راننده دوم"></div>
                <div class="col-md-4"><input class="form-control" name="second_driver_mobile" placeholder="موبایل راننده دوم"></div>
                <div class="col-md-6">
                    <label class="form-label">عکس راننده دوم</label>
                    <input type="file" class="form-control" name="second_driver_photo" accept="image/*">
                    <small class="text-muted">در صورت عدم وجود راننده دوم می‌توانید خالی بگذارید.</small>
                </div>

                <div class="col-12"><hr><h6>ناوگان</h6></div>
                <div class="col-md-3"><input class="form-control" name="vehicle_type" placeholder="نوع وسیله" required></div>
                <div class="col-md-3"><input class="form-control" name="transport_license_plate" placeholder="شماره پلاک" required></div>
                <div class="col-md-3"><input class="form-control" name="vehicle_smart_card_no" placeholder="کارت هوشمند"></div>
                <div class="col-md-3"><input type="number" step="0.01" class="form-control" name="vehicle_allowed_capacity" placeholder="ظرفیت مجاز" required></div>
                <div class="col-md-4"><input type="number" step="0.01" class="form-control" name="vehicle_gross_weight_limit" placeholder="وزن ناخالص مجاز"></div>
                <div class="col-md-4"><input class="form-control" name="vehicle_owner_national_code" placeholder="کد ملی مالک"></div>

                <div class="col-12"><hr><h6>فرستنده / گیرنده / مسیر</h6></div>
                <div class="col-md-4"><input class="form-control" name="sender_name" placeholder="نام فرستنده" required></div>
                <div class="col-md-4"><input class="form-control" name="sender_national_id" placeholder="کد ملی/شناسه" required></div>
                <div class="col-md-4"><input class="form-control" name="sender_phone" placeholder="تلفن" required></div>
                <div class="col-md-8"><input class="form-control" name="sender_address" placeholder="آدرس فرستنده" required></div>
                <div class="col-md-4"><input class="form-control" name="sender_postal_code" placeholder="کدپستی فرستنده" required></div>
                <div class="col-md-4"><input class="form-control" name="receiver_name_full" placeholder="نام گیرنده" required></div>
                <div class="col-md-4"><input class="form-control" name="receiver_national_id" placeholder="کد ملی گیرنده" required></div>
                <div class="col-md-4"><input class="form-control" name="receiver_mobile" placeholder="موبایل گیرنده" required></div>
                <div class="col-md-8"><input class="form-control" name="receiver_address_full" placeholder="آدرس گیرنده" required></div>
                <div class="col-md-4"><input class="form-control" name="receiver_postal_code" placeholder="کدپستی گیرنده" required></div>
                <div class="col-md-4"><input class="form-control" name="origin_postal_code" placeholder="کدپستی مبدا" required></div>
                <div class="col-md-4"><input class="form-control" name="destination_postal_code" placeholder="کدپستی مقصد" required></div>
                <div class="col-md-4"><input type="number" step="0.01" class="form-control" name="distance_km" placeholder="مسافت کیلومتر" required></div>

                <div class="col-12"><hr><h6>اطلاعات مالی</h6></div>
                <div class="col-md-2"><input type="number" step="0.01" class="form-control" name="freight_amount" placeholder="کرایه" required></div>
                <div class="col-md-2"><input type="number" step="0.01" class="form-control" name="insurance_amount" placeholder="بیمه" required></div>
                <div class="col-md-2"><input type="number" step="0.01" class="form-control" name="tax_amount" placeholder="مالیات" required></div>
                <div class="col-md-2"><input type="number" step="0.01" class="form-control" name="toll_amount" placeholder="عوارض" required></div>
                <div class="col-md-4"><input type="number" step="0.01" class="form-control" name="total_amount" placeholder="جمع کل" required></div>

                <div class="col-12"><hr><h6>توضیحات و امضا</h6></div>
                <div class="col-md-4"><input class="form-control" name="payment_method" placeholder="نحوه پرداخت"></div>
                <div class="col-md-8"><input class="form-control" name="contract_terms" placeholder="شرایط قرارداد"></div>
                <div class="col-md-12"><textarea class="form-control" name="transport_notes" placeholder="توضیحات حمل"></textarea></div>
                <div class="col-md-4"><input class="form-control" name="digital_signature" placeholder="امضای دیجیتال شرکت" required></div>
                <div class="col-md-4"><input class="form-control" name="driver_signature" placeholder="امضای راننده" required></div>
                <div class="col-md-2"><select class="form-select" name="organization_approval" required><option value="1">تایید سازمان</option><option value="0">عدم تایید</option></select></div>
                <div class="col-md-2"><input class="form-control" name="organization_confirmation_code" placeholder="کد تایید" required></div>

                <div class="col-md-3"><input class="form-control" name="container_no" placeholder="شماره کانتینر"></div>
                <div class="col-md-3"><input class="form-control" name="weighbridge_code" placeholder="کد باسکول"></div>
                <div class="col-md-3"><input class="form-control" name="weighbridge_receipt_no" placeholder="شماره قبض باسکول"></div>
                <div class="col-md-3"><input class="form-control" name="order_type" placeholder="نوع سفارش حمل"></div>

                <div class="col-12"><hr><h6>ردیف های کالایی</h6></div>
                <div class="col-12">
                    <div id="cargo-items-container" class="row g-2">
                        <div class="cargo-row row g-2 align-items-center mb-2" data-index="0">
                            <div class="col-md-2"><input class="form-control" name="cargo_items[0][cargo_name]" placeholder="نام کالا" required></div>
                            <div class="col-md-2"><input class="form-control" name="cargo_items[0][cargo_code]" placeholder="کد کالا" required></div>
                            <div class="col-md-2"><input class="form-control" name="cargo_items[0][packaging_type]" placeholder="بسته بندی"></div>
                            <div class="col-md-1"><input type="number" class="form-control" name="cargo_items[0][quantity]" placeholder="تعداد" min="1" required></div>
                            <div class="col-md-1"><input type="number" step="0.01" class="form-control" name="cargo_items[0][weight]" placeholder="وزن" min="0" required></div>
                            <div class="col-md-2"><input type="number" step="0.01" class="form-control" name="cargo_items[0][value]" placeholder="ارزش" min="0" required></div>
                            <div class="col-md-2"><select class="form-select" name="cargo_items[0][oil_type]"><option value="non_oil">غیر نفتی</option><option value="oil">نفتی</option></select></div>
                            <div class="col-md-2 d-grid"><button type="button" class="btn btn-outline-danger btn-sm remove-cargo-row" disabled>حذف ردیف</button></div>
                        </div>
                    </div>
                </div>
                <div class="col-12">
                    <button type="button" id="add-cargo-row" class="btn btn-outline-primary btn-sm">
                        <i class="fas fa-plus me-1"></i>
                        افزودن ردیف کالا
                    </button>
                </div>
            </div>
        </div>
        <div class="card-footer d-flex justify-content-between align-items-center">
            <small class="text-muted">در پنل، این سند به عنوان حواله نمایش داده می‌شود و پس از تکمیل سرویس RMTO به بارنامه رسمی تبدیل خواهد شد.</small>
            <button class="btn btn-primary" type="submit">ثبت حواله داخلی</button>
        </div>
    </form>
</div>
@endsection

@push('scripts')
<script>
    (function () {
        const container = document.getElementById('cargo-items-container');
        const addBtn = document.getElementById('add-cargo-row');
        let index = 1;

        function rowTemplate(i) {
            return `
                <div class="cargo-row row g-2 align-items-center mb-2" data-index="${i}">
                    <div class="col-md-2"><input class="form-control" name="cargo_items[${i}][cargo_name]" placeholder="نام کالا" required></div>
                    <div class="col-md-2"><input class="form-control" name="cargo_items[${i}][cargo_code]" placeholder="کد کالا" required></div>
                    <div class="col-md-2"><input class="form-control" name="cargo_items[${i}][packaging_type]" placeholder="بسته بندی"></div>
                    <div class="col-md-1"><input type="number" class="form-control" name="cargo_items[${i}][quantity]" placeholder="تعداد" min="1" required></div>
                    <div class="col-md-1"><input type="number" step="0.01" class="form-control" name="cargo_items[${i}][weight]" placeholder="وزن" min="0" required></div>
                    <div class="col-md-2"><input type="number" step="0.01" class="form-control" name="cargo_items[${i}][value]" placeholder="ارزش" min="0" required></div>
                    <div class="col-md-2"><select class="form-select" name="cargo_items[${i}][oil_type]"><option value="non_oil">غیر نفتی</option><option value="oil">نفتی</option></select></div>
                    <div class="col-md-2 d-grid"><button type="button" class="btn btn-outline-danger btn-sm remove-cargo-row">حذف ردیف</button></div>
                </div>
            `;
        }

        addBtn.addEventListener('click', function () {
            container.insertAdjacentHTML('beforeend', rowTemplate(index));
            index += 1;
        });

        container.addEventListener('click', function (event) {
            if (event.target.classList.contains('remove-cargo-row')) {
                const rows = container.querySelectorAll('.cargo-row');
                if (rows.length > 1) {
                    event.target.closest('.cargo-row').remove();
                }
            }
        });
    })();
</script>
@endpush
