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


if($_POST)
{
    $i = 0;    $j=1;
    foreach($_POST['name'] as $name){

        $insAry = array(
                "name"  =>  $_POST['name'][$i],
                "sname" =>  $_POST['sname'][$i],
                "type"  =>  $_POST['type'][$i],
                "calculate_mode"    =>  $_POST["mode$j"],
                "session"   =>  $_SESSION["admin"]["session"],
                "added_by"  =>  $_SESSION["admin"]["uid"],
            );

        $id = $_POST['id'][$i];
        $check = $db->getVal("select id from salary_components where id='$id' ");
        if($check > 0){
                $db->updateAry("salary_components", $insAry, "where id='$id' ");
        } else {
            $db->insertAry("salary_components",$insAry);
        }
            $j++;
            $i++;
    }
    
        $k = 0;    $l=1;
    foreach($_POST['dname'] as $dname){

        $insAry = array(
                "name"  =>  $_POST['dname'][$k],
                "sname" =>  $_POST['dsname'][$k],
                "type"  =>  $_POST['dtype'][$k],
                "calculate_mode"    =>  $_POST["dmode$l"],
                "session"   =>  $_SESSION["admin"]["session"],
                "added_by"  =>  $_SESSION["admin"]["uid"],
            );

        $id = $_POST['did'][$k];
        $check = $db->getVal("select id from salary_components where id='$id' ");
        if($check > 0){
                $db->updateAry("salary_components", $insAry, "where id='$id' ");
        } else {
            $db->insertAry("salary_components",$insAry);
        }
            $k++;
            $l++;
    }
    redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Payroll", "page_id"=>$pgMod)));    

// 	$_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> Create Salary Components</h3>
      </div>
      <!--<div class="well">-->

        <!--<div class="span12">-->

      <!-- / Forms: Top Bar -->
      <input type="hidden" name="session" value="<?php echo $_SESSION["admin"]["session"] ?>">
      <!-- Forms: Content -->
      <div class="well no-padding">
        <div class="control-group">
          <label class="control-label" for="class">Addition <br>Components</label>
          <div class="controls">
            <table  class="table">
              <tfoot>
                <tr>
                  <td colspan="2"><a id="Add2-A" href="javascript:;">
                    <button type="button" class="btn btn-info">Add Other</button>
                    </a></td>
                </tr>
              </tfoot>
              <thead>
                  <tr style="color:white;">
                      <th>S.No.</th>
                      <th>Component Name</th>
                      <th>Short Name</th>
                      <th>Calculation Mode</th>
                      <th>Action</th>
                  </tr>
              </thead>
              <tbody id="tbody2-A">
                <?php
					 $lifeskill=$db->getRows("select * from salary_components where type='Addition' ");
					 $i=1;
					  if(is_array($lifeskill) && count($lifeskill))
                      {
                        foreach($lifeskill as $iSkill)
                        {
					 ?>
                <tr>
                  <td>(<?php echo $i; ?>)</td>
                    <td>
                        <input type="hidden" value="<?php echo $iSkill['id']; ?>" name="id[]" />
                        <input type="hidden" value="Addition" name="type[]">
                        <input type="text" class="input-block-level" value="<?php echo $iSkill["name"]; ?>" name="name[]">
                    </td>
                    <td>
                        <input type="text" class="input-block-level" value="<?php echo $iSkill["sname"]; ?>" name="sname[]" />
                    </td>
                    <td>
                        <input type="radio" name="mode<?php echo $i; ?>" value="Amount" <?php if($iSkill["calculate_mode"]=="Amount"){echo "checked";} ?> > Amount <br>
                        <input type="radio" name="mode<?php echo $i; ?>" value="Percent" <?php if($iSkill["calculate_mode"]=="Percent"){echo "checked";} ?>> Percent
                    </td>
                    <td>
                        <a href="javascript:;" onclick="delete_fee('<?php echo $iSkill['id']; ?>')" class="confirmation"><button type="button" class="btn btn-danger" s>Delete</button></a>
                    </td>
                </tr>
                <?php $i++;
					 	} 
					 }else{
					 ?>
                <tr>
                  <td>(<?php echo $i; ?>)</td>
                    <td>
                        <input type="hidden" value="0" name="id[]" />
                        <input type="hidden" value="Addition" name="type[]">
                        <input type="text" class="input-block-level"  name="name[]">
                    </td>
                  <td>
                      <input type="text" class="input-block-level" name="sname[]" />
                  </td>
                  <td>
                        <input type="radio" name="mode<?php echo $i; ?>" value="Amount"> Amount <br>
                        <input type="radio" name="mode<?php echo $i; ?>" value="Percent"> Percent
                    </td>
                </tr>
                <?php
					 }
				?>
              </tbody>
            </table>
          </div>
        </div>

        <div class="control-group">
          <label class="control-label" for="class">Deduction <br>Components</label>
          <div class="controls">
            <table  class="table">
              <tfoot>
                <tr>
                  <td colspan="2"><a id="Add2-B" href="javascript:;">
                    <button type="button" class="btn btn-info">Add Other</button>
                    </a></td>
                </tr>
              </tfoot>
              <thead>
                  <tr style="color:white;">
                      <th>S.No.</th>
                      <th>Component Name</th>
                      <th>Short Name</th>
                      <th>Calculation Mode</th>
                      <th>Action</th>
                  </tr>
              </thead>
              <tbody id="tbody2-B">
                <?php
					 $lifeskill=$db->getRows("select * from salary_components where type='Deduction' ");
					 $i=1;
					  if(is_array($lifeskill) && count($lifeskill))
                      {
                        foreach($lifeskill as $iSkill)
                        {
					 ?>
                <tr>
                  <td>(<?php echo $i; ?>)</td>
                    <td>
                        <input type="hidden" value="<?php echo $iSkill['id']; ?>" name="did[]" />
                        <input type="hidden" value="Deduction" name="dtype[]">
                        <input type="text" class="input-block-level" value="<?php echo $iSkill["name"]; ?>" name="dname[]">
                    </td>
                    <td>
                        <input type="text" class="input-block-level" value="<?php echo $iSkill["sname"]; ?>" name="dsname[]" />
                    </td>
                    <td>
                        <input type="radio" name="dmode<?php echo $i; ?>" value="Amount" <?php if($iSkill["calculate_mode"]=="Amount"){echo "checked";} ?> > Amount <br>
                        <input type="radio" name="dmode<?php echo $i; ?>" value="Percent" <?php if($iSkill["calculate_mode"]=="Percent"){echo "checked";} ?>> Percent
                    </td>
                    <td>
                        <a href="javascript:;" onclick="delete_fee('<?php echo $iSkill['id']; ?>')" class="confirmation"><button type="button" class="btn btn-danger" s>Delete</button></a>
                    </td>
                </tr>
                <?php $i++;
					 	} 
					 }else{
					 ?>
                <tr>
                  <td>(<?php echo $i; ?>)</td>
                    <td>
                        <input type="hidden" value="0" name="did[]" />
                        <input type="hidden" value="Deduction" name="dtype[]">
                        <input type="text" class="input-block-level"  name="dname[]">
                    </td>
                  <td>
                      <input type="text" class="input-block-level" name="dsname[]" />
                  </td>
                  <td>
                        <input type="radio" name="dmode<?php echo $i; ?>" value="Amount"> Amount <br>
                        <input type="radio" name="dmode<?php echo $i; ?>" value="Percent"> Percent
                    </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>
      <!--</div>-->
      <!-- / Forms: Content --> 
      
    </div>
    <!-- / Forms: Box -->
    
  </form>
</div>
<script>
$(document).ready(function(){
	$('tbody td a.delete').click(function() { 
		$(this).parent().parent().remove(); 
	});	
	$("#Add2-A").click(function(){
		var rowCount = ($('#tbody2-A tr').length)+1;
		var add_tr_skill='<tr><td>('+rowCount+')</td>'+
		'<td><input type="hidden" value="0" name="id[]" /><input type="hidden" value="Addition" name="type[]"><input type="text" class="input-block-level"  name="name[]"></td>'+
		'<td><input type="text" class="input-block-level" name="sname[]" /></td>' +
		'<td><input type="radio" name="mode'+rowCount+'" value="Amount"> Amount <br> <input type="radio" name="mode'+rowCount+'" value="Percent"> Percent</td>' +
		'<td><a class="delete" href="javascript:;"><button type="button" class="btn btn-info">Remove</button></a></td></tr>';

		$("#tbody2-A").append($(add_tr_skill));
		$('#tbody2-A td a.delete').click(function() { 
			$(this).parent().parent().remove(); 
		});	
	});
	$("#Add2-B").click(function(){
		var rowCount = ($('#tbody2-B tr').length)+1;
		var add_tr_skill='<tr><td>('+rowCount+')</td>'+
		'<td><input type="hidden" value="0" name="did[]" /><input type="hidden" value="Deduction" name="dtype[]"><input type="text" class="input-block-level"  name="dname[]"></td>'+
		'<td><input type="text" class="input-block-level" name="dsname[]" /></td>' +
		'<td><input type="radio" name="dmode'+rowCount+'" value="Amount"> Amount <br> <input type="radio" name="dmode'+rowCount+'" value="Percent"> Percent</td>' +
		'<td><a class="delete" href="javascript:;"><button type="button" class="btn btn-info">Remove</button></a></td></tr>';


		$("#tbody2-B").append($(add_tr_skill));
		$('#tbody2-B td a.delete').click(function() { 
			$(this).parent().parent().remove(); 
		});	
	});

});


function delete_fee(id){
  var r = confirm("Are you sure want to delete?");
  if (r == true) {
    $.post( "Payroll/ajax.php", { type: "delete_fee" , id: id } , function( data ) {
        alert(data);
        location.reload();
	});
  }

}

</script> 
<!-- / Forms -->