<?php $count = 1;?>
<div class="row">
    <?php if($GatewayName != 'WorldSMS'): ?>
    <div class="col-md-12 cdrrerateaccountsbox">
        <div class="form-group">
            <label class="control-label">CDR Rerate Accounts <span type="button" class="label label-info popover-primary" data-toggle="popover" data-trigger="hover" data-placement="top" data-original-title="ReRate Accounts" data-content="If blank all accounts CDRs will be rated. Otherwise only selected accounts CDRs will be rated.">?</span></label>
            <?php echo Form::select('Accounts[]', $Accounts, isset($gatewayconfigval->Accounts) ? $gatewayconfigval->Accounts : [] ,array("class"=>"form-control select2", "multiple"=>"multiple")); ?>

        </div>
    </div>
    <?php endif; ?>
    <?php if($GatewayName == 'Free PBX'): ?>
        <div class="form-group col-md-6">
            <label class="control-label">Accounts</label>
            <?php echo Form::select('AccountID', $Accounts, '' ,array("class"=>"form-control select2")); ?>

        </div>
    <?php endif; ?>
<?php foreach($gatewayconfig as $configkey => $configtitle): ?>
    <?php $selectd_val ='';
        if(isset($gatewayconfigval) && isset($gatewayconfigval->$configkey)){
            $selectd_val =$gatewayconfigval->$configkey;
        }
        if($configkey != 'AllowAccountImport'){
            $count++;
        }
        $NameFormat =  GatewayConfig::$NameFormat;
        if($GatewayName == 'CenterNext'){
            $NameFormat = GatewayConfig::$CenterNext_NameFormat;
        }
        else if($GatewayName == 'Porta'){
            $NameFormat = GatewayConfig::$Porta_NameFormat;
        }else if($GatewayName == 'VOS'){
            $NameFormat = GatewayConfig::$Vos_NameFormat;
        }else if($GatewayName == 'SippySFTP'){
            $NameFormat = GatewayConfig::$Sippy_NameFormat;
        }else if($GatewayName == 'PBX'){
            $NameFormat = GatewayConfig::$Mirta_NameFormat;
        }else if($GatewayName == 'MOR'){
            $NameFormat = GatewayConfig::$MOR_NameFormat;
        }else if($GatewayName == 'CallShop'){
            $NameFormat = GatewayConfig::$CallShop_NameFormat;
        }else if($GatewayName == 'Streamco'){
            $NameFormat = GatewayConfig::$Streamco_NameFormat;
        }else if($GatewayName == 'FusionPBX'){
            $NameFormat = GatewayConfig::$FusionPBX_NameFormat;
        }else if($GatewayName == 'M2'){
            $NameFormat = GatewayConfig::$M2_NameFormat;
        }else if($GatewayName == 'VoipNow'){
            $NameFormat = GatewayConfig::$VoipNow_NameFormat;
        }else if($GatewayName == 'SippySQL'){
            $NameFormat = GatewayConfig::$SippySQL_NameFormat;
        }else if($GatewayName == 'VoipMS'){
            $NameFormat = GatewayConfig::$VoipMS_NameFormat;
        }else if($GatewayName == 'ClarityPBX'){
            $NameFormat = GatewayConfig::$ClarityPBX_NameFormat;
        }else if($GatewayName == 'VodiaPBX'){
            $NameFormat = GatewayConfig::$VodiaPBX_NameFormat;
        }else if($GatewayName == 'AlarisLab' || $GatewayName == 'AlarisLabSSH'){
            $NameFormat = GatewayConfig::$AlarisLab_NameFormat;
        }else if($GatewayName == 'SQLServer'){
            $NameFormat = GatewayConfig::$SQLServer;
        }else if($GatewayName == 'AsteriskDB'){
            $NameFormat = GatewayConfig::$AsteriskDB;
        }else if($GatewayName == 'MysqlDB'){
            $NameFormat = GatewayConfig::$MysqlDB;
        }else if($GatewayName == 'WorldSMS'){
            $NameFormat = GatewayConfig::$WorldSMS_NameFormat;
        }else if($GatewayName == 'ConnexCS' ){
            $NameFormat = GatewayConfig::$ConnexCS_NameFormat;
        }else if($GatewayName == 'BatchDialer' ){
            $NameFormat = GatewayConfig::$BatchDialer_NameFormat;
        }
    ?>


    <?php if($count%2 == 0 && $configkey != 'AllowAccountImport'): ?>
            <div class="clear"></div>
    <?php endif; ?>

        <?php if($configkey == 'AllowAccountIPImport'): ?>
            <input id="AllowAccountIPImport"  type="hidden" name="AllowAccountIPImport" value="1">
        <?php endif; ?>
        <?php if($configkey == 'AllowAccountImport'): ?>
            <input id="AllowAccountImport"  type="hidden" name="AllowAccountImport" value="1">
        <?php else: ?>

     <div class="col-md-6 " <?php if($configkey == 'RateFormat'): ?> id="rate_dropdown" <?php endif; ?>>
        <div class="form-group" id="<?php echo $configkey; ?>Box">
            <?php if($configkey != 'AllowAccountImport' && $configkey != 'AllowAccountIPImport'): ?>
            <label  class="control-label <?php if($configkey == 'RateCDR'): ?> col-md-13 <?php endif; ?>"><?php echo $configtitle; ?>

                <?php if($configkey=='AutoAddIP'): ?> <span type="button" class="label label-info popover-primary" data-toggle="popover" data-trigger="hover" data-placement="top" data-original-title="Auto Add IP" data-content="IP will be automatically added to the account if account name matches to the switch account name. Turn ON Auto Add IP notification from Admin > Notifications.">?</span> <?php endif; ?>
                <?php if($configkey=='NameFormat' && $GatewayName == 'FusionPBX'): ?> <span type="button" class="label label-info popover-primary" data-html="true" data-toggle="popover" data-trigger="hover" data-placement="top" data-original-title="Authentication Rule" data-content="Account Name will be match with domain_description. <br/> Account Number will match with domain_name.">?</span> <?php endif; ?>
           </label>
            <?php endif; ?>

            <?php if($configkey == 'NameFormat'): ?>
                <?php echo Form::select($configkey,$NameFormat,$selectd_val,array( "class"=>"select2 small","id"=>$configkey)); ?>

            <?php elseif($configkey == 'CallType'): ?>
                <?php echo Form::select($configkey,GatewayConfig::$CallType,$selectd_val,array( "class"=>"select2 small")); ?>

            <?php elseif($configkey == 'BillingTime'): ?>
                <?php echo Form::select($configkey,Company::$billing_time,$selectd_val,array( "class"=>"select2 small")); ?>

            <?php elseif($configkey == 'RateCDR'): ?>
                <div class="clear col-md-13">
                <p class="make-switch switch-small">
                    <input id="RateCDR"  type="checkbox"   <?php if($selectd_val == 1): ?> checked=""  <?php endif; ?> name="RateCDR" value="1">
                </p>
                </div>
            <?php elseif($configkey == 'RateMethod'): ?>
                <?php echo Form::select($configkey,UsageDetail::$RateMethod,$selectd_val,array( "class"=>"select2 small ReRateOptions","id"=>$configkey)); ?>

            <?php elseif($configkey == 'SpecifyRate'): ?>
                 <input id="SpecifyRate"  type="text"  class="form-control ReRateOptions"  value="<?php echo $selectd_val; ?>"  name="SpecifyRate" >
            <?php elseif($configkey == 'AutoAddIP'): ?>
                <div class="clear col-md-13">
                    <p class="make-switch switch-small">
                        <input id="AutoAddIP"  type="checkbox"   <?php if($selectd_val == 1): ?> checked=""  <?php endif; ?> name="AutoAddIP" value="1">
                    </p>
                </div>
            <?php elseif($configkey == 'RateFormat'): ?>
                <?php
                $disable = array();
                if(!empty($selectd_val)){
                    $disable = array('disabled'=>'disabled');
                }
                $options = array_merge(array( "class"=>"select2 small"),$disable);
                ?>
                <input type="hidden" name="RateFormat" value="<?php echo $selectd_val; ?>">
                <?php echo Form::select($configkey,Company::$rerate_format,$selectd_val,$options); ?>


            <?php elseif($configkey == 'key'): ?>
                    <br/>
                    <input type="hidden" name="oldkey" value="<?php if(isset($gatewayconfigval) && isset($gatewayconfigval->$configkey)): ?><?php echo $gatewayconfigval->$configkey; ?><?php endif; ?>">
                    <input  name="<?php echo $configkey; ?>" type="file"
                           class="form-control file2 inline btn btn-primary"
                           data-label="<i class='glyphicon glyphicon-circle-arrow-up'></i>&nbsp;   Browse"/>
                 <span class="file-input-name">
                        <?php if(isset($gatewayconfigval->$configkey)): ?>
                            <?php echo basename($gatewayconfigval->$configkey); ?>

                        <?php endif; ?>
                 </span>

            <?php elseif($configkey == 'protocol_type'): ?>
                    <?php echo Form::select($configkey,Gateway::$protocol_type,$selectd_val,array( "class"=>"select2 small","id"=>$configkey)); ?>

            <?php elseif($configkey == 'passive_mode'): ?>
                <div class="clear col-md-13">
                    <p class="make-switch switch-small">
                        <input id="<?php echo $configkey; ?>"  type="checkbox"   <?php if($selectd_val == 1): ?> checked=""  <?php endif; ?> name="<?php echo $configkey; ?>" value="1">
                    </p>
                </div>

            <?php elseif($configkey == 'ssl'): ?>
                <div class="clear col-md-13">
                    <p class="make-switch switch-small">
                        <input id="<?php echo $configkey; ?>"  type="checkbox"   <?php if($selectd_val == 1): ?> checked=""  <?php endif; ?> name="<?php echo $configkey; ?>" value="1">
                    </p>
                </div>
            <?php else: ?>
                <?php if($configkey != 'AllowAccountIPImport'): ?>
                    <input <?php if($configkey == 'password' || $configkey == 'dbpassword' || $configkey == 'sshpassword' || $configkey == 'api_password'): ?> type="password" <?php else: ?> type="text" <?php endif; ?>  value="<?php if(isset($gatewayconfigval) && isset($gatewayconfigval->$configkey) && !empty($gatewayconfigval->$configkey) && ($configkey == 'password' || $configkey == 'dbpassword' || $configkey == 'sshpassword' || $configkey == 'api_password')): ?><?php echo ''; ?><?php elseif(isset($gatewayconfigval) && isset($gatewayconfigval->$configkey)): ?><?php echo $gatewayconfigval->$configkey; ?><?php endif; ?>" name="<?php echo $configkey; ?>" class="form-control"  placeholder="">
                <?php endif; ?>

                <?php if($configkey == 'password'): ?><input type="hidden" disabled value="<?php if(isset($gatewayconfigval) && isset($gatewayconfigval->$configkey) && !empty($gatewayconfigval->$configkey) && $configkey == 'password'   ): ?><?php echo ''; ?><?php endif; ?>" name="<?php echo $configkey; ?>_disabled" class="form-control"  placeholder=""><?php endif; ?>
                <?php if($configkey == 'dbpassword'): ?><input type="hidden" disabled value="<?php if(isset($gatewayconfigval) && isset($gatewayconfigval->$configkey) && !empty($gatewayconfigval->$configkey) && $configkey == 'dbpassword'   ): ?><?php echo ''; ?><?php endif; ?>" name="<?php echo $configkey; ?>_disabled" class="form-control"  placeholder=""><?php endif; ?>
                <?php if($configkey == 'sshpassword'): ?><input type="hidden" disabled value="<?php if(isset($gatewayconfigval) && isset($gatewayconfigval->$configkey) && !empty($gatewayconfigval->$configkey) && $configkey == 'sshpassword'   ): ?><?php echo ''; ?><?php endif; ?>" name="<?php echo $configkey; ?>_disabled" class="form-control"  placeholder=""><?php endif; ?>
                <?php if($configkey == 'api_password'): ?><input type="hidden" disabled value="<?php if(isset($gatewayconfigval) && isset($gatewayconfigval->$configkey) && !empty($gatewayconfigval->$configkey) && $configkey == 'api_password'   ): ?><?php echo ''; ?><?php endif; ?>" name="<?php echo $configkey; ?>_disabled" class="form-control"  placeholder=""><?php endif; ?>

            <?php endif; ?>
         </div>
    </div>
        <?php endif; ?>
