<%@ page extends="oneit.servlets.jsp.JSPInclude" %>
<%@ include file="/inc/stdimports50.jsp" %><%-- This is in cougar --%>
<%@ include file="/inc/stdcms.jsp" %><%-- This is in cougar --%>
<%@ include file="/extensions/performa/inc/stdimports.jsp" %>

<oneit:dynIncluded>
<%
    Job     job         =   (Job) process.getAttribute("Job");
    String  nextPage    =   WebUtils.getSamePageInRenderMode(request, WebUtils.JOB_REVIEW);
    String  jobPage     =   WebUtils.getSamePageInRenderMode(request, "Page");

    Debug.assertion(job != null, "Job is null in admin portal create job");

    CultureCriteriaTemplate[]   templates   =   job.getCultureTemplates();   
%>
    
    <script type="text/javascript">
        
        function moveImportanceSection(elementRating){
            
            var importanceDiv   =   $(elementRating).closest('.criteria_section').find('.work-rating');
            var radioGroup      =   $("[name='" + $(elementRating).attr('name') +"']");
            
            if(radioGroup.is(':checked')){
                importanceDiv.insertAfter($("[name='" + $(elementRating).attr('name') +"']:checked").parent());
                importanceDiv.show();
            }
            else{
                importanceDiv.hide();
            }
        }
        
        $(document).ready(function(){
            
            recalcFunction = setupRecalc ($("form#editJob"), {'recalcOnError':true});
            
            $(".element_rating_radio").each(function (){
                moveImportanceSection(this);
            });
            
            $(".element_rating_radio").change(function(){
                moveImportanceSection(this);
            });
            
            $(".importance_radio").change(function(){
                var importanceGroup =   $(this).closest('.rate-toggle');
                
                importanceGroup.find('li.active').removeClass("active" );
                importanceGroup.find("input[type='radio']:checked").closest('li').addClass("active" );
            });
            
            $("select[name$='CultureTemplate']").change(function(){
                $("button[name$='loadCultureFromTemplate']").click();
            });
                        
            var showError = true;
            addPostRecalcHandler (function ($fieldThatChanged) {
                if(showError) {
                    showError = false;
                    $('.errorField input, .errorField select, .errorField textarea, .errorField .assocObjDesc').eq(0).focus();
                }
                recalcFunction();
            });
        });
        
    </script>
    
    <oneit:form name="editJob" method="post" enctype="multipart/form-data">
    
        <div class="container-fluid">
                 
            <oneit:dynInclude   page="/extensions/applicantportal/inc/multifieldtext.jsp"   data="<%= CollectionUtils.EMPTY_MAP%>"/>
            
            <h1 class="page-title">Create a job</h1>
       
            <oneit:dynInclude   page="/extensions/adminportal/inc/job_main_tabs.jsp"    PageNumber="3"    data="<%= CollectionUtils.EMPTY_MAP%>"/>
            
            <%
                job.setCompletedCulture(Boolean.TRUE);
            %>

            <oneit:button   value=" "   name="loadCultureFromTemplate"  cssClass="hide"
                            requestAttribs="<%= CollectionUtils.mapEntry ("Job", job)
                                                                .mapEntry (UpdateMappedObjFP.FAIL_VALIDATION_ERRORS, Boolean.FALSE)
                                                                .toMap() %>" />
            <div class="form-page-area">
                <div class="create-job">
                    <div class="assessment-criteria">
                        <div class="form-group">
                            <label class="label-16 work-title">Culture</label>
                            <p>
                            </p>      
                        </div>
                        <%
                            if(templates.length > 0)
                            {
                        %>
                                <div class="form-group row wider-select">
                                    <div class="col-md-12">
                                    <tagfile:ormsingleasso_select   obj="<%= job %>"    assocName="CultureTemplate" options="<%= templates %>"  
                                                                    blankValue="Optional: Select from previously defined Cultures"/>
                                    </div>
                                </div>              
                        <%
                            }
                        %>
                        <div class="form-group">
                            <label><oneit:label GUIName="Organisation Culture Statement" /></label>
                            <oneit:ormtextarea    obj="<%= job %>"  attributeName="CultureStatement"  cssClass="form-control textarea-box"/>
                        </div>
                    <%
                        FormTag     jobForm     = FormTag.getActiveFormTag(request);
                        FormBuilder formBuilder = jobForm.getFormBuilder();

                        job.pipelineJob().toCultureCriterias().toCultureElement().toRatings().uniqueVals(); //preloading data

                        for(CultureClass cultureClass : CultureClass.getCultureClassArray())
                        {
                    %>
                            <div class="form-group">
                                <div class="work-belonging">
                                    <oneit:toString value="<%= cultureClass %>" mode="EscapeHTML"/>
                                </div>
                                <div><%= cultureClass.getClassDescription()%></div>
                            <%
                                for(CultureCriteria criteria : job.getCultureCriteriasForClass(cultureClass))
                                {
                                    String  optionKey   = WebUtils.getRadioSingleAssocKey(request, criteria, CultureCriteria.SINGLEREFERENCE_CultureElementRating);
                                    String  formValue   = formBuilder.fieldValue (optionKey, criteria.getCultureElementRating() == null ? "" : String.valueOf(criteria.getCultureElementRatingID()));
                            %>
                                    <div class="criteria_section culture-margin-bottom">
                                        <div class="work-radio-title">
                                            <oneit:toString value="<%= criteria.getCultureElement() %>" mode="EscapeHTML"/>
                                        </div>
                                        <span class="pull-right">
                                            <label class="switch">
                                                <oneit:recalcClass  htmlTag="span"  classScript="criteria.getIsApplicable() ? 'checkbox checked': 'checkbox unchecked'"    criteria="<%= criteria %>">
                                                    <oneit:ormInput obj="<%= criteria %>"    attributeName="IsApplicable"   type="checkbox"/>
                                                </oneit:recalcClass>
                                                <div class="slider round"></div>
                                            </label>
                                        </span>
                                        <oneit:recalcClass  htmlTag="div"   classScript="criteria.getIsApplicable() ? 'show': 'hide'"  criteria="<%= criteria %>">
                                        <%
                                            for (CultureElementRating rating : criteria.getCultureElement().getRatingsSet())
                                            {
                                                String  ratingId    = String.valueOf(rating.getID().longID());
                                                String  selectedStr = (CollectionUtils.equals(ratingId, formValue) ? "checked" : "");
                                        %>
                                                <div class="<%= "radio radio-primary workplace-radio " %>">
                                                    <input type="radio" name="<%= optionKey %>" id="<%= ratingId %>"    class="element_rating_radio"    value="<%= ratingId %>" <%= selectedStr %> >
                                                    <label for="<%= ratingId %>">
                                                        <oneit:toString value="<%= rating %>"   mode="EscapeHTML"/>
                                                    </label>
                                                </div>
                                        <%
                                            }
                                        %>
                                        </oneit:recalcClass>
                                        <div class="work-rating">
                                            <div class="rate-background">
                                                <span class="arrow-up-gray"></span>
                                                <span class="rate-label">
                                                    <oneit:label GUIName="Rate Importance" />
                                                </span>
                                                <span class="rate-toggle">
                                                    <ul class="nav nav-pills nav-justified">
                                                    <%
                                                        String  importanceKey   =   WebUtils.getInputKey(request, criteria, CultureCriteria.FIELD_Importance);
                                                        String  importanceValue =   formBuilder.fieldValue (importanceKey, criteria.getImportance() == null ? "" : criteria.getImportance().getName());

                                                        for(Importance importance : Utils.getImportancesForCulture())
                                                        {
                                                            boolean isSelected      =   CollectionUtils.equals(importanceValue, importance.getName());
                                                            String  importanceId    =   String.valueOf(criteria.getID().longID()) + "_importance_" + importance.getName();
                                                            String  selected        =   isSelected ? "checked" : "";
                                                            String  activeClass     =   isSelected ? "active" : "";
                                                    %>
                                                            <li class="<%= activeClass%>">
                                                                <a href="javascript:void(0)" class="importance">

                                                                    <input type="radio" name="<%= importanceKey %>" id="<%= importanceId %>"    class="importance_radio"    value="<%= importance.getName() %>" <%= selected %>/>
                                                                    <label for="<%= importanceId %>"><oneit:toString value="<%= importance.getDescription() %>" mode="EscapeHTML" /></label>
                                                                </a>
                                                            </li>
                                                    <%
                                                        }
                                                    %>
                                                    </ul>
                                                </span>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="form-brack-line-sub"></div>
                            <%
                                }
                            %>
                            </div>
                    <%
                        }
                    %>
                        
                        <div class="form-group">
                            <div class="styled_checkboxes">
                                <div class="checkbox checkbox-primary">
                                    <oneit:ormInput obj="<%= job %>"  id="save-assess-check"  attributeName="SaveCultureTemplate"   type="checkbox"/>
                                    <oneit:recalcClass  htmlTag="span"  classScript="job.showSaveCultureTemplate() ? 'checked': 'unchecked'"    job="<%= job %>">
                                        <label for="save-assess-check">
                                            <oneit:ormlabel  obj="<%= job %>"    field="SaveCultureTemplate" />
                                        </label>
                                    </oneit:recalcClass>
                                </div>
                            </div>
                        </div>
                        <oneit:recalcClass  htmlTag="div"   classScript="job.showSaveCultureTemplate() ? 'form-group template-save show': 'form-group template-save hide'"  job="<%= job %>">
                            <div class="input-group input-group-lg">
                                <div class="icon-addon addon-lg">
                                    <oneit:ormInput obj="<%= job %>"    type="text" attributeName="CultureTemplateName" cssClass="form-control" />
                                </div>
                                <span class="input-group-btn">
                                    <oneit:button   value="SAVE"    name="saveCultureTemplate"  cssClass="btn btn-primary"
                                                    requestAttribs="<%= CollectionUtils.mapEntry("Job", job)
                                                                                        .mapEntry (UpdateMappedObjFP.FAIL_VALIDATION_ERRORS, Boolean.FALSE)
                                                                                        .toMap() %>" />
                                </span>
                            </div>
                        </oneit:recalcClass>
                        <div class="text-center">
                            <oneit:button   value="Save as draft"   name="saveJob"  cssClass="btn btn-primary top-margin-25 largeBtn greyBtn"
                                            requestAttribs="<%= CollectionUtils.mapEntry("nextPage", jobPage)
                                                                                .mapEntry(UpdateMappedObjFP.FAIL_VALIDATION_ERRORS, Boolean.FALSE)
                                                                                .toMap() %>"/>
                            <oneit:button   value="Review Job Details" name="processCulture" cssClass="btn btn-primary top-margin-25 largeBtn"
                                            requestAttribs="<%= CollectionUtils.mapEntry("nextPage", nextPage)
                                                                            .mapEntry("Job", job)
                                                                            .mapEntry("procParams", CollectionUtils.mapEntry("Job", job).toMap())
                                                                            .toMap() %>" />
                        </div>
                    </div>
                </div>
            </div>                                    
        </div>
    </oneit:form>
</oneit:dynIncluded>