<?php
   include('session.php');
   include('header1.php');
   include('ietpartial.php');
   include('funcGetValueFromTable.php');

// start role specific 
$user_role = $_SESSION['role'];


  if(isset($_GET['updated'])) {

    $updated = $_GET['updated'];

  }

      $data = "SELECT *
      FROM valuesettings;";

      $resultData = mysqli_query($db,$data);

      $countData = mysqli_num_rows($resultData);


  echo "<main role=\"main\" class=\"col-md-9 ml-sm-auto col-lg-10 px-4\">";

  echo "<div class=\"d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom\">";

      echo "<script type=\"text/javascript\" src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js\"></script>";
    	echo "<h1 class=\"h2\">Data Table</h1>";
      echo "<a href=\"technicalsettings.php\" class=\"btn btn-sm btn-outline-secondary\" role=\"button\">Back</a>";
    
    	echo "</div>";

      echo "<table class = \"table\">";


      if ($_SESSION['login_user'] == 'admin') {

        if ($countData !== 0) { 
          echo "<thead><td>Name</td><td>Description</td><td>Value</td><td>Update</td></thead>";
          while ($rowData = mysqli_fetch_array($resultData)) {
            echo "<form id=\"valueform\" name=\"form\">";
            echo "<input type=\"hidden\" id = \"settingid" . $rowData['settingid'] . "\" value = \"" . $rowData['settingid'] . "\" >";
            
            echo "<tr><td>" . $rowData['settingName'] . "</td><td>" . $rowData['settingDesc'] . "</td><td><input type=\"numeric\" value=\"" . $rowData['settingInt'] . "\" class=\"form-control\" id = \"settingInt" . $rowData['settingid'] . "\" ></td>";
            echo "<td><input id=\"submit" . $rowData['settingid'] . "\" class=\"submit\" onclick=\"formsubmit" . $rowData['settingid'] . "()\" type=\"button\" value=\"Update\"></td>";

            echo "    

            <script type=\"text/javascript\">
            
                function formsubmit" . $rowData['settingid'] . "()
                {
                 
                 var settingid =document.getElementById( \"settingid" . $rowData['settingid'] . "\" ).value;
                 var settingInt =document.getElementById( \"settingInt" . $rowData['settingid'] . "\" ).value;

                  var formdata = 
                      'settingid=' + settingid + 
                      '&settingInt=' + settingInt;

          
          // AJAX code to submit form.
          $.ajax({
             type: \"POST\",
             url: \"updatevaluetable.php\", 
             data: formdata,
             cache: false,
             success: function(html) {
              alert(html);
             }
          });

          return false;
        }
            
                </script>";



            echo "</form>";
          }
        }
    echo "</table>";

}


    echo "<br>";
    //echo valueFromTable("car_benefit_less_2000cc",$db);

    echo "<hr>";
    echo "<h1 class=\"h2\">PRGF Flags</h1>";

    $getBenifitsListing = "SELECT * FROM benefits;";

    $resultBenefits = mysqli_query($db,$getBenifitsListing);

    $countBenefits = mysqli_num_rows($resultBenefits);

    if ($countBenefits !== 0) {
      echo "<table class = \"table\">";
        echo "<thead><td>Benefit Name</td><td>PRGF Flag</td><td>Update</td></thead>";
            while ($rowBenefit = mysqli_fetch_array($resultBenefits)) {
              echo "<form id=\"valueform\" name=\"form\">";
              echo "<input type=\"hidden\" id = \"benefitid" . $rowBenefit['benefitid'] . "\" value = \"" . $rowBenefit['benefitid'] . "\" >";
              
              echo "<tr><td>" . $rowBenefit['benefitname'] . "</td>";

              echo "<td>";

              $checkFlag = "";

              if ($rowBenefit['prgf_flag'] == '1') {
                $checkFlag = "checked";
              }

              echo "<select class=\"form-control\" id = \"benefitflag" . $rowBenefit['benefitid'] . "\">";

              if ($checkFlag == "checked") {
                echo "<option selected>Yes</option>";
                echo "<option>No</option>";
              } else {
                echo "<option>Yes</option>";
                echo "<option selected>No</option>";
              }

              

              echo "</select>";

              echo "</td>";
              
              
              echo "<td><input id=\"submitprgf" . $rowBenefit['benefitid'] . "\" class=\"submit\" onclick=\"formsubmitprgf" . $rowBenefit['benefitid'] . "()\" type=\"button\" value=\"Update\"></td>";

              echo "    

              <script type=\"text/javascript\">
              
                  function formsubmitprgf" . $rowBenefit['benefitid'] . "()
                  {
                  
                  var benefitid =document.getElementById( \"benefitid" . $rowBenefit['benefitid'] . "\" ).value;
                  var benefitflag =document.getElementById( \"benefitflag" . $rowBenefit['benefitid'] . "\" ).value;

                    var formdata = 
                        'benefitid=' + benefitid + 
                        '&benefitflag=' + benefitflag;

            
            // AJAX code to submit form.
            $.ajax({
              type: \"POST\",
              url: \"updatebenefitflag.php\", 
              data: formdata,
              cache: false,
              success: function(html) {
                alert(html);
              }
            });

            return false;
          }
              
                  </script>";



              echo "</form>";
            }
          
        echo "</table>";


    } else {
      echo "<p>Benefits not available.</p>";
    }


include('footer1.php');
?>