<?php
include('include/headscript.php'); ?>
<?php
// pickup the get variable
if (isset($_POST["idtrf"])) {
  $idtrf = $_POST["idtrf"];
}
if (isset($_GET["idtrf"])) {
  $idtrf = $_GET["idtrf"];
}

if (isset($_POST["addchem"])) {
  $addchem = $_POST["addchem"];
}
if (isset($_POST["addchemform"])) {
  $addchemform = $_POST["addchemform"];
} else {
  $addchemform = "";
}

if (isset($_GET["codestep"])) {
  $code = "6";

  $InsertQuery = new WA_MySQLi_Query($cmctrfdb);
  $InsertQuery->Action = "insert";
  $InsertQuery->Table = "wheretrfstep";
  $InsertQuery->bindColumn("idtrf", "i", "$idtrf", "WA_DEFAULT");
  $InsertQuery->bindColumn("code", "i", "$code", "WA_DEFAULT");
  $InsertQuery->saveInSession("");
  $InsertQuery->execute();
  $InsertGoTo = "";

  $InsertQuery->redirect($InsertGoTo);
}

?>


<?php
$trfnumberfinal = new WA_MySQLi_RS("trfnumberfinal", $cmctrfdb, 1);
$trfnumberfinal->setQuery("SELECT * FROM `trf-details` WHERE `trf-details`.idtrfdetails='$idtrf'");
$trfnumberfinal->execute();
$idarticletype = $trfnumberfinal->getColumnVal("idarticletype");
$idcertn = $trfnumberfinal->getColumnVal("idcertification");
?>
<?php $idcert = $trfnumberfinal->getColumnVal("idcertification") ?>
<?php
$certname = new WA_MySQLi_RS("certname", $cmctrfdb, 1);
$certname->setQuery("SELECT * FROM certificationtype WHERE certificationtype.idcertificationtype='$idcert'");
$certname->execute(); ?>
<?php
$chemicalagentlist = new WA_MySQLi_RS("chemicalagentlist", $cmctrfdb, 0);
$chemicalagentlist->setQuery("SELECT * FROM chemicalagent ORDER BY chemicalagent.name_chemicalagent");
$chemicalagentlist->execute();
?>


<?php
if ($addchemform == "Y") {


  $checkchemical = new WA_MySQLi_RS("checkchemical", $cmctrfdb, 1);
  $checkchemical->setQuery("SELECT * FROM trfchemicalagent WHERE trfchemicalagent.idtrf='$idtrf' AND trfchemicalagent.idchemicalagent='$addchem'");
  $checkchemical->execute();
  if (empty($checkchemical->getColumnVal("idtrfchemicalagent"))) {

    $InsertQuery = new WA_MySQLi_Query($cmctrfdb);
    $InsertQuery->Action = "insert";
    $InsertQuery->Table = "trfchemicalagent";
    $InsertQuery->bindColumn("idtrf", "i", "$idtrf", "WA_DEFAULT");
    $InsertQuery->bindColumn("idchemicalagent", "i", "$addchem", "WA_DEFAULT");
    $InsertQuery->saveInSession("");
    $InsertQuery->execute();
    $InsertGoTo = "";

    $InsertQuery->redirect($InsertGoTo);
  }
}
?>
<?php
$chemagentselected = new WA_MySQLi_RS("chemagentselected", $cmctrfdb, 0);
$chemagentselected->setQuery("SELECT * FROM trfchemicalagent LEFT JOIN chemicalagent ON trfchemicalagent.idchemicalagent=chemicalagent.idchemicalagent WHERE trfchemicalagent.idtrf='$idtrf'");
$chemagentselected->execute();
?>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
  <title><?php echo $titlepage; ?> </title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta content="CIMAC TRF Portal" name="description" />
  <meta content="" name="author" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />

  <!-- App favicon -->
  <link rel="shortcut icon" href="../images/favicon.ico">

  <!--Form Wizard-->
  <link href="../plugins/jquery-steps/jquery.steps.css" rel="stylesheet" type="text/css">

  <!-- App css -->
  <link href="assets/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
  <link href="assets/css/jquery-ui.min.css" rel="stylesheet">
  <link href="assets/css/icons.min.css" rel="stylesheet" type="text/css" />
  <link href="assets/css/metisMenu.min.css" rel="stylesheet" type="text/css" />
  <link href="assets/css/app.min.css" rel="stylesheet" type="text/css" />


  <!-- submit form with button -->
  <script>
    function formSubmit() {
      document.forms["myForm"].submit();
    }
  </script>


</head>

