<script type="text/javascript">
  
   $(document).ready(function() {
    $('select').select2();
    
     jQuery('.datepicker2').datepicker({
			dateFormat : 'yy-mm-dd',
			changeMonth : true,
			changeYear : true
			});
    
            $.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();
            });
            });
    });
            function enable_lumpsum(id) {
            if (document.getElementById(id).disabled == true) {
            document.getElementById(id).disabled = false;
            } else {
            document.getElementById(id).disabled = true;
            }
            return true;
            }
   

    

    function delRow(me){
    var td = me.parentNode;
            var tr = td.parentNode;
            var tBody = tr.parentNode;
            tBody.removeChild(tr);
    }

    function deleteRow(me){
            var i = 0;
            delRow(me);
            $("#listDetails > tbody > tr").each (function(){
                $(this).find("td").eq(0).text(++i);
             });
            $("#trInfosSale").attr("data-currentRow", i - 1);
             $($("#trInfosSale")).find("td").eq(0).text("");
    }

    function msg(type, message){
    $("#msg_div p").html(message);
            $("#msg_div").attr("class", "alert " + type)
    }

   
    




    function savewakaf(){
        if ($("#formRow").validationEngine('validate') && finanEngine('financhkSrt') && finanEngine('financhkEnd')){
                $.ajax({
                type: "POST",
                        data : {
                               acctNo : $("#saleAcctNo").val(),
                               from : $("#startdate").val(),
                               to : $("#enddate").val(),
                               wakafPercent: accounting.unformat($("#iPerAll").val())
                        },
                        url: "<?php echo base_url() ?>wakafsetup/saveOneRowWakaf",
                        success: function(data) {
                        if (data > 0){
                            $('<tr><td>' + ($("#trInfosSale").attr("data-currentRow") * 1 + 1) + '</td><td data-acctNo="' + $("#saleAcctNo").val() + '">' + $("#saleAcctNo option:selected").text() + '</td>' +
                                    '<td>' + $("#startdate").val() + '</td>' +
                                    '<td>' + $("#enddate").val() + '</td>' +
                                    '<td class="tar">' + accounting.formatNumber($("#iPerAll").val(), 2) + '</td>' +
                                    '<td>' + 
                                    '<a data-myid="'+ data +'" data-acct="'+ $("#editAcct").val() +'" onclick="editRow(this)" class="btn-icon btn-mini btn-warning-form"><i class="i-pen"></i></a>' +
                                     ' <a onclick="deleteWakaf(this)" data-myid="'+ data +'" class="btn-icon btn-mini btn-danger-form"><i class="i-trashcan"></i></a></td>').insertBefore("#trInfosSale");
                                    $("#trInfosSale").attr("data-currentRow", $("#trInfosSale").attr("data-currentRow") * 1 + 1);
                                    msg ("alert-success", "Wakaf saved !!!");
                            }
                        }
                });
        }
    }

       
       
       function editRow(me){
        if (!$("#editAcct").length) {
            var tr = $(me).parent().parent();
                   $(tr).replaceWith('<tr><td>' + $(me).parent().parent().find("td").eq(0).html() + '</td>' +
                        '<td><select id="editAcct" data-lineNo="' + $(me).parent().parent().find("td").eq(0).html() + '" data-previewvalue="' + $(me).parent().parent().find("td").eq(1).html() + '" class= "span12 validate[required]"><option></option>' +
                        <?php
                        if ($chargeAccount) {
                            foreach ($chargeAccount as $row) {
                                ?>
                                               '<option   value="<?php echo $row->ID; ?>"><?php
                                echo substr($row->acctCode,0,4).'-'.substr($row->acctCode,4,3).'-'.substr($row->acctCode,7) ;
                                echo str_replace("'", " ", $row->acctName, $count);
                                ?></option>' +
                                <?php
                            }
                        }
                        ?>
                   '</select></td>' +
                    '<td><input value = "' + $(me).parent().parent().find("td").eq(2).html() + '" data-previewValue="'+ $(me).parent().parent().find("td").eq(2).html() +'" onkeydown= "return false" type="text" id="editFrom" class="span10 datepicker2 financhk validate[required, past[#enddate]]" /></td>' +
                    '<td><input value = "' + $(me).parent().parent().find("td").eq(3).html() + '" data-previewValue="'+ $(me).parent().parent().find("td").eq(3).html() +'"  onkeydown= "return false" type="text" id="editTo" class="span10 datepicker2 financhk validate[required, future[#startdate]]" /></td>' +
                     '<td><input value = "' + $(me).parent().parent().find("td").eq(4).html() + '" data-previewValue="'+ $(me).parent().parent().find("td").eq(4).html() +'"  id="editPercentage" type="text" class="span10 validate[required, future[#startdate]]" /></td>' +
                    '<td>' +
                    '<button onclick="updateRow(this)" data-acct="' + $(me).attr("data-acct") +'" data-myid="' + $(me).attr("data-myid") + '" class="btn-icon btn-mini btn-success-form" type="button"><span class="i-checkmark-3"></span></button>' +
                    ' <button  id="btnCancel" data-acct="' + $(me).attr("data-acct") +'" data-myid="' + $(me).attr("data-myid") + '" onclick="cancelEdit(this)" class="btn-icon btn-mini btn-danger-form" type="button"><span class="i-cross"></span></button></td>' +
                    '</tr>');
                        $("#editAcct").val($(me).attr("data-acct"));
                        jQuery('#editFrom').datepicker({
			dateFormat : 'yy-mm-dd',
			changeMonth : true,
			changeYear : true
			});
                        jQuery('#editTo').datepicker({
			dateFormat : 'yy-mm-dd',
			changeMonth : true,
			changeYear : true
			});
                        $('#editAcct').select2();
                         $.each($("#editAcct"), 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();
                        });
                        });
                    }else {
                         msg ("", "A row is been editing ... !!!");
                    }
               }
               function updateRow(me){
                var tr = $(me).parent().parent();
                  
                 $.ajax({
                    type: "POST",
                    data : {
                        rowId : $(me).attr("data-myid"),
                        acct : $("#editAcct").val(),
                        from :  $("#editFrom").val(),
                        to :  $("#editTo").val(),
                        percent:  accounting.unformat($("#editPercentage").val()),
                                
                    },
                    url: "<?php echo base_url() ?>wakafsetup/updatewakafAcrual",
                    success: function(data) {
                    if (data > 0){
                            msg ("alert-success", "Wakaf updated !!!");
                             $(tr).replaceWith('<tr><td>' + $("#editAcct").attr("data-lineNo") + '</td>' +
                                '<td>' + $("#editAcct option:selected").text()  + '</td>' +
                                '<td>' + $("#editFrom").val() + '</td>' +
                                '<td>' + $("#editTo").val() + '</td>' +
                                '<td class="tar">' + accounting.formatNumber($("#editPercentage").val(),2) + '</td>' +
                                '<td>' + 
                                '<a data-myid="'+ $(me).attr("data-myid") +'" data-acct="'+ $("#editAcct").val() +'" onclick="editRow(this)" class="btn-icon btn-mini btn-warning-form"><i class="i-pen"></i></a>' +
                                 ' <a onclick="deleteWakaf(this)" data-myid="'+ $(me).attr("data-myid")+'" class="btn-icon btn-mini btn-danger-form"><i class="i-trashcan"></i></a></td>' );
               
                    }
                    }
                 });
               }
               
               function cancelEdit(me){
                   
                    var tr = $(me).parent().parent();
                    $(tr).replaceWith('<tr><td>' + $("#editAcct").attr("data-lineNo") + '</td>' +
                            '<td>' + $("#editAcct").attr("data-previewvalue") + '</td>' +
                            '<td>' + $("#editFrom").attr("data-previewvalue") + '</td>' +
                            '<td>' + $("#editTo").attr("data-previewvalue") + '</td>' +
                            '<td class="tar">' + $("#editPercentage").attr("data-previewvalue") + '</td>' +
                            '<td>' + 
                                '<a data-myid="'+ $(me).attr("data-myid") +'" data-acct="'+ $(me).attr("data-myid") +'" onclick="editRow(this)" class="btn-icon btn-mini btn-warning-form"><i class="i-pen"></i></a>' +
                                 ' <a data-myid="'+ $(me).attr("data-myid") +'" onclick="deleteWakaf(this)" class="btn-icon btn-mini btn-danger-form"><i class="i-trashcan"></i></a></td>' );
               
                 
               }
               
               function deleteWakaf(me){
                   $('#popupModal').modal('show');
            $("#_popupTitle").html("Confirm delete");
            $("#_popupContent").html("Your about to delete definively this row. <br/> Click on ok to confirm");
            $('#_popupFooter').empty();
            $('#_popupFooter').append('<button class="btn-form btn-success-form" id="detButton">Ok</button>' +
            '<button class="btn-form btn-danger-form" id="canb">Cancel</button>');
            $("#_popupTitle").html("Confirm delete");
            $("#canb").click(function() {
    $('#popupModal').modal('hide');
    });
            $("#detButton").click(function() {
    $('#popupModal').modal('hide');
            $.ajax({
            type: "POST",
                    data: {
                    rowId :$(me).attr("data-myid"),
                    },
                    url: "<?php echo base_url() ?>wakafsetup/deleteWakafAcrual",
                    success: function(data) {
                        if(data>0){
                        msg ("alert-success", "Wakaf deleted !!!");
                        deleteRow(me);
                    }
                    } });
    });
               }

        function getListByDate(){
                window.location ="<?php echo base_url() ?>wakafsetup/wakaf_acrual_list/" + $("#sstartdate").val() + "/" + $("#senddate").val() ; 
        }

