<script>
$(document).ready(function(){
	$("#addLine").hide(); 
	$('select').select2();
	jQuery('.datepicker2').datepicker({
			dateFormat : 'dd-mm-yy',
			changeMonth : true,
			changeYear : true
			});
    var oTable = $("#test").dataTable( {
        "iDisplayLength": 10,
		 "aLengthMenu": [5,10,25,50,100],
		 "sPaginationType": "full_numbers", 
		 "aoColumns": [ { 
		 	"bSortable": false }, 
			null, 
			null, {
			"bSortable": false }	 ]});
    $("table.editable").on("click",".edit",function(){
        rRow = $(this).parents("tr");
        ID = $('td:first', $(this).parents('tr')).text(); 
        $("#row_delete").dialog("open");
    });
    function remove_row(row){
        row.remove();
 		$.ajax({
  			type: "POST",
  			url: "<?php echo base_url()?>zakatSetup/nisabDetailsDelete",
			data:"ID="+ID,
  			success: function(){
				alert('Row has been deleted...');
			},
		});       // Here your ajax action after delete confirmed
    }
    $("#row_delete").dialog({
        autoOpen: false,
        resizable: false,        
        modal: true,

        buttons: {
            "Delete": function() {
                remove_row(rRow);
                $(this).dialog("close");
            },
            Cancel: function() {
                $(this).dialog("close");
            }
        }
    });    
});
function addRow(){
	var currFrom = $("#currencyFrom option:selected").val();
	var currTo = $("#currencyTo option:selected").val();
	var tablerows = $("#bill").val();
    var rowToInsert = '';
	if(currFrom == '' || currTo == ''){
		return false;
	}
    $("#oTable tbody").append(
		"<tr>" +
        "<td  style='display:none'></td>"+
        "<td class='span4 tac'><select id='accountID' style='width: 250px;' onChange='dispDesc(this)'></select></td>"+
        "<td class='span7'><input type='text' class='span12' disabled/></td>"+
        "<td class='tac'><button class='btn btn-mini btn-danger remove' title='<?php echo $this->lang->line('cancel') ?>' onClick='cancelRow(this)'><span class='icon-remove-circle'></button>" + 
		"<button class='btn btn-mini btn-primary' title='<?php echo $this->lang->line('save') ?>' onClick='saveDetails(this)'><span class='icon-ok-circle'></button></td>"+
        "</tr>");
  selectoption();
	$('select').select2({
	});
  	$("#bill").val(parseInt(tablerows) + 1);
	$("#addLine").hide(); 
}
function cancelRow(obj){
    var eRow   = $(obj).parents('tr');
	eRow.remove();
	$("#addLine").show(); 
}
function selectoption(){
		$.ajax({
			type: "POST",
        	url: '<?php echo base_url();?>gltransaction/accountSearch',
			dataType:"json",
			success: function(content){
				if(content.status == "success") {
					var items = [];
					items.push('<option value="">-Please Select-</option>');
					for ( var i = 0; i < content.message.length; i++) {
					items.push('<option value="'+content.message[i].ID+'">'
						+ content.message[i].acctCode + ' ['+ content.message[i].acctName + ']</option>');
					}
 						var rowId = "#accountID";
						jQuery(rowId).empty();
						jQuery(rowId).append(items.join('</br>'));
				} else {
					$("#error").html('<p>'+content.message+'</p>');
				}
					return false;
			}
		});
}
function dispDesc(obj){
	var ID = obj.value;
    var eRow   = $(this).parents('tr');
	var postData = {
		'ID' : ID
	};
		$.ajax({
			type: "POST",
        	url: '<?php echo base_url();?>gltransaction/accountSearch_ID',
			dataType:"json",
			data:postData,
			success: function(content){
				if(content.status == "success") {
 					$('td:eq(2)', $(obj).parents('tr')).find("input").val(content.message.acctName);
				} else {
					$("#error").html('<p>'+content.message+'</p>');
				}
					return false;
			}
		});
}
function selectGroup(obj){
	$("#addLine").show(); 
 	$("#oTable tbody tr").remove(); 
	var ID = obj.value;
		postData = {
			ID : ID
		};
		$.ajax({
			type: "POST",
        	url: '<?php echo base_url();?>Zakatsetup/nisabDetailsSearch',
			dataType:"json",
			data: postData,
			success: function(content){
				if(content.status == "success") {
					for ( var i = 0; i < content.message.length; i++) {
    						$("#oTable tbody").append(
							"<tr><td style='display:none'>"+content.message[i].ID+"</td>" +
        					"<td>"+content.message[i].acctCode+"</td>" +
							"<td>"+content.message[i].acctName+"</td>" +
							"<td class='tac'><button class='btn btn-mini btn-danger edit' title='<?php echo $this->lang->line('delete');?>'><span class='i-trashcan'></button>" +
					        "</tr>");
					}
				}
			}
		});
}
function saveDetails(obj){
    var eRow   = $(obj).parents('tr');
	var parentID = $("#parentID option:selected").val();
	if(parentID == ''){
		return false;
	}
	var acctID = $('td:eq(1)', $(obj).parents('tr')).find("option:selected").val();
	var acctName = $('td:eq(2)', $(obj).parents('tr')).find("input").val();
	postData = {
		parentID : parentID,
		acctID : acctID,
	};
	$.ajax({
		type: "POST",
		url: '<?php echo base_url();?>Zakatsetup/nisabDetailsInsert',
		dataType:"json",
		data: postData,
		async: false,
		success: function(content){
			if(content.status == "success") {
				eRow.remove();
    			$("#oTable tbody").append(
				"<tr><td style='display:none'>"+content.message.ID+"</td>" +
        		"<td>"+content.message.acctCode+"</td>" +
				"<td>"+content.message.acctName+"</td>" +
				"<td class='tac'><button class='btn btn-mini btn-danger edit' title='<?php echo $this->lang->line('edit');?>'><span class='i-trashcan'></button>" +
					        "</tr>");
				$("#addLine").show(); 
			}
		}
	});
}
</script>
<div id="content">                        
<div class="wrap">
                  
