<!---- 
    Author 	: Herpan / Tanggal  : 18-08-2014
    Rec=visi    : Ferry / Tanggal   : 03-12-2014
-->

<div class="easyui-layout" data-options="fit:true,border:false">
    <form id="fm_company_coa" name="fm_company_coa" method="post" action="">

        <!-- top view-->
        <div id="form-header" data-options="region:'north',border:false" >
            <div id="form-header-wrapper">
                <table cellpadding="0" cellspacing="0" width="100%" height="100%" border="0">
                    <tr>
                        <td align="left" valign="middle">
                        	<a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-reload'" onclick="location.reload();">Refresh</a>
                        </td>							

                        <td height="25" align="right" valign="middle">								
                            <span class="form-title">Company G/L Account</span>
                        </td>
                    </tr>
                </table>		     	
            </div>			
        </div>
        <!-- end top view -->

        <!-- center view -->
        <div id="form-body" data-options="region:'center',border:false">
            <div id="form-wrapper" class="easyui-panel" style="padding:5px;" fit="true">
                <div class="easyui-layout" fit="true">					
                    <div data-options="region:'south',split:true,collapsible:false" style="height:160px;">
                        <table id="dtg_company" title="List Of Company" class="easyui-datagrid" style="width:auto"
                            url="<?php echo base_url()?>list/coa/get_company"
                            idField="CompanyCode"
                            iconCls="" toolbar="#toolbar" data-options="singleSelect:true">

                            <thead>
                                <tr>
                                    <th field="ck" checkbox="true"></th>
                                    <th width="230" align="left" halign="center" field="CompanyCode"><b>Company Code</b></th>
                                    <th width="250" align="left" halign="center" field="CityName"><b>City Name</b></th>
                                    <th width="820" align="left" halign="center" field="CompanyName"><b>Company Name</b></th>																		
                                </tr>
                            </thead>
                        </table>
                    </div>

                    <div data-options="region:'east',split:true,collapsible:false" style="width:550px;">
                        <table id="selected_coa" class="easyui-datagrid" rownumbers="true" title="Selected G/L Account"
                            data-options="border:false,fit:true,fitColumns:true" idField="AccountNumber" singleSelect="true">

                            <thead>
                                <tr>
                                    <th data-options="field:'AccountNumber'" formatter="format_acc" width="80" halign="center"><b>Account Number</b></th>
                                    <th data-options="field:'AccountName'" formatter="format_acc" width="100" halign="center"><b>Account Name</b></th>
                                    <th data-options="field:'AccountTypeName'" width="80" halign="center"><b>Account Type</b></th>	
                                    <th data-options="field:'AccountSubTypeID'" width="80" halign="center" hidden><b>Account Type Id</b></th>
                                    <th data-options="field:'AccountChild'" width="80" halign="center" hidden><b>Account Type Id</b></th>
                                    								
                                </tr>
                            </thead>
                        </table>						
                    </div>

                    <div data-options="region:'west',split:true,collapsible:false" style="width:550px;">
                        <table id="source_coa" class="easyui-datagrid" rownumbers="true" title="List Of G/L Account" 
                            data-options = "border:false,singleSelect:false,fit:true,fitColumns:true" 
                            idField="AccountNumber"
                            
                        >
                            <thead>
                                <tr>
                                    <th field="ck" checkbox="true"></th>
                                    <th data-options="field:'AccountNumber'" formatter="format_acc" width="80" halign="center"><b>Account Number</b></th>
                                    <th data-options="field:'AccountName'" formatter="format_acc" width="100" halign="center"><b>Account Name</b></th>
                                    <th data-options="field:'AccountTypeName'" width="80" halign="center"><b>Account Type</b></th>
                                    <th data-options="field:'AccountSubTypeID'" width="80" halign="center" hidden><b>Account Type Id</b></th>									
                                </tr>
                            </thead>
                        </table>					
                    </div>
                    
                    <div data-options="region:'center',collapsible:false">	
                        <table style="width:100%;height:100%;">
                            <tr>
                                <td align="center" valign="middle">
                                    <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-next',size:'large'" onclick="move_right();"></a>							
                                <td>
                            </tr>

                            <tr>
                                <td align="center" valign="middle">
                                    <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-back',size:'large'" onclick="move_left();"></a>
                                <td>
                            </tr>
                        </table>						
                    </div>
                </div>						          
            </div>	

            
        </div>
        <!-- end center view -->	

        <div id="form-footer" data-options="region:'south',border:false">
            <table width="100%" cellpadding="0" cellspacing="0">
                <tr>
                    <td align="right">
                        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-help" onclick="" plain="true">Help</a>
                        <a href="javascript:void(0)" class="easyui-linkbutton form-button" id="save_button"  iconCls="icon-save" onclick="save_data_company_coa('list/coa');">Save</a>					  
                        <a href="javascript:void(0)" class="easyui-linkbutton  form-button" iconCls="icon-cancel" onclick="">Cancel</a>						
                    </td>
                </tr>
            </table>
        </div>
    </form>	