<?php endforeach; ?>
</div>

<script>


    $(document).ready(function() {
        $('[data-toggle="popover"]').each(function(i, el)
        {
            var $this = $(el),
                placement = attrDefault($this, 'placement', 'right'),
                trigger = attrDefault($this, 'trigger', 'click'),
                popover_class = $this.hasClass('popover-secondary') ? 'popover-secondary' : ($this.hasClass('popover-primary') ? 'popover-primary' : ($this.hasClass('popover-default') ? 'popover-default' : ''));

            $this.popover({
                placement: placement,
                trigger: trigger
            });

            $this.on('shown.bs.popover', function(ev)
            {
                var $popover = $this.next();

                $popover.addClass(popover_class);
            });
        });

        $(document).on('change', 'select[name="RateMethod"]', function() {
            $('#SpecifyRateBox').parent().addClass('hidden');
            if ($(this).val() == 'SpecifyRate' || $(this).val() == 'ValueAgainstCost') {
                $('#SpecifyRateBox').parent().removeClass('hidden');
            }
        });

        $(document).on('change', 'select[name="protocol_type"]', function() {

            if ($(this).val() == '<?php echo Gateway::SSH_FILE_TRANSFER; ?>' ){

                $("input[name='port']").val("").parents(".form-group").parent().addClass("hidden");
                $("input[name='ssl']").parents(".form-group").parent().addClass("hidden");
                $("input[name='passive_mode']").parents(".form-group").parent().addClass("hidden");

            }else {

                $("input[name='port']").parents(".form-group").parent().removeClass("hidden");
                $("input[name='ssl']").parents(".form-group").parent().removeClass("hidden");
                $("input[name='passive_mode']").parents(".form-group").parent().removeClass("hidden");

            }
        });
        $('select[name="protocol_type"]').trigger('change');


        $(document).on('change', 'select[name="NameFormat"]', function() {

        });

        // Replaced File Input
        $("input.file2[type=file]").each(function(i, el)
        {
            var $this = $(el),
                    label = attrDefault($this, 'label', 'Browse');

            $this.bootstrapFileInput(label);
        });

        // Jasny Bootstrap | Fileinput
        if ($.isFunction($.fn.fileinput))
        {
            $(".fileinput").fileinput()
        }

    });

</script>