<script>
function update_pri(id) {
  var idn='pri'+id;
    if (id.length == 0) {
        alert("Input Error!");
        return;
    } else {
      var pri=document.getElementById(idn).value;
      document.getElementById(idn).disabled = true;
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
              document.getElementById(idn).disabled = false;
                document.getElementById(idn).value = this.responseText;
            }
        };
        xmlhttp.open("GET", "/execute.php?id=" + id + "&pri="+pri, true);
        xmlhttp.send();
    }
}
</script>

<script>
function switch_state(id) {
  var idn='act'+id;
    if (id.length == 0) {
        alert("Input Error!");
        return;
    } else {
      var active=document.getElementById(idn).value;
      document.getElementById(idn).disabled = true;
      console.log(idn);
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
              document.getElementById(idn).disabled = false;
                document.getElementById(idn).value = this.responseText;
            }
        };
        xmlhttp.open("GET", "/execute2.php?id=" + id + "&active="+active, true);
        xmlhttp.send();
    }
}
</script>

<div class="container table-responsive">
  <table class="table table-striped table-bordered table-hover">
    <tr>
      <th>ID</th>
      <th>TITLE</th>
      <th>DATE</th>
      <th>CATEGORY</th>
      <th>PRIORITY</th>
      <th>STATE</th>
    </tr>
      <?php
      $conn = new mysqli($GLOBALS['host'], $GLOBALS['user'], $GLOBALS['pass'], $GLOBALS['db']);
      $conn->set_charset("utf8");
      if ($conn->connect_error) {
          die("Connection failed: " . $conn->connect_error);
      }

      $sql = "SELECT * FROM ".$GLOBALS['table1'];
      $result = $conn->query($sql);

      if ($result->num_rows > 0) {
          while($row = $result->fetch_assoc()) {
              //echo "id: " . $row["N_TITLE"]. " - Name: " . $row["N_DATE"]. " " . $row["N_CATEGORY"]." " . $row["pri"]. "<br>";
              $ID=$row["id"];
              echo "
              <tr>
                <td>" . $ID. "</td>
                <td>" . $row["N_TITLE"]. "</td>
                <td>" . $row["N_DATE"]. "</td>
                <td>" . $row["N_CATEGORY"]. "</td>
                <td>
                  <form method='post'>
                    <input type='number' name='pri' id='pri" .$ID. "' value='" . $row["pri"]. "'>
                    <input type='hidden' name='id' value='" .$ID. "'>
                    <input class='btn btn-success' type='button' name='priority' value='SAVE' onclick='update_pri(" .'"'.$ID.'"'. ")'>
                  </form>
                </td>
                <td>
                  <select name='state'  id='act" .$ID. "' onchange='switch_state(" .'"'.$ID.'"'. ")' >
                    <option>".$row['ACTIVE']."</option>
                    <option>0</option>
                    <option>1</option>
                  </select>
                </td>
              </tr>";
          }
      } else {
          echo "0 results";
      };
      $conn->close();
      ?>
      <?php
            if(isset($_POST['priority']) && $_POST['priority']=='SUBMIT')
                {

                  $conn = new mysqli($GLOBALS['host'], $GLOBALS['user'], $GLOBALS['pass'], $GLOBALS['db']);
                  if ($conn->connect_error) {
                      die("Connection failed: " . $conn->connect_error);
                  }

                  $sql = "UPDATE  `".$GLOBALS["table1"]."` SET  `pri` =  '".$_POST['pri']."' WHERE  `news`.`id` =".$_POST['id'];
                  $result = $conn->query($sql);
                  if(!$result) echo mysqli_error();  else  echo "Successfully Updated! <br />";
                  $conn->close();
                 }
      ?>

      <?php
            if(isset($_POST['statebtn']) && $_POST['statebtn']=='SUBMIT')
                {

                  $conn = new mysqli($GLOBALS['host'], $GLOBALS['user'], $GLOBALS['pass'], $GLOBALS['db']);
                  if ($conn->connect_error) {
                      die("Connection failed: " . $conn->connect_error);
                  }

                  $sql = "UPDATE  `".$GLOBALS["table1"]."` SET  `ACTIVE` =  '".$_POST['active']."' WHERE  `news`.`id` =".$_POST['id'];
                  $result = $conn->query($sql);
                  if(!$result) echo mysqli_error();  else  echo "Successfully Updated! <br />";
                  $conn->close();
                 }
      ?>
  </table>
</div>
