<!--

    Copyright © 2016-2017 The Thingsboard Authors

    Licensed 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.

-->
<div layout="row" width="100%" layout-wrap tb-help="'outgoingMailSettings'" help-container-id="help-container">
	<md-card flex-gt-sm="60" flex="100" >
       <md-card-title>
          <md-card-title-text layout="row">
           	  <span translate class="md-headline">admin.outgoing-mail-settings</span>
			  <span flex></span>
			  <div id="help-container"></div>
          </md-card-title-text>
        </md-card-title>	
   	    <md-progress-linear md-mode="indeterminate" ng-disabled="!loading" ng-show="loading"></md-progress-linear>
  	    <span style="min-height: 5px;" flex="" ng-show="!loading"></span>
        <md-card-content>	        		
			<form name="vm.settingsForm" ng-submit="vm.save()" tb-confirm-on-exit confirm-form="vm.settingsForm">
				<fieldset ng-disabled="loading">
					<md-input-container class="md-block">
						<label translate>admin.mail-from</label>
						<input required name="mailFrom" ng-model="vm.settings.jsonValue.mailFrom">
						<div ng-messages="vm.settingsForm.mailFrom.$error">
	         					<div translate ng-message="required">admin.mail-from-required</div>
	       				</div>				
					</md-input-container>
					<md-input-container class="md-block">
						<label translate>admin.smtp-protocol</label>
			            <md-select ng-disabled="loading" ng-model="vm.settings.jsonValue.smtpProtocol">
			              	<md-option ng-repeat="smtpProtocol in vm.smtpProtocols" value="{{smtpProtocol}}">
			                	{{smtpProtocol.toUpperCase()}}
			             	 </md-option>
			            </md-select>
					</md-input-container>	
					<div layout-gt-sm="row">
						<md-input-container class="md-block" flex="100" flex-gt-sm="60">
							<label translate>admin.smtp-host</label>
							<input required name="smtpHost" ng-model="vm.settings.jsonValue.smtpHost"
								   placeholder="localhost">	
							<div ng-messages="vm.settingsForm.smtpHost.$error">
		         					<div translate ng-message="required">admin.smtp-host-required</div>
		       				</div>				
						</md-input-container>	
						<md-input-container class="md-block" flex="100" flex-gt-sm="40">
							<label translate>admin.smtp-port</label>
							<input required name="smtpPort" ng-model="vm.settings.jsonValue.smtpPort"
									placeholder="25" 
									ng-pattern="/^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/"
									md-maxlength="5">	
				            <div ng-messages="vm.settingsForm.smtpPort.$error" role="alert" multiple>
				              <div translate ng-message="required">admin.smtp-port-required</div>
				              <div translate ng-message="pattern">admin.smtp-port-invalid</div>
				              <div translate ng-message="md-maxlength">admin.smtp-port-invalid</div>
				            </div>	
						</md-input-container>						
					</div>						
					<md-input-container class="md-block">
						<label translate>admin.timeout-msec</label>
						<input required name="timeout" ng-model="vm.settings.jsonValue.timeout"
							   placeholder="10000" 
							   ng-pattern="/^[0-9]{1,6}$/"
							   md-maxlength="6">	
						<div ng-messages="vm.settingsForm.timeout.$error" role="alert" multiple>
	         				  <div translate ng-message="required">admin.timeout-required</div>
				              <div translate ng-message="pattern">admin.timeout-invalid</div>
				              <div translate ng-message="md-maxlength">admin.timeout-invalid</div>
	       				</div>				
					</md-input-container>	
					<md-checkbox ng-disabled="loading" ng-true-value="'true'" ng-false-value="'false'"
								 aria-label="{{ 'admin.enable-tls' | translate }}" ng-model="vm.settings.jsonValue.enableTls">{{ 'admin.enable-tls' | translate }}</md-checkbox>
					<md-input-container class="md-block">
						<label translate>common.username</label>
						<input name="username" placeholder="{{ 'common.enter-username' | translate }}" ng-model="vm.settings.jsonValue.username">
					</md-input-container>				
					<md-input-container class="md-block">
						<label translate>common.password</label>
						<input name="password" placeholder="{{ 'common.enter-password' | translate }}" type="password" ng-model="vm.settings.jsonValue.password">
					</md-input-container>				
					<div layout="row" layout-align="end center" width="100%" layout-wrap>
						<md-button ng-disabled="loading || vm.settingsForm.$invalid" ng-click="vm.sendTestMail()" class="md-raised">{{'admin.send-test-mail' | translate}}</md-button>
						<md-button ng-disabled="loading || vm.settingsForm.$invalid || !vm.settingsForm.$dirty" type="submit" class="md-raised md-primary">{{'action.save' | translate}}</md-button>
					</div>
				</fieldset>
			</form>
	    </md-card-content>
    </md-card>
</div>
