@using PAW.Contracts.Enums
@using PantheonRA.WebApi.Contracts.Enums
@model PantheonRA.BackOffice.Models.SetItemCreateViewModel

@{
    ViewBag.Title = Model.ProductEditData != null && string.IsNullOrEmpty(Model.ProductEditData.Id) ? Loc.CreateNewProduct : Loc.EditProduct;
    Layout = null;
}
@Html.Hidden("DurationMinute", Durations.Minute)
@Html.Hidden("DurationHour", Durations.Hour)

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "productForm" }))
{
    <div class="row">
        <div class="col-md-6">
            @Html.HiddenFor(model => model.IsVATObligee)
            @Html.HiddenFor(model => model.IsEdit)
            @Html.HiddenFor(model => model.DefaultDecimalPlacesPrice)
            @Html.HiddenFor(model => model.ProductEditData.GenerItemNoLen)
            @Html.HiddenFor(model => model.ProductEditData.GenerItemNoFieldLen)
            @Html.HiddenFor(model => model.ProductEditData.GenerItemNoConst)
            @Html.HiddenFor(model => model.ProductEditData.GenerItemNoField)
            <div class="form-horizontal form-md  m-t-md">
                <h3>@Loc.BasicDataIdent</h3>

                <div class="form-group">
                    @Html.LabelFor(model => model.ProductEditData.IsActive, Loc.IsActive, new { @class = "col-md-4 control-label" })
                    <div class="col-md-8 m-t-xs">
                        @Html.CheckBoxFor(model => model.ProductEditData.IsActive, new { @class = "form-control js-switch" })
                    </div>
                </div>

                <div class="form-group">
                    @Html.LabelFor(model => model.ProductEditData.Id, Loc.Code + " / " + Loc.Ident, new { @class = "col-md-4 control-label" })
                    <div class="col-md-8">
                        @Html.TextBoxFor(model => model.ProductEditData.Id,
                            HtmlAttributesHelper.CreateInputAttributes("form-control", Html.IdFor(m => m.ProductEditData.Id).ToString(), Model.IsEdit, Loc.Code,
                            false, new { @maxlength = "16" }))
                        @Html.ValidationMessageFor(model => model.ProductEditData.Id, "", new { @class = "text-danger", @id = "ProductEditData_Id-validation" })
                    </div>
                </div>

                <div class="form-group">
                    @Html.LabelFor(model => model.ProductEditData.Name, Loc.ProductName, new { @class = "col-md-4 control-label" })
                    <div class="col-md-8">
                        @Html.TextBoxFor(model => model.ProductEditData.Name, new { @class = "form-control", @placeholder = Loc.ProductName, @maxlength = "80" })
                        @Html.ValidationMessageFor(model => model.ProductEditData.Name, "", new { @class = "text-danger", @id = "ProductEditData_Name-validation" })
                    </div>
                </div>
                <div class="m-t-md">
                    <div class="form-group m-t-lg">
                        @Html.LabelFor(model => model.ProductEditData.SuppliersPrice, Loc.SuppliersPrice, new { @class = "col-md-4 control-label" })
                        <div class="col-md-4">
                            <div>
                                @Html.TextBoxFor(model => model.ProductEditData.SuppliersPrice, new { @class = "form-control numberInput", @step = Model.DefaultDecimalPlacesPrice, @autocomplete = "off" })
                                @*TODO: When we add currency*@
                                @*<span class="input-group-addon">@Model.ProductEditData.Currency</span>*@
                            </div>
                            @Html.ValidationMessageFor(model => model.ProductEditData.SuppliersPrice, "", new { @class = "text-danger", @id = "ProductEditData_SuppliersPrice-validation" })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.ProductEditData.SellingPrice, !string.IsNullOrEmpty(Model.ProductEditData.Price1) ? Model.ProductEditData.Price1 : Loc.SellingPrice, new { @class = "col-md-4 control-label" })
                        <div class="col-md-4">
                            <div>
                                <input id="@Html.IdFor(model => model.ProductEditData.SellingPrice)" name="@Html.NameFor(model => model.ProductEditData.SellingPrice)" class="form-control numberInput" 
                                    step="@Model.DefaultDecimalPlacesPrice" autocomplete="off" value="@Model.ProductEditData.SellingPrice" data-val="@Model.ProductEditData.SellingPrice" />
                            </div>
                            @Html.ValidationMessageFor(model => model.ProductEditData.SellingPrice, "", new { @class = "text-danger", @id = "ProductEditData_SellingPrice-validation" })
                        </div>
                    </div>

                    @if (Model.IsVATObligee)
                    {
                        <div class="form-group">
                            @Html.LabelFor(model => model.ProductEditData.SelectedTaxGroupId, Loc.TaxRate, new { @class = "col-md-4 control-label" })
                            <div class="col-md-8">
                                @Html.HiddenFor(model => model.ProductEditData.TaxRateNumber)
                                <select id="@Html.IdFor(model => model.ProductEditData.SelectedTaxGroupId)"
                                        name="@Html.NameFor(model => model.ProductEditData.SelectedTaxGroupId)"
                                        class="form-control custom-select @(ViewData.ModelState.IsValidField("ProductEditData.SelectedTaxGroupId") ? "" : "input-validation-error")">
                                    @foreach (var tax in Model.TaxGroups)
                                    {
                                        <option value="@tax.VATCode" data-vat="@tax.VAT" selected="@(tax.VATCode == Model.ProductEditData.SelectedTaxGroupId)">
                                            @tax.VATCode - @tax.VAT - @tax.Name
                                        </option>
                                    }
                                </select>
                                @Html.ValidationMessageFor(model => model.ProductEditData.SelectedTaxGroupId, "", new { @class = "text-danger", @id = "ProductEditData_SelectedTaxGroupId-validation" })
                            </div>
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(model => model.ProductEditData.SelectedTaxGroupIdReceive, Loc.TaxReceiving, new { @class = "col-md-4 control-label" })
                            <div class="col-md-8">
                                @Html.HiddenFor(model => model.ProductEditData.TaxRateNumberReceive)
                                <select id="@Html.IdFor(model => model.ProductEditData.SelectedTaxGroupIdReceive)"
                                        name="@Html.NameFor(model => model.ProductEditData.SelectedTaxGroupIdReceive)"
                                        class="form-control custom-select @(ViewData.ModelState.IsValidField("ProductEditData.SelectedTaxGroupIdReceive") ? "" : "input-validation-error")">
                                    @foreach (var tax in Model.TaxGroups)
                                    {
                                        <option value="@tax.VATCode" data-vat="@tax.VAT" selected="@(tax.VATCode == Model.ProductEditData.SelectedTaxGroupIdReceive)">
                                            @tax.VATCode - @tax.VAT - @tax.Name
                                        </option>
                                    }
                                </select>
                                @Html.ValidationMessageFor(model => model.ProductEditData.SelectedTaxGroupId, "", new { @class = "text-danger", @id = "ProductEditData_SelectedTaxGroupIdReceive-validation" })
                            </div>
                        </div>
                    }

                    <div class="form-group">
                        @Html.LabelFor(model => model.ProductEditData.RetailPrice, !string.IsNullOrEmpty(Model.ProductEditData.Price0) ? Model.ProductEditData.Price0 : Loc.RetailPrice, new { @class = "col-md-4 control-label" })
                        <div class="col-md-4">
                            <div>
                                @Html.TextBoxFor(model => model.ProductEditData.RetailPrice, new { @class = "form-control numberInput", @step = Model.DefaultDecimalPlacesPrice, @autocomplete = "off" })
                            </div>
                            @Html.ValidationMessageFor(model => model.ProductEditData.RetailPrice, "", new { @class = "text-danger", @id = "ProductEditData_RetailPrice-validation" })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.ProductEditData.WSPrice, Model.ProductEditData.Price2, new { @class = "col-md-4 control-label" })
                        <div class="@(string.IsNullOrEmpty(Model.ProductEditData.Price2) ? "col-md-offset-4" : string.Empty) col-md-4">
                            <div>
                                @Html.TextBoxFor(model => model.ProductEditData.WSPrice, new { @class = "form-control numberInput", @step = Model.DefaultDecimalPlacesPrice, @autocomplete = "off" })
                            </div>
                            @Html.ValidationMessageFor(model => model.ProductEditData.WSPrice, "", new { @class = "text-danger", @id = "ProductEditData_WSPrice-validation" })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.ProductEditData.WSPrice2, Model.ProductEditData.Price3, new { @class = "col-md-4 control-label" })
                        <div class="@(string.IsNullOrEmpty(Model.ProductEditData.Price3) ? "col-md-offset-4" : string.Empty) col-md-4">
                            <div>
                                @Html.TextBoxFor(model => model.ProductEditData.WSPrice2, new { @class = "form-control numberInput", @step = Model.DefaultDecimalPlacesPrice, @autocomplete = "off" })
                            </div>
                            @Html.ValidationMessageFor(model => model.ProductEditData.WSPrice2, "", new { @class = "text-danger", @id = "ProductEditData_WSPrice2-validation" })
                        </div>
                    </div>
                </div>

                <div class="m-t-lg">
                    <div class="form-group">
                        @Html.LabelFor(model => model.ProductEditData.SelectedProductPrimaryCategoryId, Loc.PrimaryClassification, new { @class = "col-md-4 control-label" })
                        <div class="col-md-8">
                            @Html.DropDownListFor(model => model.ProductEditData.SelectedProductPrimaryCategoryId, Model.PrimaryCategories, Loc.SelectCategory, new { @class = "form-control custom-select" })
                            @Html.ValidationMessageFor(model => model.ProductEditData.SelectedProductPrimaryCategoryId, "", new { @class = "text-danger", @id = "ProductEditData_SelectedProductPrimaryCategoryId-validation" })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.ProductEditData.SelectedProductSecondaryCategoryId, Loc.SecondaryClassification, new { @class = "col-md-4 control-label" })
                        <div class="col-md-8">
                            @Html.DropDownListFor(model => model.ProductEditData.SelectedProductSecondaryCategoryId, Model.SecondaryCategories, Loc.SelectCategory, new { @class = "form-control custom-select" })
                            @Html.ValidationMessageFor(model => model.ProductEditData.SelectedProductSecondaryCategoryId, "", new { @class = "text-danger", @id = "ProductEditData_SelectedProductSecondaryCategoryId-validation" })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.ProductEditData.SelectedProductTypeId, Loc.AccountingType, new { @class = "col-md-4 control-label" })
                        <div class="col-md-8">
                            @Html.DropDownListFor(model => model.ProductEditData.SelectedProductTypeId, Model.Types, Loc.SelectType, new { @class = "form-control custom-select" })
                            @Html.ValidationMessageFor(model => model.ProductEditData.SelectedProductTypeId, "", new { @class = "text-danger", @id = "ProductEditData_SelectedProductTypeId-validation" })
                        </div>
                    </div>
                </div>

                <div class="m-t-lg">
                    <div class="form-group">
                        @Html.LabelFor(model => model.ProductEditData.Barcode, Loc.SuppliersCode, new { @class = "col-md-4 control-label" })
                        <div class="col-md-8">
                            @Html.TextBoxFor(model => model.ProductEditData.Barcode, new { @class = "form-control", @placeholder = Loc.SuppliersCode, @autocomplete = "off" })
                            @Html.ValidationMessageFor(model => model.ProductEditData.Barcode, "", new { @class = "text-danger" })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.ProductEditData.SupplierId, Loc.MainSupplier, new { @class = "col-md-4 control-label", @title = Loc.Supplier })
                        <div class="col-md-8">
                            @Html.TextBoxFor(model => model.ProductEditData.SupplierId,
                                HtmlAttributesHelper.CreateInputAttributes("form-control typeahead_3 typeahead_wide", Html.IdFor(m => m.ProductEditData.SupplierId).ToString(), false, Loc.MainSupplier, false,
                                new { dataUrl = Url.Action("SearchSuppliers", "Subjects"), @autocomplete = "off" }))
                            @Html.ValidationMessageFor(model => model.ProductEditData.SupplierId, "", new { @class = "text-danger", @id = "ProductEditData_SupplierId-validation" })
                        </div>
                    </div>

                    @if (PantheonRA.BackOffice.CurrentUser.Localization == Localization.Code.SI)
                    {
                        <div class="form-group">
                            @Html.LabelFor(model => model.ProductEditData.ForSubject, Loc.FiscalizationOnBehalfOfSubject, new { @class = "col-md-4 control-label", @title = Loc.FiscalizationOnBehalfOfSubject })
                            <div class="col-md-8">
                                @Html.TextBoxFor(model => model.ProductEditData.ForSubject,
                                     HtmlAttributesHelper.CreateInputAttributes("form-control typeahead_3 typeahead_wide", Html.IdFor(m => m.ProductEditData.ForSubject).ToString(), false, Loc.FiscalizationOnBehalfOfSubject, false,
                                     new { dataUrl = Url.Action("SearchPartners", "Documents"), @autocomplete = "off" }))
                                @Html.ValidationMessageFor(model => model.ProductEditData.SupplierId, "", new { @class = "text-danger", @id = "ProductEditData_ForSubject-validation" })
                            </div>
                        </div>
                    }

                    <div class="form-group">
                        @Html.LabelFor(model => model.ProductEditData.Stock, Loc.Stock, new { @class = "col-md-4 control-label" })
                        <div class="col-md-8">
                            @Html.TextBoxFor(model => model.ProductEditData.Stock, new { @class = "form-control numberInput", @readonly = "readonly" })
                            @Html.ValidationMessageFor(model => model.ProductEditData.Stock, "", new { @class = "text-danger" })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.ProductEditData.SelectedMeasurementUnitId, Loc.MainMeasurementUnit, new { @class = "col-md-4 control-label" })
                        <div class="col-md-8">
                            @Html.DropDownListFor(model => model.ProductEditData.SelectedMeasurementUnitId, Model.MeasurementUnits, Loc.SelectUnit, new { @class = "form-control custom-select" })
                            @Html.ValidationMessageFor(model => model.ProductEditData.SelectedMeasurementUnitId, "", new { @class = "text-danger", @id = "ProductEditData_SelectedMeasurementUnitId-validation" })
                        </div>
                    </div>

                    @if (PantheonRA.BackOffice.CurrentUser.Localization == Localization.Code.HR)
                    {
                        <div class="form-group">
                            @Html.LabelFor(model => model.ProductEditData.CustTariff, Loc.CustomsTariff, new { @class = "col-md-4 control-label", @title = Loc.CustomsTariff })
                            <div class="col-md-8">
                                @Html.DropDownListFor(model => model.ProductEditData.CustTariff, Model.Tariffs, Loc.CustomsTariff, new { @class = "form-control custom-select" })
                                @Html.ValidationMessageFor(model => model.ProductEditData.CustTariff, "", new { @class = "text-danger", @id = "ProductEditData_CustTariff-validation" })
                            </div>
                        </div>
                    }

                    @if (Localization.IsInFiscalPrinterLocalization(PantheonRA.BackOffice.CurrentUser.DatabaseLocalization, PantheonRA.BackOffice.CurrentUser.IsInServerFiscalization)
                        && !string.IsNullOrEmpty(Model.ProductEditData.Id) && PantheonRA.BackOffice.CurrentUser.Localization != Localization.Code.MK)
                    {
                        <div class="form-group">
                            @Html.LabelFor(model => model.ProductEditData.PluCode, Loc.PluCode, new { @class = "col-md-4 control-label" })
                            <div class="col-md-8">
                                @Html.TextBoxFor(model => model.ProductEditData.PluCode, new { @class = "form-control numberInput", @step = "0" })
                                @Html.ValidationMessageFor(model => model.ProductEditData.PluCode, "", new { @class = "text-danger", @id = "ProductEditData_PluCode-validation" })
                            </div>
                        </div>
                    }
                    else
                    {
                        @Html.HiddenFor(model => model.ProductEditData.PluCode)
                    }

                    <div class="form-group">
                        @Html.LabelFor(model => model.ProductEditData.PrstTime, Loc.Duration, new { @class = "col-md-4 control-label" })
                        <div class="col-md-4">
                            <input type="time"
                                   id="Time"
                                   class="ident-time-picker"
                                   onblur="setIdentTimeValues(this.value)"
                                   autocomplete="off">
                            <input type="hidden" id="PrstTime" name="ProductEditData.PrstTime" value="@Model.ProductEditData.PrstTime" />
                            <input type="hidden" id="PrstUMTime" name="ProductEditData.PrstUMTime" value="@Model.ProductEditData.PrstUMTime" />
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.ProductEditData.ColorCode, Loc.MainColor, new { @class = "col-md-4 control-label" })
                        <div class="col-md-4">
                            <div class="dropup ident-color-picker" data-toggle="dropdown">
                                <p class="dropdown-toggle fa fa-chevron-down" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                    <div class="dropdown color-picker-box" id="input-main-color-box" style="background-color: @Model.ProductEditData.ColorCode"></div>
                                    <input type="hidden" id="color-code" name="ProductEditData.ColorCode" value="@Model.ProductEditData.ColorCode" />
                                </p>
                                <div class="dropdown-menu dropdown-menu-right rounded-0 box-shadow">
                                    <div class="row">
                                        @foreach (var color in Model.ProductEditData.MainColors)
                                        {
                                            <div class="col-md-12"
                                                 onclick="setColor('@color.Key.ToString()', '@color.Name')">
                                                <div class='color-picker-box' style="background-color: @color.Key.ToString()"></div>
                                                <p> @color.Name</p>
                                            </div>
                                        }
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
}

<div id="Company" data-text="@Loc.Company" style="display:none"></div>
<div id="CompanyName" data-text="@Loc.Name" style="display:none"></div>
<div id="CompanyAddress" data-text="@Loc.Address" style="display:none"></div>
<div id="CompanyPost" data-text="@Loc.Post" style="display:none"></div>
<div id="CompanyRegisterVatNumber" data-text="@Loc.RegisterVatNumber" style="display:none"></div>
<input id="GetItemPriceUrl" value="@Url.Action("GetItemPrice", "SetItem")" hidden />
<input type="hidden" value="@ViewBag.ReturnUrl" id="ReturnUrl" name="ReturnUrl" />
<input type="hidden" id="YesLoc" value="@Loc.Yes" />
<input type="hidden" id="NoLoc" value="@Loc.No" />
<input type="hidden" id="FieldRequiredForCreatingKeyLoc" value="@Loc.FieldRequiredForCreatingKey" />
<input type="hidden" id="FieldDoesNotExistLoc" value="@Loc.FieldDoesNotExist" />
