import { Component, ElementRef, OnInit, TemplateRef, ViewChild } from '@angular/core' import { FormControl, FormGroup } from '@angular/forms' import { NzModalService } from 'ng-zorro-antd/modal' import { ApiService } from 'app/services' import { AnyObject, ServerPaginatedTableComponent, TableOption } from 'app/components/server-paginated-table' import { SharedModule } from 'app/shared/shared.module' import { FormValidators } from 'app/utils' import { lastValueFrom } from 'rxjs' import { EntityDTO, ProductDTO } from 'app/services/api.dto' import { NzMessageService } from 'ng-zorro-antd/message' import { format } from 'date-fns' import { AuthorizeComponent } from 'app/components/authorize/authorize.component' @Component({ standalone: true, imports: [SharedModule], selector: 'app-entity-list', templateUrl: './entity-list.component.html', styleUrls: ['./entity-list.component.less'], }) export class EntityListComponent implements OnInit { constructor( private api: ApiService, private modal: NzModalService, private msg: NzMessageService, ) {} @ViewChild('entityFormTpl') entityFormTpl!: TemplateRef<{}> table = new TableOption(this.fetchData.bind(this), [{ key: 'name', title: '实体名称' }], [], { cacheUrls: ['/dashboard'], }) queryForm = new FormGroup({ name: new FormControl(''), createTime: new FormControl(), }) createForm = new FormGroup({ name: new FormControl('', [FormValidators.required('请输入实体名称'), FormValidators.maxLength(30)]), // secretKey: new FormControl('', [FormValidators.required('请输入序列号')]), contact: new FormControl('', FormValidators.maxLength(30)), // address: new FormControl('', FormValidators.maxLength(200)), remark: new FormControl('', FormValidators.maxLength(200)), }) product: { id: string; name: string }[] = [] ngOnInit(): void { this.table .setColumn([ { key: 'name', title: '实体名称' }, { key: 'contact', title: '联系方式' }, // { key: 'address', title: '地址' }, { key: 'remark', title: '说明' }, { key: 'createTime', title: '创建时间' }, ]) .setRowOperate([ { title: '添加授权', premissions: [], onClick: this.authorize.bind(this) }, { title: '编辑', onClick: this.create.bind(this) }, { title: '删除', onClick: this.deleteItem.bind(this) }, ]) this.api.getAllProduct().subscribe((res) => { this.product = res.data }) } fetchData(p: {}, q: AnyObject) { if (Array.isArray(q['createTime'])) { const createTimeStart = q['createTime']?.[0] const createTimeEnd = q['createTime']?.[1] q['createTimeStart'] = createTimeStart ? format(new Date(createTimeStart), 'yyyy-MM-dd HH:mm:ss') : '' q['createTimeEnd'] = createTimeEnd ? format(new Date(createTimeEnd), 'yyyy-MM-dd HH:mm:ss') : '' } return this.api.getEntityPage(p, q) } onSearch() {} authorize(v: EntityDTO) { this.modal.create({ nzTitle: '添加授权', nzContent: AuthorizeComponent, nzWidth: 800, nzData: { product: this.product, entity: v, }, nzFooter: null, }) } create(entity?: EntityDTO) { if (entity) { this.createForm.patchValue(entity) this.createForm.get('secretKey')?.disable() } else { this.createForm.get('secretKey')?.enable() } this.modal.create({ nzTitle: entity ? '编辑实体' : '添加实体', nzContent: this.entityFormTpl, nzOnOk: async () => { if (FormValidators.validateFormGroup(this.createForm)) { await lastValueFrom(this.api.saveEntity({ ...(entity ?? {}), ...this.createForm.value })) this.msg.success('保存成功') this.table.ref?.reload() this.createForm.reset() return true } return false }, nzOnCancel: () => { this.createForm.reset() }, }) } deleteItem(entity: EntityDTO) { this.modal.confirm({ nzTitle: '警告', nzContent: '是否要删除该实体?', nzOkDanger: true, nzOnOk: async () => { await lastValueFrom(this.api.deleteEntity(entity.id)) this.msg.success('删除成功') this.table.ref?.reload() }, }) } }