<%@ 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>
<%
    String          hiringTeamList  =   WebUtils.getSamePageInRenderMode(request, "Page");
    String          nextPage        =   WebUtils.getSamePageInRenderMode(request, "ManagePlan");
    SecUser         loggedInUser    =   SecUser.getTXUser(transaction);
    CompanyUser     companyUser     =   loggedInUser.getExtension(CompanyUser.REFERENCE_CompanyUser);
    Company         company         =   companyUser.getCompany();
    HiringTeam      hiringTeam      =   (HiringTeam) process.getAttribute("HiringTeam");

    Debug.assertion(company != null , "Invalid company in admin portal my company");
    
    Debug.assertion(hiringTeam != null , "Invalid Hiring Team in admin portal manage plan");
    
    PaymentPlan[]   paymentPlans    =   PaymentPlan.SearchByAll()
                                                    .andDisabled(new EqualsFilter<>(Boolean.FALSE))
                                                    .andUsageType(new EqualsFilter<>(UsageType.LICENSED))
                                                    .andActiveJobCount(new GreaterThanFilter<>(0))
                                                    .search(transaction);
        
    List<PaymentPlan>   sortedPlans =   ObjstoreUtils.sort(Arrays.asList(paymentPlans), 
                                                        new ObjectTransform[]{PaymentPlan.pipesPaymentPlan().toActiveJobCount()}, 
                                                        new Comparator[]{CollectionUtils.DEFAULT_COMPARATOR_NULLS_FIRST});

    Subscription    subscription    =   null;
    PaymentPlan     currentPlan     =   hiringTeam.getPaymentPlan();
    boolean         hasValidCoupon  =   hiringTeam.hasValidCoupon();

    if(!hiringTeam.isPPJ() && currentPlan != null)
    {
        subscription    =   StripeUtils.retrieveSubscription(hiringTeam.getStripeSubscription());
    }
