import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd/message';
import { firstValueFrom } from 'rxjs';
import { FormUtils } from 'src/app/lib/utils/form-utils';
import { ContentService } from '../content/content.service';
import { EpgService } from './epg.service';
@Component({
selector: 'app-column-add',
template: `
`,
styles: [
]
})
export class ColumnAddComponent implements OnInit {
@Input() epgId: string;
@Output() onSuccess = new EventEmitter();
@Output() onError = new EventEmitter();
form: FormGroup;
isLoading = false;
translateOptions: any[] = [];
translate: string; // 多语翻译ID
constructor(
private epgService: EpgService,
private contentService: ContentService,
private fb: FormBuilder,
private message: NzMessageService,
) {
firstValueFrom(this.contentService.translateOptions()).then((resp: any[]) => {
this.translateOptions = resp;
})
}
ngOnInit(): void {
this.form = this.fb.group({
id: [null, [Validators.required]],
visible: [false, [Validators.required]],
translate: [null, [Validators.required]],
// en: [null, [Validators.required]],
// zh: [null, [Validators.required]],
})
}
onSubmit() {
if (this.form.invalid) {
FormUtils.markAsDirty(this.form);
return;
}
this.isLoading = true;
Object.assign(this.form.value, { _id: this.epgId });
firstValueFrom(this.epgService.columnAdd(this.form.value))
.then(res => {
this.onSuccess.emit(this.form.value);
this.message.success($localize`添加成功`);
})
.catch(err => {
this.message.error(err.error?.message || err.message);
this.onError.emit(err);
}).then(() => {
this.isLoading = false;
})
}
}