<?php
$pgMod="subject";
$pgAct="view";
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);


if($_POST)
{
	if(is_array($_POST["name"]) && count($_POST["name"])>0)
	{
		$db->delete("template","where 1");
		for($i=0; $i<count($_POST["name"]); $i++){
			if(trim($_POST["name"][$i])!=""){
				$insAry=array(  "name"=>$_POST["name"][$i],
                				"templateid"=>$_POST["templateid"][$i],
                				"entityid"=>$_POST["entityid"][$i],
                				"senderid"=>$_POST["senderid"][$i],
                				"content"=>$_POST["content"][$i],
								"added_by"=>$_SESSION["admin"]["uid"]
								);
				$db->insertAry("template",$insAry);//$alert_err[]=$db->getErMsg().$db->getLastQuery();
			}
		}

	}
    // print_r($_POST);
    // die();
// 	$_SESSION['form']=$_POST;
// 	if(is_array($_POST["name"]) && count($_POST["name"])>0)
// 	{
// 		$db->delete("criteria","where school_id=".$_SESSION["admin"]["school_id"]);
// 		for($i=0; $i<count($_POST["name"]); $i++){
// 			if(trim($_POST["name"][$i])!=""){
// 				$insAry=array("name"=>$_POST["name"][$i],
// 								"school_id"=>$_SESSION["admin"]["school_id"],
// 								"added_by"=>$_SESSION["admin"]["uid"],
// 								"type"=>$_POST["type"][$i]);
// 				$db->insertAry("criteria",$insAry);//$alert_err[]=$db->getErMsg().$db->getLastQuery();
// 			}
// 		}
		
// 	}
// 	redirect();
}
if(isset($_SESSION['msg']) && $_SESSION['msg']!="")
{
	?>

<div class="alert alert-success"> <a class="close" data-dismiss="alert">×</a> <i class="icon icon-thumbs-up-alt"></i> <b>Notice</b><?php echo $_SESSION['msg'] ?></i> </div>
<?php
	unset($_SESSION["msg"]);
}
if(is_array($alert_err) && count($alert_err)>0)
{
	?>
<div class="alert alert-error"> <a class="close" data-dismiss="alert">×</a> <i class="icon icon-warning-sign"></i> <b>Notice</b> <?php echo $alert_err[0] ?></i> </div>
<?php
}
?>
<!-- Forms -->
<div class="row-fluid">
  <form method="post" action="">
    <!-- Forms: Box -->
    <div class="span12"> 

      <!-- Forms: Top Bar -->
      <div class="top-bar">
        <h3><i class="icon-cog"></i> Add Template</h3>
      </div>
      <!-- / Forms: Top Bar -->

      <!-- Forms: Content -->
      <div class="well no-padding">

        <div class="control-group">
          <label class="control-label" for="class">Enter Detail</label>
          <div class="controls">
            <table  class="table">
              <tfoot>
                <tr>
                    <td colspan="2">
                        <a id="AddAwareness" href="javascript:;">
                            <button type="button" class="btn btn-info">Add Other</button>
                        </a>
                    </td>
                </tr>
              </tfoot>
              <tbody id="tbodyAwareness">
                <?php
					 $lifeskill=$db->getRows("select * from template ");

					 $i=1;

					  if(is_array($lifeskill) && count($lifeskill))
                      {
                        foreach($lifeskill as $iSkill)
                        {
					 ?>
                <tr>
                  <td>(<?php echo $i; ?>)</td>
                  <td>
                    <input type="text" placeholder="Template name" value="<?php echo $iSkill["name"]; ?>" name="name[]">
                  </td>
                  <td>
                      <input type="text" placeholder="Template DLT Id" value="<?php echo $iSkill["templateid"]; ?>" name="templateid[]" />
                  </td>
                  <td>
                      <input type="text" placeholder="Entity Id" value="<?php echo $iSkill["entityid"]; ?>" name="entityid[]" />
                  </td>
                  <td>
                      <input type="text" placeholder="Sender Id" value="<?php echo $iSkill["senderid"]; ?>" name="senderid[]" />
                  </td>
                  <td>
                      <!--<input type="text" placeholder="Content" value="<?php echo $iSkill["content"]; ?>" name="content[]" />-->
                      <textarea placeholder="Content" name="content[]" rows="4"><?php echo $iSkill["content"]; ?></textarea>
                  </td>
                  <td>
                      <a class="delete" href="javascript:;">
                        <button type="button" class="btn btn-info">remove</button>
                      </a>
                  </td>
                </tr>
                <?php $i++;
					 	} 
					 
					 }else{
					 ?>
                <tr>
                  <td>(<?php echo $i; ?>)</td>
                  <td>
                    <input type="text" placeholder="Template name"  name="name[]">
                  </td>
                  <td>
                      <input type="text" placeholder="Template DLT Id" name="templateid[]"/>
                  </td>
                  <td>
                      <input type="text" placeholder="Entity Id" name="entityid[]"/>
                  </td>
                  <td>
                      <input type="text" placeholder="Sender Id" name="senderid[]"/>
                  </td>
                  <td>
                      <!--<input type="text" placeholder="Content" name="content[]" />-->
                      <textarea placeholder="Content" name="content[]" rows="4"></textarea>
                  </td>
                  <td></td>
                </tr>
                <?php					 
					 }
					 
								?>
              </tbody>
            </table>
          </div>
        </div>
        <div class="form-actions">
          <button type="submit" class="btn btn-primary"> Save</button>
          <button type="button" class="btn"> Cancel</button>
        </div>
      </div>
      <!-- / Forms: Content --> 
      
    </div>
    <!-- / Forms: Box -->
    
  </form>
</div>
<script>
$(document).ready(function(){
	$('tbody td a.delete').click(function() {
		$(this).parent().parent().remove(); 
	});

	$("#AddAwareness").click(function(){
		var rowCount = ($('#tbodyAwareness tr').length)+1;
		var add_tr_Awareness='<tr><td>('+rowCount+')</td><td><input class="input-block-level" type="text" placeholder="Template name" name="name[]"></td><td><input type="text" placeholder="Template DLT Id" name="templateid[]" /></td><td><input type="text" placeholder="Entity Id" name="entityid[]"/><td><input type="text" placeholder="Sender Id" name="senderid[]"/></td><td><textarea placeholder="Content" rows="4" name="content[]"></textarea></td><td><a class="delete" href="javascript:;"><button type="button" class="btn btn-info">Remove</button></a></td></tr>';
		$("#tbodyAwareness").append($(add_tr_Awareness));
		$('#tbodyAwareness td a.delete').click(function() { 
			$(this).parent().parent().remove(); 
		});	
	});
	
});
</script> 
<!-- / Forms -->