<div class="head">
	<div class="info">
		<h1><?php echo element('compName', $this->session->userdata('logged_in')) ?> [ 
			<?php echo element('compNo', $this->session->userdata('logged_in')) ?> ]</h1>
			<ul class="breadcrumb">
            	<li><a href='<?php echo base_url()."home" ?>'><?php echo $this->lang->line('dashboard') ?></a> <span class="divider">-</span></li>
                <li><a href='<?php echo base_url()."gl/home" ?>'> <?php echo $this->lang->line('module') ?></a> <span class="divider">-</span></li>
                <li class="active"><?php echo $this->lang->line('title') ?></li>
            </ul>
	</div>
                        
	<div class="search">
		<form action="<?php echo base_url() ?>admin/search" method="post">
			<input name="search_text" type="text" placeholder="search..."/>                                
            <button type="submit"><span class="i-magnifier"></span></button>
		</form>
	</div>                        
</div>  <!-- Head -->                                                                  
<div class="content">
	<div class="wrap">    
		<div class="row-fluid">
			<div class="span12">
            	<div class="block">
                	<div class="head">
                    	<h2><?php echo $this->lang->line('title') ?></h2>
                    	<div class="side fr">
                         	<button class="btn btn-link" onClick="do_print()">Print</button>
                    	</div>
                    </div><!-- head -->
					<div class="content np">    
                      <div class="controls-row"> <!-- row 1 -->
                   	<div class="span2"><?php echo $this->lang->line('group') ?></div>
                   		<div class="span4">
						<?php
							if($zakatData){
								$preselgroup = $zakatData->ID;
							}else{
								$preselgroup = '';
							}
							$options='';
							$options[] = '--Please Select--';
							if($zakatGroup != ''){
								foreach($zakatGroup as $row){
									if($row->parentID != 0){
										$options[$row->ID] = $row->name . " [" . $row->description . "]";
									}
								}
							}
							//$js='style="width: 250px;"';
							$js='id="parentID" style="width: 250px;" onChange="selectGroup(this)"';
							echo form_dropdown('parentID', $options, $preselgroup, $js);
						?>       
                         </div>                
                  </div><!-- End Row 1 -->
                  <div class="controls-row">
                    	<div class="span2"><?php echo $this->lang->line('description') ?></div>
                   		<div class="span10">
                            	<?php 								
								$data = array(
									'name'=>'description', 
									'id'=>'description', 
									'disabled'=>'disabled', 
									'class'=>'span12 validate[required,maxSize[255]]',); 
							if($zakatData){
								echo form_input($data, $zakatData->description); 
							}else{
								echo form_input($data, set_value('description')); 
							} ?>
                  		</div>
                  </div>
					</div><!-- content -->
				</div><!-- block -->
			</div><!-- span12 -->
		</div><!-- row-fluid -->
	<div class="row-fluid">                            
	  <div class="span12">
	 	<div class="block" id="detailsTable">
        	<div class="head">
            	<h2><?php echo $this->lang->line('titlelist') ?></h2>
                    	<ul class="buttons" id="addLine">
                    		<li><a title="Add New" onclick="addRow()"><span class="i-plus-2"></span></a></li>
                        </ul>                                        
        	</div><!-- head -->
          	<div class="content np">
            	<table cellpadding="0" cellspacing="0" width="100%" id="oTable" class="editable">
                	<thead>
                    <tr>
            			<th style="display:none"></th>
            			<th><?php echo $this->lang->line('accountCode')?></th>
            			<th><?php echo $this->lang->line('accountName')?></th>
            			<th class="tac" width="1px"><?php echo $this->lang->line('action')?></th>
            		</tr>
        			</thead>
       				 <tbody>
					</tbody>
			</table>                                         
        	</div><!-- content -->
		</div><!-- detailsTable -->
	</div><!-- span12 -->
</div><!-- row-fluid -->
</div>
</div>
<div class="dialog" id="row_delete" style="display: none;" title="Delete?">
    <p>Row will be deleted</p>
</div>   