<body>

  <!-- Top Bar Start -->
  <?php include('include/topbar.php'); ?>
  <!-- Top Bar End -->


  <!-- Left Sidenav -->
  <?php include('include/leftsidenav.php'); ?>
  <!-- end left-sidenav-->

  <div class="page-wrapper">
    <!-- Page Content-->
    <div class="page-content">

      <div class="container-fluid">
        <!-- Page-Title -->
        <div class="row">
          <div class="col-sm-12">
            <div class="page-title-box">
              <div class="float-right">
                <ol class="breadcrumb">
                  <li class="breadcrumb-item"><a href="javascript:void(0);"><?php echo $titlepage; ?></a></li>
                  <li class="breadcrumb-item active">Starter</li>
                </ol>
              </div>
              <h4 class="page-title"><?php echo $titlewb; ?></h4>
            </div><!--end page-title-box-->
          </div><!--end col-->
        </div>
        <div class="row">
          <div class="col-sm-12">
            <div class="card">
              <div class="card-body">
                <div class="media">

                  <?php include('include/appform.php'); ?>

                </div><!--end media-->

              </div><!--end card-body-->
            </div><!--end card-->

            <div class="progress mb-4">
              <div class="progress-bar" role="progressbar" style="width: 80%;" aria-valuenow="80" aria-valuemin="0" aria-valuemax="100">80%</div>
            </div>
            <!-- card for additional info gloves -->

            <div class="card">
              <div class="card-body">
                <h4 class="mt-0 header-title"><?php echo $chemagenttitle; ?></h4>
                <p class="text-muted mb-3"><?php echo $chemagenttitle_help; ?></p>
                <form class="was-validated" action="" method="post" name="myForm">
                  <div class="form-group row">

                    <div class="col-sm-2">

                      <select class="form-control" id="addchem" name="addchem">
                        <option value=""><?php echo $pleaseselect; ?></option>
                        <?php
                        $wa_startindex = 0;
                        while (!$chemicalagentlist->atEnd()) {
                          $wa_startindex = $chemicalagentlist->Index;
                        ?>
                          <option value="<?php echo ($chemicalagentlist->getColumnVal("idchemicalagent")); ?>"><?php echo ($chemicalagentlist->getColumnVal("name_chemicalagent")); ?></option>
                        <?php
                          $chemicalagentlist->moveNext();
                        }
                        $chemicalagentlist->moveFirst(); //return RS to first record
                        unset($wa_startindex);
                        unset($wa_repeatcount);
                        ?>
                      </select>

                    </div>
                  </div>




                  <input type="hidden" name="addchemform" id="addchemform" value="Y">
                  <input type="hidden" name="idtrf" id="idtrf" value="<?php echo ($trfnumberfinal->getColumnVal("idtrfdetails")); ?>">

                  <br><br>
                  <input type="button" onclick="formSubmit()" class="btn btn-gradient-success waves-effect waves-light" value="<?php echo $addtitle; ?>">

              </div>
              </form>

            </div><!--end card-body-->

            <!-- card for show requirements -->
            <div class="card">
              <div class="card-body">
                <h4 class="mt-0 header-title"><?php echo $addchemselected; ?></h4>
                <p class="text-muted mb-3"><?php echo $addchemselected_help; ?></p>


                <table class="table table-striped mb-0">
                  <thead>
                    <tr>
                      <th><?php echo $addchemtitle; ?></th>

                      <th><?php echo $action; ?></th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php
                    $wa_startindex = 0;
                    while (!$chemagentselected->atEnd()) {
                      $wa_startindex = $chemagentselected->Index;
                    ?>
                      <tr>
                        <td><?php echo ($chemagentselected->getColumnVal("name_chemicalagent")); ?></td>

                        <td>

                          <a href="deletechemadd.php?idchemadd=<?php echo ($chemagentselected->getColumnVal("idtrfchemicalagent")); ?>&idtrf=<?php echo $idtrf; ?>"><i class="fas fa-trash-alt text-danger font-16"></i></a>
                        </td>
                      </tr>
                    <?php
                      $chemagentselected->moveNext();
                    }
                    $chemagentselected->moveFirst(); //return RS to first record
                    unset($wa_startindex);
                    unset($wa_repeatcount);
                    ?>
                  </tbody>
                </table>
                <a href="identificationparts.php?idtrf=<?php echo $idtrf; ?>&codestep=6"><button type="button" class="btn btn-success waves-effect waves-light"><?php echo $nextsteptitle; ?></button> </a>
                <a href="addrequirements.php?idtrf=<?php echo $idtrf; ?>" <button type="button" class="btn btn-dark waves-effect waves-light"><?php echo $backstep; ?></button></a>
              </div><!--end card-body-->


            </div><!--end card-->




          </div><!--end col-->
        </div>
        <!-- end page title end breadcrumb -->


      </div><!-- container -->
      <!-- footer start -->
      <?php include('include/footer.php'); ?>
      </footer><!--end footer-->
    </div>
    <!-- end page content -->
  </div>
  <!-- end page-wrapper -->




  <!-- jQuery  -->
  <script src="assets/js/jquery.min.js"></script>
  <script src="assets/js/bootstrap.bundle.min.js"></script>
  <script src="assets/js/metismenu.min.js"></script>
  <script src="assets/js/waves.js"></script>
  <script src="assets/js/feather.min.js"></script>
  <script src="assets/js/jquery.slimscroll.min.js"></script>
  <script src="assets/js/jquery-ui.min.js"></script>

  <script src="../plugins/jquery-steps/jquery.steps.min.js"></script>
  <script src="assets/pages/jquery.form-wizard.init.js"></script>

  <!-- App js -->
  <script src="assets/js/app.js"></script>

</body>

</html>