@using CarCareTracker.Helper
@inject IConfigHelper config
@inject ITranslationHelper translator
@model SupplyRecordInput
@{
    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 Supply Record") : translator.Translate(userLanguage, "Edit Supply Record"))<small style="display:none; @(isNew ? "" : "cursor:pointer;")" class="cached-banner ms-2 text-warning" onclick='@(isNew ? "" : $"showEditSupplyRecordModal({Model.Id}, true)" )'>@translator.Translate(userLanguage, "Unsaved Changes")</small></h5>
    <button type="button" class="btn-close" onclick="hideAddSupplyRecordModal()" 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="supplyRecordDate">@translator.Translate(userLanguage,"Date")</label>
                    <div class="input-group">
                        <input type="text" id="supplyRecordDate" class="form-control" placeholder="@translator.Translate(userLanguage,"Date purchased")" value="@Model.Date">
                        <span class="input-group-text"><i class="bi bi-calendar-event"></i></span>
                    </div>
                    <label for="supplyRecordPartNumber">@translator.Translate(userLanguage,"Part Number")</label>
                    <input type="text" id="supplyRecordPartNumber" class="form-control" placeholder="@translator.Translate(userLanguage,"Part #/Model #/SKU #")" value="@(isNew ? "" : Model.PartNumber)">
                    <label for="supplyRecordDescription">@translator.Translate(userLanguage,"Description")</label>
                    <input type="text" id="supplyRecordDescription" class="form-control" placeholder="@translator.Translate(userLanguage,"Description of the Part/Supplies")" value="@Model.Description">
                    <label for="supplyRecordSupplier">@translator.Translate(userLanguage,"Supplier/Vendor")</label>
                    <input type="text" id="supplyRecordSupplier" class="form-control" placeholder="@translator.Translate(userLanguage,"Part Supplier")" value="@Model.PartSupplier">
                    <div class="row">
                        <div class="col-md-6 col-12">
                            <label for="supplyRecordQuantity">@translator.Translate(userLanguage,"Quantity")</label>
                            <div class="input-group">
                                <input type="text" inputmode="decimal" id="supplyRecordQuantity" class="form-control" placeholder="@translator.Translate(userLanguage,"Quantity")" value="@(isNew ? "1" : Model.Quantity)">
                                <div class="input-group-text">
                                    <button type="button" class="btn btn-sm zero-y-padding btn-primary" onclick="replenishSupplies()"><i class="bi bi-plus"></i></button>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6 col-12">
                            <label for="supplyRecordCost">@translator.Translate(userLanguage,"Cost")</label>
                            <input type="text" inputmode="decimal" id="supplyRecordCost" class="form-control" placeholder="@translator.Translate(userLanguage,"Cost")" value="@(isNew ? "" : Model.Cost)">
                        </div>
                    </div>
                    <label for="supplyRecordTag">@translator.Translate(userLanguage, "Tags(optional)")</label>
                    <select multiple class="form-select" id="supplyRecordTag">
                        @foreach (string tag in Model.Tags)
                        {
                            <!option value="@tag">@tag</!option>
                        }
                    </select>
                    @foreach (ExtraField field in Model.ExtraFields)
                    {
                        var elementId = Guid.NewGuid();
                        <div class="extra-field">
                            <label for="@elementId">@field.Name</label>
                            <input type="text" id="@elementId" class="form-control @(field.IsRequired ? "extra-field-required" : "")" placeholder="@field.Name" value="@field.Value">
                        </div>
                    }
                </div>
                <div class="col-md-6 col-12">
                    <label for="supplyRecordNotes">@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="supplyRecordNotes" class="form-control" rows="5">@Model.Notes</textarea>
                    @if (Model.Files.Any())
                    {
                        <div>
                            @await Html.PartialAsync("_UploadedFiles", Model.Files)
                            <label for="supplyRecordFiles">@translator.Translate(userLanguage,"Upload more documents")</label>
                            <input onChange="uploadVehicleFilesAsync(this)" type="file" multiple accept="@config.GetAllowedFileUploadExtensions()" class="form-control-file" id="supplyRecordFiles">
                            <br /><small class="text-body-secondary">@translator.Translate(userLanguage,"Max File Size: 28.6MB")</small>
                        </div>
                    }
                    else
                    {
                        <label for="supplyRecordFiles">@translator.Translate(userLanguage,"Upload documents(optional)")</label>
                        <input onChange="uploadVehicleFilesAsync(this)" type="file" multiple accept="@config.GetAllowedFileUploadExtensions()" class="form-control-file" id="supplyRecordFiles">
                        <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-clock-history"></i></button>
        }
        <button type="button" class="btn btn-danger" onclick="deleteSupplyRecord(@Model.Id)" style="margin-right:auto;">@translator.Translate(userLanguage,"Delete")</button>
    }
    <button type="button" class="btn btn-secondary" onclick="hideAddSupplyRecordModal()">@translator.Translate(userLanguage,"Cancel")</button>
    @if (isNew)
    {
        <button type="button" class="btn btn-primary" onclick="saveSupplyRecordToVehicle()">@translator.Translate(userLanguage,"Add New Supply Record")</button>
    }
    else if (!isNew)
    {
        <button type="button" class="btn btn-primary" onclick="saveSupplyRecordToVehicle(true)">@translator.Translate(userLanguage,"Edit Supply Record")</button>
    }
</div>
@await Html.PartialAsync("_SupplyRequisitionHistory", Model.RequisitionHistory)
<script>
    var uploadedFiles = [];
    getUploadedFilesFromModel();
    function getUploadedFilesFromModel() {
        @foreach (UploadedFiles filesUploaded in Model.Files)
        {
            @:uploadedFiles.push({ name: "@filesUploaded.Name", location: "@filesUploaded.Location" });
        }
    }
    function getSupplyRecordModelData() {
        return { id: @Model.Id}
    }
</script>