@extends('admin.layouts.app')

@section('title', $pageTitle)

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">{{ $pageTitle }}</h3>
                    <div class="card-tools">
                        <a href="{{ route('admin.new-certificates-templates.show', $template->id) }}" class="btn btn-info">
                            <i class="fas fa-eye"></i> View
                        </a>
                        <a href="{{ route('admin.new-certificates-templates.index') }}" class="btn btn-secondary">
                            <i class="fas fa-arrow-left"></i> Back to List
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    <form action="{{ route('admin.new-certificates-templates.update', $template->id) }}" method="POST" enctype="multipart/form-data">
                        @csrf
                        @method('PUT')

                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="name">Template Name <span class="text-danger">*</span></label>
                                    <input type="text"
                                           class="form-control @error('name') is-invalid @enderror"
                                           id="name"
                                           name="name"
                                           value="{{ old('name', $template->name) }}"
                                           required>
                                    @error('name')
                                        <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>

                                <div class="form-group">
                                    <label for="background_image">Background Image</label>
                                    <input type="file"
                                           class="form-control-file @error('background_image') is-invalid @enderror"
                                           id="background_image"
                                           name="background_image"
                                           accept="image/*">
                                    <small class="form-text text-muted">
                                        Leave empty to keep current image. Supported formats: JPG, JPEG, PNG. Max size: 2MB.
                                    </small>
                                    @error('background_image')
                                        <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>

                                @if($template->image_path)
                                    <div class="form-group">
                                        <label>Current Image</label>
                                        <div class="text-center">
                                            <img src="{{ Storage::url($template->image_path) }}"
                                                 alt="{{ $template->name }}"
                                                 style="max-width: 200px; max-height: 120px; object-fit: cover;"
                                                 class="img-thumbnail">
                                        </div>
                                    </div>
                                @endif
                            </div>

                            <div class="col-md-6">
                                <div class="form-group">
                                    <label>Text Positioning</label>
                                    <div class="row">
                                        <div class="col-6">
                                            <label for="name_x">Name X Position <span class="text-danger">*</span></label>
                                            <input type="number"
                                                   class="form-control @error('name_x') is-invalid @enderror"
                                                   id="name_x"
                                                   name="name_x"
                                                   value="{{ old('name_x', $template->name_x) }}"
                                                   min="0"
                                                   required>
                                            @error('name_x')
                                                <span class="invalid-feedback">{{ $message }}</span>
                                            @enderror
                                        </div>
                                        <div class="col-6">
                                            <label for="name_y">Name Y Position <span class="text-danger">*</span></label>
                                            <input type="number"
                                                   class="form-control @error('name_y') is-invalid @enderror"
                                                   id="name_y"
                                                   name="name_y"
                                                   value="{{ old('name_y', $template->name_y) }}"
                                                   min="0"
                                                   required>
                                            @error('name_y')
                                                <span class="invalid-feedback">{{ $message }}</span>
                                            @enderror
                                        </div>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <div class="row">
                                        <div class="col-6">
                                            <label for="date_x">Date X Position <span class="text-danger">*</span></label>
                                            <input type="number"
                                                   class="form-control @error('date_x') is-invalid @enderror"
                                                   id="date_x"
                                                   name="date_x"
                                                   value="{{ old('date_x', $template->date_x) }}"
                                                   min="0"
                                                   required>
                                            @error('date_x')
                                                <span class="invalid-feedback">{{ $message }}</span>
                                            @enderror
                                        </div>
                                        <div class="col-6">
                                            <label for="date_y">Date Y Position <span class="text-danger">*</span></label>
                                            <input type="number"
                                                   class="form-control @error('date_y') is-invalid @enderror"
                                                   id="date_y"
                                                   name="date_y"
                                                   value="{{ old('date_y', $template->date_y) }}"
                                                   min="0"
                                                   required>
                                            @error('date_y')
                                                <span class="invalid-feedback">{{ $message }}</span>
                                            @enderror
                                        </div>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <div class="row">
                                        <div class="col-6">
                                            <label for="serial_x">Serial X Position <span class="text-danger">*</span></label>
                                            <input type="number"
                                                   class="form-control @error('serial_x') is-invalid @enderror"
                                                   id="serial_x"
                                                   name="serial_x"
                                                   value="{{ old('serial_x', $template->serial_x) }}"
                                                   min="0"
                                                   required>
                                            @error('serial_x')
                                                <span class="invalid-feedback">{{ $message }}</span>
                                            @enderror
                                        </div>
                                        <div class="col-6">
                                            <label for="serial_y">Serial Y Position <span class="text-danger">*</span></label>
                                            <input type="number"
                                                   class="form-control @error('serial_y') is-invalid @enderror"
                                                   id="serial_y"
                                                   name="serial_y"
                                                   value="{{ old('serial_y', $template->serial_y) }}"
                                                   min="0"
                                                   required>
                                            @error('serial_y')
                                                <span class="invalid-feedback">{{ $message }}</span>
                                            @enderror
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-save"></i> Update Template
                            </button>
                            <a href="{{ route('admin.new-certificates-templates.show', $template->id) }}" class="btn btn-secondary">
                                Cancel
                            </a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection


