let new_startDate = "";
let new_endDate = "";
let new_price = "";

let count = 0;

const tableBody = document.querySelector("#billingsTable");


function newPrice() {
  document.getElementById("addPriceDiv").classList.remove("d-none");
  document.getElementById("submitBtn").setAttribute("onclick",`submitAdd()`);
}

function closePrice(){
  document.getElementById("addPriceDiv").classList.add("d-none");
}
function closeDelete(){
  document.getElementById("deleteDiv").classList.add("d-none");
}

function edit(id) {
  document.getElementById("addPriceDiv").classList.remove("d-none");

  document.getElementById("submitBtn").setAttribute("onclick",`submitEdit(${id})`);
  document.getElementById("billingId").value = id;
  document.getElementById("startDate").value = document.getElementById(id).children[1].textContent;
  document.getElementById("endDate").value = document.getElementById(id).children[2].textContent;
  document.getElementById("price").value = document.getElementById(id).children[3].textContent;

  // new_startDate = document.getElementById("startDate").value;
  // new_endDate = document.getElementById("endDate").value;
  // new_price = document.getElementById("price").value;
  // console.log(document.getElementById(id).children[0].textContent);
}

function deleteBilling(id){
  document.getElementById("deleteDiv").classList.remove("d-none");
  document.getElementById("deleteBtn").setAttribute("onclick",`submitDelete(${id})`);
}

function getBillings(){
  let billings = new Array();
  let data = new FormData();

  data.append("key", "brt6k11t");

  fetch("api/getPrice.php", {
      method: "POST",
      body: data
  })
  .then(res => res.json())
  .then(res => {
      if(res.success == "yes"){
          res.result.forEach(element => {
            billings += element;
          });
          console.log(res.result);

          res.result.forEach(item => {
            count++;
              const row = document.createElement("tr");
              row.setAttribute("id",item.id);

              const idCell = document.createElement("td");
              idCell.textContent = count;
              row.appendChild(idCell);

              const startDateCell = document.createElement("td");
              startDateCell.textContent = item.start_date;
              row.appendChild(startDateCell);

              const endDateCell = document.createElement("td");
              endDateCell.textContent = item.end_date;
              row.appendChild(endDateCell);

              const priceCell = document.createElement("td");
              priceCell.textContent = item.price;
              row.appendChild(priceCell);

              const actionCell = document.createElement("td");
              const editButton = document.createElement("button");
              editButton.innerHTML = "Edytuj";
              editButton.setAttribute("class","btn btn-info");
              editButton.setAttribute("onclick",`edit(${item.id})`);
              actionCell.appendChild(editButton);
              
              const deleteButton = document.createElement("button");
              deleteButton.innerHTML = "Kasuj";
              deleteButton.setAttribute("class","btn btn-danger");
              deleteButton.setAttribute("onclick",`deleteBilling(${item.id})`);
              actionCell.appendChild(deleteButton);

              row.appendChild(actionCell);
              
              tableBody.appendChild(row);
          });
      }else{
          // document.getElementById("error").innerText=res.errormessage;
          alert("Błąd: "+res.message);
      }
  })

  // document.getElementById("billingsTable").innerHTML = "";
  // console.log(billings);
}

function submitAdd(){
  let data = new FormData();

  data.append("key", "pricepassword");
  // data.append("id", id);
  data.append("startDate", document.getElementById("startDate").value);
  data.append("endDate", document.getElementById("endDate").value);
  data.append("price", document.getElementById("price").value);

  console.log(data);

  fetch("api/addPrice.php", {
      method: "POST",
      body: data
  })
  .then(res => res.json())
  .then(res => {
    console.log(res);
      if(res.success == "yes"){

        const row = document.createElement("tr");
        row.setAttribute("id",res.message.id);

        const idCell = document.createElement("td");
        idCell.textContent = count+1;
        row.appendChild(idCell);

        const startDateCell = document.createElement("td");
        startDateCell.textContent = res.message.startDate;
        row.appendChild(startDateCell);

        const endDateCell = document.createElement("td");
        endDateCell.textContent = res.message.endDate;
        row.appendChild(endDateCell);

        const priceCell = document.createElement("td");
        priceCell.textContent = res.message.price;
        row.appendChild(priceCell);

        const actionCell = document.createElement("td");
        const editButton = document.createElement("button");
        editButton.innerHTML = "Edytuj";
        editButton.setAttribute("class","btn btn-info");
        editButton.setAttribute("onclick",`edit(${res.message.id})`);
        actionCell.appendChild(editButton);
        
        const deleteButton = document.createElement("button");
        deleteButton.innerHTML = "Kasuj";
        deleteButton.setAttribute("class","btn btn-danger");
        deleteButton.setAttribute("onclick",`deleteBilling(${res.message.id})`);
        actionCell.appendChild(deleteButton);

        row.appendChild(actionCell);
        
        tableBody.appendChild(row);
        closePrice();
      }else{
          alert("Błąd: "+res.message);
      }
  })
}

function submitEdit(id){
  let data = new FormData();

  data.append("key", "pricepassword");
  data.append("id", id);
  data.append("startDate", document.getElementById("startDate").value);
  data.append("endDate", document.getElementById("endDate").value);
  data.append("price", document.getElementById("price").value);

  console.log(data);

  fetch("api/editPrice.php", {
      method: "POST",
      body: data
  })
  .then(res => res.json())
  .then(res => {
      if(res.success == "yes"){
        document.getElementById(id).children[1].textContent = document.getElementById("startDate").value;
        document.getElementById(id).children[2].textContent = document.getElementById("endDate").value;
        document.getElementById(id).children[3].textContent = document.getElementById("price").value;
        closePrice();
      }else{
          alert("Błąd: "+res.message);
      }
  })
}

function submitDelete(id){
  let data = new FormData();

  data.append("key", "pricepassword");
  data.append("id", id);

  console.log(data);

  fetch("api/deletePrice.php", {
      method: "POST",
      body: data
  })
  .then(res => res.json())
  .then(res => {
      if(res.success == "yes"){
        tableBody.removeChild(document.getElementById(id));
        closeDelete();
      }else{
        alert("Błąd: "+res.message);
      }
  })
}

// console.log(Cookies.get("key"));
getBillings();