</div>

<div id="dlg_importfile" class="easyui-dialog" data-options="closed:true,width:'270',height:'140',modal:true" style="">
   <br />
   
   <center><img src='<?php echo base_url();?>assets/custom/img/c_loader.gif' id='loading_img' alt='loadingImg' /></center>
   <br />
   <center><span id='id_span'>Jangan menutup halaman ini selama proses perpindahan</span></center>
</div>

<script>
    function format_acc(val,row){
        return '<span style="padding-left:'+row.AccountLevel+'em;">'+val+'</span>';	
    }		
	
	$(function(){
		var source_coa = $('#source_coa').datagrid({remoteFilter:true,type:'post',url:'<?php echo base_url(); ?>list/coa/account_gl'});
		$('#source_coa').datagrid('enableFilter');
		
		var selected_coa = $('#selected_coa').datagrid({type:'post'});
		$('#selected_coa').datagrid('enableFilter');
		
	});
	
    function move_right(){
        var data_to_move=$('#source_coa').datagrid('getChecked');

        $.each(data_to_move, function( index, value ) {    
            if($('#selected_coa').datagrid('getRowIndex', value.AccountNumber) == -1){
                $('#selected_coa').datagrid('appendRow',{
                    AccountNumber: value.AccountNumber,
                    AccountName: value.AccountName,
                    AccountLevel: value.AccountLevel,
                    AccountTypeName: value.AccountTypeName,
					AccountSubTypeID: value.AccountSubTypeID,
					AccountChild: value.AccountChild
                });  
            }/*
            else{
                $.messager.alert('Maaf','Account dengan nomor:'+value.AccountNumber+' Sudah Di Pilih','error',function(){
					                                    
				});
				//alert('Account dengan nomor: '+value.AccountNumber+' sudah dipilih');
            }*/
        });			
    }

    function move_left(){
        var data_to_move=$('#selected_coa').datagrid('getChecked');
        var index_to_remove;

        $.each(data_to_move, function(index, value ) {
            index_to_remove=$('#selected_coa').datagrid('getRowIndex', value.AccountNumber);

            if(index_to_remove === -1){
                alert('Silahkan pilih akun');
            }
            else{
                $('#selected_coa').datagrid('deleteRow', index_to_remove);
            }
        });			
    }
	
    function save_data_company_coa(url) {
        url = base_url + url + '/insert_companygl';
        
        var selected_coa        = JSON.stringify($('#selected_coa').datagrid('getRows'));
        var selected_company    = JSON.stringify($('#dtg_company').datagrid('getChecked'));
        
        $.messager.confirm('Warning','Pastikan G/L Account yang dipilih sudah benar...!!',  
            function(r){
                if (r){
                    modal_open('Progress Bar','dlg_importfile');
					$.post(url ,{selected_coa:selected_coa,selected_company:selected_company},function(result){
                        if (result.status == true){
							modal_close('dlg_importfile');
							$.messager.alert('Success',result.message,'info',function(){
								location.reload();
								});				
                        } 
                        else {
							modal_close('dlg_importfile');
							$.messager.alert('Error',result.message,'error',function(){
								location.reload();	
								});				
                        }
                    },'json');
                }
            }
        );
    }
</script>