<script data-cfasync="false" type="text/javascript">
    productaddon = {};
    productaddon.id = <?php echo $this->productaddonid; ?>;
    productaddon.grouptype = <?php echo $this->grouptype; ?>;
    productaddon.pluginvar = "<?php echo $this->pluginvar; ?>";
    <?php
        $js_array = json_encode($this->productgroups);
        echo "productaddon.productgroups = ". $js_array . ";\n";
    ?>

    productaddon.filterBy = function(el,filter) {
        $(el).closest('.nav-pills').find('li').removeClass('active')
        $(el).closest('li').addClass('active');

        $('.language').hide();
        $('.'+filter).show();
    }

    //Code for prices in different currencies
    function addonPricingFilterBy (el,filter)
    {
        $(el).closest('.nav-pills').find('li').removeClass('active')
        $(el).closest('li').addClass('active');

        $('.currencyprice').hide();
        $('.currencyCode'+filter).show();
    }
    //Code for prices in different currencies
</script>
<?php
    //DOMAIN
    $displayClass = 'displayIfDomain';

    //NOT DOMAIN
    if($this->grouptype != PACKAGE_TYPE_DOMAIN){
        $displayClass = 'displayIfOther';
    }
?>

<div class="alert alert-info ce-page-header">
  <div class="alert-header"><?php echo $this->user->lang('Product Addon'); ?></div>
  <p class="alert-content"><?php echo $this->user->lang('Addons are additional services/product addons that can be attached to product orders.'); ?></p>
</div>

<div style="margin-top:-8px;margin-bottom:10px;"><i class="icon-level-up icon-flip-horizontal icon-back-to-parent-list"></i> <a href="index.php?fuse=admin&controller=addons&view=productaddons"><?php echo $this->user->lang("Addon list"); ?></a></div>

<div class="fieldwrapper withcolor">
    <form id="addondata">
    <?php $languages = CE_Lib::getEnabledLanguages(true);
    if(count($languages) > 1){
        $active = true; ?>
        <ul class="nav nav-pills">
        <?php foreach($languages as $languageKey => $language){ ?>
            <li <?php if($active){echo 'class="active"';} ?> >
                <a <?php if($active){$active = false; echo 'class="default-language-tab"';} ?> href="javascript:void(0)" onclick="productaddon.filterBy(this,'<?php echo $languageKey; ?>')"><?php echo $language; ?></a>
            </li>
        <?php } ?>
        </ul>
    <?php } ?>

    <label><?php echo $this->user->lang("Name of Addon"); ?></label>
    <?php if(count($languages) > 1){
        $active = true;
        foreach($languages as $languageKey => $language){ ?>
            <span class="language <?php echo $languageKey; ?> text name" <?php if(!$active){echo 'style="display: none"';} ?> ><input class="required" type="text" style="width:500px;" name="product-addon-name<?php echo $languageKey; ?>" <?php if($active){$active = false; echo 'id="mainlanguageproduct-addon-name"';} ?> value="<?php echo $this->addonNameLanguages[$languageKey]; ?>" /></span>
        <?php }
    }else{ ?>
        <span class="language text name" ><input class="required" type="text" style="width:500px;" name="product-addon-name" id="mainlanguageproduct-addon-name" value="<?php echo $this->addonName; ?>" /></span>
    <?php } ?>

    <label style="margin-top:15px;"><span class='tip-target' data-toggle="tooltip" data-placement='right' title='Select which products groups this addon is available to.  Assigning product groups helps in managing addon selection in the product edit screen as it limits only addons that are applicable to your product.'><?php echo $this->user->lang("Available To Product Groups"); ?></span></label>
    <select multiple="multiple" placeholder="Select product group(s)" id="product-groups" name="product-groups[]" style="width:100%;display:none;" class="disableSelect2AutoLoad" id="product-groups">
        <?php
            foreach ($this->productgroups as $key=>$cat) {
                echo "<optgroup value='0' data-grouptype='".$cat[0]['typeid']."'  label='Groups of type: ".$key."'>";
                foreach($cat as $productgroup)
                {
                    $selected = (in_array($productgroup['id'],$this->productgroup_ids)) ? "selected" : "";
                    echo "<option ".$selected." value='".$productgroup['id']."'>".$productgroup['name']."</option>";
                }
                echo "</optgroup>";
            }
        ?>
    </select>
    <br/>
    <label style="margin-top:15px;"><span class='tip-target'  data-placement='right' data-toggle="tooltip" title='Visible to clients during the order process.'><?php echo $this->user->lang("Addon description"); ?></span></label>
    <?php if(count($languages) > 1){
        $active = true;
        foreach($languages as $languageKey => $language){ ?>
            <span class="language <?php echo $languageKey; ?> textarea" <?php if(!$active){echo 'style="display: none"';} ?> ><textarea class="wysihtml5" name="product-addon-description<?php echo $languageKey; ?>" <?php if($active){$active = false; echo 'id="mainlanguageproduct-addon-description"';} ?> style='height:70px;display:none;'><?php echo $this->descriptionLanguages[$languageKey]; ?></textarea></span>
        <?php }
    }else{ ?>
        <span class="language textarea" ><textarea class="wysihtml5" name="product-addon-description" id="mainlanguageproduct-addon-description" style='height:70px;display:none;'><?php echo $this->description; ?></textarea></span>
    <?php } ?>

    <ul class="nav nav-tabs" style="padding-top:20px;">
        <li class="active"><span data-toggle="tab" data-target="#div-addonoptions"><a href="#"><?php echo $this->user->lang('Addon Options'); ?></a></span></li>
        <li><span data-toggle="tab" data-target="#div-addonpluginsettings"><a href="#"><?php echo $this->user->lang('Advanced Settings'); ?></a></span></li>
    </ul>
    <div class="tab-content">
        <div class="tab-pane active" id="div-addonoptions">

            <div class="alert alert-warning">
            <?php echo $this->user->lang('Prices left empty will not be shown as an available option for this addon. Tick the Force checkbox below the billing period price to force showing this option even when the client has selected a non matching billing cycle during signup.'); ?>
            </div>

