import { Component } from '@angular/core';
import { Router } from '@angular/router';
import News from '../../types/news';

@Component({
  selector: 'app-admin-page',
  standalone: true,
  imports: [],
  templateUrl: './admin-page.component.html',
  styleUrl: './admin-page.component.less'
})
export class AdminPageComponent {
  news: News[] = [];
  constructor(private router: Router) { 
    $.ajax({
      url: 'https://localhost/web/Lab%208/BackEnd/Repositories/Repository.php?action=isLogged',
      type: 'GET',
      xhrFields: {
        withCredentials: true
      },
      success: function(data: any){
        if (data === 'false'){
          router.navigate(['/login']);
        }
      }
    });
    $.ajax({
      context: this,
      url: 'https://localhost/web/Lab%208/BackEnd/Repositories/Repository.php?action=getAllNews',
      type: 'GET',
      xhrFields: {
        withCredentials: true
      },
      success: function(data: any){
        this.news = JSON.parse(data);
      }
    })
    
  }
  logout = () => {
    let router = this.router;
    $.ajax({
      url: 'https://localhost/web/Lab%208/BackEnd/Repositories/Repository.php?action=logout',
      type: 'GET',
      xhrFields: {
        withCredentials: true
      },
      success: function(data: any){
        router.navigate(['/login']);
      }
    });
  }
  addNews = () => {
    this.router.navigate(['/add']);
  }
  editNews = (id: number) => {
    this.router.navigate([`/edit/${id}`]);
  }
}
