<?php
/**
 * Copyright © 2012 Alcatel-Lucent.
 *
 * See the NOTICE file distributed with this work for additional information regarding copyright ownership. Licensed to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
 */

/**
 * View Snippet for TDR Rule form elements
 *
 * Date: 8/29/12
 *
 * @var TdrData $tdrData
 * @var
 */

$totalCount = @count($tdrData->dynamicTdrs)+@count($tdrData->staticTdrs);

$tdrRuleNameTip = $translate->translate("Enter a unique name for this rule");
$tdrRuleTypeTip = $translate->translate("Enter the type of rule");
$tdrRuleExtractTip = $translate->translate("Enter when to apply the rule");
$tdrRuleValueTip = $translate->translate("Enter a value for this rule");
$tdrRuleAddTip = $translate->translate("Add additional rule");
$tdrRuleRemoveTip = $translate->translate("Remove rule");

for($i = 0; $i <= $totalCount; $i++):
    $dynSize = @count($tdrData->dynamicTdrs);
    $rule = $i >= $dynSize? @$tdrData->staticTdrs[$i-$dynSize]:$tdrData->dynamicTdrs[$i];
    if (empty($rule)){
        $rule = new stdClass();
    }
    ?>
<div class="control-group tdrRule <?=!empty($validationErrors['tdr'][$i])?"error":""?>" count="<?=$i?>" id="tdrRuleGroup<?=$i?>">
    <? if($i === 0): ?>
    <label class="control-label" for="tdrRules"><? echo $translate->translate("TDR Rules")?></label>
    <? endif ?>
    <div class="controls">
        <input type="text" class="input-small" placeholder="Name" name="tdr[<?=$i?>][name]" id="tdrRules"
               rel="tooltip" title="<?=$tdrRuleNameTip?>" value="<?=@$rule->tdrPropName?>">
        <?
        $selected = 0;
        if($rule instanceof StaticTdr && $rule->kind === TdrRuleType::PROPERTY) $selected = 1;
        else if($rule instanceof StaticTdr && $rule->kind === TdrRuleType::STATIC_VAL) $selected = 2;
        else if($rule instanceof DynamicTdr) $selected = 3;

        ?>
        <select class="input-small tdrType" name="tdr[<?=$i?>][type]" item="<?=$i?>" rel="tooltip" title="<?=$tdrRuleTypeTip?>">
            <option>Type</option>
            <option <?= $selected == 1?"selected":""?>><? echo $translate->translate("Property")?></option>
            <option <?= $selected == 2?"selected":""?>><? echo $translate->translate("Static")?></option>
            <option <?= $selected == 3?"selected":""?>><? echo $translate->translate("Dynamic")?></option>
        </select>
        <? $showExtractFrom = ($rule instanceof DynamicTdr); ?>
        <select class="input-small" name="tdr[<?=$i?>][extractFrom]" style="<?=$showExtractFrom?"":"display: none;"?>
            width: 120px;" id="tdrRuleExtractFrom<?=$i?>" rel="tooltip" title="<?=$tdrRuleExtractTip?>">
            <option>Extract From</option>
            <? /** @var StaticTdr $rule */?>
            <option <?= @$rule->extractFrom===TdrRuleExtractFrom::EITHER?"selected":""?>><? echo $translate->translate("Either")?></option>
            <option <?= @$rule->extractFrom===TdrRuleExtractFrom::REQUEST?"selected":""?>><? echo $translate->translate("Request")?></option>
            <option <?= @$rule->extractFrom===TdrRuleExtractFrom::RESPONSE?"selected":""?>><? echo $translate->translate("Response")?></option>
        </select>
        <? $value = "";
        if($rule instanceof StaticTdr){
            if(empty($rule->value)) $value = $rule->property;
            else $value = $rule->value;
        }
        else if($rule instanceof DynamicTdr){
            $value = $rule->httpHeaderName;
        }
        ?>
        <input type="text" class="input-small" placeholder="<? echo $translate->translate("Value")?>"
               name="tdr[<?=$i?>][value]" value="<?=$value?>" id="tdrRuleValue<?=$i?>" style="width: 120px"
               rel="tooltip" title="<?=$tdrRuleValueTip?>">
        <? if($i === 0): ?>
        <button class="btn newItem" type="tdrRule" title="<?=$tdrRuleAddTip?>"
                id="tdrPlus" rel="tooltip" data-placement="right"><i class="icon-plus"></i></button>
        <? else: ?>
        <button class="btn removeItem" type="tdrRule" number="<?=$i?>" title="<?=$tdrRuleRemoveTip?>"
                rel="tooltip" data-placement="right"><i class="icon-minus"></i></button>
        <? endif ?>
        <span class="help-inline">
                <?if(!empty($validationErrors['tdr'][$i])):
            foreach($validationErrors['tdr'][$i] as $error): ?>
                <?= $error ?><br/>
                <? endforeach ?>
            <? endif ?>
                </span>
    </div>
</div>
<? endfor ?>
