<?php
include("core/init.php");
include("session.php");

/***********Table Used**********/
$mainTable="q_a";

/****************Category Add & Edit Process Starts Here**********************/

if(Input::exists() && Token::check(Input::getInput("token"))) {

  if(Input::getInput("q_aId")=="" && Input::getInput("question")!="")
  {
    Db::getInstance()->insert($mainTable,array("set_id"=>Input::getInput("set_id"),"question"=>Input::getInput("question"),
        "a1"=>Input::getInput("a1"),
        "a2"=>Input::getInput("a2"),
        "a3"=>Input::getInput("a3"),
        "a4"=>Input::getInput("a4"),
        "a5"=>Input::getInput("a5"),
        "a6"=>Input::getInput("a6")));

    $lastId=Db::getInstance()->lastid();
    foreach(Input::getInput("answers") as $answer)
    {
      Db::getInstance()->insert("answers",array("q_id"=>$lastId,
          "answer"=>$answer));

    }



    Session::flash("message",'<div class="alert-success" style="padding: 5px;">Question Added.</div><Br>');
    Redirect::to("Q_A");
  }
  else if(Input::getInput("q_aId")!="" && Input::getInput("question")!="")
  {
    Db::getInstance()->delete("answers",array("q_id"=>Input::getInput("q_aId")));

    Db::getInstance()->update($mainTable,
        array("set_id"=>Input::getInput("set_id"),
            "question"=>Input::getInput("question"),
            "a1"=>Input::getInput("a1"),
            "a2"=>Input::getInput("a2"),
            "a3"=>Input::getInput("a3"),
            "a4"=>Input::getInput("a4"),
            "a5"=>Input::getInput("a5"),
            "a6"=>Input::getInput("a6")),
        array("id"=>Input::getInput("q_aId"))

    );

    foreach(Input::getInput("answers") as $answer)
    {
      Db::getInstance()->insert("answers",array("q_id"=>Input::getInput("q_aId"),
          "answer"=>$answer));

    }

    Session::flash("message",'<div class="alert-success" style="padding: 5px;">Question Edited Successfully.</div><Br>');
    Redirect::to("Q_A");
  }
  else {
    Session::flash("message",'<div class="alert-error" style="padding: 5px;">Please Fill Question.</div><Br>');
    Redirect::to("Q_A");
  }


}
/****************Category Add & Edit Process Starts Here**********************/




/****************Category Edit Populate**********************/
$populateQ_A=null;
if(Input::particularInputExists("get","editId")) {

  if (Db::getInstance()->num_rows("select id from {$mainTable} where id='" . Input::getInput("editId") . "'") > 0) {
    $populateQ_A = Db::getInstance()->get_row("select * from {$mainTable} where id='" . Input::getInput("editId") . "'");


  }
  else {

    Redirect::to("Q_A");
  }
}
/****************Category Edit Populate Ends**********************/





/****************Category Delete**********************/


if(Input::particularInputExists("get","deleteId")) {

  if (Db::getInstance()->num_rows("select id from {$mainTable} where id='" . Input::getInput("deleteId") . "'") > 0) {
    Db::getInstance()->delete($mainTable,array("id"=>Input::getInput("deleteId")));

    Session::flash("message",'<div class="alert-success" style="padding: 5px;">Question Deleted.</div><Br>');
    Redirect::to("Q_A");

  } else {
    Redirect::to("Q_A");
  }
}

/****************Category Delete Ends**********************/



?><!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Q A - <?php echo Config::get("project/site_title"); ?></title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <link rel="shortcut icon" href="dist/img/admin-panel.png">
  <!-- Bootstrap 3.3.6 -->
  <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="plugins/datatables/dataTables.bootstrap.css">


  <link rel="stylesheet" href="dist/css/AdminLTE.min.css">
  <!-- AdminLTE Skins. Choose a skin from the css/skins
       folder instead of downloading all of them to reduce the load. -->
  <link rel="stylesheet" href="dist/css/skins/_all-skins.min.css">

  <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->


  <Style>
    #result{
      margin-left:5px;
    }

    .short{
      color:#FF0000;
    }

    .weak{
      color:#E66C2C;
    }

    .good{
      color:#2D98F3;
    }

    .strong{
      color:#006400;
    }</Style>

