import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { GridModule } from '@progress/kendo-angular-grid';
import { InputsModule } from '@progress/kendo-angular-inputs';
import { ButtonsModule } from '@progress/kendo-angular-buttons';
import { DialogsModule } from '@progress/kendo-angular-dialog';
import { GivingCategoryApiService } from '../../services/giving-category-api.service';
import {
  GivingCategoryDto, CreateGivingCategoryRequest, UpdateGivingCategoryRequest,
} from '../../models/giving.model';
import { PageHeaderActionsDirective } from '../../../../shared/directives/page-header-actions.directive';

@Component({
  selector: 'app-giving-categories-page',
  standalone: true,
  imports: [CommonModule, FormsModule, GridModule, InputsModule, ButtonsModule, DialogsModule, PageHeaderActionsDirective],
  templateUrl: './giving-categories-page.component.html',
  styleUrls: ['./giving-categories-page.component.scss'],
})
export class GivingCategoriesPageComponent implements OnInit {
  data: GivingCategoryDto[] = [];
  isLoading = false;
  includeInactive = false;

  showDialog = false;
  editing: GivingCategoryDto | null = null;
  form: UpdateGivingCategoryRequest = this.blankForm();

  constructor(private api: GivingCategoryApiService) {}

  ngOnInit(): void { this.load(); }

  load(): void {
    this.isLoading = true;
    this.api.getAll(this.includeInactive).subscribe({
      next: rows => { this.data = rows; this.isLoading = false; },
      error: () => { this.isLoading = false; },
    });
  }

  openAdd(): void { this.editing = null; this.form = this.blankForm(); this.showDialog = true; }

  openEdit(c: GivingCategoryDto): void {
    this.editing = c;
    this.form = {
      name_en: c.name_en, name_zh: c.name_zh,
      description_en: c.description_en, description_zh: c.description_zh,
      isActive: c.isActive, sortOrder: c.sortOrder,
    };
    this.showDialog = true;
  }

  save(): void {
    if (this.editing) {
      this.api.update(this.editing.id, this.form).subscribe(() => { this.showDialog = false; this.load(); });
    } else {
      const create: CreateGivingCategoryRequest = {
        name_en: this.form.name_en, name_zh: this.form.name_zh,
        description_en: this.form.description_en, description_zh: this.form.description_zh,
        sortOrder: this.form.sortOrder,
      };
      this.api.create(create).subscribe(() => { this.showDialog = false; this.load(); });
    }
  }

  deactivate(c: GivingCategoryDto): void {
    if (!confirm(`Deactivate "${c.name_en}"?`)) return;
    this.api.deactivate(c.id).subscribe(() => this.load());
  }

  private blankForm(): UpdateGivingCategoryRequest {
    return { name_en: '', name_zh: null, description_en: null, description_zh: null, isActive: true, sortOrder: 0 };
  }
}
