@using CarCareTracker.Helper
@inject IConfigHelper config
@inject ITranslationHelper translator
@model ServiceRecordInput
@{
    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 Service Record") : translator.Translate(userLanguage, "Edit Service Record"))<small style="display:none; @(isNew ? "" : "cursor:pointer;")" class="cached-banner ms-2 text-warning" onclick='@(isNew ? "" : $"showEditServiceRecordModal({Model.Id}, true)" )'>@translator.Translate(userLanguage, "Unsaved Changes")</small></h5>
    <button type="button" class="btn-close" onclick="hideAddServiceRecordModal()" 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="serviceRecordDate">@translator.Translate(userLanguage,"Date")</label>
                    <div class="input-group">
                        <input type="text" id="serviceRecordDate" class="form-control" placeholder="@translator.Translate(userLanguage,"Date service was performed")" value="@Model.Date">
                        <span class="input-group-text"><i class="bi bi-calendar-event"></i></span>
                    </div>
                    <label for="serviceRecordMileage">@translator.Translate(userLanguage, "Odometer")</label>
                    <div class="input-group">
                        <input type="number" inputmode="numeric" id="serviceRecordMileage" class="form-control" placeholder="@translator.Translate(userLanguage,"Odometer reading when serviced")" 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('serviceRecordMileage')"><i class="bi bi-plus"></i></button>
                            </div>
                        }
                    </div>
                    <label for="serviceRecordDescription">@translator.Translate(userLanguage,"Description")</label>
                    <input type="text" id="serviceRecordDescription" class="form-control" placeholder="@translator.Translate(userLanguage,"Description of item(s) serviced(i.e. Oil Change)")" value="@Model.Description">
                    @if (isNew)
                    {
                        <div class="row">
                            <div class="col-12">
                                <a onclick="showRecurringReminderSelector('serviceRecordDescription', 'serviceRecordNotes')" class="btn btn-link">@translator.Translate(userLanguage, "Select Reminder")</a>
                            </div>
                        </div>
                    }
                    <label for="serviceRecordCost">@translator.Translate(userLanguage,"Cost")</label>
                    <input type="text" inputmode="decimal" onkeydown="interceptDecimalKeys(event)" onkeyup="fixDecimalInput(this, 2)" id="serviceRecordCost" class="form-control" placeholder="@translator.Translate(userLanguage,"Cost of the service")" value="@(isNew ? "" : Model.Cost)">
                    @await Html.PartialAsync("_SupplyStore", new SupplyStore { Tab = "ServiceRecord", AdditionalSupplies = Model.RequisitionHistory.Any() })
                    <label for="serviceRecordTag">@translator.Translate(userLanguage,"Tags(optional)")</label>
                    <select multiple class="form-select" id="serviceRecordTag">
                        @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="serviceRecordNotes">@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="serviceRecordNotes" class="form-control" rows="5">@Model.Notes</textarea>
                    @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="serviceRecordFiles">
                            <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="serviceRecordFiles">@translator.Translate(userLanguage,"Upload documents(optional)")</label>
                        <input onChange="uploadVehicleFilesAsync(this)" type="file" multiple accept="@config.GetAllowedFileUploadExtensions()" class="form-control-file" id="serviceRecordFiles">
                        <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="deleteServiceRecord(@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, 'ServiceRecord', 'RepairRecord')">@translator.Translate(userLanguage,"Repairs")</a></li>
                <li><a class="dropdown-item" href="#" onclick="moveRecord(@Model.Id, 'ServiceRecord', 'UpgradeRecord')">@translator.Translate(userLanguage,"Upgrades")</a></li>
            </ul>
        </div>
    }
    <button type="button" class="btn btn-secondary" onclick="hideAddServiceRecordModal()">@translator.Translate(userLanguage,"Cancel")</button>
    @if (isNew)
    {
        <button type="button" class="btn btn-primary" onclick="saveServiceRecordToVehicle()">@translator.Translate(userLanguage,"Add New Service Record")</button>
    }
    else if (!isNew)
    {
        <button type="button" class="btn btn-primary" onclick="saveServiceRecordToVehicle(true)">@translator.Translate(userLanguage,"Edit Service Record")</button>
    }
</div>
@await Html.PartialAsync("_SupplyRequisitionHistory", new SupplyRequisitionHistory { RequisitionHistory = Model.RequisitionHistory, CostInputId = "serviceRecordCost" })
<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 getServiceRecordModelData(){
        return {id: @Model.Id}
    }
</script>