</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('w') ?><?php echo $this->lang->line('wakafPreSetup') ?></h2>
                            <div class="side fr">
                                <button class="btn-form btn-danger-form" onClick="clear_form('#validate');" type="button"><?php echo $this->lang->line('clearForm') ?></button>
                            </div>
                        </div>
                       
                    </div>  <!-- content -->
                </div>  <!-- block -->
            </div>  <!-- span12 -->
        
 <div class="row-fluid scRow">  
            
            <div class="span12 scCol">
                <div class="block" id="grid_block_4">
                    <div class="content">
                        <div class="controls-row side fl">
                                <div class="form-inline">
   
                           
                            
                        
                        <div class="input-prepend span4"><span class="add-on span4"><?php echo $this->lang->line('from') ?></span>
                                                <?php
                                                $data = array(
                                                    'name' => 'sstartdate',
                                                    'id' => 'sstartdate',
                                                    'value' =>'',
                                                    'type' => 'text',
                                                    'onkeydown' => 'return false',
                                                    'class' => 'span8 datepicker2 financhk validate[required, future[#startdate]]',);
                                                echo form_input($data, date("d-m-Y"));
                                                ?>
                                            </div>
                            <div class="input-prepend span4"><span class="add-on span4"><?php echo $this->lang->line('to') ?></span>
                                                <?php
                                                $data = array(
                                                    'name' => 'senddate',
                                                    'id' => 'senddate',
                                                    'value' =>'',
                                                    'type' => 'text',
                                                    'onkeydown' => 'return false',
                                                    'class' => 'span8 datepicker2 financhk validate[required, future[#startdate]]',);
                                                echo form_input($data, date("d-m-Y"));
                                                ?>
                                            </div>
                                    
                                    <a href="#" class="btn-form btn-info-form" style="margin-left: 10px;" onclick="getListByDate()" id="btnSubmit" ><?php echo $this->lang->line('getList') ?></a>
                        </div>
                             </div>
                    </div>
                </div>
            </div>

        <div class="row-fluid scRow">                            
            <div class="span12 scCol">
                <div class="block" id="grid_block_4">
                    <div class="head">
                        <h2><?php echo $this->lang->line('') ?>
                                </h2>
                        <ul class="fr buttons">
                            <li><a class="block_toggle collapsed" data-toggle="collapse" data-parent="#accordion2" href="#collapseOne">
                                    <span class="i-arrow-down-3"></span></a></li>
                        </ul>                                  
                    </div><!-- head -->
                    <div class="content np">
                        <form id="formRow" name="formRow" >
                        <table id="listDetails" cellpadding="0" cellspacing="0" width="100%">
                            <thead>
                                <tr>
                                    <th class="span1">#</th>
                                    <th class="span3"><?php echo $this->lang->line('saleaccountno') ?></th>
                                    <th class="span2"><?php echo $this->lang->line('from') ?></th>
                                    <th class="span2"><?php echo $this->lang->line('to') ?></th>
                                    <th class="tar span2"><?php echo $this->lang->line('percentage') ?></th>
                                    <th class="span2"><?php echo $this->lang->line('action') ?></th>
                                </tr>
                            </thead>
                            <tbody>
                            <?php 
                             $i=0;
                             if($listWkaf){
                                
                                 foreach ($listWkaf as $row) {
                            
                            ?>
                                <tr>
                                    <td><?php echo ++$i; ?></td>
                                    <td><?php echo '['.$row->acctCode.'] '.$row->acctName; ?></td>
                                    <td><?php echo $row->from; ?></td>
                                     <td><?php echo $row->to; ?></td>
                                     <td class="tar"><?php echo number_format($row->wakafPercent,2); ?></td>
                                     <td>
                                         <a data-myid="<?php echo $row->ID; ?>" data-acct="<?php echo $row->salesAccountID; ?>" onclick="editRow(this)" class="btn-icon btn-mini btn-warning-form ">
                                         <i class="i-pen"></i></a>
                                         <a data-myid="<?php echo $row->ID; ?>" onclick="deleteWakaf(this)" class="btn-icon btn-mini btn-danger-form remove">
                                         <i class="i-trashcan"></i></a>
                                     </td>
                                </tr>
                                
                               <?php
                             }
                             }
                               ?>
                                <tr data-currentRow="<?php echo $i; ?>" id="trInfosSale">
                                    <td></td>
                                    <td>
                                        <?php
                                        $preselcurr = '0';
                                        $options = '';
                                        $options[NULL] = $this->lang->line('pleaseSelect');
                                        if(isset($chargeAccount) && !empty($chargeAccount))	
											foreach ($chargeAccount as $row) {
                                            	$options[$row->ID] = '[' . $row->acctCode . '] ' . $row->acctName;
                                        	}
                                        $js = ' id="saleAcctNo"  class = "span12 validate[required]"';
                                        echo form_dropdown('chargeAccount', $options, $preselcurr, $js);
                                        ?>    
                                    </td>
                                    <td>
                                        <div class="input-prepend span12"><span class="add-on span3"><i class="i-calendar"></i></span>
                                                <?php
                                                $data = array(
                                                    'name' => 'startdate',
                                                    'id' => 'startdate',
                                                    'value' =>'',
                                                    'type' => 'text',
                                                    'onkeydown' => 'return false',
                                                    'class' => 'span9 datepicker2 financhkSrt validate[required, past[#enddate]]',);
                                                echo form_input($data, date("d-m-Y"));
                                                ?>
                                            </div>
                                    </td>
                                    <td>
                                        <div class="input-prepend span12"><span class="add-on span3"><i class="i-calendar"></i></span>
                                                <?php
                                                $data = array(
                                                    'name' => 'enddate',
                                                    'id' => 'enddate',
                                                    'value' =>'',
                                                    'type' => 'text',
                                                    'onkeydown' => 'return false',
                                                    'class' => 'span9 datepicker2 financhkEnd validate[required, future[#startdate]]',);
                                                echo form_input($data, date("d-m-Y"));
                                                ?>
                                            </div>
                                    </td>
                                    <td>
                                        <div class="input-prepend input-append">
                                            <input class="span8 tar validate[required, max[100]]" type="text" id="iPerAll" />
                                            <span class="add-on">&#37;</span>
                                        </div>
                                    </td>
                                    <td>
                                                <a id="btnInserRow" onclick="savewakaf()" class="btn-icon btn-mini btn-success-form"><i class="i-checkmark-3"></i></a>
                                     <?php
                            $data = array(
                                'name' => 'validate',
                                'style' => 'margin-right:5px;',
                                'class' => 'btn-form btn-warning-form',
                                'onClick' => "$('#validate').validationEngine('hide');");
                            echo form_button($data, $this->lang->line('hide'));
                            ?>
                                    </td>
                                
                                </tr>
                            
                            </tbody>
                        </table> 
                        </form>
                    </div><!-- content -->
                </div><!-- grid block 1-->
            </div><!-- scCol -->
        </div><!-- row-fluid scRow-->


        <div class="row-fluid scRow">  
            <div class="span12 scCol">
                <div class="block" id="grid_block_4">
                    <div class="content">
                        <div style="padding:0;" class="controls-row">
                            <div id="msg_div" style="min-height: 22px;" class="alert alert-info"> <p></p> </div>
                        </div>
                    </div>
                </div>
            </div>

        </div><!-- row-fluid scRow-->
        </div>  <!-- row-fluid -->
    </div>  <!-- content -->
</div>  <!-- wrap -->
</div>  <!-- content -->
