<nz-layout>

  <nz-card>
    <form class="ctx-form-implicit" nz-form [formGroup]="_menuSearchForm">
      <div nz-row [nzGutter]="[16, 16]" [style.max-width]="'calc(100% + 16px)'">
        <div nz-col>
          <nz-form-item>
            <nz-form-label nzRequired>应用</nz-form-label>
            <nz-form-control [nzErrorTip]="controlErrorTemplate">
              <nz-select formControlName="applicationCode"
                         nzPlaceHolder="请选择应用"
                         [nzLoading]="_applicationOptionResource.optionLoading"
                         nzAllowClear
                         nzShowSearch>
                <nz-option *ngFor="let application of _applicationOptionResource.optionList"
                           [nzLabel]="application.name"
                           [nzValue]="application.code"></nz-option>
              </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)="_requeryMenuList()">查询</button></div>
            <div nz-col><button nz-button (click)="_resetMenuSearchForm()">重置</button></div>
          </nz-form-item>
        </div>
      </div>
    </form>
  </nz-card>

  <nz-content>
    <nz-card class="ctx-card ctx-card-fixed" [nzTitle]="menuActionTemplate" [nzExtra]="menuExtraActionTemplate">
      <ng-template #menuActionTemplate>
        <div nz-row [nzGutter]="[8, 8]">
          <div nz-col *ngIf="_createMenuStatus"><button nz-button
                    nzType="primary"
                    [disabled]="_createMenuStatus !== true && _createMenuStatus.disabled"
                    [title]="_createMenuStatus !== true && _createMenuStatus.hint || ''"
                    (click)="_createMenu()"><i nz-icon nzType="plus" nzTheme="outline"></i>新增</button></div>
        </div>
      </ng-template>
      <ng-template #menuExtraActionTemplate>
        <div nz-row>
          <div nz-col><button nz-button nzType="text" title="展开" (click)="_expandMenuList()"><i nz-icon nzType="double-right" nzTheme="outline" [nzRotate]="90"></i></button></div>
          <div nz-col><button nz-button nzType="text" title="折叠" (click)="_collapseMenuList()"><i nz-icon nzType="double-left" nzTheme="outline" [nzRotate]="90"></i></button></div>
          <div nz-col><button nz-button nzType="text" title="刷新" (click)="_queryMenuList()"><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]="menuFieldSettingTitleTemplate"
                    [nzPopoverContent]="menuFieldSettingContentTemplate"><i nz-icon nzType="setting" nzTheme="outline"></i></button></div>
        </div>
      </ng-template>
      <nz-table class="ctx-table-fixed"
                #menuTable
                nzTableLayout="fixed"
                [nzData]="_menuTreeNodeList"
                [nzLoading]="_menuLoading"
                [nzLoadingDelay]="500"
                [nzFrontPagination]="false"
                [nzShowPagination]="false">
        <thead>
          <tr>
            <th nzLeft="0px"
                nzShowCheckbox
                [nzChecked]="_menuAllChecked"
                [nzIndeterminate]="_menuIndeterminate"
                [nzDisabled]="menuTable.data.length === 0"
                (nzCheckedChange)="_checkAllMenuList($event)"></th>
            <ng-container *ngFor="let field of _menuFieldList">
              <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="menuTreeNodeListTemplate; context: { menuTreeNodeList: menuTable.data }"></ng-container>
          <ng-template #menuTreeNodeListTemplate let-menuTreeNodeList="menuTreeNodeList">
            <ng-container *ngFor="let menuTreeNode of menuTreeNodeList">
              <tr>
                <td nzLeft="0px" nzShowCheckbox [(nzChecked)]="_menuExtras[menuTreeNode.source.id].checked" (nzCheckedChange)="_updateMenuCheckStatus()"></td>
                <ng-container *ngFor="let field of _menuFieldList">
                  <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">{{ menuTreeNode.source | ncProperty:field.key | nzSafeNull:'-' }}</div>
                    <ng-template #bodyTemplate>
                      <ng-container *ngTemplateOutlet="field.bodyTemplate!; context: { $implicit: menuTreeNode.source | ncProperty:field.key, source: menuTreeNode, extra: _menuExtras[menuTreeNode.source.id] }"></ng-container>
                    </ng-template>
                  </td>
                </ng-container>
                <td nzRight="0px">
                  <button class="ctx-btn-link" nz-button nzType="link" (click)="_updateMenu(menuTreeNode.source)">编辑</button>
                  <nz-divider nzType="vertical"></nz-divider>
                  <button class="ctx-btn-link" nz-button nzType="link" (click)="_deleteMenu(menuTreeNode.source)">删除</button>
                </td>
                <td></td>
              </tr>
              <ng-container *ngIf="menuTreeNode.children && menuTreeNode.expand">
                <ng-container *ngTemplateOutlet="menuTreeNodeListTemplate; context: { menuTreeNodeList: menuTreeNode.children }"></ng-container>
              </ng-container>
            </ng-container>
          </ng-template>
        </tbody>
      </nz-table>
    </nz-card>
  </nz-content>

</nz-layout>

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

<ng-template #menuFieldSettingContentTemplate>
  <nz-table #menuFieldTable
            [nzData]="_menuFieldList"
            [nzBordered]="false"
            [nzFrontPagination]="false"
            [nzShowPagination]="false">
    <tbody cdkDropList (cdkDropListDropped)="_dropMenuFieldList($event)">
      <tr *ngFor="let field of menuFieldTable.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-menuTreeNode="source">
  <div class="ctx-table-tree-switcher" [style.paddingLeft]="menuTreeNode.level * 20 + 'px'">
    <a *ngIf="menuTreeNode.children; else childContentTemplate" (click)="menuTreeNode.expand = !menuTreeNode.expand">
      <i nz-icon nzType="caret-right" nzTheme="outline" [nzRotate]="menuTreeNode.expand ? 90 : 0"></i>
      <i *ngIf="menuTreeNode.source.icon" nz-icon [nzType]="menuTreeNode.source.icon"></i>{{ title }}
    </a>
    <ng-template #childContentTemplate>
      <i nz-icon nzType="caret-right" nzTheme="outline" [style.visibility]="'hidden'"></i>
      <i *ngIf="menuTreeNode.source.icon" nz-icon [nzType]="menuTreeNode.source.icon"></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 #visibleBodyTemplate let-visible>
  <nz-tag [nzColor]="visible ? 'success' : 'warning'">{{ visible ? '显示' : '隐藏' }}</nz-tag>
</ng-template>

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