<%@ 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>
<%
    boolean toRedirect  =   GenericObjDF.getOrCreateObject (request, "Job", Job.REFERENCE_Job);
    Job     job         =   (Job) process.getAttribute("Job");

    Debug.assertion(job != null && !toRedirect, "Invalid job in applicant portal");

    SecUser     secUser     =   SecUser.getTXUser(transaction);
    
    Debug.assertion(secUser != null, "Invalid candidate in applicant portal");
    
    Candidate   candidate   =   secUser.getExtension(Candidate.REFERENCE_Candidate);

    Debug.assertion(candidate != null, "Invalid candidate in applicant portal");

    String          successPage     =   WebUtils.getSamePageInRenderMode(request, "VerificationSent") + "&JobID=" + job.getID();
    JobApplication  jobApplication  =   JobApplication.searchCandidateJob(transaction, candidate, job);
    
    Debug.assertion(jobApplication != null, "Invalid Job Application in applicant portal");
    
    if(jobApplication.diversityCompleted())
    {
        response.sendRedirect(WebUtils.getSamePageInRenderMode(request, "VerificationSent") + "&JobID=" + job.getID());
    }

    Boolean isSelectionComplete     =   jobApplication.selectionCompleted();
    Boolean isCultureComplete       =   jobApplication.cultureCompleted();
    Boolean isAssesmentComplete     =   jobApplication.assessmentCompleted();
    Article jobApplicationArticle   =   WebUtils.getArticleByShortCut(transaction, WebUtils.JOB_APPLICATION);
    String  nextPage                =   jobApplicationArticle.getLink(request) + "?JobID="+ job.getID().toString();;

    if(!jobApplication.hasStartedApplication())
    {
        nextPage    =   WebUtils.getArticleLink(request, transaction, WebUtils.JOB_APPLICATION, "Page") + "&JobID="+ job.getID().toString();
    }
    else if(!isSelectionComplete)
    {
        nextPage    =   LoopbackHTTP.getRemoteAccessURL(request)
                                        + jobApplicationArticle.getLink(request, CollectionUtils.mapEntry("cms.rm", "SelectionCriteria").toMap(), "/");
    }
    else if(!isCultureComplete)
    {
        nextPage    =   LoopbackHTTP.getRemoteAccessURL(request)
                                        + jobApplicationArticle.getLink(request, CollectionUtils.mapEntry("cms.rm", "WorkplaceCulture").toMap(), "/");
    }
    else if(!isAssesmentComplete)
    {
        nextPage    =   LoopbackHTTP.getRemoteAccessURL(request)
                                        + jobApplicationArticle.getLink(request, CollectionUtils.mapEntry("cms.rm", "JobMatchAssessment").toMap(), "/");
    }
    
    toRedirect  =   jobApplication.createDiversityAnswerObjects();

    if(toRedirect)
    {
        %><%@include file="/saferedirect.jsp" %><%
    }
    
    jobApplication.setDiversityAnswerOptions();

    process.setAttribute("JobApplication", jobApplication);
