@extends('layouts.layoutMaster')

@section('title', __('Edit Product'))

@section('vendor-style')
@vite([
    'resources/assets/vendor/libs/select2/select2.scss',
    'resources/assets/vendor/libs/dropzone/dropzone.scss'
])
@endsection

@section('vendor-script')
@vite([
    'resources/assets/vendor/libs/select2/select2.js',
    'resources/assets/vendor/libs/dropzone/dropzone.js'
])
@endsection

@section('page-script')
<script>
const pageData = {
    urls: {
        uploadImage: '{{ route("systemcore.products.upload-image") }}'
    },
    product: {
        image: @json($product->thumbnail ? asset('storage/' . $product->thumbnail) : null),
        imagePath: @json($product->thumbnail)
    }
};
</script>
@vite(['Modules/SystemCore/resources/assets/js/product-form.js'])
@endsection

@section('content')
<x-breadcrumb
    :title="__('Edit Product')"
    :breadcrumbs="[
        ['name' => __('System Core'), 'url' => route('systemcore.dashboard')],
        ['name' => __('Products'), 'url' => route('systemcore.products.index')],
        ['name' => __('Edit')]
    ]"
/>

<div class="row">
    <div class="col-md-12">
        <form id="productForm" action="{{ route('systemcore.products.update', $product->id) }}" method="POST">
            @csrf
            @method('PUT')

            <div class="card mb-4">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">{{ __('Product Information') }}</h5>
                    <span class="badge bg-label-primary">{{ $product->product_code }}</span>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <div class="ai-context"
                                 data-ai-context="Product name: {{ $product->name }}"
                                 data-ai-field-type="title"
                                 data-ai-context-sku="{{ $product->sku }}">
                                <label for="name" class="form-label">{{ __('Product Name') }} <span class="text-danger">*</span></label>
                                <input type="text" class="form-control ai-field @error('name') is-invalid @enderror"
                                       id="name" name="name" value="{{ old('name', $product->name) }}" required>
                                @error('name')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>

                        <div class="col-md-6 mb-3">
                            <label for="product_code" class="form-label">{{ __('Product Code') }}</label>
                            <input type="text" class="form-control @error('product_code') is-invalid @enderror"
                                   id="product_code" name="product_code" value="{{ old('product_code', $product->product_code) }}">
                            @error('product_code')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="col-md-6 mb-3">
                            <label for="sku" class="form-label">{{ __('SKU') }}</label>
                            <input type="text" class="form-control @error('sku') is-invalid @enderror"
                                   id="sku" name="sku" value="{{ old('sku', $product->sku) }}">
                            @error('sku')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="col-md-6 mb-3">
                            <label for="barcode" class="form-label">{{ __('Barcode') }}</label>
                            <input type="text" class="form-control @error('barcode') is-invalid @enderror"
                                   id="barcode" name="barcode" value="{{ old('barcode', $product->barcode) }}">
                            @error('barcode')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="col-md-6 mb-3">
                            <label for="category_id" class="form-label">{{ __('Category') }}</label>
                            <select id="category_id" name="category_id" class="select2 form-select @error('category_id') is-invalid @enderror"
                                    data-placeholder="{{ __('Select a category') }}">
                                <option value="">{{ __('Select a category') }}</option>
                                @foreach($categories as $category)
                                    <option value="{{ $category->id }}" {{ old('category_id', $product->category_id) == $category->id ? 'selected' : '' }}>
                                        {{ $category->name }}
                                    </option>
                                @endforeach
                            </select>
                            @error('category_id')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="col-md-6 mb-3">
                            <label for="unit_of_measure" class="form-label">{{ __('Unit of Measure') }}</label>
                            @if($units->isNotEmpty())
                            <select id="unit_of_measure" name="unit_of_measure" class="select2 form-select @error('unit_of_measure') is-invalid @enderror"
                                    data-placeholder="{{ __('Select a unit') }}">
                                <option value="">{{ __('Select a unit') }}</option>
                                @foreach($units as $unit)
                                    <option value="{{ $unit->code }}" {{ old('unit_of_measure', $product->unit_of_measure) == $unit->code ? 'selected' : '' }}>
                                        {{ $unit->name }} ({{ $unit->code }})
                                    </option>
                                @endforeach
                            </select>
                            @else
                            <input type="text" class="form-control @error('unit_of_measure') is-invalid @enderror"
                                   id="unit_of_measure" name="unit_of_measure" value="{{ old('unit_of_measure', $product->unit_of_measure) }}"
                                   placeholder="{{ __('e.g., pcs, kg, ltr') }}">
                            @endif
                            @error('unit_of_measure')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="col-md-6 mb-3">
                            <label for="brand" class="form-label">{{ __('Brand') }}</label>
                            <input type="text" class="form-control @error('brand') is-invalid @enderror"
                                   id="brand" name="brand" value="{{ old('brand', $product->brand) }}">
                            @error('brand')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="col-md-6 mb-3">
                            <label for="status" class="form-label">{{ __('Status') }} <span class="text-danger">*</span></label>
                            <select id="status" name="status" class="form-select @error('status') is-invalid @enderror" required>
                                <option value="active" {{ old('status', $product->status?->value ?? $product->status) == 'active' ? 'selected' : '' }}>{{ __('Active') }}</option>
                                <option value="inactive" {{ old('status', $product->status?->value ?? $product->status) == 'inactive' ? 'selected' : '' }}>{{ __('Inactive') }}</option>
                                <option value="discontinued" {{ old('status', $product->status?->value ?? $product->status) == 'discontinued' ? 'selected' : '' }}>{{ __('Discontinued') }}</option>
                            </select>
                            @error('status')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="col-md-12 mb-3">
                            <div class="ai-context"
                                 data-ai-context="Product: {{ $product->name }}"
                                 data-ai-field-type="product_description"
                                 data-ai-context-sku="{{ $product->sku }}"
                                 data-ai-context-brand="{{ $product->brand }}">
                                <label for="description" class="form-label">{{ __('Description') }}</label>
                                <textarea class="form-control ai-field @error('description') is-invalid @enderror"
                                          id="description" name="description" rows="3">{{ old('description', $product->description) }}</textarea>
                                @error('description')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="mb-0">{{ __('Pricing Information') }}</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-4 mb-3">
                            <label for="base_price" class="form-label">{{ __('Base Price') }} <span class="text-danger">*</span></label>
                            <input type="number" step="0.01" class="form-control @error('base_price') is-invalid @enderror"
                                   id="base_price" name="base_price" value="{{ old('base_price', $product->base_price) }}" required>
                            @error('base_price')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="col-md-4 mb-3">
                            <label for="cost_price" class="form-label">{{ __('Cost Price') }}</label>
                            <input type="number" step="0.01" class="form-control @error('cost_price') is-invalid @enderror"
                                   id="cost_price" name="cost_price" value="{{ old('cost_price', $product->cost_price) }}">
                            @error('cost_price')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="col-md-4 mb-3">
                            <label for="selling_price" class="form-label">{{ __('Selling Price') }} <span class="text-danger">*</span></label>
                            <input type="number" step="0.01" class="form-control @error('selling_price') is-invalid @enderror"
                                   id="selling_price" name="selling_price" value="{{ old('selling_price', $product->selling_price) }}" required>
                            @error('selling_price')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>
                    </div>
                </div>
            </div>

            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="mb-0">{{ __('Stock & Tracking') }}</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-4 mb-3">
                            <label for="min_stock_level" class="form-label">{{ __('Min Stock Level') }}</label>
                            <input type="number" class="form-control @error('min_stock_level') is-invalid @enderror"
                                   id="min_stock_level" name="min_stock_level" value="{{ old('min_stock_level', $product->min_stock_level) }}">
                            @error('min_stock_level')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="col-md-4 mb-3">
                            <label for="max_stock_level" class="form-label">{{ __('Max Stock Level') }}</label>
                            <input type="number" class="form-control @error('max_stock_level') is-invalid @enderror"
                                   id="max_stock_level" name="max_stock_level" value="{{ old('max_stock_level', $product->max_stock_level) }}">
                            @error('max_stock_level')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="col-md-4 mb-3">
                            <label for="reorder_level" class="form-label">{{ __('Reorder Level') }}</label>
                            <input type="number" class="form-control @error('reorder_level') is-invalid @enderror"
                                   id="reorder_level" name="reorder_level" value="{{ old('reorder_level', $product->reorder_level) }}">
                            @error('reorder_level')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="col-md-4 mb-3">
                            <div class="form-check form-switch mt-4">
                                <input class="form-check-input" type="checkbox" id="is_trackable" name="is_trackable"
                                       {{ old('is_trackable', $product->is_trackable) ? 'checked' : '' }}>
                                <label class="form-check-label" for="is_trackable">{{ __('Track Inventory') }}</label>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="mb-0">{{ __('Product Image') }}</h5>
                </div>
                <div class="card-body">
                    @if($product->thumbnail)
                    <div id="currentImage" class="mb-3">
                        <img src="{{ asset('storage/' . $product->thumbnail) }}" alt="{{ $product->name }}"
                             class="rounded" width="150" height="150" style="object-fit: cover;">
                        <p class="text-muted mt-1">{{ __('Upload a new image to replace the current one') }}</p>
                    </div>
                    @endif
                    <div class="dropzone" id="productImageDropzone"></div>
                    <input type="hidden" name="thumbnail" id="product_image_path" value="{{ $product->thumbnail }}">
                    @error('thumbnail')
                        <div class="text-danger mt-1">{{ $message }}</div>
                    @enderror
                </div>
            </div>

            <div class="card">
                <div class="card-body">
                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-primary">
                            <i class="bx bx-save me-1"></i> {{ __('Update Product') }}
                        </button>
                        <a href="{{ route('systemcore.products.index') }}" class="btn btn-label-secondary">
                            <i class="bx bx-x me-1"></i> {{ __('Cancel') }}
                        </a>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
@endsection