%>
    <script type="text/javascript">
        var Popup = null;
        $(document).ready(function()
        {
            setTabingSideBarHeight();
            
            recalcFunction = setupRecalc ($("form#editCompany"), {'recalcOnError':true});
       
            Popup = new jBox('Modal', {
                  id : "change-plan",
                  overlay : true, 
                  width : 600, height : 640
                  });
         
            var isinvliadCoupon = getParameterByName("invalidcoupon");

            if (isinvliadCoupon==='true'){
                PopupCoupon = new jBox('Modal', {
                    id : "change-plan",
                    overlay : true, content :  $("#invalid-coupon-popup") ,
                    width : 500, height : 400
                    });       
                   PopupCoupon.open();
            }

            $(".invalid-coupon-close-button").on("click",function(){             
                PopupCoupon.close();
            });  

            $(".change-plan-no-button").on("click",function(){             
                Popup.close();
            });               

            $(".select-btn-popup").on("click",function(){             
                Popup.setContent($("#" + $(this).data('popupid')  ));
                Popup.open();
            }); 
            
            $(".manage-plan-row input").on("change", function(){
                setTabingSideBarHeight();
            });
        }); 
        
        function paymentPlanChanged()
        {
            $("#savePaymentPlan").click();
        }
    </script> 
    
    <div class="container-fluid">
        <div class="row content">
            <div class="main-content-area">
                <oneit:form name="editCompany"  method="post"    enctype="multipart/form-data">
                    
                    <h1 class="page-title page-title-with-arrow">
                        <oneit:button   cssClass="arrow-btn-blue"  name="gotoPage" value=" " skin="link"
                                        requestAttribs="<%= CollectionUtils.mapEntry("nextPage", hiringTeamList)
                                                                            .mapEntry("cancelProcess", true)
                                                                            .toMap() %>" >
                            <img src="images/arrow-left-prev_blue.svg" />
                        </oneit:button>
                        <%= hiringTeam.getHiringTeamName() %>
                    </h1>
                    <div class="my-company-area">
                        
                        <div style="padding-left: 15px; padding-right: 15px;">
                            <oneit:dynInclude   page="/extensions/applicantportal/inc/multifieldtext.jsp"   data="<%= CollectionUtils.EMPTY_MAP%>"/>
                        </div>

                        <oneit:dynInclude   page="/extensions/adminportal/inc/my_company_tabs.jsp"    TabNumber="2"    data="<%= CollectionUtils.EMPTY_MAP%>"/>

                        <div class="company-content-area">
                            <!-- Tab panes -->
                            <div class="tab-content">
                                <div class="tab-pane active" id="company-detail">
                                    <div>  
                                        <div class="tabpage-title manage-plan">
                                            <label class="label-20">Manage Plan</label>                
                                        </div>
                                        <div class="apply-coupon">
                                            <oneit:ormInput obj="<%= hiringTeam %>"    type="text" attributeName="CouponCode"   placeholder="Coupon Code"  cssClass="form-control" />      
                                            <oneit:button   value="APPLY"     name="applyCoupon"    cssClass="btn btn-input-inside"
                                                                        requestAttribs="<%= CollectionUtils.mapEntry("nextPage", nextPage)
                                                                                                            .mapEntry ("restartProcess", Boolean.TRUE)
                                                                                                            .mapEntry ("procParams", CollectionUtils.mapEntry("HiringTeam", hiringTeam).toMap())
                                                                                                            .mapEntry ("attribNamesToRestore", Collections.singleton("HiringTeam"))
                                                                                                            .toMap() %>" />
                                        </div>
                                    </div>
                                    <%
                                        if(hasValidCoupon)
                                        {
                                            StripeCoupon    coupon  =   hiringTeam.getCoupon();
                                    %>
                                            <div class="coupon-applied-alert">
                                                coupon applied 
                                                <span class="coupon_code">
                                                    <oneit:toString value="<%= coupon.getCouponCode()%>" mode="EscapeHTML"/> - save
                                                    <oneit:toString value="<%= coupon.getPercentageOff()%>" mode="PercentageWholeNumber"/>
                                                </span>
                                                <%
                                                    if(hiringTeam.getCouponExpiryDate() != null)
                                                    {
                                                %>
                                                    <span class="expire">
                                                        Expires
                                                        <oneit:toString value="<%= hiringTeam.getCouponExpiryDate() %>" mode="MidDate"/>
                                                    </span>
                                                <%
                                                    }
                                                %>
                                            </div>
                                    <%
                                        }
                                    %>
                                    <div class="form-group row">
                                        <div class="col-md-12 oneit-radio coupon-applied">
                                            <div class="rectangle-5">
                                                <div class="manage-plan-row">
                                                    <div class="radio">
                                                        <label>
                                                            <oneit:ormInput obj="<%= hiringTeam %>" type="radio" attributeName="IsPPJ" value="true" onchange="paymentPlanChanged()"/>
                                                        </label>
                                                     </div>
                                                    <div class="manage-plan-title">
                                                        <h3>Pay Per Job</h3>                                                        
                                                        <span>
                                                            You'll be charged each time you add a new job
                                                        </span>
                                                    </div>
                                                    <div class="manage-plan-payplan">
                                                        <div class="per-job-amount">
                                                            <oneit:toString value="<%= PaymentPlan.PER_PER_JOB_AMOUNT %>" mode="CurrencyDollarsOnly"/>/ Job
                                                        </div>
                                                        <div class="per-job-amount-past">
                                                            was <oneit:toString value="<%= PaymentPlan.PER_PER_JOB_AMOUNT %>" mode="CurrencyDollarsOnly"/> per job
                                                        </div>
                                                    </div>
                                                    <div class="clearboth"></div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <div class="col-md-12 oneit-radio  coupon-applied">                                             
                                            <div class="rectangle-5">
                                                <div class="manage-plan-row">
                                                    <div class="radio">
                                                        <label>
                                                            <oneit:ormInput obj="<%= hiringTeam %>" type="radio" attributeName="IsPPJ" value="false"/>
                                                        </label>
                                                    </div>
                                                    <div class="manage-plan-title">
                                                        <h3>Subscribe & Save</h3>                                                        
                                                        <span>
                                                            Intend to post multiple jobs this month?<br>
                                                            Find a plan to suit your hiring needs, and save today.
                                                        </span>
                                                    </div>
                                                    <div class="manage-plan-payplan">
                                                        <div class="per-job-amount">
                                                        </div>
                                                        <div class="per-job-amount-past">
                                                        </div>
                                                    </div>
                                                    <div class="clearboth"></div>
                                                </div>
                                                <div class="manage-plan-hidden-info">  
                                                <%
                                                    if(subscription != null)
                                                    {

                                                        int     dayOfMonth  =   Utils.getDayOfMonth(new Date(subscription.getCurrentPeriodEnd() * 1000));
                                                        String  suffix      =   Utils.getDayNumberSuffix(dayOfMonth);
                                                %>
                                                        <div class="subscription-billed">
                                                            <img src="images/icon-calendar.png"> Your subscription is billed and resets on the <%= dayOfMonth %><%= suffix %> of every month
                                                        </div>
                                                <%
                                                    }
                                                %>
                                                    <div class="choose-plan-row">    
                                                        <div class="choose-your-plan-title">
                                                            Choose Your Plan
                                                        </div>
                                                        <div class="choose-plan-headings">  
                                                            <div class="jobs-per-month">Jobs Per Month</div>
                                                            <div class="cost-per-job">Cost Per Job</div>
                                                            <div class="monthly-fee">Monthly Fee</div>
                                                        </div>
                                                    <%
                                                        for (PaymentPlan paymentPlan : sortedPlans)
                                                        {
                                                            boolean     isSelectedPlan  =   CollectionUtils.equals(paymentPlan, currentPlan);
                                                    %>
                                                            <div class="choose-plan-item <%= isSelectedPlan ? "active" : ""%>">
                                                                <div class="jobs">
                                                                    <%= paymentPlan.getActiveJobCount() %> Jobs 
                                                                </div>
                                                                <div class="cost <%= hasValidCoupon ? "discounted" : ""%>">
                                                                    <oneit:toString value="<%= paymentPlan.getAmountPerJob(hiringTeam) %>"  mode="CurrencyDollarsOnly"/>
                                                                    <%
                                                                        if(hasValidCoupon)
                                                                        {
                                                                    %>
                                                                            <span class="past-val">
                                                                                Was <oneit:toString value="<%= paymentPlan.getAmount() / paymentPlan.getActiveJobCount() %>" mode="CurrencyDollarsOnly"/>
                                                                            </span>
                                                                    <%
                                                                        }
                                                                    %>
                                                                </div>
                                                                <div class="fee <%= hasValidCoupon ? "discounted" : ""%>">
                                                                    <oneit:toString value="<%= paymentPlan.getTotalAmount(hiringTeam) %>" mode="CurrencyDollarsOnly"/><span class="month"> / month </span>
                                                                    <%
                                                                        if(hasValidCoupon)
                                                                        {
                                                                    %>
                                                                            <span class="past-val">
                                                                                Was <oneit:toString value="<%=  paymentPlan.getAmount() %>" mode="CurrencyDollarsOnly"/>
                                                                            </span>
                                                                    <%
                                                                        }
                                                                    %>
                                                                </div>
                                                                <div class="select-button">
                                                                    <%
                                                                        if(isSelectedPlan)
                                                                        {
                                                                    %>
                                                                            <span class="btn active-btn">Active</span>
                                                                    <%
                                                                        }
                                                                        else if(currentPlan != null && subscription != null)
                                                                        {
                                                                            boolean isUpgrade       =   paymentPlan.getActiveJobCount() > currentPlan.getActiveJobCount();
                                                                            int     remainingJobs   =   isUpgrade ? ( paymentPlan.getActiveJobCount() - hiringTeam.getUsedCredits()) : hiringTeam.getRemainingJobs();
                                                                    %>
                                                                            <a href="#" class="btn select-btn select-btn-popup" data-popupid ="change-plan-popup-<%=paymentPlan.getID()%>" >Select</a>
                                                                            
                                                                            <div id="change-plan-popup-<%=paymentPlan.getID()%>" style="display:none" >
                                                                                <div class="change-plan-content">
                                                                                    <h3>
                                                                                        <%= isUpgrade ? "Upgrade" : "Change" %> to <%= paymentPlan.getActiveJobCount() %> Jobs per month?
                                                                                    </h3>
                                                                                    <div class="upgrade-info">
                                                                                        <%
                                                                                            if(isUpgrade)
                                                                                            {
                                                                                        %>
                                                                                                Upgrading your plan to <%= paymentPlan.getActiveJobCount() %> Jobs per month will incur an additional cost of 
                                                                                                <%= FormatUtils.stringify(paymentPlan.getAmount() - currentPlan.getAmount(), "CurrencyDollarsOnly", "") %> be billed immediately. 
                                                                                                <span>
                                                                                                    This cost is to cover the difference between your current active <%= currentPlan.getActiveJobCount() %> job plan (<%= FormatUtils.stringify(currentPlan.getAmount(), "Currency", "") %>) 
                                                                                                    and the new <%= paymentPlan.getActiveJobCount() %> job plan (<%= FormatUtils.stringify(paymentPlan.getAmount(), "CurrencyDollarsOnly", "") %>).
                                                                                                </span>
                                                                                        <%
                                                                                            }
                                                                                            else
                                                                                            {
                                                                                        %>
                                                                                                Changing your plan down to 5 Jobs per month will not incur any additional costs, and will begin on <%= FormatUtils.stringify(new Date(subscription.getCurrentPeriodEnd() * 1000), "LongDate", "")%>.
                                                                                                <span>
                                                                                                    Because your plan has already been billed, your new plan will commence at the beginning of the next billing period.                               
                                                                                                </span>
                                                                                        <%
                                                                                            }
                                                                                        %>
                                                                                    </div>
                                                                                    <div class="change-plan-list">
                                                                                        <div class="list-item">
                                                                                            <span class="item-title">Jobs remaining this month</span>
                                                                                            <span class="item-value"><%= remainingJobs %> Job Remaining</span>
                                                                                        </div>
                                                                                        <div class="list-item">
                                                                                            <span class="item-title">Date your plan will reset</span>
                                                                                            <span class="item-value"><oneit:toString  value="<%= new Date(subscription.getCurrentPeriodEnd() * 1000) %>"   mode="LongDate"/></span>
                                                                                        </div>
                                                                                        <div class="list-item">
                                                                                            <span class="item-title">Date of next billing period</span>
                                                                                            <span class="item-value"><%= FormatUtils.stringify(paymentPlan.getAmount(), "CurrencyDollarsOnly", "") %> on <oneit:toString  value="<%= new Date(subscription.getCurrentPeriodEnd() * 1000) %>"   mode="LongDate"/></span>
                                                                                        </div>
                                                                                    </div>
                                                                                    <div class="upgrade-info">
                                                                                        <span>
                                                                                            Would you like to proceed with <%= isUpgrade ? "upgrading your account?" : "this change?" %>
                                                                                        </span>
                                                                                    </div>
                                                                                </div>
                                                                                <div class="change-plan-button">
                                                                                    <a class="change-plan-no-button popup-no-button">No</a>
                                                                                    <oneit:button   skin="link" value="Yes"     name="saveCompany"      cssClass="change-plan-yes-button"
                                                                                                    requestAttribs="<%= CollectionUtils.mapEntry("nextPage", nextPage)
                                                                                                                                .mapEntry ("restartProcess", Boolean.TRUE)
                                                                                                                                .mapEntry ("IsPayment", Boolean.TRUE)
                                                                                                                                .mapEntry ("PaymentPlan", paymentPlan)
                                                                                                                                .mapEntry ("procParams", CollectionUtils.mapEntry("HiringTeam", hiringTeam).toMap())
                                                                                                                                .mapEntry ("attribNamesToRestore", Collections.singleton("HiringTeam"))
                                                                                                                                .toMap() %>" />

                                                                                </div>
                                                                            </div> 
                                                                    <%
                                                                        }
                                                                        else
                                                                        {
                                                                    %>
                                                                            <oneit:button   skin="link" value="Select"     name="saveCompany"      cssClass="btn select-btn"
                                                                                            requestAttribs="<%= CollectionUtils.mapEntry("nextPage", nextPage)
                                                                                                                                .mapEntry ("restartProcess", Boolean.TRUE)
                                                                                                                                .mapEntry ("IsPayment", Boolean.TRUE)
                                                                                                                                .mapEntry ("PaymentPlan", paymentPlan)
                                                                                                                                .mapEntry ("procParams", CollectionUtils.mapEntry("HiringTeam", hiringTeam).toMap())
                                                                                                                                .mapEntry ("attribNamesToRestore", Collections.singleton("HiringTeam"))
                                                                                                                                .toMap() %>" />
                                                                    <%
                                                                        }
                                                                    %>
                                                                </div>
                                                            </div>
                                                    <%
                                                        }
                                                    %>                                                                    
                                                            <div class="additional-jobs-over">
                                                                <span class="blue-alert">
                                                                    Additional jobs over plan allowance are billed at the Cost Per Job for the subscription level
                                                                </span>
                                                                <span>
                                                                    Would you like to set a maximum limit on the total jobs able to be posted
                                                                </span>
                                                            </div>
                                                            <div class="set-a-cap-option">
                                                                <div >
                                                                    <span> No, leave it open</span>
                                                                    <label class="switch">
                                                                        <oneit:recalcClass  htmlTag="span"  classScript="hiringTeam.isTrue(hiringTeam.getHasCap()) ? 'checkbox checked': 'checkbox unchecked'"    hiringTeam="<%= hiringTeam %>">
                                                                            <oneit:ormInput obj="<%= hiringTeam %>"    attributeName="HasCap"   type="checkbox" onChange="paymentPlanChanged()"/>
                                                                        </oneit:recalcClass>
                                                                        <div class="slider round"></div>
                                                                    </label>
                                                                    <span>yes, set a cap</span>
                                                                </div>
                                                            </div>
                                                            <div class="setmax">
                                                                <oneit:recalcClass  htmlTag="div"  classScript="hiringTeam.isTrue(hiringTeam.getHasCap()) ? 'show': 'hide'"    hiringTeam="<%= hiringTeam %>">
                                                                    <oneit:ormInput obj="<%= hiringTeam %>"    type="text" attributeName="MaxCap"    cssClass="form-control" />      

                                                                    <oneit:button   value="Set Max"     name="saveCompany"      cssClass="btn btn-primary"
                                                                                    requestAttribs="<%= CollectionUtils.mapEntry("nextPage", nextPage)
                                                                                                                        .mapEntry ("restartProcess", Boolean.TRUE)
                                                                                                                        .mapEntry ("attribNamesToRestore", Collections.singleton("HiringTeam"))
                                                                                                                        .toMap() %>" />
                                                                </oneit:recalcClass>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    <div class="form-group row">
                                        <oneit:button   value=" " name="saveCompany"      cssClass="btn btn-primary largeBtn btn-green save-btn" style="display:none;"  id="savePaymentPlan"
                                                        requestAttribs="<%= CollectionUtils.mapEntry("nextPage", nextPage)
                                                                                        .mapEntry ("restartProcess", Boolean.TRUE)
                                                                                        .mapEntry(NotificationUtils.DISPLAY_NOTIFICATION_PARAM, false)
                                                                                        .mapEntry ("attribNamesToRestore", Collections.singleton("HiringTeam"))
                                                                                        .toMap() %>" />
                                    </div>
                                    <div class="text-center footer-note">
                                        Looking to cancel your account? Please <a href="http://www.talentology.com/">contact us.</a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </oneit:form>
            </div>
        </div>
    </div>
                                                        
    
     <div id="invalid-coupon-popup" style="display:none" >
                 <div class="change-plan-content">
                    <h3>Oops!</h3>
                    <div class="upgrade-info">
                        <span>We're having some difficulty validating your coupon. <br> Please check the code supplied and try again</span>
                        <span>For further assitance, please <a href='#'>contact us</a></span>
                    </div>
                </div>
                <div class="change-plan-button">
                    <a class="invalid-coupon-close-button popup-no-button">Close & Try</a>
                </div>        
     </div>           
      
                                                         
</oneit:dynIncluded>
