<div page-content>
  <div header>
    <admin-tabs tab="template"></admin-tabs>
    <div *ngIf="page" class="uk-width-expand">
      <a routerLink="../pages" class="uk-flex uk-flex-middle uk-h5 uk-link-reset">
            <span class="uk-margin-right">
              <icon name="west" ratio="1.7" [flex]="true"></icon>
            </span>
        <h1 class="uk-h5 uk-margin-remove">{{page.name}}</h1>
      </a>
    </div>
    <ul *ngIf="page" class="uk-subnav uk-subnav-pill uk-margin-medium-top">
      <li [class.uk-active]="filterForm.get('position').value === 'all'" class="uk-margin-small-bottom"><a
          (click)="filterForm.get('position').setValue('all')"><span
          class="title">All placements</span></a></li>
      <li *ngFor="let position of  pluginUtils.placementsOptions; let i=index"
          [class.uk-active]="filterForm.get('position').value === position.value" class="uk-margin-small-bottom"><a
          (click)="filterForm.get('position').setValue(position.value)"><span
          class="title">{{position.label}}</span></a></li>
    </ul>
  </div>
  <div actions>
    <div class="uk-section-xsmall">
      <div class="uk-flex uk-flex-right@m uk-flex-center uk-flex-wrap uk-flex-middle uk-grid" uk-grid>
        <!--        <div search-input [disabled]="showLoading" [expandable]="true" [searchControl]="filterForm.get('keyword')" searchInputClass="outer" placeholder="Search class" class="uk-width-1-3@xl uk-width-2-5@l uk-width-1-2@m uk-width-1-1"></div>-->
        <div>
          <button class="uk-button uk-button-default uk-flex uk-flex-middle" (click)="newPlugin()"
                  [disabled]="showLoading" [class.uk-disabled]="showLoading">
            <icon name="add" [flex]="true"></icon>
            <span class="uk-margin-small-left uk-text-bold uk-text-uppercase">Add plugin template</span>
          </button>
        </div>
      </div>
    </div>
  </div>
  <div inner>
    <div class="uk-section uk-section-small uk-position-relative" style="min-height: 60vh">
      <div *ngIf="showLoading" class="uk-position-center">
        <loading></loading>
      </div>
      <ng-container *ngIf="!showLoading">
        <!--<div *ngIf="checkboxes.length == 0"
             class="uk-card uk-card-default uk-padding-large uk-text-center uk-margin-bottom uk-text-bold">
          <div>No templates found</div>
        </div>-->
        <div class="uk-grid uk-child-width-1-1">
          <ng-container *ngFor="let placement of pluginUtils.placementsOptions">

            <ng-container
                *ngIf="filterForm.get('position').value == placement.value || filterForm.get('position').value == 'all'">
              <div
                  *ngIf="templatesByPlacement.get(placement.value) && templatesByPlacement.get(placement.value).length >0 && page"
                  class="uk-heading-divider uk-h6 uk-margin-left uk-padding-remove-left uk-text-capitalize ">{{placement.value}}</div>
              <div *ngFor="let template of templatesByPlacement.get(placement.value) ; let i=index">
                <div class="uk-card uk-card-default uk-margin-bottom">
                  <div class="uk-card-body uk-flex">
                    <div class="uk-width-expand uk-text-small">
                      <h6>{{template.name}}</h6>
                      <div class="uk-margin-small-bottom">
                        <span class="uk-label uk-padding-xsmall uk-text-capitalize"
                              [class.uk-label-success]="template.plan !='extended'"
                              [class.uk-label-danger]="template.plan =='extended'">Plan: {{template.plan}}  </span>
                        <span *ngIf="template.portalSpecific && template.portalSpecific.length > 0"
                              class="uk-label uk-label-primary uk-padding-xsmall uk-text-capitalize uk-margin-left">Communities: {{template.portalSpecific.join(', ')}}  </span>
                        <span *ngIf="template.defaultIsActive"
                              class="uk-label uk-label-success uk-padding-xsmall uk-text-capitalize uk-margin-left">Default status: Active </span>
                      </div>
                      <div class="uk-margin-small-bottom">
                        {{template.description}}
                      </div>
                      <div class="uk-margin-small-bottom">
                        <span class="uk-text-meta">Page: </span>{{getPageAsString(template.page)}} -
                        <span class="uk-text-meta">Placement: </span>{{template.placement}} -
                        <span class="uk-text-meta">Order: </span>{{template.order}}
                      </div>
                      <div class="uk-margin-small-bottom">
                        <span class="uk-text-meta">Portal type: </span>{{template.portalType}}
                      </div>
                      <ul uk-accordion>
                        <li>
                          <a class="uk-accordion-title">Preview</a>
                          <div class="uk-accordion-content">
                            <plugin-wrapper [pluginTemplate]="template"
                                            [pluginObject]="template.object"></plugin-wrapper>
                          </div>
                        </li>
                      </ul>
                    </div>
                  </div>
                  <div class="uk-card-footer uk-padding-remove-vertical">
                    <div class="uk-grid uk-grid-small uk-flex-nowrap uk-grid-divider uk-flex-right" uk-grid>

                      <ng-container *ngIf="page">
                        <div *ngIf="template.order>0">
                          <div class="uk-padding-small uk-padding-remove-horizontal">
                            <button
                                class="uk-button uk-button-link uk-flex uk-flex-middle"
                                (click)="swap(i, i-1, template.placement)">
                              <!--                        <icon name="" [flex]="true"></icon>-->
                              <span class="uk-margin-xsmall-left"> Up</span>
                            </button>
                          </div>
                        </div>
                        <div *ngIf="template.order < templatesByPlacement.get(placement.value).length -1 ">
                          <div class="uk-padding-small uk-padding-remove-horizontal">
                            <button
                                class="uk-button uk-button-link uk-flex uk-flex-middle"
                                (click)="swap(i+1, i, template.placement)">
                              <!--                        <icon name="" [flex]="true"></icon>-->
                              <span class="uk-margin-xsmall-left"> Down</span>
                            </button>
                          </div>
                        </div>
                      </ng-container>
                      <div>
                        <div class="uk-padding-small uk-padding-remove-horizontal">
                          <button
                              class="uk-button uk-button-link uk-flex uk-flex-middle" (click)="edit(template)">
                            <icon name="edit" [flex]="true"></icon>
                            <span class="uk-margin-xsmall-left"> Edit</span>
                          </button>
                        </div>
                      </div>
                      <div>
                        <div class="uk-padding-small uk-padding-remove-horizontal">
                          <button class="uk-button uk-button-link uk-flex uk-flex-middle"
                                  (click)="confirmDelete(template)">
                            <icon name="delete" [flex]="true"></icon>
                            <span class="uk-margin-xsmall-left"> Delete</span>
                          </button>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>

                <!--          </div>-->
              </div>
              <!--          </div>-->
            </ng-container>
          </ng-container>
        </div>
      </ng-container>
    </div>
  </div>
