@model PantheonRA.BackOffice.Models.EmployeeCreateViewModel
@using PantheonRA.BackOffice.Models
@using PantheonRA.BackOffice.Enums
@using PantheonRA.BackOffice;
@using PantheonRA.WebApi.Contracts.Enums;
@using PAW.Contracts.Enums;

@{
    ViewBag.Title = Loc.Invoice;
    var editMode = (bool)ViewBag.editMode;
    var modelValid = ViewBag.modelValid == null ? true : (bool)ViewBag.modelValid;
    var ActionMethod = "Create";
    var selectedMale = string.Empty;
    var selectedFemale = string.Empty;
    var departmentList = new List<string>();
    var addressList = new List<string>();
    var phoneNumbers = new List<string>();
    var emailAddresses = new List<string>();
    bool locBA = Loc.CurrentLocalization == Localization.Code.BA;
    bool locRS = Loc.CurrentLocalization == Localization.Code.RS;
    bool locMK = Loc.CurrentLocalization == Localization.Code.MK;
    bool locME = Loc.CurrentLocalization == Localization.Code.ME;
    var languageCode = CurrentUser.Localizationi18n;

    if (editMode)
    {
        ActionMethod = "Edit";
    }

    if (!string.IsNullOrEmpty(Model.Job))
    {
        departmentList.Add(Model.Job);
    }

    if (!string.IsNullOrEmpty(Model.Department))
    {
        departmentList.Add(Model.Department);
    }

    if (!string.IsNullOrEmpty(Model.Address))
    {
        addressList.Add(Model.Address);
    }

    if (!string.IsNullOrEmpty(Model.Post))
    {
        addressList.Add(Model.Post);
    }

    if (!string.IsNullOrEmpty(Model.Country))
    {
        addressList.Add(Model.Country);
    }
}


@Html.Hidden("EmployeeAdditionalInfoUrl", Url.Action("GetEmployeeAdditionalInfoAsync", "Employees"))
@Html.Hidden("GetNameConfiguration", Url.Action("GetNameConfiguration", "Employees"))
@Html.Hidden("GetCitizenship", Url.Action("GetCitizenship", "Employees"))

@Html.Hidden("ActionMethod", ActionMethod)
@Html.Hidden("ModelValid", modelValid)
@Html.Hidden("PreSelectedStatuses", string.Join(",", Model.SelectedStatuses))
@Html.Hidden("PreSelectedLeaders", string.Join(",", Model.ActiveLeaders))
@Html.Hidden("NameLoc", Loc.Name)
@Html.Hidden("NoResults", Loc.NoResults)
@Html.Hidden("CodeLoc", Loc.Code)
@Html.Hidden("ConfirmLoc", Loc.Confirm)
@Html.Hidden("PostDoesNotExist", Loc.PostDoesNotExist)
@Html.Hidden("ContinueUsingExpiredGoogleMapKeyEmployee", Loc.ContinueUsingExpiredGoogleMapKeyEmployee)

