import { Component } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-view-page',
  standalone: true,
  imports: [],
  templateUrl: './view-page.component.html',
  styleUrl: './view-page.component.less'
})
export class ViewPageComponent {
  constructor(private router: Router) {
    $.ajax({
      url: "https://localhost/web/Lab%208/BackEnd/Repositories/Repository.php?action=getAllNews",
      type: "GET",
      xhrFields: {
        withCredentials: true,
      },
      success: function (response) {
        response = JSON.parse(response);
        $("#tableBody").empty();
        for (var i = 0; i < response.length; i++) {
          var newRow =
            "<tr><td>" +
            response[i].title +
            "</td><td>" +
            response[i].news_text +
            "</td><td>" +
            response[i].category +
            "</td><td>" +
            response[i].date +
            "</td><td>" +
            response[i].producer +
            "</td></tr>";
          $("#tableBody").append(newRow);
        }
      },
      error: function (response) {
        alert("Error getting log!");
      },
    });
  }
  back = (e: MouseEvent) => {
    e.preventDefault();
    this.router.navigate(["/"]);
  }

  filter = (e: MouseEvent) => {
    var date = $("#dateField").val();
    var category = $("#categoryField").val();
    $.ajax({
      url: "https://localhost/web/Lab%208/BackEnd/Repositories/Repository.php",
      type: "GET",
      data: {
        date: date,
        category: category,
        action: "getAllNewsByCategoryAndDate",
      },
      xhrFields: {
        withCredentials: true,
      },
      success: function (response) {
        response = JSON.parse(response);
        $("#tableBody").empty();
        for (var i = 0; i < response.length; i++) {
          var newRow =
            "<tr><td>" +
            response[i].title +
            "</td><td>" +
            response[i].news_text +
            "</td><td>" +
            response[i].category +
            "</td><td>" +
            response[i].date +
            "</td><td>" +
            response[i].producer +
            "</td></tr>";
          $("#tableBody").append(newRow);
        }
      },
    })
  }
}