<?php
    //Code for prices in different currencies
    include_once 'modules/billing/models/CurrencyGateway.php';

    $gateway = new CurrencyGateway($this->user);
    $currencies = $gateway->GetCurrencies();

    if ($currencies['totalcount'] > 1) {
?>
        <ul class="nav nav-pills">
<?php
        foreach ($currencies['currencies'] as $currencyValues) {
?>
            <li <?php if ($currencyValues['isdefault']) { echo 'class="active"'; } ?> >
                <a <?php if ($currencyValues['isdefault']) { echo 'class="default-language-tab"'; } ?> href="javascript:void(0)" onclick="addonPricingFilterBy(this,'<?php echo $currencyValues['abrv']; ?>')"><?php echo $currencyValues['name']; ?></a>
            </li>
<?php
        }
?>
        </ul>
<?php
    }
    //Code for prices in different currencies
?>
            <table id="table-addonoptions" class="table table-hover">
                <thead>
                    <tr class="nodrag nodrop">
                        <th style="width:13px;"></th>
                        <th>&nbsp;<span class='tip-target' data-html="true" data-toggle="tooltip"  data-placement='right' title="<?php echo $this->user->lang("Description of the addon option displayed to client"); ?>"><?php echo $this->user->lang('Option Label'); ?></span></th>
                        <?php foreach ($this->productAddonsPricesExtraData as $cycle => $extraDataValues) {
                            if ($cycle == 0) {
                                continue;
                            } ?>
                                <th class="<?php echo implode(' ', $this->productAddonsPricesExtraData[$cycle]['class']); ?>" <?php echo (in_array($displayClass, $this->productAddonsPricesExtraData[$cycle]['class']))? '': ' style="display:none;" '; ?>>&nbsp;<?php echo $this->user->lang($this->productAddonsPricesExtraData[$cycle]['name']); ?></th>
                        <?php } ?>
                        <th class="<?php echo implode(' ', $this->productAddonsPricesExtraData[0]['class']); ?>" <?php echo (in_array($displayClass, $this->productAddonsPricesExtraData[0]['class']))? '': ' style="display:none;" '; ?>>&nbsp;<?php echo $this->user->lang($this->productAddonsPricesExtraData[0]['name']); ?></th>
                        <th class="pluginvaluecell">&nbsp;<span class='tip-target' data-html="true" data-toggle="tooltip" title="<?php echo $this->user->lang("The value sent to selected plugin when this option is selected"); ?>"><?php echo $this->user->lang('Plugin Value'); ?></span></th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    <tr class="nooption nodrag nodrop" <?php if (count($this->productAddonsPrices) > 0) {?> style="display:none;" <?php } ?>>
                        <td colspan="10" class="nooptionsavailable"><div><?php echo $this->user->lang('No options have been defined for this addon'); ?></div>
                    </td>
                    </tr>
                    <tr class="clone-addon-option" style="display:none;">
                        <td><div class="drag-icon"></div></td>
                        <td>
                            <?php if(count($languages) > 1){
                                $active = true;
                                foreach($languages as $languageKey => $language){ ?>
                                    <span class="language <?php echo $languageKey; ?> text <?php if($active){echo 'mainlanguageoptionname';} ?>" <?php if(!$active){echo 'style="display: none"';}else{$active = false;} ?> ><input class="required" type="text" name="optionname<?php echo $languageKey; ?>" style="width:110px;"><br/></span>
                                <?php }
                            }else{ ?>
                                <input class="required" type="text" name="optionname" style="width:110px;"><br/>
                            <?php } ?>
                            <label class="checkbox inline">
                              <input type="checkbox" name="openticket"> <span data-toggle="tooltip" title='Open ticket upon selecting this addon'><small><?php echo $this->user->lang('Open Ticket'); ?></small></span>
                            </label>
                        </td>
