@extends('layouts.admin')

@section('admin-content')
    <div class="container-fluid">

        {{-- Header --}}
        <div class="row mb-4">
            <div class="col-12">
                <div class="card border-0 shadow-sm">
                    <div class="card-body d-flex justify-content-between align-items-center">
                        <div>
                            <h1 class="h3 mb-1 text-dark">
                                <i class="fas fa-edit text-primary me-2"></i>
                                {{ trans('app.Edit Customization & Addon') }}
                            </h1>
                            <p class="text-muted mb-0">
                                {{ trans('app.Update a Customization & Addon') }}
                            </p>
                        </div>

                        <a href="{{ url()->previous() }}" class="btn btn-outline-secondary">
                            <i class="fas fa-arrow-left me-1"></i>
                            {{ trans('app.Back') }}
                        </a>
                    </div>
                </div>
            </div>
        </div>

        {{-- Validation Errors --}}
        @if ($errors->any())
            <div class="alert alert-danger">
                <ul class="mb-0">
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif

        {{-- Form --}}
        <form method="POST"
            action="{{ route('admin.products.client-versions.update', [
                'product' => $product->id,
                'clientVersion' => $clientVersion->id,
            ]) }}"
            class="needs-validation" novalidate>
            @csrf
            @method('PUT')

            {{-- REQUIRED CONTEXT --}}
            <input type="hidden" name="product_id" value="{{ $product->id }}">
            <input type="hidden" name="category_id" value="{{ $clientVersion->category_id }}">

            <div class="row">

                {{-- LEFT --}}
                <div class="col-lg-8">
                    <div class="card mb-4">
                        <div class="card-header bg-primary text-white">
                            <h5 class="card-title mb-0"> <i class="fas fa-code-branch me-2"></i>
                                {{ trans('app.Version Details') }}
                            </h5>
                        </div>

                        <div class="card-body">

                            {{-- Title --}}
                            <div class="mb-3">
                                <label class="form-label">
                                    {{ trans('app.Title') }}
                                    <span class="text-danger">*</span>
                                </label>
                                <input type="text" name="title"
                                    class="form-control @error('title') is-invalid @enderror"
                                    value="{{ old('title', $clientVersion->title) }}" required>
                                @error('title')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>

                            {{-- Version --}}
                            <div class="mb-3">
                                <label class="form-label">
                                    {{ trans('app.Version') }}
                                </label>
                                <input type="text" name="version" class="form-control"
                                    value="{{ old('version', $clientVersion->version) }}" disabled>
                            </div>

                            {{-- Tag --}}
                            <div class="mb-3">
                                <label class="form-label">
                                    {{ trans('app.Tag') }}
                                    <span class="text-danger">*</span>
                                </label>
                                <select name="tag" class="form-select @error('tag') is-invalid @enderror" required>
                                    <option value="">— {{ trans('app.Select') }} —</option>
                                    <option value="addon"
                                        {{ old('tag', $clientVersion->tag) === 'addon' ? 'selected' : '' }}>
                                        {{ trans('app.Addon') }}
                                    </option>
                                    <option value="customization"
                                        {{ old('tag', $clientVersion->tag) === 'customization' ? 'selected' : '' }}>
                                        {{ trans('app.Customization') }}
                                    </option>
                                </select>
                                @error('tag')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>

                            {{-- Description --}}
                            <div class="mb-3">
                                <label class="form-label">{{ trans('app.Description') }}<span class="text-danger">*</span></label>
                                <textarea name="description" rows="4" class="form-control">{{ old('description', $clientVersion->description) }}</textarea>
                            </div>

                            {{-- Note --}}
                            <div class="mb-3">
                                <label class="form-label">{{ trans('app.Note') }}</label>
                                <textarea name="note" rows="3" class="form-control">{{ old('note', $clientVersion->note) }}</textarea>
                            </div>
                        </div>
                    </div>
                </div>

                {{-- RIGHT --}}
                <div class="col-lg-4">
                    <div class="card mb-4">
                        <div class="card-body">

                            {{-- Cost --}}
                            <div class="mb-3">
                                <label class="form-label">{{ trans('app.Cost') }}</label>
                                

                                <div class="input-group">
                                <span class="input-group-text">$</span>

                                <input type="number"
                                    step="0.01"
                                    name="cost"
                                    class="form-control"
                                    value="{{ old('cost', $clientVersion->cost) }}">

                            </div>
                            </div>

                            {{-- Timeline --}}
                            @php
                                $timelineNumber = '';
                                $timelineUnit = '';

                                if (old('timeline_number')) {
                                    $timelineNumber = old('timeline_number');
                                } elseif (!empty($clientVersion->timeline)) {
                                    $parts = explode(' ', $clientVersion->timeline);
                                    $timelineNumber = $parts[0] ?? '';
                                    $timelineUnit = $parts[1] ?? '';
                                }

                                if (old('timeline_unit')) {
                                    $timelineUnit = old('timeline_unit');
                                }
                            @endphp

                            <div class="mb-3">
                                <label class="form-label">{{ __('Timeline') }} <span class="text-danger">*</span></label>
                                <div class="input-group">
                                    <input type="number" name="timeline_number"
                                        class="form-control @error('timeline_number') is-invalid @enderror"
                                        value="{{ $timelineNumber }}" min="1" placeholder="Enter number" required>
                                    <select name="timeline_unit"
                                        class="form-select @error('timeline_unit') is-invalid @enderror" required>
                                        <option value="hours" {{ $timelineUnit == 'hours' ? 'selected' : '' }}>Hours
                                        </option>
                                        <option value="days" {{ $timelineUnit == 'days' ? 'selected' : '' }}>Days
                                        </option>
                                        <option value="weeks" {{ $timelineUnit == 'weeks' ? 'selected' : '' }}>Weeks
                                        </option>
                                    </select>
                                    @error('timeline_number')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                    @error('timeline_unit')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>


                            <button type="submit" class="btn btn-primary w-100">
                                <i class="fas fa-save me-1"></i>
                                {{ trans('app.Update') }}
                            </button>

                        </div>
                    </div>
                </div>

            </div>
        </form>
    </div>
@endsection
