<?php

/**
 * @copyright 2024 Double Bastion LLC <www.doublebastion.com>
 *
 * @author Double Bastion LLC
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

session_start();

define('ACCESSCONST', TRUE);

if (($_SESSION['loggedtorspanel'] == true) && ($_SESSION['userrole'] == 'superadmin')) {

   require('db-connect.php');

   // Insert new component data into the database
   if (isset($_POST['userrole']) && isset($_POST['username']) && isset($_POST['componentname']) && isset($_POST['componentrole']) && isset($_POST['componentplace'])) {

      $ordno = $_POST['ordno'];
      $dbid = $_POST['dbid'];
      $userrole = $_POST['userrole'];
      $username = $_POST['username'];
      $componentname = $_POST['componentname'];
      $componentrole = $_POST['componentrole'];
      $componentplace = $_POST['componentplace'];
      $imagename = $_POST['imagename'];
      $versionscript = $_POST['versionscript'];
      $popupinfo = $_POST['infopopup'];

      if (($userrole != '') && ($username != '') && ($componentname != '')) {

         if ($dbid == '') {

             try {
                  $query1 = $mysqli->prepare("INSERT INTO components (orderno, userrole, username, component, componentrole, componentplace, imagename, versionscript,
                                              info) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);");
                  $query1->bind_param("issssssss", $ordno, $userrole, $username, $componentname, $componentrole, $componentplace, $imagename, $versionscript, $popupinfo);
                  $query1->execute();
                  $query1->close();
                  $responsave = "The component has been added successfully !";

             } catch(mysqli_sql_exception $e) {

                  $responsave = "An error occurred when adding the component. Please check your input data !";
               }

         } else {

             try {

               if ($imagename != '') {
                   $query2 = $mysqli->prepare("UPDATE components SET orderno=?, component=?, componentrole=?, componentplace=?, imagename=?, versionscript=?,
                                              info=? WHERE id=?");
                   $query2->bind_param("issssssi", $ordno, $componentname, $componentrole, $componentplace, $imagename, $versionscript, $popupinfo, $dbid);
                   $query2->execute();
                   $query2->close();

               } else {
                   $query3 = $mysqli->prepare("UPDATE components SET orderno=?, component=?, componentrole=?, componentplace=?, versionscript=?,
                                              info=? WHERE id=?");
                   $query3->bind_param("isssssi", $ordno, $componentname, $componentrole, $componentplace, $versionscript, $popupinfo, $dbid);
                   $query3->execute();
                   $query3->close();
                 }
                   $responsave = "The component has been updated successfully !";

             } catch(mysqli_sql_exception $e) {

                   $responsave = "An error occurred when updating the component. Please check your input data !";
               }
           }

      } else { $responsave = "Error. Please check your input data !"; }
  }

   $result = array(
        'responsave' => $responsave
   );

   echo json_encode($result);

} elseif (empty($_SESSION['loggedtorspanel'])) {

      header("Location: panel-login.php");

} elseif (($_SESSION['loggedtorspanel'] == true) && ($_SESSION['userrole'] != 'superadmin'))  {

      header("Location: index.php");
}

?>
