<nz-layout>

  <nz-card>
    <form class="ctx-form-implicit" nz-form [formGroup]="_organizationSearchForm">
      <div nz-row [nzGutter]="[16, 16]" [style.max-width]="'calc(100% + 16px)'">
        <div nz-col>
          <nz-form-item>
            <nz-form-label>模糊查询</nz-form-label>
            <nz-form-control [nzErrorTip]="controlErrorTemplate">
              <nz-select #keywordsInputMultiple
                         formControlName="keywords"
                         nzPlaceHolder="输入任意查询条件"
                         nzMode="tags"
                         [nzDropdownStyle]="{ display: keywordsInputMultiple['searchValue'] ? '': 'none' }"
                         nzAllowClear>
              </nz-select>
            </nz-form-control>
          </nz-form-item>
        </div>
        <div nz-col [nzFlex]="1">
          <nz-form-item [nzGutter]="[8, 8]" nzJustify="end">
            <div nz-col><button nz-button nzType="primary" (click)="_requeryOrganizationList()">查询</button></div>
            <div nz-col><button nz-button (click)="_resetOrganizationSearchForm()">重置</button></div>
          </nz-form-item>
        </div>
      </div>
    </form>
  </nz-card>

  <nz-content>
    <nz-card class="ctx-card ctx-card-fixed" [nzTitle]="organizationActionTemplate" [nzExtra]="organizationExtraActionTemplate">
      <ng-template #organizationActionTemplate>
        <div nz-row [nzGutter]="[8, 8]">
          <div nz-col><button nz-button nzType="primary" (click)="_createOrganization()"><i nz-icon nzType="plus" nzTheme="outline"></i>新增</button></div>
        </div>
      </ng-template>
      <ng-template #organizationExtraActionTemplate>
        <div nz-row>
          <div nz-col><button nz-button nzType="text" title="展开" (click)="_expandOrganizationList()"><i nz-icon nzType="double-right" nzTheme="outline" [nzRotate]="90"></i></button></div>
          <div nz-col><button nz-button nzType="text" title="折叠" (click)="_collapseOrganizationList()"><i nz-icon nzType="double-left" nzTheme="outline" [nzRotate]="90"></i></button></div>
          <div nz-col><button nz-button nzType="text" title="刷新" (click)="_queryOrganizationList()"><i nz-icon nzType="reload" nzTheme="outline"></i></button></div>
          <div nz-col><button nz-button
                    nzType="text"
                    title="列设置"
                    nz-popover
                    nzPopoverTrigger="click"
                    nzPopoverPlacement="bottomRight"
                    nzPopoverOverlayClassName="ctx-table-setting"
                    [nzPopoverTitle]="organizationFieldSettingTitleTemplate"
                    [nzPopoverContent]="organizationFieldSettingContentTemplate"><i nz-icon nzType="setting" nzTheme="outline"></i></button></div>
        </div>
      </ng-template>
      <nz-table class="ctx-table-fixed"
                #organizationTable
                nzTableLayout="fixed"
                [nzData]="_organizationTreeNodeList"
                [nzLoading]="_organizationLoading"
                [nzLoadingDelay]="500"
                [nzFrontPagination]="false"
                [nzShowPagination]="false">
        <thead>
          <tr>
            <th nzLeft="0px"
                nzShowCheckbox
                [nzChecked]="_organizationAllChecked"
                [nzIndeterminate]="_organizationIndeterminate"
                [nzDisabled]="organizationTable.data.length === 0"
                (nzCheckedChange)="_checkAllOrganizationList($event)"></th>
            <ng-container *ngFor="let field of _organizationFieldList">
              <th *ngIf="field.visible"
                  nz-resizable
                  nzBounds="window"
                  nzPreview
                  [nzAlign]="field.align!"
                  [nzWidth]="field.width!"
                  (nzResizeEnd)="field.width = $event.width + 'px'">
                <div *ngIf="!field.headTemplate; else field.headTemplate!" nz-typography nzEllipsis>{{ field.name }}</div>
                <nz-resize-handle nzDirection="right"></nz-resize-handle>
              </th>
            </ng-container>
            <th nzRight="0px" nzWidth="100px">操作</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <ng-container *ngTemplateOutlet="organizationTreeNodeListTemplate; context: { organizationTreeNodeList: organizationTable.data }"></ng-container>
          <ng-template #organizationTreeNodeListTemplate let-organizationTreeNodeList="organizationTreeNodeList">
            <ng-container *ngFor="let organizationTreeNode of organizationTreeNodeList">
              <tr>
                <td nzLeft="0px" nzShowCheckbox [(nzChecked)]="_organizationExtras[organizationTreeNode.source.id].checked" (nzCheckedChange)="_updateOrganizationCheckStatus()"></td>
                <ng-container *ngFor="let field of _organizationFieldList">
                  <td *ngIf="field.visible" [nzAlign]="field.align!">
                    <div *ngIf="!field.bodyTemplate; else bodyTemplate"
                         nz-typography
                         [nzEllipsis]="field.ellipsis !== false"
                         [nzEllipsisRows]="field.ellipsisRows! > 0 ? field.ellipsisRows : 1">{{ organizationTreeNode.source | ncProperty:field.key | nzSafeNull:'-' }}</div>
                    <ng-template #bodyTemplate>
                      <ng-container *ngTemplateOutlet="field.bodyTemplate!; context: { $implicit: organizationTreeNode.source | ncProperty:field.key, source: organizationTreeNode, extra: _organizationExtras[organizationTreeNode.source.id] }"></ng-container>
                    </ng-template>
                  </td>
                </ng-container>
                <td nzRight="0px">
                  <button class="ctx-btn-link" nz-button nzType="link" (click)="_updateOrganization(organizationTreeNode.source)">编辑</button>
                  <nz-divider nzType="vertical"></nz-divider>
                  <button class="ctx-btn-link" nz-button nzType="link" (click)="_deleteOrganization(organizationTreeNode.source)">删除</button>
                </td>
                <td></td>
              </tr>
              <ng-container *ngIf="organizationTreeNode.children && organizationTreeNode.expand">
                <ng-container *ngTemplateOutlet="organizationTreeNodeListTemplate; context: { organizationTreeNodeList: organizationTreeNode.children }"></ng-container>
              </ng-container>
            </ng-container>
          </ng-template>
        </tbody>
      </nz-table>
    </nz-card>
  </nz-content>

