<script src="../javascript/accounting.js"></script>

<script data-cfasync="false" type="text/javascript">
      var currency = {};
      currency.symbol = '<?php echo $this->currency["symbol"]; ?>';
      currency.decimalssep = "<?php echo $this->currency['decimalssep']; ?>";
      currency.thousandssep = "<?php echo $this->currency['thousandssep']; ?>";
      currency.alignment = "<?php echo $this->currency['alignment']; ?>";
      currency.precision = "<?php echo $this->currency['precision']; ?>";
      currency.abrv = "<?php echo $this->currency['abrv']; ?>";
      currency.showabrv = "<?php echo $this->currency['showabrv']; ?>";
</script>

<div class="alert alert-info ce-alert ce-alert-info">
    <?php echo $this->upgradeDowngradeStatus['upgradeMessage']; ?>
</div>

<input type="hidden" name="id" value="<?php echo $this->packageId; ?>" />
<input type="hidden" name="customerTax" value="<?php echo $this->customerTax; ?>" />
<input type="hidden" name="customerTax2" value="<?php echo $this->customerTax2; ?>" />
<input type="hidden" name="customerTax2Compound" value="<?php echo $this->customerTax2Compound; ?>" />
<input type="hidden" name="upgradeDowngradeStatusValue" value="<?php echo $this->upgradeDowngradeStatus['upgradeValue']; ?>" />

<div class="form-group">
    <label><?php echo $this->user->lang('Upgrade To Product'); ?>:</label>
    <select name="upgradePackage" id="upgradePackage" class="selectpicker" onChange="loadTotalsInfo();" >
        <?php foreach($this->upgradePackages as $package) { ?>
            <option value="<?php echo $package["id"]; ?>" data-taxable="<?php echo $package['taxable']; ?>"><?php echo $this->user->lang($package["plannamelanguage"]); ?></option>
        <?php } ?>
    </select>
</div>

