<?
/**
 * 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 File for the Auth form
 */
/**
 * @var Flow $flow
 */
$flow = $this->flow;

/**
 * @var Auth $auth
 */
$auth = $flow->flowScope['auth'];
$validationErrors = @$flow->flowScope['validationErrors'];
$username = "";
$password = "";
$howMany = $flow->flowScope['howMany'];
$authTypeLocked = @$flow->flowScope['authTypeLocked'];

if($auth->type === AuthType::$BASIC){
    $username = $auth->basicAuth->username;
    $password = $auth->basicAuth->password;
}
else if($auth->type === AuthType::$WSSE){
    $username = $auth->wsseAuth->username;
    $password = $auth->wsseAuth->password;
}

$relatedProperties = @$flow->flowScope["relatedProperties"];

$authIdTip = $this->translate("A unique ID for this Auth");
$authTypeBasicTip = $this->translate("Use basic access (username and password)");
$authTypeAuthKeyTip = $this->translate("Authenticate with key data");
$authTypeWsseTip = $this->translate("Use WS-Security (username and password)");
$authTypeWhitelistTip = $this->translate("Use a list of approved IP addresses");
$usernameTip = $this->translate("Enter a user name");
$passwordTip = $this->translate("Enter a password");
$authKeyTip = $this->translate("Enter key data");
$whitelistTip = $this->translate("Enter a comma-separated list of IPs");
$enabledTip = $this->translate("Enable this Auth");
$disabledTip = $this->translate("Disable this Auth");
$saveButtonTip = $this->translate("Post and return to Auth list");
?>
<div class="container">
    <h1><?=($flow->flowScope['isNew'])?$this->translate("Create"):$this->translate("Update")?> <? echo $this->translate("Auth")?></h1>