<!-- CODE FOR PRICES IN DIFFERENT CURRENCIES -->
<?php
    if ($currencies['totalcount'] > 1) {
?>
                        <?php foreach ($this->productAddonsPricesExtraData as $cycle => $extraDataValues) {
                            if ($cycle == 0) {
                                continue 1;
                            } ?>
                                <td class="<?php echo implode(' ', $this->productAddonsPricesExtraData[$cycle]['class']); ?>" <?php echo (in_array($displayClass, $this->productAddonsPricesExtraData[$cycle]['class']))? '': ' style="display:none;" '; ?>>
<?php
                                    foreach ($currencies['currencies'] as $currencyValues) {
?>
                                        <span class="currencyprice currencyCode<?php echo $currencyValues['abrv']; ?>" <?php if (!$currencyValues['isdefault']) { echo 'style="display: none"'; } ?> >
                                            <input class="price" type="text" name='<?php echo $currencyValues['abrv']; ?>_price<?php echo $cycle; ?>' <?php if ($currencyValues['isdefault']) { echo 'id="maincurrencyprice'.$cycle.'"'; } ?> style="width:40px;text-align:right;" ><br/>
                                            <label class="checkbox inline displayIfOther" <?php echo (in_array($displayClass, array('displayIfOther')))? '': ' style="display:none;" '; ?>>
                                              <input class="force" type="checkbox" name="<?php echo $currencyValues['abrv']; ?>_price<?php echo $cycle; ?>_force" <?php if ($currencyValues['isdefault']) { echo 'id="maincurrencyprice'.$cycle.'_force"'; } ?>> <span data-toggle="tooltip" title='Click checkbox if you want to force showing this period for this option even if the client has not selected this billing cycle as their selected billing period.'><small><?php echo $this->user->lang('Force'); ?></small></span>
                                            </label>
                                        </span>
<?php
                                    }
?>
                                </td>
                        <?php } ?>
                        <td class="<?php echo implode(' ', $this->productAddonsPricesExtraData[0]['class']); ?>" <?php echo (in_array($displayClass, $this->productAddonsPricesExtraData[0]['class']))? '': ' style="display:none;" '; ?>>
<?php
                            foreach ($currencies['currencies'] as $currencyValues) {
?>
                                <span class="currencyprice currencyCode<?php echo $currencyValues['abrv']; ?>" <?php if (!$currencyValues['isdefault']) { echo 'style="display: none"'; } ?> >
                                    <input class="price" type="text" name='<?php echo $currencyValues['abrv']; ?>_price0' <?php if ($currencyValues['isdefault']) { echo 'id="maincurrencyprice0"'; } ?> style="width:40px;text-align:right;" ><br/>
                                </span>
<?php
                            }
?>
                        </td>
<?php
    } else {
?>
                        <?php foreach ($this->productAddonsPricesExtraData as $cycle => $extraDataValues) {
                            if ($cycle == 0) {
                                continue;
                            } ?>
                                <td class="<?php echo implode(' ', $this->productAddonsPricesExtraData[$cycle]['class']); ?>" <?php echo (in_array($displayClass, $this->productAddonsPricesExtraData[$cycle]['class']))? '': ' style="display:none;" '; ?>>
                                    <input class="price" type="text" name='price<?php echo $cycle; ?>' id='maincurrencyprice<?php echo $cycle; ?>' style="width:40px;text-align:right;" ><br/>
                                    <label class="checkbox inline displayIfOther" <?php echo (in_array($displayClass, array('displayIfOther')))? '': ' style="display:none;" '; ?>>
                                      <input class="force" type="checkbox" name="price<?php echo $cycle; ?>_force" id="maincurrencyprice<?php echo $cycle; ?>_force"> <span data-toggle="tooltip" title='Click checkbox if you want to force showing this period for this option even if the client has not selected this billing cycle as their selected billing period.'><small><?php echo $this->user->lang('Force'); ?></small></span>
                                    </label>
                                </td>
                        <?php } ?>
                        <td class="<?php echo implode(' ', $this->productAddonsPricesExtraData[0]['class']); ?>" <?php echo (in_array($displayClass, $this->productAddonsPricesExtraData[0]['class']))? '': ' style="display:none;" '; ?>>
                            <input class="price" type="text" name='price0' id='maincurrencyprice0' style="width:40px;text-align:right;" ><br/>
                        </td>
<?php
    }