</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">




  <?php include("includes/header.php"); ?>






<?php include("includes/sidebar.php"); ?>


  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
       Q A
        <small></small>
      </h1>
      <ol class="breadcrumb">
        <li><a href="Dashboard"><i class="fa fa-dashboard"></i> Home</a></li>
        <li class="active">Q A</li>
      </ol>
    </section>

    <!-- Main content -->
    <section class="content">

      <div class="row">


        <div class="col-md-12">

            <?php echo Session::flashDisplay("message"); ?>
            
          <a href="Q_A?addNew" class="btn btn-primary" style="margin-bottom: 10px;">Add New</a>
          <?php if(isset($_GET["addNew"]) || isset($_GET["editId"])){ ?>
          <!-- general form elements -->
          <div class="box box-primary">
            <div class="box-header with-border">

            </div>
            <!-- /.box-header -->
            <!-- form start -->
            <form role="form" action="Q_A" method="post">
              <div class="box-body">
                <?php



                $dbSets=Db::getInstance()->get_results("select id,set_name from sets");

                $sets=array();
                if(count($dbSets)) foreach($dbSets as $dbSet)
                {
                  $sets[$dbSet["id"]]=$dbSet["set_name"];
                }
                // print_r($provinces);
                ?>


                <div class="form-group">
                  <label for="exampleInputPassword1">Select Set *</label>
                  <select name="set_id" class="form-control">
                    <?php echo Input::getSelected("set_id",$populateQ_A,$sets); ?>
                  </select>


                </div>



                <div class="form-group">
                  <label for="exampleInputPassword1">Question *</label>
                  <input type="text" class="form-control" name="question"  id="question" value="<?php echo Input::getInput("question",$populateQ_A); ?>">
                </div>

                <div class="form-group">
                  <label for="exampleInputPassword1">Answer 1 </label>
                  <input type="text" class="form-control" name="a1"  id="a1" value="<?php echo Input::getInput("a1",$populateQ_A); ?>">
                </div>

                <div class="form-group">
                  <label for="exampleInputPassword1">Answer 2</label>
                  <input type="text" class="form-control" name="a2"  id="a2" value="<?php echo Input::getInput("a2",$populateQ_A); ?>">
                </div>

                <div class="form-group">
                  <label for="exampleInputPassword1">Answer 3</label>
                  <input type="text" class="form-control" name="a3"  id="a3" value="<?php echo Input::getInput("a3",$populateQ_A); ?>">
                </div>

                <div class="form-group">
                  <label for="exampleInputPassword1">Answer 4</label>
                  <input type="text" class="form-control" name="a4"  id="a4" value="<?php echo Input::getInput("a4",$populateQ_A); ?>">
                </div>

                <div class="form-group">
                  <label for="exampleInputPassword1">Answer 5</label>
                  <input type="text" class="form-control" name="a5"  id="a5" value="<?php echo Input::getInput("a5",$populateQ_A); ?>">
                </div>


                <div class="form-group">
                  <label for="exampleInputPassword1">Answer 6</label>
                  <input type="text" class="form-control" name="a6"  id="a6" value="<?php echo Input::getInput("a6",$populateQ_A); ?>">
                </div>





                <?php

                $answers=array();
                for($i=1;$i<=6;$i++)
                {
                  $answers["a".$i]="Answer ".$i;
                }
               // print_r($provinces);



                      $answerArray=null;
                      if(Input::particularInputExists("get","editId"))
                      {
                        $resultArrays=Db::getInstance()->get_results("select id,answer  from answers where q_id='".Input::getInput("editId")."'");

                        foreach($resultArrays as $resultArray)
                        {
                          $answerArray[$resultArray["id"]]=$resultArray["answer"];
                        }


                      }



                      ?>




                <div class="form-group">
                  <label for="exampleInputPassword1">Select Correct Answer *</label>
                  <select name="answers[]" class="form-control" multiple>
                    <?php //echo Input::getMultiSelected("answer",$populateQ_A,$answers); ?>

                    <?php echo Input::getMultiSelected("q_id",$answerArray,$answers); ?>

                  </select>


                </div>







                <input type="hidden" name="q_aId" value="<?php echo Input::getInput("id",$populateQ_A); ?>">

                <input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
              </div>
              <!-- /.box-body -->

              <div class="box-footer">
                <button type="submit" class="btn btn-primary">Submit</button>
              </div>
            </form>
          </div>
          <!-- /.box -->

          <?php } ?>


        </div>
        <!-- /.col -->






        <div class="col-md-12">
          <div class="box">
            <div class="box-header">
              <h3 class="box-title">Questions</h3>
            </div>
            <!-- /.box-header -->
            <div class="box-body">
              <table id="example1" class="table table-bordered table-striped">
                <thead>
                <tr>
                  <th>No.</th>
                  <th>Question</th>
                  <th>Set</th>

                  <th>Creation Date</th>
                  <th>Edit</th>
                  <th>Delete</th>
                </tr>
                </thead>
                <tbody>

                <?php $q_As=Db::getInstance()->get_results("select q_a.*,sets.set_name from {$mainTable}
                JOIN sets ON (sets.id=q_a.set_id)");
                $sno=1;
                if(count($q_As)) {
                  foreach ($q_As as $q_A) {

                    $date = new DateTime($q_A["date_time"]);

                    echo '<tr>';
                    echo '<Td>' . $sno . '</Td>';
                    echo '<Td>' . $q_A["question"] . '</Td>';
                    echo '<Td>' . $q_A["set_name"] . '</Td>';



                    echo '<Td>' . $date->format("Y-m-d") . '</Td>';

                    echo '<Td><A href="Q_A?editId='.$q_A["id"].'"> <i class="fa fa-pencil-square-o fa-lg" aria-hidden="true"></i></A> </Td>';

                    echo '<Td><A href="Q_A?deleteId='.$q_A["id"].'" onclick="return confirm(\'Are you sure you want to delete Question?\');"> <i class="fa fa-trash fa-lg" aria-hidden="true"></i></A> </Td>';

                    echo '</tr>';

                    $sno++;

                  }
                }


                ?>


                </tbody>

              </table>
            </div>
            <!-- /.box-body -->
          </div>

        </div>










      </div>





    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->

 <?php include("includes/footer.php"); ?>


  <!-- Add the sidebar's background. This div must be placed
       immediately after the control sidebar -->
  <div class="control-sidebar-bg"></div>
</div>
<!-- ./wrapper -->




<!-- jQuery 2.2.3 -->
<script src="plugins/jQuery/jquery-2.2.3.min.js"></script>
<!-- Bootstrap 3.3.6 -->
<script src="bootstrap/js/bootstrap.min.js"></script>
<!-- Slimscroll -->
<script src="plugins/slimScroll/jquery.slimscroll.min.js"></script>
<!-- FastClick -->
<script src="plugins/fastclick/fastclick.js"></script>
<!-- AdminLTE App -->
<script src="dist/js/app.min.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="dist/js/demo.js"></script>
<script src="plugins/datatables/jquery.dataTables.min.js"></script>
<script src="plugins/datatables/dataTables.bootstrap.min.js"></script>
<script>
  $(function () {
    $("#example1").DataTable();
    $('#example2').DataTable({
      "paging": true,
      "lengthChange": false,
      "searching": false,
      "ordering": true,
      "info": true,
      "autoWidth": false
    });
  });



</script>


</body>
</html>