<?php $firstPackageId = false; ?>
<?php $firstPackageTerm = array(); ?>
<?php $packageStyle = ''; ?>
<?php foreach($this->upgradePackages as $package) { ?>
    <div id="upgradePackageDescription_<?php echo $package["id"]; ?>" class="form-group upgradePackageDescription" <?php echo $packageStyle; ?> >
        <?php echo $package["descriptionlanguage"]; ?>

        <div class="table-responsive">
            <table class="table ce-table table-striped table-hover pricing-options table-responsive">
                <thead>
                    <tr class="caption">
                        <th class="text-center" width="15"></th>
                        <th class="text-center"><?php echo $this->user->lang("Term Length");?></th>
                        <th><?php echo $this->user->lang("Renewal Price");?></th>
                        <?php if (!$this->hideSetupFees) { ?>
                            <th class="text-right"><?php echo $this->user->lang("Setup Fee");?></th>
                        <?php } ?>
                        <th class="text-right" <?php if (!$this->showSaved) {?>style="display:none"<?php } ?>><?php if($this->showSaved) { echo $this->user->lang("You Save");}?></th>
                    </tr>
                </thead>
                <tbody>
                <?php foreach ($package['pricing'] as $pricing) { ?>
                    <?php if (!isset($firstPackageTerm[$package['id']])) {
                        $firstPackageTerm[$package['id']] = $pricing['termId']; ?>
                    <?php } ?>
                    <tr>
                        <td class="text-center">
                            <input class="priceTerm_<?php echo $package['id'];?>" name="priceTerm_<?php echo $package['id'];?>" type="radio" value="<?php echo $pricing['termId']; ?>" data-subtotal_amount="<?php echo $pricing['subtotal_amount']; ?>" <?php if($pricing['selected'] == 1) { echo " checked";} ?> onChange="loadTotalsInfo();" />
                        </td>
                        <td class="text-center"><?php echo $pricing['term'];?></td>
                        <td><?php echo $pricing['price'];?></td>
                        <?php if (!$this->hideSetupFees) { ?>
                            <td class="text-right"><?php echo $pricing['setup'];?></td>
                        <?php } ?>
                        <td class="text-right" <?php if (!$this->showSaved) {?>style="display:none"<?php } ?>><?php if($this->showSaved) { echo $pricing['save'];}?></td>
                    </tr>
                <?php } ?>
                </tbody>
            </table>
        </div>

        <?php if (isset($this->packageAddons[$package["id"]]) && is_array($this->packageAddons[$package["id"]]) && count($this->packageAddons[$package["id"]]) > 0) { ?>
            <?php $addonStyle = ''; ?>
            <?php foreach ($this->packageAddons[$package["id"]] as $packageTerm => $addonOptions) { ?>
                <div id="upgradeAddonDescription_<?php echo $package["id"]; ?>_<?php echo $packageTerm; ?>" class="form-group upgradeAddonDescription" <?php echo $addonStyle; ?> >
                    <?php if (count($addonOptions) > 0) { ?>
                    <div class="addons-wrapper container-fluid" >
                        <h2><?php echo $this->user->lang("Product Add-ons");?></h2>

                        <?php foreach ($addonOptions as $addon) {
                            if (!is_array($addon['prices']) || count($addon['prices']) == 0) {
                                continue;
                            }

                            ?>
                            <div class="col-12 form-group addon-type">
                                <label class="customfield_label control-label">
                                    <?php if ($addon['desclanguage'] != '') { ?>
                                    <span data-toggle="popover-hover" data-html="true" data-placement="top" title="<?php echo $this->user->lang("Description");?>" data-content="<?php echo $this->escape($addon['desclanguage']);?>" class="tip-target">
                                    <?php } ?>
                                        <b><?php echo $this->user->lang($addon['namelanguage']);?></b>
                                    <?php if ($addon['desclanguage'] != '') { ?>
                                    </span>
                                    <?php } ?>
                                </label>


                                <?php switch (@$addon['addon_type']) {
                                    case '2':    // quantity ?>
                                        <?php $countAddonOptions = count($addon['prices']); ?>
                                        <div class="col-sm-6">
                                        <table style="width:100%">
                                            <tr>
                                                <td style="width:75px">
                                                    <?php echo $this->user->lang("Quantity");?> *</br>
                                                </td>
                                                <td>
                                                    <?php if ($countAddonOptions > 1) { ?>
                                                        <?php echo $this->user->lang("Option");?></br>
                                                    <?php } ?>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td style="width:75px">
                                                    <input type="number" min="0" style="width:70px;" id="addonQuantity_<?php echo $package["id"]; ?>_<?php echo $packageTerm; ?>_<?php echo $addon['id']; ?>" name="addonQuantity_<?php echo $package["id"]; ?>_<?php echo $packageTerm; ?>_<?php echo $addon['id']; ?>" class="quantity form-control" value="0" onChange="loadTotalsInfo();">
                                                </td>
                                                <td>
                                                    <?php if ($countAddonOptions == 1) { ?>
                                                        <input type="hidden" name="addonSelect_<?php echo $package["id"]; ?>_<?php echo $packageTerm; ?>_<?php echo $addon['id']; ?>" value="addon_<?php echo $addon['id']; ?>_<?php echo $addon['prices'][0]['price_id']; ?>_<?php echo $addon['prices'][0]['recurringprice_cyle']; ?>" data-subtotal_amount="<?php echo $addon['prices'][0]['item_subtotal_amount']; ?>">
                                                        <?php
                                                            //Removing Addon Name from the Addon Option Name if it is there at the beginning
                                                            $prefix = $addon['namelanguage'];
                                                            $str = $addon['prices'][0]['pricelanguage'];
                                                            if (substr($str, 0, strlen($prefix)) == $prefix) {
                                                                $str = substr($str, strlen($prefix));
                                                            }
                                                            echo $str;
                                                        ?>
                                                    <?php } else { ?>
                                                        <select name="addonSelect_<?php echo $package["id"]; ?>_<?php echo $packageTerm; ?>_<?php echo $addon['id']; ?>" class="selectpicker form-control" onChange="loadTotalsInfo();">
                                                            <?php foreach ($addon['prices'] as $price) { ?>
                                                            <option value="addon_<?php echo $addon['id']; ?>_<?php echo $price['price_id']; ?>_<?php echo $price['recurringprice_cyle']; ?>" data-subtotal_amount="<?php echo $price['item_subtotal_amount']; ?>" <?php echo $price['price_selected']; ?>><?php echo $price['pricelanguage']; ?></option>
                                                            <?php } ?>
                                                        </select>
                                                    <?php } ?>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan="2">
                                                    <?php echo "* ".$this->user->lang("Prices will be multiplied by the quantity");?></br>
                                                </td>
                                            </tr>
                                        </table>
                                        </div>
                                        <?php break;
                                    case '0':    // dropdown ?>
                                    <div class="col-sm-6">
                                        <select name="addonSelect_<?php echo $package["id"]; ?>_<?php echo $packageTerm; ?>_<?php echo $addon['id']; ?>" class="selectpicker form-control" onChange="loadTotalsInfo();">
                                            <?php foreach ($addon['prices'] as $price) { ?>
                                            <option value="addon_<?php echo $addon['id']; ?>_<?php echo $price['price_id']; ?>_<?php echo $price['recurringprice_cyle']; ?>" data-subtotal_amount="<?php echo $price['item_subtotal_amount']; ?>" <?php echo $price['price_selected']; ?>><?php echo $price['pricelanguage']; ?></option>
                                            <?php } ?>
                                        </select>
                                        <br>
                                    </div>
                                        <?php break;
                                    case '1':    // radio buttons ?>
                                    <div class="col-12">
                                        <?php
                                        foreach ($addon['prices'] as $price) { ?>
                                            <div class="col-12 col-sm-6 pb-3">
                                                <label class="radio align-middle"">
                                                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="addonSelect_<?php echo $package["id"]; ?>_<?php echo $packageTerm; ?>_<?php echo $addon['id']; ?>" class="dropdown" id="addonSelect_<?php echo $package["id"]; ?>_<?php echo $packageTerm; ?>_<?php echo $addon['id']; ?>" value="addon_<?php echo $addon['id']; ?>_<?php echo $price['price_id']; ?>_<?php echo $price['recurringprice_cyle']; ?>" data-subtotal_amount="<?php echo $price['item_subtotal_amount']; ?>" style="background:none; border:0px;" <?php echo $price['price_selected']; ?> onChange="loadTotalsInfo();">
                                                    <?php echo $price['pricelanguage']; ?>
                                                </label>
                                            </div>
                                        <?php } ?>
                                        </div>
                                        <?php
                                        break;
                                } ?>
                                </br>
                            </div>
                        <?php } ?>
                    </div>
                    <?php } ?>
                </div>
                <?php if ($firstPackageTerm[$package['id']] !== $packageTerm) {
                    $addonStyle = 'style="display:none"';
                } ?>
            <?php } ?>
        <?php } ?>

        <?php if ($firstPackageId === false) {
            $firstPackageId = $package["id"];
            $packageStyle = 'style="display:none"';
        } ?>
    </div>
<?php } ?>

