@inject IConfigHelper config
@inject ITranslationHelper translator
@using CarCareTracker.Helper
@model UpgradeRecordInput
@{
    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 Upgrade Record") : translator.Translate(userLanguage, "Edit Upgrade Record"))<small style="display:none; @(isNew ? "" : "cursor:pointer;")" class="cached-banner ms-2 text-warning" onclick='@(isNew ? "" : $"showEditUpgradeRecordModal({Model.Id}, true)" )'>@translator.Translate(userLanguage, "Unsaved Changes")</small></h5>
    <button type="button" class="btn-close" onclick="hideAddUpgradeRecordModal()" aria-label="Close"></button>
</div>
<div class="modal-body" onkeydown="handleEnter(this)">
    <form>
        <div class="form-group">
            <div class="row">
                <div class="col-md-6 col-12">
                    <input type="text" id="workAroundInput" style="height:0px; width:0px; display:none;">
                    <label for="upgradeRecordDate">@translator.Translate(userLanguage, "Date")</label>
                    <div class="input-group">
                        <input type="text" id="upgradeRecordDate" class="form-control" placeholder="@translator.Translate(userLanguage,"Date upgrade/mods was installed")" value="@Model.Date">
                        <span class="input-group-text"><i class="bi bi-calendar-event"></i></span>
                    </div>
                    <label for="upgradeRecordMileage">@translator.Translate(userLanguage, "Odometer")</label>
                    <div class="input-group">
                        <input type="number" inputmode="numeric" id="upgradeRecordMileage" class="form-control" placeholder="@translator.Translate(userLanguage,"Odometer reading when upgraded/modded")" value="@(isNew || Model.Mileage == default ? "" : Model.Mileage)">
                        @if (isNew)
                        {
                            <div class="input-group-text">
                                <button type="button" class="btn btn-sm btn-primary zero-y-padding" onclick="getLastOdometerReadingAndIncrement('upgradeRecordMileage')"><i class="bi bi-plus"></i></button>
                            </div>
                        }
                    </div>
                    <label for="upgradeRecordDescription">@translator.Translate(userLanguage, "Description")</label>
                    <input type="text" id="upgradeRecordDescription" class="form-control" placeholder="@translator.Translate(userLanguage,"Description of item(s) upgraded/modded")" value="@Model.Description">
                    @if (isNew)
                    {
                        <div class="row">
                            <div class="col-12">
                                <a onclick="showRecurringReminderSelector('upgradeRecordDescription', 'upgradeRecordNotes')" class="btn btn-link">@translator.Translate(userLanguage, "Select Reminder")</a>
                            </div>
                        </div>
                    }
                    <label for="upgradeRecordCost">@translator.Translate(userLanguage, "Cost")</label>
                    <input type="text" inputmode="decimal" onkeydown="interceptDecimalKeys(event)" onkeyup="fixDecimalInput(this, 2)" id="upgradeRecordCost" class="form-control" placeholder="@translator.Translate(userLanguage,"Cost of the upgrade/mods")" value="@(isNew ? "" : Model.Cost)">
                    @await Html.PartialAsync("_SupplyStore", new SupplyStore { Tab = "UpgradeRecord", AdditionalSupplies = Model.RequisitionHistory.Any() })
                    <label for="upgradeRecordTag">@translator.Translate(userLanguage, "Tags(optional)")</label>
                    <select multiple class="form-select" id="upgradeRecordTag">
                        @foreach (string tag in Model.Tags)
                        {
                            <!option value="@tag">@tag</!option>
                        }
                    </select>
                    @await Html.PartialAsync("_ExtraField", Model.ExtraFields)
                </div>
                <div class="col-md-6 col-12">
                    <label for="upgradeRecordNotes">@translator.Translate(userLanguage, "Notes(optional)")<a class="link-underline link-underline-opacity-0" onclick="showLinks(this)"><i class="bi bi-markdown ms-2"></i></a></label>
                    <textarea id="upgradeRecordNotes" class="form-control" rows="5">@Model.Notes</textarea>
                    @if (Model.Files.Any())
                    {
                        <div>
                            @await Html.PartialAsync("_UploadedFiles", Model.Files)
                            <label for="upgradeRecordFiles">@translator.Translate(userLanguage, "Upload more documents")</label>
                            <input onChange="uploadVehicleFilesAsync(this)" type="file" multiple accept="@config.GetAllowedFileUploadExtensions()" class="form-control-file" id="upgradeRecordFiles">
                            <br /><small class="text-body-secondary">@translator.Translate(userLanguage, "Max File Size: 28.6MB")</small>
                        </div>
                    }
                    else
                    {
                        @if (isNew)
                        {
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" value="" id="addReminderCheck">
                                <label class="form-check-label" for="addReminderCheck">
                                    @translator.Translate(userLanguage, "Add Reminder")
                                </label>
                            </div>
                        }
                        <label for="upgradeRecordFiles">@translator.Translate(userLanguage,"Upload documents(optional)")</label>
                        <input onChange="uploadVehicleFilesAsync(this)" type="file" multiple accept="@config.GetAllowedFileUploadExtensions()" class="form-control-file" id="upgradeRecordFiles">
                        <br /><small class="text-body-secondary">@translator.Translate(userLanguage, "Max File Size: 28.6MB")</small>
                    }
                    <div id="filesPendingUpload"></div>
                </div>
            </div>
        </div>
    </form>
</div>
<div class="modal-footer">
    @if (!isNew)
    {
        @if (Model.RequisitionHistory.Any())
        {
            <button type="button" class="btn btn-warning" onclick="toggleSupplyUsageHistory()"><i class="bi bi-shop"></i></button>
        }
        <div class="btn-group" style="margin-right:auto;">
            <button type="button" class="btn btn-md mt-1 mb-1 btn-danger" onclick="deleteUpgradeRecord(@Model.Id)">@translator.Translate(userLanguage, "Delete")</button>
            <button type="button" class="btn btn-md btn-danger btn-md mt-1 mb-1 dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
                <span class="visually-hidden">Toggle Dropdown</span>
            </button>
            <ul class="dropdown-menu">
                <li><h6 class="dropdown-header">@translator.Translate(userLanguage, "Move To")</h6></li>
                <li><a class="dropdown-item" href="#" onclick="moveRecord(@Model.Id, 'UpgradeRecord', 'ServiceRecord')">@translator.Translate(userLanguage, "Service Records")</a></li>
                <li><a class="dropdown-item" href="#" onclick="moveRecord(@Model.Id, 'UpgradeRecord', 'RepairRecord')">@translator.Translate(userLanguage, "Repairs")</a></li>
            </ul>
        </div>
    }
    <button type="button" class="btn btn-secondary" onclick="hideAddUpgradeRecordModal()">@translator.Translate(userLanguage, "Cancel")</button>
    @if (isNew)
    {
        <button type="button" class="btn btn-primary" onclick="saveUpgradeRecordToVehicle()">@translator.Translate(userLanguage, "Add New Upgrade Record")</button>
    }
    else if (!isNew)
    {
        <button type="button" class="btn btn-primary" onclick="saveUpgradeRecordToVehicle(true)">@translator.Translate(userLanguage, "Edit Upgrade Record")</button>
    }
</div>
@await Html.PartialAsync("_SupplyRequisitionHistory", new SupplyRequisitionHistory { RequisitionHistory = Model.RequisitionHistory, CostInputId = "upgradeRecordCost" })
<script>
    var uploadedFiles = [];
    var selectedSupplies = [];
    var recurringReminderRecordId = [];
    var copySuppliesAttachments = false;
    getUploadedFilesFromModel();
    function getUploadedFilesFromModel() {
    @foreach (UploadedFiles filesUploaded in Model.Files)
    {
        @:uploadedFiles.push({ name: "@filesUploaded.Name", location: "@filesUploaded.Location" });
    }
            }
    function getUpgradeRecordModelData() {
        return { id: @Model.Id}
    }
</script>