@extends('_layout')
@section('title', ' - Edit paste')
    @include ('_sidebar_toggle')
    <form action="/edit/{{ $paste->link }}" id="edit-form" method="POST" class="editor-form" style="margin: 0; margin-block-end: 0;">
        {!! csrf_field() !!}
        <div class="sidebar">
            @include('_logo')

            <div class="options">
                <div class="paste-properties">
                @if($paste->pinned)
                <span><i class="material-icons">push_pin</i><span>Pinned</span></span><br>
                @endif
                @if($paste->password != null)
                <span><i class="material-icons">password</i><span>Password protected</span></span><br>
                @endif
                @if($paste->private)
                <span><i class="material-icons">lock</i><span>Private</span></span><br>
                @endif
                @if($paste->unlisted)
                <span><i class="material-icons">visibility_off</i><span>Unlisted</span></span><br>
                @endif
                </div>
                @if($myUser->admin == 0)
                <p style="color: red; font-size: 12px; text-align: center;">Use this to add information or remove inaccurate information from a dox.<br>
                <u>Don't try to remove a dox with the edit feature or the request will be denied.</u></p>
                @endif
                @if ($error != null)         
                    <p style="color:red; text-align: center; font-size: 15px;">{{$error}}</p>
                @endif
                
                @if ($myUser != null && $myUser->isManager(true)) <p><span style="font-weight:bold; color: white;">ID:</span> <span style="float: right;">{{$paste->id}}</span></p>@endif
                <p><span style="font-weight:bold; color: white;">Title:</span><span style="float: right;"><a target="_blank" style="color: #2a9fd6" href="/upload/{{ $paste->link }}">{{$paste->title}}</a></span></p>
                <p><span style="font-weight:bold; color: white;">Created:</span><span style="float: right;">{{ $paste->created_at->format('M jS, Y') }}</span></p>
                <p><span style="font-weight:bold; color: white;">Created by:</span><span style="float: right;">
                @if ($created_by == null)
                    Anonymous
                @elseif ($created_by == "[deleted]")
                    [deleted]
                @else
                    {!! $created_by->getNameHtml(true) !!}
                @endif
                </span></p>
                <p><span style="font-weight:bold; color: white;">Views:</span> <span style="float: right;">{{ number_format($paste->views) }}</span></p>
                @if($paste->disable_cmts == 0)
                    <p><span style="font-weight:bold; color: white;">Comments:</span> <span style="float: right;">{{ $paste->comments }}</span></p>
                @endif
                @if(!$myUser->isManager(true))
			<p style="margin: 8px 0 2px 0"><b>Reason for edit (optional):</b></p>
                	<input type="text" name="reason" maxlength="300" value="" placeholder="Added info, removed inaccurate info, etc">
        @endif
                @if($myUser->isManager(true))
                <h3 style="margin-bottom: 3px;">Title:</h3>
                <input style="margin: 0;" type="text" id="edit-title" name="pasteTitle" maxlength="70" value="{{ $paste->title }}">
                @endif
            </div>
            <div class="options">
                <p style="color:red;" id="edit-error">{{ isset($error) ? $error : '' }}</p>
                @include('layouts.captcha')
            </div>
            <div class="options">
                <ul>
                    <li><input type="submit" value="Save" data-bptrigger="1" class="btn"></li>
		@if(!$myUser->isManager(true))
                    <p style="color: #cccccc; font-size: 12px; text-align: center; margin: 0px;">All changes are checked by staff before they are published. Invalid edits will be denied.</p><br>
                @endif
			<li><a id="back-btn" href="#" class="btn back" style="font: 400 13.3333px Arial;">Back</a></li>
                </ul>
            </div>
            <p>Please note that all posted information is publicly available and must follow our <a href="https://{{$currentDomain}}/tos" style="text-decoration: underline;">TOS.</a></p>
        </div>

        <div class="editor-container">
            <textarea name="pasteContent" id="edit-content" class="editor mousetrap" wrap="off" style="position: relative; margin-bottom: 0;">{{ (isset($newcontent) ? $newcontent : $paste->content)}}</textarea>
        </div>
    </form>
    

    <div id="modal" style="z-index: 99999;">
        <div id="modal-bg" class="modal-background">
        </div>
        <div class="modal-container" style="">
            <p style="font-size: 20px; font-weight: bold; margin: 0;">Unsaved Changes</p>
            <p>Are you sure you want to leave the page? All changes will be lost.</p>
            <a class="btn confirm" id="confirm" href="#" style="position: absolute;width: auto;bottom: 25px;">Confirm</a>
            <a style="right: 0; position: absolute; margin-right: 15px; width: auto; bottom: 25px;" class="btn cancel" id="cancel" href="#">Cancel</a>
        </div>
    </div>
    <script src="https://{{$currentDomain}}/legacy/jquery.min.js"></script>
    @include('layouts.captchascript')
    <script type="text/javascript">
        var submitting = false;

    @if(env('CAPTCHA') == 'RECAPTCHA')

        $('#edit-form').on('submit', function(e){
            if(!submitting){
                submitting = true;
                $.post('/edit/{{ $paste->link }}', {
                    _token: $('input[name=_token]').val(),
                    {!! $myUser->isManager(true) ? "pasteTitle: $('#edit-title').val()," : "" !!}
                    pasteContent: $('#edit-content').val(),
                    recaptcha_token: grecaptcha.getResponse(),
                    reason: $('[name=reason]').val(),
                }, function(resp){
                    if(resp.status == "err"){
                        $('#edit-error').text(resp.msg);
                    }else{
                        window.location = resp.redirect;
                    }
                }).fail(function(){
                    $('#edit-error').text('Error');
                });
                submitting = false;
            }
            e.preventDefault();
        });

    @elseif(env('CAPTCHA') == 'HCAPTCHA')


        $('#edit-form').on('submit', function(e){
            if(!submitting){
                submitting = true;
                $.post('/edit/{{ $paste->link }}', {
                    _token: $('input[name=_token]').val(),
                    {!! $myUser->isManager(true) ? "pasteTitle: $('#edit-title').val()," : "" !!} {{-- Allows for Manager+ to change paste title not just Manager. Reiko, Mar 15th 2024 --}}
                    pasteContent: $('#edit-content').val(),
                    hcaptcha_token: $('[name=h-captcha-response]').val(),
                    reason: $('[name=reason]').val(),
                }, function(resp){
                    if(resp.status == "err"){
                        $('#edit-error').text(resp.msg);
                    }else{
                        window.location = resp.redirect;
                    }
                }).fail(function(){
                    $('#edit-error').text('Error');
                });
                submitting = false;
            }
            e.preventDefault();
        });

    @elseif(env('CAPTCHA') == 'TURNSTILE')

        $('#edit-form').on('submit', function(e){
            if(!submitting){
                submitting = true;
                $.post('/edit/{{ $paste->link }}', {
                    _token: $('input[name=_token]').val(),
                    {!! $myUser->isManager(true) ? "pasteTitle: $('#edit-title').val()," : "" !!}
                    pasteContent: $('#edit-content').val(),
                    turnstile_token: $('[name=cf-turnstile-response]').val(),
                    reason: $('[name=reason]').val(),
                }, function(resp){
                    if(resp.status == "err"){
                        $('#edit-error').text(resp.msg);
                    }else{
                        window.location = resp.redirect;
                    }
                }).fail(function(){
                    $('#edit-error').text('Error');
                });
                submitting = false;
            }
            e.preventDefault();
        });
    @endif


        $('#back-btn').on('click', function(e){
            modal('/upload/{{$paste->link}}');
            e.preventDefault();
        });

        $('#modal-bg, #cancel').on('click', function(e){
            $('#modal').fadeOut();
            e.preventDefault();
        });

        $(document).on('click', 'a', function(e){
            if($(this).attr('href') != '#' && $(this).attr('id') != "confirm"){
                modal($(this).attr('href'));
                e.preventDefault();
            }
        });
        function modal(link){
            $('#confirm').attr('href', link);
            $('#modal').fadeIn();
        }
    </script>
