import { Component, ViewContainerRef } from '@angular/core';
import { NzModalService } from 'ng-zorro-antd/modal';
import { CommonNzModule } from '../../../../../common.nz.module';
import { KnowledgeService } from '../../../../../services/knowledge.service';
import {
  KnowledgeRiskTypeFormData,
  KnowledgeTypeFormComponent,
} from './knowledge-type-form/knowledge-type-form.component';

@Component({
  selector: 'knowledge-risk-type',
  standalone: true,
  imports: [CommonNzModule],
  templateUrl: './knowledge-risk-type.component.html',
  styleUrl: './knowledge-risk-type.component.less',
})
export class KnowledgeRiskTypeComponent {
  constructor(
    private readonly knowledge: KnowledgeService,
    private readonly modal: NzModalService,
    private readonly viewContainerRef: ViewContainerRef
  ) {}
  get tableData() {
    return this.knowledge.types();
  }
  get updateDate() {
    return this.knowledge.typeUpdateDate();
  }

  updateRiskType() {
    this.modal.create<KnowledgeTypeFormComponent, KnowledgeRiskTypeFormData>({
      nzClassName: 'precaution-modal type-modal',
      nzTitle: '更新风险项分类',
      nzOkText: '关闭',
      nzContent: KnowledgeTypeFormComponent,
      nzWidth: 800,
      nzCancelText: null,
      nzClosable: false,
      nzViewContainerRef: this.viewContainerRef,
      nzAutofocus: null,
      nzData: {
        data: this.tableData,
      },
      nzOnOk: async instance => {
        this.knowledge.fetchRiskTypes();
      },
    });
  }
}