<div align="right">
    <table cellpadding="4" cellspacing="4" border=0 width="100%">
        <tr>
            <td align='right' class='totallabel'>
                <?php echo $this->user->lang("SubTotal"); ?>:
            </td>
            <td width='100px' class='totallabel' align='right'>
                <strong class='total_subtotal_price'>
                </strong>
            </td>
        </tr>
        <tr class='total_coupon_discount_row' <?php echo ($this->upgradeDowngradeStatus['upgradeValue'] == 0) ? 'style="display:none;"' : ''; ?> >
            <td align='right' class='totallabel'>
                <?php echo $this->user->lang("Prorated Credit"); ?>:
            </td>
            <td class='totallabel' width='100px' style='text-align:right;'>
                <strong class='total_subtotal_couponDiscount'>
                </strong>
            </td>
        </tr>
        <tr class='total_tax_row'>
            <td align='right' class='totallabel'>
                <?php echo $this->user->lang($this->customerTaxName) . ' (' . (float)$this->customerTax . '%)'; ?>:
            </td>
            <td class='totallabel' width='100px' style='text-align:right;'>
                <strong class='total_tax_amount'>
                </strong>
            </td>
        </tr>
        <tr class='total_tax2_row'>
            <td align='right' class='totallabel'>
                <?php echo $this->user->lang($this->customerTax2Name) . ' (' . (float)$this->customerTax2 . '%)'; ?>:
            </td>
            <td class='totallabel' width='100px' style='text-align:right;'>
                <strong class='total_tax2_amount'>
                </strong>
            </td>
        </tr>
        <tr>
            <td colspan="2" style="height:15px;">
            </td>
        </tr>
        <tr bgcolor='#eeeeee'>
            <td class='totallabel' align='right' style='height:30px;'>
                <strong>
                    <?php echo $this->user->lang("Grand Total");?>:
                </strong>
            </td>
            <td width='120px' class='totallabel' style='text-align:right;font-size:larger;height:7px;'>
                <strong class='total_total_pay'>
                </strong>
            </td>
        </tr>
    </table>
</div>

