import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core' import { SharedModule } from 'app/shared/shared.module' import { ApiService } from 'app/services' import { FormBuilder, FormGroup } from '@angular/forms' import { AnyObject, TableOption } from 'app/shared/components/server-paginated-table' import { NzSafeAny } from 'ng-zorro-antd/core/types' import { NzModalService } from 'ng-zorro-antd/modal' import { NzMessageService } from 'ng-zorro-antd/message' import { NzDrawerRef, NzDrawerService } from 'ng-zorro-antd/drawer' import { lastValueFrom } from 'rxjs' import { FormValidators } from 'app/utils' @Component({ selector: 'app-basic-manufacturer', standalone: true, imports: [SharedModule], templateUrl: './basic-manufacturer.component.html', styleUrl: './basic-manufacturer.component.less', }) export class BasicManufacturerComponent { constructor( private modal: NzModalService, private msg: NzMessageService, private drawer: NzDrawerService, private api: ApiService, private fb: FormBuilder, ) {} table = new TableOption(this.fetchData.bind(this)) queryForm!: FormGroup createForm!: FormGroup drawerRef?: NzDrawerRef @ViewChild('drawerFooterTpl') drawerFooterTpl!: TemplateRef @ViewChild('formContentTpl') formContentTpl!: TemplateRef initCreateForm() { this.createForm = this.fb.group({ manufacturersVendorId: [], name: ['', [FormValidators.required('请输入')]], code: ['', []], notes: ['', []], address: ['', []], }) } initQueryForm() { this.queryForm = this.fb.group({ name: [''], code: [''], }) } ngOnInit(): void { this.table .setColumn([ { key: 'manufacturersVendorId', title: '主键', visible: false }, { key: 'name', title: '名称' }, { key: 'code', title: '编码' }, { key: 'address', title: '地址' }, { key: 'notes', title: '备注' }, { key: 'createTime', title: '创建时间' }, ]) .setRowOperate([ { title: '编辑', onClick: this.onCreate.bind(this) }, { title: '删除', onClick: this.deleteItem.bind(this) }, ]) this.initQueryForm() this.initCreateForm() } fetchData(p: {}, q: AnyObject) { return this.api.getBasicManufacturersVendorPage({ ...p, ...q }) } onCreate(data?: NzSafeAny) { if (data) { this.createForm.patchValue(data) } this.drawerRef = this.drawer.create({ nzTitle: data ? '编辑生产商' : '新增生产商', nzContent: this.formContentTpl, nzFooter: this.drawerFooterTpl, nzWidth: 600, nzOnCancel: this.onCancel.bind(this), }) } onConfirm() { if (FormValidators.validateFormGroup(this.createForm)) { const { value } = this.createForm this.api .saveBasicManufacturersVendor({ ...value, // manufacturersVendorId: value.manufacturersVendorId ?? 0, }) .subscribe((res) => { this.msg.success(res.desc) this.onCancel() this.table.ref.reload() }) } } async onCancel() { this.drawerRef?.close() this.createForm.reset({}) } deleteItem(item: NzSafeAny) { this.modal.confirm({ nzTitle: '警告', nzContent: '是否要删除该生产商?', nzOnOk: async () => { const res = await lastValueFrom(this.api.deleteBasicManufacturersVendor([item.manufacturersVendorId])) this.msg.success(res.desc) this.table.ref.reload() }, }) } }