?>
<!-- /CODE FOR PRICES IN DIFFERENT CURRENCIES -->
                        <td class="pluginvaluecell">
                            <input type="text" name='plugin_var_value' style="width:50px;text-align:right;">
                        </td>
                        <td>
                            <button type="button" data-candelete="1" class="close removeoption">&times;</button>
                        </td>
                    </tr>
<!-- CODE FOR PRICES IN DIFFERENT CURRENCIES -->
<?php
    if ($currencies['totalcount'] > 1) {
?>
                    <?php foreach ($this->productAddonsPrices as $option) { ?>
                        <tr class="addon-option" data-new-id="0" data-addon-id="<?php echo $option['id']; ?>">
                            <td><div class="drag-icon"></div></td>
                            <td>
                                <?php if(count($languages) > 1){
                                    $active = true;
                                    foreach($languages as $languageKey => $language){ ?>
                                        <span class="language <?php echo $languageKey; ?> text <?php if($active){echo 'mainlanguageoptionname';} ?>" <?php if(!$active){echo 'style="display: none"';}else{$active = false;} ?> ><input class="required" type="text" name="optionname<?php echo $languageKey; ?>" style="width:110px;" value="<?php echo $option['detailLanguages'][$languageKey]; ?>" placeholder=""><br/></span>
                                    <?php }
                                }else{ ?>
                                    <input class="required" type="text" name="optionname" style="width:110px;" value="<?php echo $option['detail']; ?>" placeholder=""><br/>
                                <?php } ?>
                                <label class="checkbox inline">
                                  <input type="checkbox" name="openticket" <?php if($option['openticket'] == 1) echo "checked"; ?>> <span data-toggle="tooltip" title='Open ticket upon selecting this addon'><small>Open Ticket</small></span>
                                </label>
                            </td>
                            <?php foreach ($this->productAddonsPricesExtraData as $cycle => $extraDataValues) {
                                if ($cycle == 0) {
                                    continue 1;
                                } ?>
                                <td class="<?php echo implode(' ', $this->productAddonsPricesExtraData[$cycle]['class']); ?>" <?php echo (in_array($displayClass, $this->productAddonsPricesExtraData[$cycle]['class']))? '': ' style="display:none;" '; ?>>
<?php
                                    foreach ($currencies['currencies'] as $currencyValues) {
?>
                                        <span class="currencyprice currencyCode<?php echo $currencyValues['abrv']; ?>" <?php if (!$currencyValues['isdefault']) { echo 'style="display: none"'; } ?> >
                                            <input class="price" type="text" name='<?php echo $currencyValues['abrv']; ?>_price<?php echo $cycle; ?>' <?php if ($currencyValues['isdefault']) { echo 'id="maincurrencyprice'.$cycle.'"'; } ?> style="width:40px;text-align:right;" value="<?php echo $this->productAddonsPricesCurrencies[$currencyValues['abrv']][$option['id']]['price'.$cycle]; ?>"><br/>
                                            <label class="checkbox inline displayIfOther" <?php echo (in_array($displayClass, array('displayIfOther')))? '': ' style="display:none;" '; ?>>
                                              <input class="force" type="checkbox" name="<?php echo $currencyValues['abrv']; ?>_price<?php echo $cycle; ?>_force" <?php if ($currencyValues['isdefault']) { echo 'id="maincurrencyprice'.$cycle.'_force"'; } ?> <?php if($this->productAddonsPricesCurrencies[$currencyValues['abrv']][$option['id']]['price'.$cycle.'_force'] == 1) echo "checked"; ?>> <span data-toggle="tooltip" title='Click checkbox if you want to force showing this period for this option even if the client has not selected this billing cycle as their selected billing period.'><small><?php echo $this->user->lang('Force'); ?></small></span>
                                            </label>
                                        </span>
<?php
                                    }
?>
                                </td>
                            <?php } ?>
                            <td class="<?php echo implode(' ', $this->productAddonsPricesExtraData[0]['class']); ?>" <?php echo (in_array($displayClass, $this->productAddonsPricesExtraData[0]['class']))? '': ' style="display:none;" '; ?>>
<?php
                                    foreach ($currencies['currencies'] as $currencyValues) {
?>
                                <span class="currencyprice currencyCode<?php echo $currencyValues['abrv']; ?>" <?php if (!$currencyValues['isdefault']) { echo 'style="display: none"'; } ?> >
                                    <input class="price" type="text" name='<?php echo $currencyValues['abrv']; ?>_price0' <?php if ($currencyValues['isdefault']) { echo 'id="maincurrencyprice0"'; } ?> style="width:40px;text-align:right;" value="<?php echo $this->productAddonsPricesCurrencies[$currencyValues['abrv']][$option['id']]['price0']; ?>"><br/>
                                </span>
<?php
                                    }
?>
                            </td>
                            <td class="pluginvaluecell">
                                <input type="text" name='plugin_var_value' style="width:50px;text-align:right;" value="<?php echo $option['plugin_var_value']; ?>">
                            </td>
                            <td>
                                <button type="button" data-candelete="<?php echo $option['delete_ok']; ?>" class="close removeoption">&times;</button>
                            </td>
                        </tr>
                    <?php } ?>
<?php
    } else {
?>
                    <?php foreach ($this->productAddonsPrices as $option) { ?>
                        <tr class="addon-option" data-new-id="0" data-addon-id="<?php echo $option['id']; ?>">
                            <td><div class="drag-icon"></div></td>
                            <td>
                                <?php if(count($languages) > 1){
                                    $active = true;
                                    foreach($languages as $languageKey => $language){ ?>
                                        <span class="language <?php echo $languageKey; ?> text <?php if($active){echo 'mainlanguageoptionname';} ?>" <?php if(!$active){echo 'style="display: none"';}else{$active = false;} ?> ><input class="required" type="text" name="optionname<?php echo $languageKey; ?>" style="width:110px;" value="<?php echo $option['detailLanguages'][$languageKey]; ?>" placeholder=""><br/></span>
                                    <?php }
                                }else{ ?>
                                    <input class="required" type="text" name="optionname" style="width:110px;" value="<?php echo $option['detail']; ?>" placeholder=""><br/>
                                <?php } ?>
                                <label class="checkbox inline">
                                  <input type="checkbox" name="openticket" <?php if($option['openticket'] == 1) echo "checked"; ?>> <span data-toggle="tooltip" title='Open ticket upon selecting this addon'><small>Open Ticket</small></span>
                                </label>
                            </td>
                            <?php foreach ($this->productAddonsPricesExtraData as $cycle => $extraDataValues) {
                                if ($cycle == 0) {
                                    continue;
                                } ?>
                                <td class="<?php echo implode(' ', $this->productAddonsPricesExtraData[$cycle]['class']); ?>" <?php echo (in_array($displayClass, $this->productAddonsPricesExtraData[$cycle]['class']))? '': ' style="display:none;" '; ?>>
                                    <input class="price" type="text" name='price<?php echo $cycle; ?>' style="width:40px;text-align:right;" value="<?php echo $option['price'.$cycle]; ?>"><br/>
                                    <label class="checkbox inline displayIfOther" <?php echo (in_array($displayClass, array('displayIfOther')))? '': ' style="display:none;" '; ?>>
                                      <input class="force" type="checkbox" name="price<?php echo $cycle; ?>_force" <?php if($option['price'.$cycle.'_force'] == 1) echo "checked"; ?>> <span data-toggle="tooltip" title='Click checkbox if you want to force showing this period for this option even if the client has not selected this billing cycle as their selected billing period.'><small><?php echo $this->user->lang('Force'); ?></small></span>
                                    </label>
                                </td>
                            <?php } ?>
                            <td class="<?php echo implode(' ', $this->productAddonsPricesExtraData[0]['class']); ?>" <?php echo (in_array($displayClass, $this->productAddonsPricesExtraData[0]['class']))? '': ' style="display:none;" '; ?>>
                                <input class="price" type="text" name='price0' style="width:40px;text-align:right;" value="<?php echo $option['price0']; ?>"><br/>
                            </td>
                            <td class="pluginvaluecell">
                                <input type="text" name='plugin_var_value' style="width:50px;text-align:right;" value="<?php echo $option['plugin_var_value']; ?>">
                            </td>
                            <td>
                                <button type="button" data-candelete="<?php echo $option['delete_ok']; ?>" class="close removeoption">&times;</button>
                            </td>
                        </tr>
                    <?php } ?>
<?php
    }