<script data-cfasync="false" type="text/javascript">
    function loadTotalsInfo()
    {
        packageId = $("#upgradePackage").val();

        $(".upgradePackageDescription").hide();
        $("#upgradePackageDescription_" + packageId).show();

        packageTerm = $('input[name="priceTerm_' + packageId + '"]:checked').val();

        $(".upgradeAddonDescription").hide();
        $("#upgradeAddonDescription_" + packageId + '_' + packageTerm).show();

        packageTaxable = Number($('select[name="upgradePackage"] option:selected').data('taxable'));
        packageSubtotalAmount = Number($('input[name="priceTerm_' + packageId + '"]:checked').data('subtotal_amount'));
        customerTax = Number($('input[name="customerTax"]').val());
        customerTax2 = Number($('input[name="customerTax2"]').val());
        customerTax2Compound = Number($('input[name="customerTax2Compound"]').val());
        upgradeDowngradeStatusValue = Number($('input[name="upgradeDowngradeStatusValue"]').val());

        packageAddonsString = $("#upgradeAddonDescription_" + packageId + "_" + packageTerm +" :input").serialize()

        if (packageAddonsString !== '') {
            packageAddonsArray =  packageAddonsString.split("&");
            addonQuantity = 1;
            $.each(packageAddonsArray, function (index, value) {
                packageAddonName = value.split("=");
                addonItem = $('input[name="' + packageAddonName[0] + '"]');

                if (addonItem.hasClass("quantity")) {
                    addonQuantity = Number(addonItem.val());
                } else if (addonItem.is(":radio")) {
                    addonItem = $('input[name="' + packageAddonName[0] + '"]:checked');
                    packageSubtotalAmount += (addonQuantity * Number(addonItem.data('subtotal_amount')));
                    addonQuantity = 1;
                } else if (addonItem.is(":hidden")) {
                    packageSubtotalAmount += (addonQuantity * Number(addonItem.data('subtotal_amount')));
                    addonQuantity = 1;
                } else {
                    addonItem = $('select[name="' + packageAddonName[0] + '"] option:selected');
                    packageSubtotalAmount += (addonQuantity * Number(addonItem.data('subtotal_amount')));
                    addonQuantity = 1;
                }
            });
        }

        packageTaxAmount = (packageTaxable * customerTax * packageSubtotalAmount / 100);
        packageTax2Amount = (packageTaxable * customerTax2 * (packageSubtotalAmount + (customerTax2Compound * packageTaxAmount)) / 100)
        packageTotal = packageSubtotalAmount + packageTaxAmount + packageTax2Amount - upgradeDowngradeStatusValue;

        if (packageTotal < 0) {
            packageTotal = 0;
        }

        $('.total_subtotal_price').html(accounting.formatMoney(packageSubtotalAmount, currency.symbol, currency.precision, currency.thousandssep, currency.decimalssep, currency.alignment)+currency.showabrv);

        $('.total_subtotal_couponDiscount').html(accounting.formatMoney(upgradeDowngradeStatusValue, currency.symbol, currency.precision, currency.thousandssep, currency.decimalssep, currency.alignment)+currency.showabrv);

        if (packageTaxAmount > 0) {
            $('.total_tax_amount').html(accounting.formatMoney(packageTaxAmount, currency.symbol, currency.precision, currency.thousandssep, currency.decimalssep, currency.alignment)+currency.showabrv);
            $('.total_tax_row').show();
        }else{
            $('.total_tax_row').hide();
        }

        if (packageTax2Amount > 0) {
            $('.total_tax2_amount').html(accounting.formatMoney(packageTax2Amount, currency.symbol, currency.precision, currency.thousandssep, currency.decimalssep, currency.alignment)+currency.showabrv);
            $('.total_tax2_row').show();
        }else{
            $('.total_tax2_row').hide();
        }

        $('.total_total_pay').html(accounting.formatMoney(packageTotal, currency.symbol, currency.precision, currency.thousandssep, currency.decimalssep, currency.alignment)+currency.showabrv);
    }

    $(document).ready(function() {
        clientexec.postpageload('.richwindow');

        $('.quantity').each(function() {
            $(this).keydown(function (e) {
                // Allow: delete, backspace, tab, escape, enter        Ctrl+A                                     end, home, left, up, right, down
                if ($.inArray(e.keyCode, [46, 8, 9, 27, 13]) !== -1 || (e.keyCode == 65 && e.ctrlKey === true) || (e.keyCode >= 35 && e.keyCode <= 40)) {
                         // let it happen, don't do anything
                         return;
                }

                // Ensure that it is a number and stop the keypress
                if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
                    e.preventDefault();
                }
            });
        });

        loadTotalsInfo();
    });

    clientexec.postpageload('.richwindow');
</script>
