<script>
$(document).ready(function(){
	$('select').select2();
	$.each($(".select2-container"), function (i, n) {
            $(n).next().show().fadeTo(0, 0).height("0px").css("left", "auto"); // make the original select visible for validation engine and hidden for us
            $(n).prepend($(n).next());
            $(n).delay(500).queue(function () {
                $(this).removeClass("validate[required]"); //remove the class name from select2 container(div), so that validation engine dose not validate it
                $(this).dequeue();
            });
        });	
	$("#calculateDetails").hide();
	$("#calculateHeader").hide();
});
function Calculcate(){
	var centerID = $("#centerID option:selected").val();
	var methodID = $("#methodID option:selected").val();
	var nisabYear = $("#nisabYear option:selected").val();
	if($("#formValidate").validationEngine('validate')){	
		if(methodID == 1){
			postData = {
				methodID : methodID,
				centerID : centerID,
				nisabYear : nisabYear,
			};
	
			$.ajax({
				type: "POST",
        		url: '<?php echo base_url();?>zakatTransaction/zakatCalculation',
				dataType:"json",
				data: postData,
	  					async: false,
				success: function(content){
					if(content.status == "success") {
						var bill = 0;
						var Header = '';
						var Headertotal = 0;
						for ( var i = 0; i < content.message.length; i++) {
							bill++;
							if(content.message[i].parentID != Header){
	    						$("#oTable tbody").append(
									"<tr><td colspan='3'>"+content.message[i].topGroupName+"</td>" +
					        		"</tr>"
								);
								Header = content.message[i].parentID;
							}
	    					$("#oTable tbody").append(
								"<tr><td style='display:none'>"+content.message[i].ID+"</td>" +
        						"<td class='tac'>"+bill+"</td>" +
        						"<td>"+content.message[i].acctName+"</td>" +
        						"<td class='tar'>"+accounting.formatNumber(content.message[i].totalAmount,2)+"</td>" +
					        	"</tr>"
							);
						}
						var totalNisab = 0;
						for ( var i = 0; i < content.details.length; i++) {
							if(content.details[i].parentID == 1){
								$("#totalAssets").val(accounting.formatNumber(content.details[i].totalAmount,2));
								totalNisab = parseFloat(totalNisab) + parseFloat(content.details[i].totalAmount);
							}else{
								$("#totalLiability").val(accounting.formatNumber(content.details[i].totalAmount,2));
								totalNisab = parseFloat(totalNisab)  + parseFloat(content.details[i].totalAmount);
							}
						}zakatPay
					}
					$("#zakatPay").val(accounting.formatNumber(content.zakatAmount,2));
					$("#zakatPerniagaan").val(accounting.formatNumber(totalNisab,2));
					$("#calculateDetails").show();
					$("#calculateHeader").show();
				}
			});
		}else{
		}
	}
}
function saveData(){
	var zakatPay = accounting.unformat($("#zakatPay").val());
	var zakatPerniagaan = accounting.unformat($("#zakatPerniagaan").val());
	var nisabYear = $("#nisabYear option:selected").val();
	var centerID = $("#centerID option:selected").val();
			postData = {
				'zakatPay' : zakatPay,
				'zakatPerniagaan' : zakatPerniagaan,
				'nisabYear' : nisabYear,
				'centerID' : centerID,
			};
 				$.ajax({
  					type: "POST",
  					dataType: "json",
					url: "<?php echo base_url()?>zakatTransaction/zakat_save",
					data: postData ,
	  				async: false,
					success: function(content){
						if(content.status == "success") {
							alert(content.message);
							window.location.reload(true);
						}else{
							alert(content.message);
						}
					}
				});  
}
</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="zakat/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="row-fluid">
		<div class="span12">
            <div class="block">
               	<div class="head">
                   	<h2><?php echo $this->lang->line('title') ?></h2>
				</div>
				<div class="content np" id="">    
					<form id="formValidate" action="#">
                      <div class="controls-row"> <!-- row 1 -->
                   	<div class="span2"><?php echo $this->lang->line('center') ?></div>
                   		<div class="span4">
						<?php
							$options='';
							$options[NULL] = $this->lang->line('pleaseSelect');
							if($ZakatCenter != ''){
								foreach($ZakatCenter as $row){
									$options[$row->ID] = $row->name . " [" . $row->shortName . "]";
								}
							}
							$js='id="centerID" style="width: 250px;" class="validate[required]"';
							echo form_dropdown('centerID', $options, '', $js);
						?>       
                         </div>                
                  </div><!-- End Row 1 -->
                      <div class="controls-row"> <!-- row 1 -->
                   	<div class="span2"><?php echo $this->lang->line('method') ?></div>
                   		<div class="span4">
						<?php
							$options='';
							$options[NULL] = $this->lang->line('pleaseSelect');
							$options[1] = $this->lang->line('workingCapital');
							$options[2] = $this->lang->line('extentionCapital');
							$js='id="methodID" style="width: 250px;" class="validate[required]"';
							echo form_dropdown('methodID', $options, '', $js);
						?>       
                         </div>                
                  </div><!-- End Row 1 -->
                	<div class="controls-row"><!-- Row 4-->
                    	<div class="span2"><?php echo $this->lang->line('year');?></div>
						<div class="span4">
						<?php
						$preselyear = date("Y");
						$options = '';
						for($i=-5; $i<20; $i++){
							$options[date('Y', strtotime($i . "  year"))] = date('Y', strtotime($i . "  year"));
						}
						$js='class="input-medium validate[required]" id="nisabYear"';
						echo form_dropdown('nisabYear', $options, $preselyear, $js); 
						?>       
                        </div>
                 	</div><!-- End Row 4-->
     </form>
           	  <div class="footer">
            		<div class="side fr">
                		<div class="btn-group">
                        <?php 
							$data = array(
									'name'=>'mysubmit', 
									'class'=>'btn btn-primary');
							$js='onclick="Calculcate()"';
							echo form_button($data,$this->lang->line('Calculate'),$js);
							?>
                    	</div>
                		</div>
            		</div>                                    
			</div>  <!-- content --> 
			</div>  <!-- block -->
		</div>  <!-- span12 -->
	</div>  <!-- row-fluid -->
	<div class="row-fluid" id="calculateHeader">
		<div class="span12">
            <div class="block">
               	<div class="head">
                   	<h2><?php echo $this->lang->line('title1') ?></h2>
				</div>
            	<div class="content">
                  <div class="controls-row">
                  	<div class="span2"><?php echo $this->lang->line('totalAssets') ?></div>
                   	<div class="span4">
						<?php
							$data = array(
									'name'=>'totalAssets',
									'id'=>'totalAssets',								
									'disabled'=>'disabled',								
									'class'=>'span12 tar',); 
							echo form_input($data, set_value('totalAssets')); ?>                       
                  	</div>                            
                  	<div class="span2 tar"><?php echo $this->lang->line('totalLiability') ?></div>
                   	<div class="span4">
						<?php
							$data = array(
									'name'=>'totalLiability',
									'id'=>'totalLiability',								
									'disabled'=>'disabled',								
									'class'=>'span12 tar',); 
							echo form_input($data, set_value('totalLiability')); ?>                       
                  	</div>                            
                  </div>    
                  <div class="controls-row">
                  	<div class="span8"><?php echo $this->lang->line('zakatPerniagaan') ?></div>
                   	<div class="span4 tar">
						<?php
							$data = array(
									'name'=>'zakatPerniagaan',
									'id'=>'zakatPerniagaan',								
									'disabled'=>'disabled',								
									'class'=>'span12 tar',); 
							echo form_input($data, set_value('zakatPerniagaan')); ?>                       
                  	</div>                            
                  </div>    
                  <div class="controls-row">
                  	<div class="span8"><?php echo $this->lang->line('zakatPay') ?></div>
                   	<div class="span4 tar">
						<?php
							$data = array(
									'name'=>'zakatPay',
									'id'=>'zakatPay',								
									'disabled'=>'disabled',								
									'class'=>'span12 tar',); 
							echo form_input($data, set_value('zakatPay')); ?>                       
                  	</div>                            
                  </div>    
				</div><!-- content -->
           	  <div class="footer">
            		<div class="side fr">
                		<div class="btn-group">
                        <?php 
							$data = array(
									'name'=>'Submit', 
									'class'=>'btn btn-primary');
							$js='onclick="saveData()"';
							echo form_button($data,$this->lang->line('submit'),$js);
							?>
                    	</div>
                		</div>
            		</div>                                    
			</div>  <!-- block -->
		</div>  <!-- span12 -->
	<div class="row-fluid" id="calculateDetails">
		<div class="span12">
            <div class="block">
               	<div class="head">
                   	<h2><?php echo $this->lang->line('title2') ?></h2>
				</div>
            	<div class="content np table-sorting">
            	<table cellpadding="0" cellspacing="0" width="100%" id="oTable" class="editable">
                	<thead>
                    <tr>
            			<th style="display:none"></th>
            			<th class="span1 tac">#</th>
                        <th><?php echo $this->lang->line('description') ?></th>
                        <th class="tar"><?php echo $this->lang->line('amount') ?></th>
            		</tr>
        			</thead>
       				 <tbody>
                     </tbody>
                     </table>
				</div><!-- content -->
			</div>  <!-- block -->
		</div>  <!-- span12 -->
	</div>  <!-- row-fluid -->
</div>  <!-- wrap -->
</div>  <!-- content -->