<? if(!empty($validationErrors['default'])):?>
    <div class="control-group error">
        <span class="help-inline"><? echo $this->translate("Server Error")?>: <?=$validationErrors['default']?></span>
    </div>
    <? endif; ?>
    <form class="well form-horizontal" method="POST">
        <fieldset>
            <div class="control-group <?=empty($validationErrors['howMany'])?"":"error"?>" style="<?=($flow->flowScope['isNew'])?"":"display: none;"?>">
                <label class="control-label" for="howMany">How Many?</label>
                <div class="controls" >
                    <div class="btn-group inline" data-toggle="buttons-radio" id="howMany-group">
                        <a class="btn howMany <?=$howMany === "1"?"active":""?>" id="howMany-one">1</a>
                        <a class="btn howMany <?=($howMany !== "1") || empty($howMany)?"active":""?>" id="howMany-more">More</a>
                    </div>
                    <input type="text" name="howMany" value="<?=$howMany?>" id="howMany" style="<?=$howMany === "1"?"display: none;":""?>" class="input-mini inline" placeholder="#"/>
                    <span class="help-inline inline"><?=@$validationErrors['howMany']?></span>
                </div>
            </div>
            <div class="control-group" id="authId-ctlgroup">
                <label class="control-label" for="input01">ID</label>
                <div class="controls">
					<input type="text" class="input-xlarge" id="input01" name="authid"
                           rel="tooltip" title="<?=$authIdTip?>"
                           value="<?=$auth->id===$this->translate("create")?"":$auth->id?>" <?=($flow->flowScope['isNew'])?"":$this->translate("disabled")?>/>
                </div>
            </div>

            <div class="control-group " style="<?=empty($authTypeLocked)?"":"display:none;"?>">
                <label class="control-label" for="authCheck" reado><? echo $this->translate("Type")?></label>
                <div class="controls">
                    <div class="btn-group" data-toggle="buttons-radio" id="authCheck">
                        <a class="btn auth hiddenValue <?=$auth->type==="basic"?"active":""?>"
                           rel="tooltip" title="<?=$authTypeBasicTip?>"
                           inputId="type" value="basic" id="authtype-basic"><? echo $this->translate("Basic")?></a>
                        <a class="btn auth hiddenValue <?=$auth->type==="authKey"?"active":""?>"
                           rel="tooltip" title="<?=$authTypeAuthKeyTip?>"
                           inputId="type" value="authKey" id="authtype-authkey"><? echo $this->translate("Auth Key")?></a>
                        <a class="btn auth hiddenValue <?=$auth->type==="wsse"?"active":""?>"
                           rel="tooltip" title="<?=$authTypeWsseTip?>"
                           inputId="type" value="wsse" id="authtype-wsse"><? echo $this->translate("WSSE")?></a>
                        <a class="btn auth hiddenValue <?=$auth->type==="ipWhiteList"?"active":""?>"
                           rel="tooltip" title="<?=$authTypeWhitelistTip?>"
                           inputId="type" value="ipWhiteList" id="authtype-ipwhitelist"><? echo $this->translate("IP Whitelist")?></a>
                    </div>
                    <input type="hidden" name="type" value="<?=$auth->type?>" id="type"/>
                </div>
            </div>
            <span id="un-pw" style="display:none">
                <div id="userName" class="control-group <?=(!empty($validationErrors['username']))?"error":""?>">
                    <label class="control-label" for="userName0"><? echo $this->translate("Username")?></label>
                    <div class="controls">
                        <input type="text" class="input-xlarge" id="userName0" name="username" value="<?=$username?>"
                               rel="tooltip" title="<?=$usernameTip?>"/>
                        <span class="help-inline"><?=@$validationErrors['username']?></span>
                    </div>
                </div>
                <div id="password" class="control-group <?=(!empty($validationErrors['password']))?"error":""?>">
                    <label class="control-label" for="password0"><? echo $this->translate("Password")?></label>
                    <div class="controls">
                        <input type="text" class="input-xlarge" id="password0" name="password" value="<?=$password?>"
                               rel="tooltip" title="<?=$passwordTip?>"/>
                        <span class="help-inline"><?=@$validationErrors['password']?></span>
                    </div>
                </div>
            </span>
            <div id="authKey" class="control-group <?=!empty($validationErrors['authKey'])?"error":""?>" style="display:none">
                <label class="control-label" for="authKey0"><? echo $this->translate("Auth Key")?></label>
                <div class="controls">
                    <input type="text" class="input-xlarge" id="authKey0" name="authKey" value="<?=$auth->authKeyAuth->keyValue?>"
                           rel="tooltip" title="<?=$authKeyTip?>"/>
                    <span class="help-inline"><?=@$validationErrors['authKey']?></span>
                </div>
            </div>
            <div id="IPs"  class="control-group <?=!empty($validationErrors['ipWhiteList'])?"error":""?>" style="display:none">
                <label class="control-label" for="selectIPs"><? echo $this->translate("IPs")?></label>
                <div class="controls">
                    <textarea type="text" class="input-xlarge" id="selectIPs" name="ipWhiteList"
                              rel="tooltip" title="<?=$whitelistTip?>"><?=implode(",",$auth->ipWhiteListAuth->ips) ?></textarea>
                    <span class="help-inline"><? echo $this->translate("Comma separated")?><br/> <?=@$validationErrors['ipWhiteList']?></span>
                </div>
            </div>
            <? $advancedOpen = (!empty($auth->headerTransformations) || !empty($auth->properties)
            || !empty($auth->tdrData->dynamicTdrs)
            || !empty($auth->tdrData->staticTdrs)
            || $auth->status !== 'active');?>
            <div class="control-group">
                <label class="control-label"><button class="btn" id="showAdvanced"><strong><? echo $this->translate("Advanced")?></strong>
                    <i class="icon-chevron-down advbtn" <?=!$advancedOpen?"":"style=\"display:none\""?>></i><i class="icon-chevron-up advbtn" <?=$advancedOpen?"":"style=\"display:none\""?>></i>
                </button></label>
            </div>

            <div id="advancedSection" class="well well-small second-level" style="<?= $advancedOpen?"":"display:none"?>">
                <div class="control-group">
                    <label class="control-label" for="enabled"><? echo $this->translate("Enabled")?></label>
                    <div class="controls">
                        <div class="btn-group" data-toggle="buttons-radio" id="enabled">
                            <button class="btn auth-enabled <?=$auth->status==="active"?"active":""?>" name="enabled-true" value="active"
                                    rel="tooltip" title="<?=$enabledTip?>"><? echo $this->translate("Enabled")?></button>
                            <button class="btn auth-enabled <?=$auth->status!=="active"?"active":""?>" name="enabled-false" value="inactive"
                                    rel="tooltip" title="<?=$disabledTip?>"><? echo $this->translate("Disabled")?></button>
                        </div>
                        <input type="hidden" class="enabled-true" name="status" value="<?=$auth->status?>" id="status"/>
                    </div>
                </div>
                <div id="propertyGroup">
                    <? SharedViewUtility::includePropertiesSnippet($auth->properties, $validationErrors); ?>
                </div>
                <div id="headerTransGroup">
                    <? SharedViewUtility::includeHeaderTransformationsSnippet($auth->headerTransformations, $validationErrors); ?>
                </div>
                <div id="tdrRuleGroup">
                    <? SharedViewUtility::includeTdrRulesSnippet($auth->tdrData, $validationErrors); ?>
                </div>
                <div class="hidden" id="relatedPropertiesTree"><?
                    print htmlentities($relatedProperties);
                    ?></div>
            </div>
            <input type="hidden" name="_faction" value="submit" />
            <input type="hidden" name="_fid" value="<?=$flow->id?>" />
            <input type="submit" class="btn confirm-toggle btn-primary" value="<? echo $this->translate("Save")?>"
                   rel="tooltip" title="<?=$saveButtonTip?>" data-placement="right"/>
        </fieldset>
    </form>
</div> <!-- /container -->
