@using CarCareTracker.Helper
@inject IConfigHelper config
@inject ITranslationHelper translator
@model Note
@{
    var isNew = Model.Id == 0;
    var userConfig = config.GetUserConfig(User);
    var userLanguage = userConfig.UserLanguage;
}
<div class="modal-header">
    <h5 class="modal-title">@(isNew ? translator.Translate(userLanguage, "Add New Note") : translator.Translate(userLanguage, "Edit Note"))<small style="display:none; @(isNew ? "" : "cursor:pointer;")" class="cached-banner ms-2 text-warning" onclick='@(isNew ? "" : $"showEditNoteModal({Model.Id}, true)" )'>@translator.Translate(userLanguage, "Unsaved Changes")</small></h5>
    <button type="button" class="btn-close" onclick="hideAddNoteModal()" aria-label="Close"></button>
</div>
<div class="modal-body" onkeydown="handleEnter(this)">
    <form>
        <div class="form-group">
            <div class="row">
                <div class="col-12">
                    <input type="text" id="workAroundInput" style="height:0px; width:0px; display:none;">
                    <div class="form-check form-switch">
                        <input class="form-check-input" type="checkbox" role="switch" id="noteIsPinned" checked="@Model.Pinned">
                        <label class="form-check-label" for="noteIsPinned">@translator.Translate(userLanguage,"Pinned")</label>
                    </div>
                    <label for="noteDescription">@translator.Translate(userLanguage,"Description")</label>
                    <input type="text" id="noteDescription" class="form-control" placeholder="@translator.Translate(userLanguage,"Description of the note")" value="@(isNew ? "" : Model.Description)">
                </div>
                <div class="col-12">
                    <label for="noteTextArea">@translator.Translate(userLanguage,"Notes")<a class="link-underline link-underline-opacity-0" onclick="showLinks(this)"><i class="bi bi-markdown ms-2"></i></a></label>
                    <textarea class="form-control vehicleNoteContainer" id="noteTextArea">@Model.NoteText</textarea>
                </div>
                <div class="col-12">
                    @if (Model.Files.Any())
                    {
                        <div>
                            @await Html.PartialAsync("_UploadedFiles", Model.Files)
                            <label for="serviceRecordFiles">@translator.Translate(userLanguage, "Upload more documents")</label>
                            <input onChange="uploadVehicleFilesAsync(this)" type="file" multiple accept="@config.GetAllowedFileUploadExtensions()" class="form-control-file" id="noteFiles">
                            <br /><small class="text-body-secondary">@translator.Translate(userLanguage, "Max File Size: 28.6MB")</small>
                        </div>
                    }
                    else
                    {
                        <label for="serviceRecordFiles">@translator.Translate(userLanguage, "Upload documents(optional)")</label>
                        <input onChange="uploadVehicleFilesAsync(this)" type="file" multiple accept="@config.GetAllowedFileUploadExtensions()" class="form-control-file" id="noteFiles">
                        <br />
                        <small class="text-body-secondary">@translator.Translate(userLanguage, "Max File Size: 28.6MB")</small>
                    }
                    <div id="filesPendingUpload"></div>
                </div>
                <div class="col-12">
                    <label for="noteRecordTag">@translator.Translate(userLanguage,"Tags(optional)")</label>
                    <select multiple class="form-select" id="noteRecordTag">
                        @foreach (string tag in Model.Tags)
                        {
                            <!option value="@tag">@tag</!option>
                        }
                    </select>
                </div>
            </div>
        </div>
    </form>
</div>
<div class="modal-footer">
    @if (!isNew)
    {
        <button type="button" class="btn btn-danger" onclick="deleteNote(@Model.Id)" style="margin-right:auto;">@translator.Translate(userLanguage,"Delete")</button>
    }
    <button type="button" class="btn btn-secondary" onclick="hideAddNoteModal()">@translator.Translate(userLanguage,"Cancel")</button>
    @if (isNew)
    {
        <button type="button" class="btn btn-primary" onclick="saveNoteToVehicle()">@translator.Translate(userLanguage,"Add New Note")</button>
    }
    else if (!isNew)
    {
        <button type="button" class="btn btn-primary" onclick="saveNoteToVehicle(true)">@translator.Translate(userLanguage,"Edit Note")</button>
    }
</div>
<script>
    var uploadedFiles = [];
    getUploadedFilesFromModel();
    function getUploadedFilesFromModel() {
        @foreach (UploadedFiles filesUploaded in Model.Files)
            {
            @:uploadedFiles.push({ name: "@filesUploaded.Name", location: "@filesUploaded.Location" });
        }
    }
    function getNoteModelData(){
        return { id: @Model.Id}
    }
</script>