<nz-layout>

  <div class="ctx-search-bar">
    <form nz-form [formGroup]="_companySearchForm">
      <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 class="ctx-search-actions">
        <div class="ctx-search-action-item"><button nz-button nzType="primary" (click)="_requeryCompanyList()"><i nz-icon nzType="search" nzTheme="outline"></i>查询</button></div>
        <div class="ctx-search-action-item"><button nz-button (click)="_resetCompanySearchForm()"><i nz-icon nzType="rest" nzTheme="outline"></i>重置</button></div>
      </div>
    </form>
  </div>

  <div class="ctx-list-action-bar">
    <div class="ctx-actions">
      <div class="ctx-action-item"><button nz-button nzType="primary" (click)="_createCompany()"><i nz-icon nzType="plus" nzTheme="outline"></i>新增</button></div>
    </div>
    <div class="ctx-extra-actions">
      <div class="ctx-action-item"><a nz-button nzType="text" title="刷新" (click)="_queryCompanyList()"><i nz-icon nzType="reload" nzTheme="outline"></i></a></div>
      <div class="ctx-action-item"><a nz-button
          nzType="text"
          title="列设置"
          nz-popover
          nzPopoverTrigger="click"
          nzPopoverPlacement="bottomRight"
          nzPopoverOverlayClassName="ctx-table-setting"
          [nzPopoverTitle]="companyFieldSettingTitleTemplate"
          [nzPopoverContent]="companyFieldSettingContentTemplate"><i nz-icon nzType="setting" nzTheme="outline"></i></a>
      </div>
    </div>
  </div>

  <nz-content>
    <nz-table #companyTable
      nzTableLayout="fixed"
      [nzData]="_companyList"
      [nzLoading]="_companyLoading"
      [nzLoadingDelay]="500"
      [nzFrontPagination]="false"
      [nzTotal]="_companyTotal"
      [nzShowTotal]="tableTotalTemplate"
      [(nzPageIndex)]="_companyParams.page"
      (nzPageIndexChange)="_queryCompanyList()"
      [(nzPageSize)]="_companyParams.size"
      (nzPageSizeChange)="_requeryCompanyList()"
      [nzPageSizeOptions]="[20, 50, 100, 200]">
      <thead>
        <tr>
          <th nzLeft="0px"
            nzShowCheckbox
            [nzChecked]="_companyAllChecked"
            [nzIndeterminate]="_companyIndeterminate"
            [nzDisabled]="companyTable.data.length === 0"
            (nzCheckedChange)="_checkAllCompanyList($event)"></th>
          <ng-container *ngFor="let field of _companyFieldList">
            <th *ngIf="field.visible"
              [nzShowSort]="!!field.sortKey"
              [nzSortOrder]="field.sortOrder!"
              (nzSortOrderChange)="field.sortOrder = $event!; _changeCompanyFieldSort(field)"
              nz-resizable
              nzBounds="window"
              nzPreview
              [nzAlign]="field.align!"
              [nzWidth]="field.width!"
              (nzResizeEnd)="field.width = $event.width + 'px'">
              <ng-container *ngIf="!field.titleTemplate; else field.titleTemplate!">{{ field.description }}</ng-container>
              <nz-resize-handle nzDirection="right"></nz-resize-handle>
            </th>
          </ng-container>
          <th nzRight="0px" nzWidth="160px">操作</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <tr *ngFor="let company of companyTable.data">
          <td nzLeft="0px" nzShowCheckbox [(nzChecked)]="_companyExtras[company.id].checked" (nzCheckedChange)="_updateCompanyCheckStatus()"></td>
          <ng-container *ngFor="let field of _companyFieldList">
            <td *ngIf="field.visible" [nzAlign]="field.align!">
              <ng-container *ngIf="!field.contentTemplate; else contentTemplate">{{ company[field.name] || '-' }}</ng-container>
              <ng-template #contentTemplate>
                <ng-container *ngTemplateOutlet="field.contentTemplate!; context: { $implicit: company[field.name], source: company, extra: _companyExtras[company.id] }"></ng-container>
              </ng-template>
            </td>
          </ng-container>
          <td nzRight="0px">
            <a (click)="_updateCompany(company)">编辑</a>
            <nz-divider nzType="vertical"></nz-divider>
            <a (click)="_deleteCompany(company)">删除</a>
          </td>
          <td></td>
        </tr>
      </tbody>
    </nz-table>
  </nz-content>

</nz-layout>

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

<ng-template #companyFieldSettingContentTemplate>
  <nz-table #companyFieldTable [nzData]="_companyFieldList" [nzBordered]="false" [nzFrontPagination]="false" [nzShowPagination]="false">
    <tbody cdkDropList (cdkDropListDropped)="_dropCompanyFieldList($event)">
      <tr *ngFor="let field of companyFieldTable.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.titleTemplate; else field.titleTemplate!">{{ field.description }}</ng-container>
          </label>
        </td>
      </tr>
    </tbody>
  </nz-table>
</ng-template>

<ng-template #enabledTemplate let-enabled>
  <nz-tag [nzColor]="enabled ? 'success' : 'warning'">{{ enabled ? '有效' : '无效' }}</nz-tag>
</ng-template>

<ng-template #tagListTemplate let-companyExtra="extra">
  <ng-container *ngIf="companyExtra.tagList.length > 0; else valueEmptyTemplate">
    <nz-tag *ngFor="let tag of companyExtra.tagList">{{ tag }}</nz-tag>
  </ng-container>
</ng-template>

<ng-template #applicationListTemplate let-applicationList>
  <ng-container *ngIf="applicationList.length > 0; else valueEmptyTemplate">
    <nz-tag *ngFor="let application of applicationList">{{ application.name }}</nz-tag>
  </ng-container>
</ng-template>

<ng-template #timeTemplate let-time>{{ (time | date:'yyyy-MM-dd HH:mm:ss') || '-' }}</ng-template>

<ng-template #tableTotalTemplate let-total>共 {{ total }} 条记录</ng-template>

<ng-template #valueEmptyTemplate>-</ng-template>

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