import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core';
import { ICheckboxCol, IRowKey } from '../table.type';
import { ColBase } from './col.base';

@Component({
  selector: 'bixi-table-col-checkbox',
  host: {
    '[class.bixi-table-col-checkbox]': 'true'
  },
  template: `
    <label nz-checkbox [nzDisabled]="!row._selectable" [(nzChecked)]="row._selected" (nzCheckedChange)="onCheckedChange($event)"></label>
  `,
  styleUrls: ['../style/index.less'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class BixiTableColCheckboxComponent extends ColBase {
  @Input() col: Required<ICheckboxCol>;
  @Input() selectedKeys: IRowKey[] = [];
  @Output() selectedChange = new EventEmitter<{ selected: boolean; key: IRowKey }>();

  onCheckedChange(val: boolean) {
    this.selectedChange.emit({
      key: this.row[this.col.key],
      selected: val
    });
    if (this.col.onSelected) {
      this.col.onSelected(this.row, this.index, val);
    }
  }
}