</div>
<modal-alert #editModal (alertOutput)="saveConfirmed($event)" [large]="true"
             [okDisabled]="templateForm && (templateForm.invalid || !templateForm.dirty)"
             classTitle="uk-background-primary uk-light">
  <form *ngIf="templateForm" [formGroup]="templateForm" class="uk-grid " uk-grid>
    <div class="uk-width-1-3 " *ngIf="templateForm.getRawValue()['_id']">
      <div class="uk-card uk-card-default uk-padding-small">
        <div *ngIf="!editSubmenuOpen" class="uk-text-muted uk-text-bold uk-text-center uk-margin-bottom">Plugin
          options
        </div>
        <plugin-wrapper-form [pluginTemplate]="templateForm.getRawValue()"
                             [pluginObject]="selectedTemplate.object"
                             (changed)="pluginFieldChanged($event)" [editTemplate]="true" [editSubmenuOpen]="editSubmenuOpen"></plugin-wrapper-form>
      </div>
    </div>
    <div class="uk-grid  uk-width-expand uk-child-width-1-2" uk-grid>
      <div input [formInput]="templateForm.get('name')" placeholder="Template Name"></div>
      <div input [formInput]="templateForm.get('code')" placeholder="Template Code"
           [options]="pluginUtils.availablePluginCodes" type="select"></div>
      <div class="uk-width-1-1" input [formInput]="templateForm.get('description')" placeholder="Description"
           type="textarea"></div>
      <div input [formInput]="templateForm.get('plan')" placeholder="Plan" [options]="pluginUtils.planOptions"
           type="select"></div>
      <div input [formInput]="templateForm.get('image')" placeholder="Image"></div>
      <div input [formInput]="templateForm.get('portalType')" placeholder="Portal Type"
           [options]="portalUtils.portalTypes" type="select"></div>
      <div input [formInput]="templateForm.get('placement')" placeholder="Placement"
           [options]="pluginUtils.placementsOptions" type="select"></div>
      <div *ngIf="!templateForm.get('portalType').value" input [formInput]="templateForm.get('page')" placeholder="Page"
           [options]="allPages" type="select"></div>
      <div *ngIf="templateForm.get('portalType').value" input [formInput]="templateForm.get('page')" placeholder="Page"
           [options]="allPagesByPortal.get(templateForm.get('portalType').value)" type="select"></div>
      <div input [formInput]="templateForm.get('portalSpecific')" placeholder="Available to communities"
           hint="community ids, comma seperated, no spaces"></div>
      <div input [formInput]="templateForm.get('order')" placeholder="Order"></div>
      <div>Default Status
        <mat-slide-toggle [checked]="templateForm.get('defaultIsActive').value"
                          (change)="templateForm.get('defaultIsActive').setValue($event.checked); templateForm.markAsDirty()"></mat-slide-toggle>
      </div>
      <!--<plugin-wrapper *ngIf="selectedTemplate "
                      [pluginTemplate]="this.templateForm.getRawValue()" [pluginObject]="this.templateForm.getRawValue().object" class="uk-width-1-1" ></plugin-wrapper>-->

      <div *ngFor="let attrForm of attrFormArray.controls; let i=index" class="uk-width-1-1 uk-grid uk-child-width-1-2"
           uk-grid>


        <div class="uk-heading-divider  uk-text-small uk-width-1-1 uk-margin-bottom uk-text-meta">
          <div class="uk-grid">
            <div>Setting #{{i + 1}}</div>
            <div class=" uk-width-expand uk-text-right ">
              <a class="   " (click)="removeAttr(i)">Remove</a>
            </div>

          </div>
        </div>
        <div input [formInput]="attrForm.get('key')" placeholder="Key"></div>
        <div input [formInput]="attrForm.get('name')" placeholder="Name"></div>
        <div input [formInput]="attrForm.get('type')" placeholder="Type" [options]="pluginUtils.attrTypeOptions"
             type="select" (valueChange)="attributeTypeChanged(attrForm)"></div>

        <div *ngIf="attrForm.get('type').value == 'text'" input [formInput]="attrForm.get('value')"
             placeholder="Default value" [type]="attrForm.get('type').value"></div>
        <div *ngIf="attrForm.get('type').value == 'URL'" input [formInput]="attrForm.get('value')"
             placeholder="Default value" [type]="attrForm.get('type').value" [validators]="urlValidator"></div>
        <div *ngIf="attrForm.get('type').value == 'boolean'" input [formInput]="attrForm.get('value')"
             placeholder="Default value" type="select"
             [options]="[{label: 'yes', value:true}, {label: 'no', value:false}]"></div>
        <div *ngIf="attrForm.get('type').value == 'HTML'" class="uk-width-1-1">
          <ckeditor [readonly]="false"
                    debounce="500"
                    [formControl]="attrForm.get('value')"
                    [config]="{ extraAllowedContent: '* [uk-*](*) ; span', disallowedContent: 'script; *[on*]',
                                        removeButtons:  'Save,NewPage,DocProps,Preview,Print,' +
                                                        'Form,Checkbox,Radio,TextField,Textarea,Select,Button,ImageButton,HiddenField,' +
                                                        'CreateDiv,Flash,PageBreak,' +
                                                        'Subscript,Superscript,Anchor,Smiley,Iframe,Styles,Font,About,Language',
                                        extraPlugins: 'divarea'}">
          </ckeditor>
        </div>
      </div>
      <hr class="uk-width-1-1">
      <div class="uk-width-1-1 uk-text-center ">
        <a (click)="addNewAttr()">Add settings</a>
      </div>
      <div *ngIf="templateForm.getRawValue()['_id']" class="uk-width-1-1">
        <hr class="uk-width-1-1">
        <div class="uk-text-muted uk-text-bold uk-width-1-1 uk-text-center">Plugin preview</div>

        <plugin-wrapper [pluginTemplate]="templateForm.getRawValue()"
                        [pluginObject]="selectedTemplate.object"></plugin-wrapper>
      </div>
    </div>
  </form>
</modal-alert>
<modal-alert #deleteModal (alertOutput)="confirmedDelete()" classTitle="uk-background-primary uk-light"></modal-alert>