?>
<!-- /CODE FOR PRICES IN DIFFERENT CURRENCIES -->
                </tbody>
            </table>

            <button type="button" class="btn rich-button add-product-addon"><span><?php echo $this->user->lang('Add Option'); ?></span></button>

        </div>
        <div class="tab-pane" id="div-addonpluginsettings" style="margin-left:10px;">

            <div class="alert alert-warning">
                <?php echo $this->user->lang('Integrate this Addon with the Plugin associated to your Product, providing plugins additional instructions when setting up products based on the values selected for this addon.'); ?>
            </div>

            <label style="margin-top:10px;"><?php echo $this->user->lang("Plugin Variable"); ?></label>
            <select name="pluginoption" id="pluginoption" style="width:330px;"></select><br/>
            <div id="custompluginvariable" style="display:none;">
                <label style="margin-top:15px;"><span data-toggle="tooltip" title='<?php echo $this->user->lang("Name to be passed to any API with the selected option value"); ?>.'><?php echo $this->user->lang("Variable Name"); ?></span></label>
                <input type="text" id="custompluginvariable_value" style="width:317px;" />
            </div>
            <div style="margin-top:15px;">
                <label id="pluginvar_description"></label>
                <label id="pluginvar_availablein"></label>
            </div>
        </div>
    </div>

    <div class="form-actions">
        <div class="pull-right">
          <button type="submit" class="submit-addoon btn btn-primary"><?php echo $this->user->lang("Save Changes"); ?></button>
        </div>
    </div>
    </form>
</div>