@extends('layouts.admin')

@section('title', 'Add Store / Warehouse')

@section('content')
<x-admin.form-page
    title="Add Store / Warehouse"
    :crumbs="[['label' => 'Inventory', 'url' => null], ['label' => 'Locations', 'url' => route('admin.locations.index')], 'Create']"
    :back-url="route('admin.locations.index')"
    :action="route('admin.locations.store')"
    form-id="location-create-form"
    submit-label="Save Location"
>
    <input type="hidden" name="company_id" value="{{ $company->id }}">

    <section class="pp-card">
        <div class="pp-card__head">
            <span class="pp-card__icon"><i class="bi bi-geo-alt"></i></span>
            <div>
                <h2 class="pp-card__title">Location Details</h2>
                <p class="pp-card__sub">Code, type, address and contact information</p>
            </div>
        </div>
        <div class="pp-card__body">
            <div class="pp-grid pp-grid--3">
                <div class="pp-field">
                    <label for="code">Code <span class="pp-req">*</span></label>
                    <input type="text" name="code" id="code" class="form-control" required value="{{ old('code') }}" placeholder="ST-HYD-01">
                </div>
                <div class="pp-field">
                    <label for="name">Name <span class="pp-req">*</span></label>
                    <input type="text" name="name" id="name" class="form-control" required value="{{ old('name') }}">
                </div>
                <div class="pp-field">
                    <label for="location-type">Type <span class="pp-req">*</span></label>
                    <select name="type" id="location-type" class="form-select" required>
                        <option value="store" @selected(old('type')==='store')>Store</option>
                        <option value="warehouse" @selected(old('type')==='warehouse')>Warehouse</option>
                    </select>
                </div>
                <div class="pp-field">
                    <label for="city">City</label>
                    <input type="text" name="city" id="city" class="form-control" value="{{ old('city') }}">
                </div>
                <div class="pp-field">
                    <label for="pincode">Pincode</label>
                    <input type="text" name="pincode" id="pincode" class="form-control" value="{{ old('pincode') }}">
                </div>
                <div class="pp-field">
                    <label for="priority">Priority</label>
                    <input type="number" name="priority" id="priority" class="form-control" value="{{ old('priority', 100) }}" min="1">
                    <p class="pp-hint">Lower = preferred for allocation</p>
                </div>
            </div>
            <div class="pp-field mt-3">
                <label for="address_line1">Address</label>
                <input type="text" name="address_line1" id="address_line1" class="form-control" value="{{ old('address_line1') }}">
            </div>
            <div class="pp-field mt-3">
                <label for="phone">Phone</label>
                <input type="text" name="phone" id="phone" class="form-control" value="{{ old('phone') }}">
            </div>

            <div class="pp-toggles mt-3">
                <label class="pp-toggle" for="a1">
                    <input type="hidden" name="is_active" value="0">
                    <input type="checkbox" name="is_active" value="1" class="form-check-input" role="switch" id="a1" {{ old('is_active', true) ? 'checked' : '' }}>
                    Active
                </label>
                <label class="pp-toggle" for="a2" id="fulfills-online-wrap">
                    <input type="checkbox" name="fulfills_online" value="1" class="form-check-input" role="switch" id="a2" @checked(old('fulfills_online', true))>
                    Fulfills Online
                </label>
                <label class="pp-toggle" for="a3">
                    <input type="checkbox" name="allows_pickup" value="1" class="form-check-input" role="switch" id="a3" @checked(old('allows_pickup'))>
                    Allows Pickup
                </label>
                <label class="pp-toggle" for="a4">
                    <input type="checkbox" name="is_default" value="1" class="form-check-input" role="switch" id="a4" @checked(old('is_default'))>
                    Company Default
                </label>
            </div>
            <p class="pp-hint mt-2">Online website stock uses <strong>stores</strong> with Fulfills Online only. Warehouses cannot fulfill online — transfer to a store first. The Fulfills Online checkbox is hidden when Type is Warehouse.</p>
        </div>
    </section>
</x-admin.form-page>

@push('scripts')
<script>
(function () {
  const typeEl = document.getElementById('location-type');
  const wrap = document.getElementById('fulfills-online-wrap');
  const box = document.getElementById('a2');
  function sync() {
    const isWarehouse = typeEl && typeEl.value === 'warehouse';
    if (wrap) wrap.style.display = isWarehouse ? 'none' : '';
    if (box && isWarehouse) box.checked = false;
  }
  if (typeEl) {
    typeEl.addEventListener('change', sync);
    sync();
  }
})();
</script>
@endpush
@endsection