<div class="wrapper wrapper-content">
    <div class="tabs-container">
        @Html.Partial("_EmployeesTabNavigation")
        <div class="tab-content">
            @if (CurrentUser.CanAccess(Permissions.EMPLOYEES))
            {
                <div class="row buttons-tabbed">
                    @Html.Partial("_FormButtons", new FormButtonsViewModel
                    {
                        BackUrl = Url.Action("Index", "Employees"),
                        SaveButtonGroup = new FormButtonGroup
                        {
                            IsActive = true,
                            Buttons = new List<FormButton>
                            {
                                new FormButton { OnClick = "return submitForm('false', 'submitEmployee')" }
                            }
                        },
                        CreateButtonGroup = new FormButtonGroup
                        {
                            IsActive = true,
                            IsButtonName = true,
                            ButtonName = Loc.NewEmployee,
                            Buttons = new List<FormButton>
                            {
                                new FormButton {  OnClick = "return submitForm('true', 'submitEmployee')" }
                            }
                        }

                     })
                </div>
            }
            <div class="divided-content" style="overflow: auto">
                <div class="divided-content-main employee-main">
                    @if (editMode)
                    {
                        <div class="row employee-bottom-border">
                            <div class="col-lg-1 col-md-2 col-sm-2 col-xs-12 employee-photo">
                                <div class="employee-picture m-t-md" onclick="openModal('employee-image-modal')">
                                    @if (!string.IsNullOrEmpty(Model.ProfilePhoto))
                                    {
                                        <img class="employee-image-circle" src="@Model.ProfilePhoto" />
                                    }
                                    else
                                    {
                                        <span>@Model.Avatar</span>
                                    }
                                    <div class="edit-photo hidden">
                                        @Loc.EditPhoto
                                    </div>
                                    <i class="fas fa-pen edit-photo-icon hidden"></i>
                                </div>
                            </div>
                            <div class="col-lg-11 col-md-10 col-sm-10 col-xs-12 employee-contact-data">
                                <div class="m-l-sm m-t-md">
                                    <h3>@Model.Worker</h3>
                                    <div class="default-icon-color">@string.Join(" • ", departmentList)</div>
                                    @if (Model.Residences != null)
                                    {
                                        foreach (var item in Model.Residences.Where(s => (s.Active || (s.DateFrom != null && s.DateTo == null))))
                                        {
                                            var location = string.Format("{0}{1}{2}{3}",
                                                            !string.IsNullOrEmpty(item.Street) ? item.Street : string.Empty,
                                                            !string.IsNullOrEmpty(item.Post) ? ", " + item.Post : string.Empty,
                                                            !string.IsNullOrEmpty(item.City) ? " " + item.City : string.Empty,
                                                            !string.IsNullOrEmpty(item.Country) ? ", " + item.Country : string.Empty);

                                            <span class="employee-residence-span">
                                                <i class="fas fa-map-marker-alt default-icon-color m-t-sm"></i>
                                                <span class="m-t-sm">@string.Format("{0}: {1}", item.ResidenceType == ResidenceType.Temporary ? Loc.Temporary : Loc.Permanent, location)</span>
                                            </span>
                                        }
                                    }

                                    @if (Model.ContactAddresses != null)
                                    {
                                        foreach (var item in Model.ContactAddresses)
                                        {
                                            if (item.Type == ContactTypes.PrimaryEmailAddress
                                                || item.Type == ContactTypes.EmailForSendingForms
                                                || item.Type == ContactTypes.PrivateWebsite
                                                || item.Type == ContactTypes.OtherEmailAddress
                                                || item.Type == ContactTypes.Skype)
                                            {
                                                emailAddresses.Add(item.Value);
                                            }

                                            if (item.Type == ContactTypes.PreferenceNumber
                                                || item.Type == ContactTypes.InternalNumber
                                                || item.Type == ContactTypes.HomeNumber
                                                || item.Type == ContactTypes.GSMNumber
                                                || item.Type == ContactTypes.EmergencyNumber)
                                            {
                                                phoneNumbers.Add(item.Value);
                                            }
                                        }

                                        if (emailAddresses.Count != 0)
                                        {
                                            <div class="m-t-xs">
                                                <i class="fas fa-envelope default-icon-color"></i>
                                                @string.Join(", ", emailAddresses)
                                            </div>
                                        }

                                        if (phoneNumbers.Count != 0)
                                        {
                                            <div class="m-t-xs">
                                                <i class="fas fa-phone default-icon-color"></i>
                                                @string.Join(", ", phoneNumbers)
                                            </div>
                                        }
                                    }
                                    <div class="m-t-sm"><a onclick="openModal('contact-data-modal')">@Loc.EditContactInfo</a></div>
                                    <br />
                                </div>
                            </div>
                        </div>
                    }


                    @using (Html.BeginForm(ActionMethod, "Employees", null, FormMethod.Post))
                    {
                        @Html.Hidden("redirectToCreate", "false")
                        @Html.Hidden("BirthCity", Model.BirthCity)
                        @Html.Hidden("ProfilePhoto", Model.ProfilePhoto)
                        @Html.Hidden("Avatar", Model.Avatar)
                        <input id="submitEmployee" type="submit" hidden="hidden" />
                        <input id="Id" name="Id" hidden="hidden" value="@Model.Id" />
                        <br />
                        <div class="col-lg-6">
                            <div class="m-t-md m-b-md">
                                <h3>@Loc.BasicData</h3>
                            </div>
                            <div class="row m-b-sm m-t-sm">
                                <div class="col-md-12">
                                    <div class="form-horizontal form-md">
                                        <div class="row">
                                            <div class="col-xs-12">
                                                <div class="form-group mobile-margin">
                                                    @Html.LabelFor(model => model.Active, Loc.IsActive, new { @class = "employee-width-25 control-label" })
                                                    <div class="employee-width-75">
                                                        @Html.CheckBox("Active", Model.Active, new { @class = "form-control js-switch" })
                                                    </div>
                                                </div>
                                                <div class="form-group mobile-margin">
                                                    @Html.LabelFor(model => model.Worker, Loc.Code, new { @class = "employee-width-25 control-label" })
                                                    <div class="employee-width-75">
                                                        @Html.TextBoxFor(model => model.Worker,
                                                        HtmlAttributesHelper.CreateInputAttributes("form-control employee-input", "Worker", false, Loc.Code, true,
                                                        new { @maxlength = "30" }))
                                                        @Html.ValidationMessageFor(model => model.Worker, "", new { @class = "text-danger" })
                                                    </div>
                                                </div>
                                                <div class="form-group mobile-margin">
                                                    @Html.LabelFor(model => model.RegNo, Loc.RegistrationNumberShort + "/ID", new { @class = "employee-width-25 control-label" })
                                                    <div class="employee-width-75">
                                                        @Html.TextBoxFor(model => model.RegNo,
                                                            HtmlAttributesHelper.CreateInputAttributes("form-control employee-input", "RegNo", false, Loc.RegistrationNumberShort + "/ID",
                                                            false, new { @maxlength = "13" }))

                                                        @Html.ValidationMessageFor(model => model.RegNo, "", new { @class = "text-danger" })
                                                    </div>
                                                </div>
                                                <div class="form-group mobile-margin">
                                                    @Html.LabelFor(model => model.Name, Loc.FirstName, new { @class = "employee-width-25 control-label" })
                                                    <div class="employee-width-75">
                                                        @Html.TextBoxFor(model => model.Name,
                                                            HtmlAttributesHelper.CreateInputAttributes("form-control employee-input", "Name", false, Loc.FirstName, false, new { @maxlength = "30" }))
                                                        @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
                                                    </div>
                                                </div>
                                                <div class="form-group mobile-margin">
                                                    @Html.LabelFor(model => model.Surname, Loc.LastName, new { @class = "employee-width-25 control-label" })
                                                    <div class="employee-width-75">
                                                        @Html.TextBoxFor(model => model.Surname,
                                                            HtmlAttributesHelper.CreateInputAttributes("form-control employee-input", "Surname", false, Loc.LastName, false, new { @maxlength = "30" }))
                                                        @Html.ValidationMessageFor(model => model.Surname, "", new { @class = "text-danger" })
                                                    </div>
                                                </div>
                                                <div class="form-group mobile-margin">
                                                    @Html.LabelFor(model => model.SelectedStatuses, Loc.Status, new { @class = "control-label employee-width-25" })
                                                    <div class="employee-width-75">
                                                        @Html.DropDownListFor(model => model.SelectedStatuses,
                                                            new SelectList(Model.EmployeesFilterData.EmploymentStatuses, "Name", "Name", Model.SelectedStatuses),
                                                            HtmlAttributesHelper.CreateInputAttributes("form-control selectpicker",
                                                                "SelectedStatuses", false, Loc.Status, true, new { @multiple = "multiple", data_live_search = "true", @title = Loc.Status }))
                                                        @Html.ValidationMessageFor(model => model.SelectedStatuses, "", new { @class = "text-danger" })
                                                    </div>
                                                </div>

                                                <div class="form-group mobile-margin">
                                                    @Html.LabelFor(model => model.Job, Loc.Job, new { @class = "employee-width-25 control-label" })
                                                    <div class="employee-width-75">
                                                        <select id="Job"
                                                                name="Job"
                                                                data-placeholder="@Loc.Job"
                                                                class="form-control employee-input select2withAPI"
                                                                data-url=@Url.Action("GetJobs", "Employees")
                                                                onchange="SetDepartment();">
                                                            <option selected="selected" value="@Model.Job">@Model.Job</option>
                                                        </select>
                                                        @Html.ValidationMessageFor(model => model.Job, "", new { @class = "text-danger" })
                                                    </div>
                                                </div>

                                                <div class="form-group mobile-margin">
                                                    @Html.LabelFor(model => model.Department, Loc.Department, new { @class = "employee-width-25 control-label" })
                                                    <div class="employee-width-75">
                                                        <select id="Department"
                                                                name="Department"
                                                                data-placeholder="@Loc.Department"
                                                                class="form-control select2withAPI"
                                                                data-url=@Url.Action("GetDepartments", "Employees")>
                                                            <option selected="selected" value="@Model.Department">@Model.Department</option>
                                                        </select>
                                                        @Html.ValidationMessageFor(model => model.Job, "", new { @class = "text-danger" })
                                                    </div>
                                                </div>

                                                <div class="form-group mobile-margin">
                                                    @Html.LabelFor(model => model.ActiveLeaders, Loc.Leader, new { @class = "control-label employee-width-25" })
                                                    <div class="employee-width-75 employee-leader-dropdown">
                                                        @Html.DropDownListFor(model => model.ActiveLeaders,
                                                            new SelectList(Model.EmployeesFilterData.Employees, "WorkerCode", "WorkerName", Model.ActiveLeaders),
                                                            HtmlAttributesHelper.CreateInputAttributes("form-control selectpicker",
                                                                "ActiveLeaders", false, Loc.Leader, true, new { @multiple = "multiple", data_live_search = "true", @title = Loc.Leader }))
                                                    </div>
                                                </div>

                                                <div class="form-group mobile-margin">
                                                    @Html.LabelFor(model => model.IdentificationNumber, Loc.IdentificationNumber, new { @class = "employee-width-25 control-label" })
                                                    <div class="employee-width-75">
                                                        @Html.TextBoxFor(model => model.IdentificationNumber,
                                                            HtmlAttributesHelper.CreateInputAttributes("form-control employee-input", "IdentificationNumber",
                                                            false, Loc.IdentificationNumber, true, new { @onblur = "fillEmployeeDetails(this.value);", maxlength = 13 }))

                                                        @Html.ValidationMessageFor(model => model.IdentificationNumber, "", new { @class = "text-danger" })

                                                    </div>
                                                </div>
                                                @if (!locBA && !locMK && !locRS && !locME)
                                                {
                                                    <div class="form-group mobile-margin">
                                                        @Html.LabelFor(model => model.TaxId, Loc.VatNumber, new { @class = "employee-width-25 control-label" })
                                                        <div class="tax-prefix-width">
                                                            <select name="TaxIdPrefix"
                                                                    id="Tax"
                                                                    data-placeholder="@Loc.Prefix"
                                                                    class="form-control select2withAPI"
                                                                    data-url=@Url.Action("GetCountries", "Employees")>
                                                                <option selected="selected" value="@Model.TaxIdPrefix">@Model.TaxIdPrefix</option>
                                                            </select>
                                                        </div>
                                                        <div class="employee-width-50">
                                                            @Html.TextBoxFor(model => model.TaxId,
                                                                HtmlAttributesHelper.CreateInputAttributes("form-control employee-input", "TaxId", false, Loc.VatNumber))

                                                            @Html.ValidationMessageFor(model => model.TaxId, "", new { @class = "text-danger" })
                                                        </div>
                                                    </div>
                                                }
                                                <div class="form-group mobile-margin">
                                                    @Html.LabelFor(model => model.Gender, Loc.Gender, new { @class = "employee-width-25 control-label" })
                                                    <div class="employee-width-75">
                                                        <select class="select2 inline-search"
                                                                id="Gender"
                                                                name="Gender"
                                                                allow-clear="true"
                                                                data-placeholder="@Loc.Gender">
                                                            @if (!string.IsNullOrEmpty(Model.Gender))
                                                            {
                                                                selectedMale = Model.Gender == Gender.Male ? "selected" : string.Empty;
                                                                selectedFemale = Model.Gender == Gender.Female ? "selected" : string.Empty;
                                                            }
                                                            <option></option>
                                                            <option @selectedFemale value="@Gender.Female">@Loc.Female</option>
                                                            <option @selectedMale value="@Gender.Male">@Loc.Male</option>
                                                        </select>
                                                    </div>
                                                </div>
                                                <div class="form-group mobile-margin">
                                                    @Html.LabelFor(model => model.Citizenship, Loc.Nationality, new { @class = "employee-width-25 control-label" })
                                                    <div class="employee-width-75">
                                                        @Html.DropDownListFor(model => model.Citizenship, Model.Citizenships, Loc.Nationality,
                                                            new { @id = "Citizenship", @class = "form-control select2", @placeholder = Loc.Nationality })
                                                        @Html.ValidationMessageFor(model => model.Citizenship, "", new { @class = "text-danger" })
                                                    </div>
                                                </div>
                                                @if (locBA)
                                                {
                                                    <div class="form-group mobile-margin">
                                                        @Html.LabelFor(model => model.Localization, Loc.Legislation, new { @class = "employee-width-25 control-label" })
                                                        <div class="employee-width-75">
                                                            <select class="select2 inline-search"
                                                                    id="Localization"
                                                                    name="Localization"
                                                                    data-placeholder="@Loc.Legislation">
                                                                @foreach (var item in Legislation.Legislations)
                                                                {
                                                                    <option value="@item" @(item == Model.Localization ? "selected" : string.Empty)>@item</option>
                                                                }
                                                            </select>
                                                        </div>
                                                        @Html.ValidationMessageFor(model => model.Localization, "", new { @class = "text-danger" })
                                                    </div>
                                                }
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-6">
                            <div class="form-horizontal form-md">
                                <div class="m-t-md m-b-md">
                                    <h3>@Loc.BirthData</h3>
                                </div>
                                <div class="form-group m-t-xs mobile-margin">
                                    @Html.LabelFor(model => model.Birth, Loc.BirthDate, new { @class = "employee-width-25 control-label" })
                                    <div class="employee-width-75">
                                        <div class="date input-group">
                                            <span class="input-group-addon"><i class="far fa-calendar alt"></i></span>
                                            @Html.TextBoxFor(model => model.Birth, Model.Birth.HasValue ? Model.Birth.Value.ToString(Loc.DateFormat) : null,
                                                HtmlAttributesHelper.CreateInputAttributes("form-control employee-input custom-datepicker", "Birth", false, Loc.BirthDate))
                                            @Html.ValidationMessageFor(model => model.Birth, "", new { @class = "text-danger" })
                                        </div>
                                    </div>
                                </div>

                                <div class="form-group mobile-margin">
                                    @Html.LabelFor(model => model.BirthPlace, Loc.BirthPlace, new { @class = "employee-width-25 control-label" })
                                    <div class="employee-width-75">
                                        <select name="BirthPlace"
                                                filter-by-country="Country"
                                                data-placeholder="@Loc.BirthPlace"
                                                class="form-control select2withAPI"
                                                data-column-names="@Loc.Code,@Loc.Name"
                                                data-bootstrap-columns="col-md-6,col-md-6"
                                                data-url=@Url.Action("SearchPostsLookup", "Post")>
                                            <option selected value="@Model.BirthPlace">@string.Format("{0} {1}", Model.BirthPlace, Model.BirthCity)</option>
                                        </select>
                                        @Html.ValidationMessageFor(model => model.BirthPlace, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                                <div class="form-group mobile-margin">
                                    @Html.LabelFor(model => model.BirthCountry, Loc.BirthCountry, new { @class = "employee-width-25 control-label" })
                                    <div class="employee-width-75">
                                        @Html.DropDownListFor(model => model.BirthCountry, Model.Countries, Loc.BirthCountry,
                                            new
                                            {
                                                @id = "BirthCountry",
                                                @class = "form-control select2",
                                                @OnChange = "SetCitizenship()",
                                                @data_column_names = Loc.Country,
                                                @data_bootstrap_columns = "col-md-12",
                                                @placeholder = Loc.BirthCountry
                                            })
                                        @Html.ValidationMessageFor(model => model.BirthCountry, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                                <br />
                                <div class="m-t-md m-b-md">
                                    <h3>@Loc.OtherData</h3>
                                </div>
                                <div class="form-group m-b-sm m-t-sm mobile-margin">
                                    @Html.LabelFor(model => model.VehicleRegistrationNumber, Loc.VehicleRegistrationNumber, new { @class = "employee-width-25 control-label" })
                                    <div class="employee-width-75">
                                        <select name="VehicleRegistrationNumber"
                                                id="VehicleRegistrationNumber"
                                                data-placeholder="@Loc.VehicleRegistrationNumber"
                                                class="form-control select2withAPI"
                                                data-column-names="@Loc.CarRegNo,@Loc.Name,@Loc.CompanyVehicle"
                                                data-bootstrap-columns="col-md-5,col-md-5,col-md-2"
                                                data-search-width="550"
                                                data-column-gathering="1"
                                                data-url=@Url.Action("GetVehicleItems", "GlobalHelper", new { apiPath = "/v1/vehicles/vehicles-items" })>
                                            <option selected="selected" value="@Model.VehicleRegistrationNumber">@Model.VehicleRegistrationNumber</option>
                                        </select>
                                        @Html.ValidationMessageFor(model => model.VehicleRegistrationNumber, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                                <div class="form-group m-b-sm m-t-sm mobile-margin">
                                    @Html.HiddenFor(m => m.IsCompanyVehicle)
                                    @Html.LabelFor(model => model.IsCompanyVehicle, Loc.CompanyVehicle, new { @class = "employee-width-25 control-label" })
                                    <div class="employee-width-75">
                                        @Html.CheckBoxFor(model => model.IsCompanyVehicle, new { @id = "IsCompanyVehicleDisplay", @class = "form-control js-switch" })
                                    </div>
                                </div>
                            </div>
                        </div>

                    }
                </div>
                <div class="divided-content-side">@Html.Partial("_EmployeesSideContent")</div>
            </div>

        </div>
    </div>
</div>

@Html.Partial("_ImageModal", Model)
@Html.Partial("_ContactData", Model)
@Html.Partial("~/Views/TravelOrderAllData/_GoogleAPIKeyExpiredIRIS.cshtml", Model)

@section pageScripts{
    @* TODO: remove after refactor *@
    @Scripts.Render("~/bundles/validation")
    @Scripts.Render("~/bundles/employees")
    <script type="text/javascript"
            src="@Url.Content(string.Format("https://maps.googleapis.com/maps/api/js?language={0}&libraries=places&key={1}",
                                             languageCode, Model.GoogleApiKey))">
    </script>
    <script>
        $(document).ready(function () {
            initializeEmployeesCreate();
            initializeSidebarEmployees('@Model.Worker');

            let preSelectedStatuses = $("#PreSelectedStatuses").val();
            let preSelectedLeaders = $("#PreSelectedLeaders").val();
            $("#SelectedStatuses").selectpicker('val', preSelectedStatuses.split(","));
            $("#ActiveLeaders").selectpicker('val', preSelectedLeaders.split(","));

            setCollapseTable("residence-table");
            setCollapseTable("contactaddress-table");

            initGoogleMapsSearches();
            initContactTypeahead();
        });
    </script>
}

<style>
    .custom-typeahead-dropdown {
        z-index: 99999 !important
    }
</style>