%>
    <oneit:form name="diversity" method="post" enctype="multipart/form-data">
        <script type="text/javascript">
            $(document.body).addClass('bg-color');
            $(document).ready(function() {
                recalcFunction = setupRecalc ($("form#diversity"), {'recalcOnError':true});
            });
        </script>
        <oneit:dynInclude   page="/extensions/applicantportal/inc/multifieldtext.jsp"   data="<%= CollectionUtils.EMPTY_MAP%>"/>
        <div class="apply-job-logo-header signinpage">
             <div class="box-sizing-border-box">
                <div class="logo-img">
                <%
                    BinaryContent   logo    =   job.getLogo();

                    if(logo != null)
                    {
                        int logoHeight  =   45;
                %>
                        <tagfile:img src="<%= ThumbnailUtils.filterImage(DiskFileBinaryContent.getRelativeURL(logo), "KEEP", new ScaleWithin (0, logoHeight)) %>" />
                <%
                    }
                %>
                </div>
                <div class="header-title">
                    <%= job.getTeamName() %>
                </div>
            </div>
        </div>
        <div class="main-verify-identity">
            <div class="main-box-layout main-verify-step-2">
            <%
                FormTag     applicationForm =   FormTag.getActiveFormTag(request);
                FormBuilder formBuilder     =   applicationForm.getFormBuilder();
                
                for(HTDiversityQuestion question : job.getDiversityQuestions())
                {
                    DiversityQuestion           originalQuestion    =   question.getQuestion();
                    CandidateDiversityAnswer    candidateAnswer     =   candidate.getDiversityAnswerByQuestion(originalQuestion);
                    
                    // not showing question if already answered
                    if(candidateAnswer.pipelineCandidateDiversityAnswer().toAnswers(AnswerOption.SearchByAll().andIsSelected(new EqualsFilter<>(Boolean.TRUE))).uniqueVals().size() >= 1)
                    {
                        continue;
                    }
            %>
                    <div class="form-group text-left">
                        <label><%= question.getQuestionText() %></label>
                        <%
                            if(originalQuestion.getMultipleAnswers())
                            {
                                for(AnswerOption answer : candidateAnswer.getAnswersSet())
                                {
                        %>
                                    <div class="styled_checkboxes">
                                        <div class="checkbox checkbox-primary">
                                            <oneit:ormInput obj="<%= answer %>"  id="<%= answer.getObjectID() %>"    attributeName="IsSelected"  type="checkbox"/>
                                            <oneit:recalcClass  htmlTag="span"  classScript="answer.getIsSelected() ? 'checked': 'unchecked'"    answer="<%= answer %>">
                                                <label for="<%= answer.getObjectID() %>">
                                                    <%= answer.getAnswer().getAnswer() %>
                                                </label>
                                            </oneit:recalcClass>
                                        </div>
                                    </div>
                        <%
                                }
                            }
                            else
                            {

                                String  optionKey   =   WebUtils.getRadioSingleAssocKey(request, candidateAnswer, CandidateDiversityAnswer.SINGLEREFERENCE_AnswerOption);
                                String  formValue   =   formBuilder.fieldValue (optionKey, candidateAnswer.getAnswerOption() == null ? "" : String.valueOf(candidateAnswer.getAnswerOptionID()));

                        %>
                                    <%--<div class="radio radio-primary second-radio-primary full-width">
                                        <input type="radio" name="<%= candidateAnswer.getObjectID() %>"    value="<%= answer.getIsSelected() %>"   id="<%= answer.getObjectID() %>"/>
                                        <label for="<%= answer.getObjectID() %>">
                                            <%= answer.getAnswer().getAnswer() %>
                                        </label>
                                    </div>--%>
                        <%
                                for(DiversityAnswer diversityAnswer : originalQuestion.getAnswersSet())
                                {
                                    String  answerId    =   String.valueOf(diversityAnswer.getID().longID());
                                    String  selectedStr =   (CollectionUtils.equals(answerId, formValue) ? "checked" : "");
                        %>
                                    <div class="radio radio-primary second-radio-primary full-width">
                                        <input type="radio" name="<%= optionKey %>" id="<%= answerId %>"  class="element_rating_radio"  value="<%= answerId %>" <%= selectedStr %>>
                                        <label for="<%= answerId %>">
                                            <oneit:toString value="<%= diversityAnswer.getAnswer() %>"    mode="EscapeHTML"   nullValue="-"/>
                                        </label>
                                    </div>
                        <%
                                }
                            }
                        %>
                    </div>
                    <hr class="seperate-line">
            <%
                }
            %>
                <div class="text-center">
                    <oneit:button   value="Submit"   name="saveDiversityAnswers"    cssClass="box-btn send-link-btn"
                                        requestAttribs="<%= CollectionUtils.mapEntry("nextPage", successPage)
                                                                        .mapEntry("JobApplication",jobApplication)
                                                                        .toMap() %>"/>
                </div>
            </div>
        </div>
    </oneit:form>
</oneit:dynIncluded>