<?php $__env->startSection('heading', $mode === 'edit' ? 'Edit Movie' : 'Add Movie'); ?>

<?php $__env->startSection('content'); ?>
<?php
    $isEdit = $mode === 'edit';
    $storage = app(\App\Services\MediaStorageService::class);
    $posterUrl = $isEdit && $movie->poster_path ? $storage->url($movie->poster_path) : null;
    $backdropUrl = $isEdit && $movie->backdrop_path ? $storage->url($movie->backdrop_path) : null;
?>

<style>
    .movie-editor-premium{--me-bg:#0b0e14;--me-card:#11151d;--me-soft:#171c26;--me-border:rgba(255,255,255,.09);--me-text:#f7f8fc;--me-muted:#8f98aa;--me-red:#ff2447;--me-red2:#d90e31;--me-green:#25d795;color:var(--me-text)}
    .movie-editor-premium *{box-sizing:border-box}.me-stack{display:flex;flex-direction:column;gap:20px}.me-toolbar{display:flex;align-items:center;justify-content:space-between;gap:16px;padding:18px 20px;border:1px solid var(--me-border);border-radius:18px;background:linear-gradient(135deg,rgba(255,36,71,.08),rgba(91,121,255,.04)),var(--me-card);box-shadow:0 18px 45px rgba(0,0,0,.22)}
    .me-title{display:flex;align-items:center;gap:13px;min-width:0}.me-title-icon{width:45px;height:45px;display:grid;place-items:center;flex:none;border-radius:14px;background:linear-gradient(135deg,#ff516c,var(--me-red2));box-shadow:0 12px 28px rgba(255,36,71,.25)}.me-title-icon svg{width:22px;height:22px}.me-title h2{margin:0;font-size:21px;letter-spacing:-.025em}.me-title p{margin:4px 0 0;color:var(--me-muted);font-size:12px}
    .me-actions{display:flex;align-items:center;gap:10px;flex-wrap:wrap}.me-btn{min-height:43px;display:inline-flex;align-items:center;justify-content:center;gap:8px;padding:0 15px;border:1px solid transparent;border-radius:12px;color:#fff;text-decoration:none;font-size:12px;font-weight:800;cursor:pointer;transition:.2s ease}.me-btn svg{width:17px;height:17px}.me-btn:hover{transform:translateY(-2px)}.me-btn-primary{background:linear-gradient(135deg,#ff516c,var(--me-red2));box-shadow:0 12px 27px rgba(255,36,71,.23)}.me-btn-secondary{border-color:var(--me-border);background:rgba(255,255,255,.045);color:#cbd1dc}.me-btn-secondary:hover{background:rgba(255,255,255,.08);color:#fff}.me-btn:disabled{opacity:.68;cursor:not-allowed;transform:none}
    .me-panel{overflow:hidden;border:1px solid var(--me-border);border-radius:20px;background:linear-gradient(145deg,rgba(255,255,255,.025),transparent),var(--me-card);box-shadow:0 22px 55px rgba(0,0,0,.25)}.me-panel-head{display:flex;align-items:flex-start;justify-content:space-between;gap:14px;padding:18px 20px;border-bottom:1px solid rgba(255,255,255,.065)}.me-panel-head h3{margin:0;font-size:15px}.me-panel-head p{margin:4px 0 0;color:var(--me-muted);font-size:11px;line-height:1.5}.me-panel-body{padding:20px}
    .me-import{background:radial-gradient(circle at 100% 0,rgba(255,36,71,.12),transparent 35%),var(--me-card)}.me-import-form{display:grid;grid-template-columns:minmax(0,1fr) auto;gap:12px}.me-grid{display:grid;grid-template-columns:minmax(0,1.65fr) minmax(320px,.8fr);gap:20px;align-items:start}.me-main,.me-side{display:flex;flex-direction:column;gap:20px}.me-row{display:grid;gap:13px}.me-row.two{grid-template-columns:repeat(2,minmax(0,1fr))}.me-row.three{grid-template-columns:repeat(3,minmax(0,1fr))}
    .me-field{display:flex;flex-direction:column;gap:8px;margin-bottom:16px}.me-field:last-child{margin-bottom:0}.me-field label,.me-label{color:#dfe3ec;font-size:12px;font-weight:750}.me-required{color:#ff6078}.me-input,.me-textarea,.me-select{width:100%;border:1px solid rgba(255,255,255,.1);border-radius:13px;outline:none;color:#fff;background:rgba(255,255,255,.045);font-size:13px;transition:.2s ease}.me-input{height:49px;padding:0 14px}.me-textarea{min-height:120px;padding:13px 14px;line-height:1.65;resize:vertical}.me-textarea.large{min-height:230px}.me-select{min-height:190px;padding:9px}.me-select option{background:#151922;color:#fff}.me-input:focus,.me-textarea:focus,.me-select:focus{border-color:rgba(255,36,71,.74);box-shadow:0 0 0 4px rgba(255,36,71,.09);background:rgba(255,255,255,.065)}.me-hint{margin:0;color:var(--me-muted);font-size:11px;line-height:1.6}.me-error{margin:0;color:#ff6b80;font-size:11px}
    .me-toggle-row{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:12px}.me-toggle{position:relative;display:flex;align-items:center;gap:11px;padding:13px;border:1px solid var(--me-border);border-radius:13px;background:rgba(255,255,255,.03);cursor:pointer}.me-toggle input{position:absolute;opacity:0}.me-switch{position:relative;width:42px;height:24px;flex:none;border-radius:999px;background:rgba(255,255,255,.12);transition:.2s}.me-switch:after{content:"";position:absolute;top:3px;left:3px;width:18px;height:18px;border-radius:50%;background:#aab1bf;transition:.2s}.me-toggle input:checked+.me-switch{background:linear-gradient(135deg,#ff516c,var(--me-red2));box-shadow:0 0 0 4px rgba(255,36,71,.09)}.me-toggle input:checked+.me-switch:after{transform:translateX(18px);background:#fff}.me-toggle strong{display:block;font-size:12px}.me-toggle small{display:block;margin-top:2px;color:var(--me-muted);font-size:10px}
    .me-preview{overflow:hidden;margin-bottom:11px;border:1px solid var(--me-border);border-radius:14px;background:#090b10}.me-preview img{display:block;width:100%;object-fit:cover}.me-preview.poster{max-width:250px;margin-left:auto;margin-right:auto}.me-preview.poster img{aspect-ratio:2/3}.me-preview.backdrop img{aspect-ratio:16/9}.me-empty{min-height:150px;display:grid;place-items:center;padding:20px;color:var(--me-muted);font-size:11px;text-align:center}.me-file{position:relative;display:flex;align-items:center;gap:11px;min-height:62px;padding:12px;border:1px dashed rgba(255,255,255,.16);border-radius:13px;background:rgba(255,255,255,.025);cursor:pointer}.me-file:hover{border-color:rgba(255,36,71,.5);background:rgba(255,36,71,.035)}.me-file input{position:absolute;inset:0;width:100%;height:100%;opacity:0;cursor:pointer}.me-file-icon{width:37px;height:37px;display:grid;place-items:center;flex:none;border-radius:10px;color:#ff5872;background:rgba(255,36,71,.09)}.me-file-icon svg{width:18px;height:18px}.me-file strong{display:block;max-width:250px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font-size:11px}.me-file span{color:var(--me-muted);font-size:10px}
    .me-savebar{position:sticky;bottom:14px;z-index:20;grid-column:1/-1;display:flex;align-items:center;justify-content:space-between;gap:14px;padding:13px 15px;border:1px solid rgba(255,255,255,.11);border-radius:16px;background:rgba(12,15,21,.92);box-shadow:0 20px 50px rgba(0,0,0,.42);backdrop-filter:blur(16px)}.me-save-note{color:var(--me-muted);font-size:11px}.me-save-note strong{display:block;margin-bottom:2px;color:#cdd2dc}.me-spinner{width:16px;height:16px;display:none;border:2px solid rgba(255,255,255,.35);border-top-color:#fff;border-radius:50%;animation:meSpin .7s linear infinite}.is-loading .me-spinner{display:inline-block}.is-loading .me-button-icon{display:none}@keyframes meSpin{to{transform:rotate(360deg)}}
    @media(max-width:1050px){.me-grid{grid-template-columns:1fr}.me-row.three{grid-template-columns:repeat(2,minmax(0,1fr))}}@media(max-width:680px){.me-toolbar{align-items:flex-start;flex-direction:column}.me-actions{width:100%}.me-actions .me-btn{flex:1}.me-import-form,.me-row.two,.me-row.three,.me-toggle-row{grid-template-columns:1fr}.me-savebar{align-items:stretch;flex-direction:column}.me-savebar .me-actions{width:100%}.me-savebar .me-btn{flex:1}}@media(max-width:420px){.me-title-icon{display:none}.me-panel-body{padding:16px}.me-panel-head{padding:16px}}
</style>

<div class="movie-editor-premium">
    <div class="me-stack">
        <div class="me-toolbar">
            <div class="me-title">
                <span class="me-title-icon">
                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9"><rect x="3" y="4" width="18" height="16" rx="3"/><path d="M8 4v16"/><path d="m13 9 4 3-4 3z"/></svg>
                </span>
                <div>
                    <h2><?php echo e($isEdit ? 'Edit Movie' : 'Add New Movie'); ?></h2>
                    <p><?php echo e($isEdit ? 'Update movie information, media and SEO settings.' : 'Import a movie quickly using TMDB or IMDb.'); ?></p>
                </div>
            </div>

            <div class="me-actions">
                <a href="<?php echo e(route('admin.movies.index')); ?>" class="me-btn me-btn-secondary">
                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9"><path d="M19 12H5"/><path d="m12 19-7-7 7-7"/></svg>
                    Movie List
                </a>
                <?php if($isEdit): ?>
                    <a href="<?php echo e(route('admin.movies.create')); ?>" class="me-btn me-btn-primary">
                        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M12 5v14M5 12h14"/></svg>
                        Add Movie
                    </a>
                <?php endif; ?>
            </div>
        </div>

        <?php if($mode === 'create'): ?>
            <section class="me-panel me-import">
                <div class="me-panel-head">
                    <div><h3>Import from TMDB or IMDb</h3><p>Paste a TMDB/IMDb link or ID. The system will fetch and prepare the movie automatically.</p></div>
                </div>
                <div class="me-panel-body">
                    <form id="movieImportForm" method="post" action="<?php echo e(route('admin.movies.import')); ?>" class="me-import-form">
                        <?php echo csrf_field(); ?>
                        <div>
                            <input class="me-input <?php $__errorArgs = ['reference'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" name="reference" value="<?php echo e(old('reference')); ?>" placeholder="TMDB URL, TMDB ID, IMDb URL or IMDb ID" required autofocus>
                            <?php $__errorArgs = ['reference'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?><p class="me-error" style="margin-top:7px"><?php echo e($message); ?></p><?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>
                        <button id="importButton" class="me-btn me-btn-primary" type="submit">
                            <span class="me-spinner"></span>
                            <svg class="me-button-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9"><path d="M12 3v12"/><path d="m7 10 5 5 5-5"/><path d="M5 21h14"/></svg>
                            <span class="me-button-label">Fetch &amp; Fill</span>
                        </button>
                    </form>
                    <p class="me-hint" style="margin-top:12px"><strong>Example:</strong> https://www.themoviedb.org/movie/1475803-governor</p>
                </div>
            </section>
        <?php endif; ?>

        <?php if($mode === 'edit'): ?>
            <form id="movieEditorForm" method="post" action="<?php echo e(route('admin.movies.update', $movie)); ?>" enctype="multipart/form-data" class="me-grid">
                <?php echo csrf_field(); ?>
                <?php echo method_field('PUT'); ?>

                <div class="me-main">
                    <section class="me-panel">
                        <div class="me-panel-head"><div><h3>Movie Information</h3><p>Core details shown throughout the website.</p></div></div>
                        <div class="me-panel-body">
                            <div class="me-field"><label>Movie Name <span class="me-required">*</span></label><input class="me-input" name="title" value="<?php echo e(old('title',$movie->title)); ?>" required></div>
                            <div class="me-field"><label>Slug</label><input class="me-input" name="slug" value="<?php echo e(old('slug',$movie->slug)); ?>"></div>
                            <div class="me-field"><label>Description</label><textarea class="me-textarea large rich-editor" name="overview" rows="12"><?php echo e(old('overview',$movie->overview)); ?></textarea></div>
                            <div class="me-row three">
                                <div class="me-field"><label>Release Date</label><input class="me-input" type="date" name="release_date" value="<?php echo e(old('release_date',optional($movie->release_date)->format('Y-m-d'))); ?>"></div>
                                <div class="me-field"><label>Duration (minutes)</label><input class="me-input" type="number" min="0" name="runtime" value="<?php echo e(old('runtime',$movie->runtime)); ?>"></div>
                                <div class="me-field"><label>TMDB Rating</label><input class="me-input" type="number" step="0.1" min="0" max="10" name="vote_average" value="<?php echo e(old('vote_average',$movie->vote_average)); ?>"></div>
                            </div>
                            <div class="me-row three">
                                <div class="me-field"><label>Content Rating</label><input class="me-input" name="content_rating" value="<?php echo e(old('content_rating',$movie->content_rating)); ?>" placeholder="PG-13"></div>
                                <div class="me-field"><label>Language</label><input class="me-input" name="language" value="<?php echo e(old('language',$movie->language)); ?>" placeholder="en"></div>
                                <div class="me-field"><label>Quality Badge</label><input class="me-input" name="quality_badge" value="<?php echo e(old('quality_badge',$movie->quality_badge)); ?>" placeholder="WEB-DL"></div>
                            </div>
                        </div>
                    </section>

                    <section class="me-panel">
                        <div class="me-panel-head"><div><h3>Genres &amp; Visibility</h3><p>Choose categories and publication status.</p></div></div>
                        <div class="me-panel-body">
                            <div class="me-field">
                                <label>Genres</label>
                                <select class="me-select" name="genres[]" multiple size="8">
                                    <?php $__currentLoopData = $genres; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $genre): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($genre->id); ?>" <?php if(collect(old('genres',$movie->genres->pluck('id')->all()))->contains($genre->id)): echo 'selected'; endif; ?>><?php echo e($genre->name); ?></option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                                <p class="me-hint">Hold Ctrl/Command to select multiple genres.</p>
                            </div>
                            <div class="me-toggle-row">
                                <label class="me-toggle"><input type="checkbox" name="is_featured" value="1" <?php if(old('is_featured',$movie->is_featured)): echo 'checked'; endif; ?>><span class="me-switch"></span><span><strong>Featured / Trending</strong><small>Show in highlighted sections.</small></span></label>
                                <label class="me-toggle"><input type="checkbox" name="is_active" value="1" <?php if(old('is_active',$movie->is_active)): echo 'checked'; endif; ?>><span class="me-switch"></span><span><strong>Active</strong><small>Publish this movie publicly.</small></span></label>
                            </div>
                        </div>
                    </section>

                    <section class="me-panel">
                        <div class="me-panel-head"><div><h3>SEO Settings</h3><p>Control how this title appears in search engines.</p></div></div>
                        <div class="me-panel-body">
                            <div class="me-field"><label>SEO Title</label><input class="me-input" name="seo_title" maxlength="255" value="<?php echo e(old('seo_title',$movie->seo_title)); ?>"></div>
                            <div class="me-field"><label>Meta Description</label><textarea class="me-textarea" name="meta_description" maxlength="500"><?php echo e(old('meta_description',$movie->meta_description)); ?></textarea></div>
                            <div class="me-field"><label>Keywords</label><textarea class="me-textarea" name="keywords" maxlength="1000"><?php echo e(old('keywords',$movie->keywords)); ?></textarea></div>
                        </div>
                    </section>
                </div>

                <aside class="me-side">
                    <section class="me-panel">
                        <div class="me-panel-head"><div><h3>Poster, Thumbnail &amp; Trailer</h3><p>Images are loaded from your configured Bunny Storage/CDN.</p></div></div>
                        <div class="me-panel-body">
                            <div class="me-field">
                                <label>Movie Poster</label>
                                <div class="me-preview poster">
                                    <?php if($posterUrl): ?><img id="posterPreview" src="<?php echo e($posterUrl); ?>" alt="<?php echo e($movie->title); ?> poster"><?php else: ?><div id="posterEmpty" class="me-empty">No poster uploaded</div><img id="posterPreview" src="" alt="Poster preview" style="display:none"><?php endif; ?>
                                </div>
                                <label class="me-file"><input id="posterInput" type="file" name="poster" accept="image/*"><span class="me-file-icon"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9"><path d="M12 16V4"/><path d="m7 9 5-5 5 5"/><path d="M5 20h14"/></svg></span><span><strong id="posterName">Replace Movie Poster</strong><span>JPG, PNG, WebP or AVIF</span></span></label>
                            </div>
                            <div class="me-field">
                                <label>Movie Thumbnail / Backdrop</label>
                                <div class="me-preview backdrop">
                                    <?php if($backdropUrl): ?><img id="backdropPreview" src="<?php echo e($backdropUrl); ?>" alt="<?php echo e($movie->title); ?> backdrop"><?php else: ?><div id="backdropEmpty" class="me-empty">No backdrop uploaded</div><img id="backdropPreview" src="" alt="Backdrop preview" style="display:none"><?php endif; ?>
                                </div>
                                <label class="me-file"><input id="backdropInput" type="file" name="backdrop" accept="image/*"><span class="me-file-icon"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9"><path d="M12 16V4"/><path d="m7 9 5-5 5 5"/><path d="M5 20h14"/></svg></span><span><strong id="backdropName">Replace Thumbnail / Backdrop</strong><span>Recommended ratio: 16:9</span></span></label>
                            </div>
                            <div class="me-field"><label>Official Trailer URL</label><input class="me-input" type="url" name="trailer_url" value="<?php echo e(old('trailer_url',$movie->trailer_url)); ?>" placeholder="https://www.youtube.com/watch?v=..."><p class="me-hint">YouTube trailer only. This catalog does not host copyrighted films.</p></div>
                        </div>
                    </section>
                </aside>

                <div class="me-savebar">
                    <div class="me-save-note"><strong>Ready to save</strong>Movie details and selected images will be updated.</div>
                    <div class="me-actions">
                        <a href="<?php echo e(route('admin.movies.create')); ?>" class="me-btn me-btn-secondary">
                            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M12 5v14M5 12h14"/></svg>
                            Add Another Movie
                        </a>
                        <button id="saveButton" type="submit" class="me-btn me-btn-primary">
                            <span class="me-spinner"></span>
                            <svg class="me-button-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9"><path d="M5 4h12l2 2v14H5z"/><path d="M8 4v6h8V4"/><path d="M8 20v-6h8v6"/></svg>
                            <span class="me-button-label">Save Movie</span>
                        </button>
                    </div>
                </div>
            </form>
        <?php endif; ?>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function () {
    function loading(formId, buttonId, text) {
        const form = document.getElementById(formId), button = document.getElementById(buttonId);
        if (!form || !button) return;
        form.addEventListener('submit', function (e) {
            if (!form.checkValidity()) { e.preventDefault(); form.reportValidity(); return; }
            button.disabled = true; button.classList.add('is-loading');
            const label = button.querySelector('.me-button-label'); if (label) label.textContent = text;
        });
    }
    loading('movieImportForm','importButton','Fetching...');
    loading('movieEditorForm','saveButton','Saving...');

    function preview(inputId, imageId, emptyId, nameId) {
        const input = document.getElementById(inputId), image = document.getElementById(imageId), empty = document.getElementById(emptyId), name = document.getElementById(nameId);
        if (!input || !image) return;
        input.addEventListener('change', function () {
            const file = input.files && input.files[0]; if (!file) return;
            if (!file.type.startsWith('image/')) { input.value = ''; alert('Please select a valid image file.'); return; }
            const url = URL.createObjectURL(file); image.src = url; image.style.display = 'block'; if (empty) empty.style.display = 'none'; if (name) name.textContent = file.name; image.onload = function(){ URL.revokeObjectURL(url); };
        });
    }
    preview('posterInput','posterPreview','posterEmpty','posterName');
    preview('backdropInput','backdropPreview','backdropEmpty','backdropName');
});
</script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('admin.layout', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /home/megashopltd/moviedocks.com/resources/views/admin/movie-form.blade.php ENDPATH**/ ?>