<div id="bootstrap-theme" crm-dialog="crmSearchTask">
  <form name="civiGrantTaskAddGrantForm" ng-controller="civiGrantTaskAddGrant as $ctrl">
    <div class="form-inline" ng-repeat="clause in $ctrl.values" >
      <input class="form-control crm-auto-width" ng-change="$ctrl.updateField($index)" ng-disabled="$ctrl.run" ng-model="clause[0]" crm-ui-select="{data: $ctrl.availableFields, allowClear: true, placeholder: 'Field'}" />
      <crm-search-input class="form-group" ng-model="clause[1]" field="$ctrl.getField(clause[0])" ></crm-search-input>
    </div>
    <div class="form-inline" ng-hide="$ctrl.run">
      <input class="form-control" on-crm-ui-select="$ctrl.addField(selection)" ng-disabled="!$ctrl.fields.length" ng-class="{loading: !$ctrl.fields.length}" crm-ui-select="{data: $ctrl.availableFields, placeholder: ts('Add Value')}"/>
    </div>
    <div ng-if="!$ctrl.run && $ctrl.existingGrantContacts.length" class="alert alert-warning">
      <div>
        <i class="crm-i fa-exclamation-triangle" role="img" aria-hidden="true"></i>
        {{:: ts('1 contact already has a grant.', {plural: '%count contacts already have grants.', count: $ctrl.existingGrantContacts.length}) }}
        <div class="form-inline">
          <label><input type="radio" ng-model="$ctrl.skipDuplicates" ng-value="true"> {{:: ts('Skip contact with existing grant', {plural: 'Skip contacts with existing grants', count: $ctrl.existingGrantContacts.length}) }}</label>
          <label><input type="radio" ng-model="$ctrl.skipDuplicates" ng-value="false"> {{:: ts('Add new grant anyway', {plural: 'Add new grants anyway', count: $ctrl.existingGrantContacts.length}) }}</label>
        </div>
      </div>
    </div>
    <div ng-if="$ctrl.run" class="crm-search-task-progress">
      <h5>{{:: ts('Adding grant...', {plural: 'Adding %count grants...', count: $ctrl.ids.length}) }}</h5>
      <crm-search-batch-runner entity="'Grant'" action="save" params="$ctrl.run" ids="$ctrl.ids" id-field="contact_id" success="$ctrl.onSuccess(result)" error="$ctrl.onError(error)" ></crm-search-batch-runner>
    </div>
    <crm-dialog-button text="ts('Cancel')" icons="{primary: 'fa-times'}" on-click="$ctrl.cancel()" disabled="$ctrl.run" />
    <crm-dialog-button text="ts('Save')" icons="{primary: 'fa-user-plus'}" on-click="$ctrl.submit()" disabled="$ctrl.run || $ctrl.existingCount === null || !civiGrantTaskAddGrantForm.$valid" />
  </form>
</div>