</nz-layout>

<ng-template #organizationFieldSettingTitleTemplate>
  <div nz-row nzJustify="space-between">
    <div nz-col>列设置</div>
    <div nz-col><a (click)="_resetOrganizationFieldList()">重置</a></div>
  </div>
</ng-template>

<ng-template #organizationFieldSettingContentTemplate>
  <nz-table #organizationFieldTable
            [nzData]="_organizationFieldList"
            [nzBordered]="false"
            [nzFrontPagination]="false"
            [nzShowPagination]="false">
    <tbody cdkDropList (cdkDropListDropped)="_dropOrganizationFieldList($event)">
      <tr *ngFor="let field of organizationFieldTable.data; let fieldIndex = index;" cdkDrag>
        <td>
          <i nz-icon nzType="fill-plus:drag" nzTheme="outline" title="拖拽调整顺序"></i>
          <label nz-checkbox [(nzChecked)]="field.visible">
            <ng-container *ngIf="!field.headTemplate; else field.headTemplate!">{{ field.name }}</ng-container>
          </label>
        </td>
      </tr>
    </tbody>
  </nz-table>
</ng-template>

<ng-template #titleTemplate let-title let-organizationTreeNode="source">
  <div class="ctx-table-tree-switcher" [style.paddingLeft]="organizationTreeNode.level * 20 + 'px'">
    <a *ngIf="organizationTreeNode.children; else childContentTemplate" (click)="organizationTreeNode.expand = !organizationTreeNode.expand">
      <i nz-icon nzType="caret-right" nzTheme="outline" [nzRotate]="organizationTreeNode.expand ? 90 : 0"></i>{{ title }}
    </a>
    <ng-template #childContentTemplate>
      <i nz-icon nzType="caret-right" nzTheme="outline" [style.visibility]="'hidden'"></i>{{ title }}
    </ng-template>
  </div>
</ng-template>

<ng-template #numberTemplate let-numberValue>
  <div nz-typography nzEllipsis>{{ numberValue | number:'1.0-3' | nzSafeNull:'-' }}</div>
</ng-template>

<ng-template #timeTemplate let-timeValue>
  <div nz-typography nzEllipsis>{{ timeValue | date:'yyyy-MM-dd HH:mm:ss' | nzSafeNull:'-' }}</div>
</ng-template>

<ng-template #controlErrorTemplate let-control>
  <div *ngFor="let error of control.errors | keyvalue">{{ error.value }}</div>
</ng-template>
