import { Component } from '@angular/core';
import { ICellRendererAngularComp } from 'ag-grid-angular';
import { ICellRendererParams } from 'ag-grid-community';

@Component({
  standalone: true,
  template: `
   <span style="display: flex; justify-content: center; align-items: center; height: 100%;">
    <button class="ag-grid-button-style" (click)="buttonClicked()">{{cellValue}}</button>
</span>

  `,
  styleUrls: ['./grid-button.less']
})

export class GridButtonValueRenderer implements ICellRendererAngularComp {
  public cellValue!: string;
  public cellPath!: string;
  // gets called once before the renderer is used
  agInit(params: ICellRendererParams): void {
    this.cellValue = this.getValueToDisplay(params);
  }

  // gets called whenever the user gets the cell to refresh
  refresh(params: ICellRendererParams) {
    // set value into cell again
    this.cellValue = this.getValueToDisplay(params);
    return true;
  }

  buttonClicked() {
    alert(`点击了 ${this.cellValue}`);
  }

  getValueToDisplay(params: ICellRendererParams) {
    return params.valueFormatted ? params.valueFormatted : params.value;
  }
}