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-maintainer', standalone: true, imports: [SharedModule], templateUrl: './basic-maintainer.component.html', styleUrl: './basic-maintainer.component.less', }) export class BasicMaintainerComponent { 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({ maintenanceVendorId: [], name: ['', [FormValidators.required('请输入')]], code: ['', []], uscc: ['', []], notes: ['', []], businessContactor: ['', []], businessContact: ['', []], afterSalesContactor: ['', []], afterSalesContact: ['', []], address: ['', []], }) } initQueryForm() { this.queryForm = this.fb.group({ name: [''], uscc: [''], code: [''], }) } ngOnInit(): void { this.table .setColumn([ { key: 'name', title: '名称' }, { key: 'code', title: '编码' }, { key: 'uscc', title: '统一社会信用代码' }, { key: 'businessContactor', title: '商务联系人' }, { key: 'businessContact', title: '商务联系方式' }, { key: 'afterSalesContactor', title: '售后联系人' }, { key: 'afterSalesContact', title: '售后联系方式' }, { key: 'address', title: '地址' }, { key: 'notes', 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.getBasicMaintenanceVendorPage({ ...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 .saveBasicMaintenanceVendor({ ...value, // maintenanceVendorId: value.maintenanceVendorId ?? 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.deleteBasicMaintenanceVendor([item.maintenanceVendorId])) this.msg.success(res.desc) this.table.ref.reload() }, }) } }