<!---- 
    Author			: Daswan
    Created Date	: 14-12-2015
    Revision		: ) 
-->

<?php
			//---------Untuk privellege jika dia ada tombol save----------
			if($group_user->SavePrivillage == 1){
				$cek_autority_save 		= "";
			}
			else{
				$cek_autority_save 		= "disabled";
			}
			
			//-------------end----------------------------
?>	
	
<style>
table td{
	height: 20px;
}
</style>

<div class="easyui-layout" data-options="fit:true,border:false">
    <form id="fm_fixed_asset_dispose" name="fm_fixed_asset_dispose" method="post">
		<input type="hidden" name="detail" id="detail" />

        <!-- top view-->
        <div id="form-header" data-options="region:'north',border:false" >
            <div id="form-header-wrapper">
                <table cellpadding="0" cellspacing="0" width="100%" border="0">
                <tr>
                    <td align="left">
                        <a href="#" class="easyui-linkbutton" iconCls="icon-document" plain="true" onclick="parent.tab_add('New Dispose Fixed Asset','<?=$menu_link?>','icon-coa','add')">New</a>
						<a href="#" class="easyui-linkbutton" id="refresh-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" style="margin-left:7px;">
                            <?=$menu_label?>
                        </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="width:1180px;height:526px;padding:5px;" fit='true'>
                <input type="hidden" name="" id="" value="" />
                
                <table width="100%" height="141">
                <tr>
                    <td width="50%" valign="top">
                        <table width="100%" cellpadding="0" cellspacing="1">
                        <tr height="20">
                            <td width="150"><b>Gain / Loss Account</b></td>
                            <td colspan="3">
                                <b>:</b>
                                <select name="cmb_gl" class="easyui-combogrid" id="cmb_gl" required="required" editable="false"  style="width:400px;height:22px;" data-options="
                                panelWidth: 400,
                                idField: 'AccountNumber',
                                textField: 'AccountName',
                                url: '<?= base_url().$menu_link; ?>/get_gl_account',               
                                columns: [[
                                 {field:'AccountNumber',title:'Account Number',width:130},
                                 {field:'AccountName',title:'Account Name',width:350}
                                ]],
                                
                                fitColumns: true
                                ">
                                </select> 
                                
                            </td>
                            <td width="60"&nbsp;>&nbsp;</td>
                        </tr>
                        </table>
                    </td>
                    <td width="50%" valign="top">
                        <table width="100%" cellpadding="0" cellspacing="1">
                            <tr>
                            	<td height="20">&nbsp;</td>
                                <td width="731" align="center" valign="middle"></td>
                                <td width="150" align="center" valign="middle" bgcolor="#333399" ><span style="color:#fff;">Date</span></td>
                                
                            </tr>
                            <tr>
                            	<td height="20">&nbsp;</td>
                                <td align="center" valign="top">&nbsp;</td>
                                <td align="right" valign="top"><input name="txt_date" id="txt_date" type="text" value="<?php echo $date_now;?>" class="easyui-datebox validBackward" style="width:150px;height:20px" required editable="false" /></td>
                            </tr>
                        </table>
                    </td>
            	</tr>											
                </table>	

                <!--Item Grid -->
                <div class="easyui-tabs" data-options="tabHeight:18">
                    <div title="Items">
                        <table toolbar="#tb_dg_item" id="dg_fd" class="easyui-edatagrid" idField="id"
                            rownumbers="true" fitColumns="true" 
                            singleSelect="true" style="height:256px;" 
                            >
                            <thead>
                            <tr>
                                <th field="AssetCode"   halign="center" align="left" valign="middle"    data-options="width:400,align:'left'"           editor="{options:{required:true}}">AssetCode</th>
                                <th field="ItemCode" 	halign="center"	align="left" valign="middle"	data-options="width:400,align:'left'" 			editor="{options:{required:true}}">ItemCode</th>
                                <th field="Description" halign="center" align="left" valign="middle"	data-options="width:700,align:'left'" 			editor="description">Item Description</th>
                                <th field="Qty" 		halign="center" align="left" valign="middle"	data-options="width:200,align:'left'" 			editor="{type:'numberbox',options:{required:true,min:1,disabled:true}}">Qty</th>
                                <th field="Amount"         halign="center" align="left" valign="middle"    data-options="width:200,align:'left'"           editor="{type:'numberbox',options:{required:true,min:0.01,disabled:true}}" formatter="formatRp" >Amount</th>
                                <th field="ItemAverage"         halign="center" align="left" valign="middle"    data-options="width:200,align:'left'"           editor="{type:'numberbox',options:{required:true,min:0.01,disabled:true}}" formatter="formatRp" hidden="true">ItemAverage</th>
                                <th field="ReceiptNumber" data-options="width:200,align:'left'" hidden="true">ReceiptNumber</th>
                                <th field="FixedAssetType" data-options="width:200,align:'left'" hidden="true">FixedAssetType</th>
                                <th field="ItemCategoryID" width="200" halign="center" align="left" hidden="true">ItemCategoryID</th>
                                
                            </tr>
                            </thead>
                        </table>	
                        <div id="tb_dg_item">
                            <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="add_fixed_asset();">New</a>
                            <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="remove_row();">Delete</a>
                        </div>					
                    </div>
                    
                </div>
                <!-- End Item Grid -->		

                <table width="100%" cellpadding="0" cellspacing="0" style="margin-top:5px;">
                <tr>
                    <td width="100" rowspan="2" valign="top"><b>Description</b></td>
                    <td width="362" rowspan="2" valign="top"><textarea name="txa_description" cols="55.5" rows="4" style="resize:none;"></textarea></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td width="301" align="right" valign="top" height="23"><b>Total Amount :&nbsp;</b></td>
                    <td width="144" align="right" valign="top">
                        <input type="text" id="TotalAmount" name="TotalAmount" class="easyui-numberbox" value="0" data-options="min:0.00,precision:2,decimalSeparator:',',groupSeparator:'.'" style="text-align:right;" readonly />
                        
                    </td>
                </tr>
                </table>			
            </div>
        </div>
        <div id="form-footer" data-options="region:'south',border:false">
			<table cellpadding="0" cellspacing="0" width="100%" height="100%" border="0">
				 <tr>
				   <td align="left">
						
				   </td>
					<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 save-button" 	iconCls="icon-save" 	onClick="save_me()" <?php echo $cek_autority_save;?>>Save</a> 	
                        <a href="javascript:void(0)" 	class="easyui-linkbutton form-button" 				iconCls="icon-cancel" 	onclick="location.reload();">Cancel</a>	
					</td>
				 </tr>
			</table>				
		</div>
    </form>
</div>

<div id="dialog-fixed-asset" class="easyui-dialog" closed="true" data-options="modal:true, iconCls:'icon-save'" buttons="#dialog-buttons" style="width:840px;height:auto;top:27px;background-color:#F8F8F8;padding:5px;">     
     <table id="dg_fixed_asset" class="easyui-datagrid" 
        url="<?= base_url().$menu_link;?>/get_fixed_asset"     
        remoteSort="false"
        pageSize="150"
        fitColumns="true" 
        rowNumbers="true"
        style="width:812px;height:400px" 
        data-options = "border:true,view:scrollview,autoRowheight:false"
    >                      
        <thead>
            <tr>
                <th field="ck" checkbox="true"></th>
                <th field="ReceiptNumber" width="250" halign='center' align='center' sortable="true">Receipt No.</th>
                <th field="ReceiptDate" width="130" halign='center' align='center' sortable="true" formatter="formatDate">Receipt Date</th>
                <th field="AssetCode" width="150" halign='center' align='center' sortable="true">AssetCode</th>
                <th field="ItemAverage" width="150" halign="center" align="left" formatter="formatRp">ItemAverage</th>
                <th field="PaymentAmount" width="150" halign="center" align="left" formatter="formatRp">PaymentAmount</th>
                <th field="ItemCode" width="200" halign="center" align="left" hidden="true">ItemCode</th>
                <th field="ItemDesc" width="200" halign="center" align="left" hidden="true">ItemDesc</th>
                <th field="ItemQty" width="200" halign="center" align="left" hidden="true">ItemQty</th>
                <th field="FixedAssetType" width="200" halign="center" align="left" hidden="true">FixedAssetType</th>
                <th field="ItemCategoryID" width="200" halign="center" align="left" hidden="true">ItemCategoryID</th>
            </tr>
        </thead>
    </table>
    
    <div style="border:none; margin-top:10px; float:right;">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onClick="choose_fd();">&nbsp; OK &nbsp; &nbsp;</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="modal_close('dialog-fixed-asset');">Cancel</a>
    </div>
                     
</div>

<script type="text/javascript">
var form_click_aktiv = 0;
$(document).ready(function(){
	
     $("#fm_fixed_asset_dispose").click(function(){
        if(form_click_aktiv > 0){
            $('#dg_fd').edatagrid('saveRow');
        }
    });

});

function add_fixed_asset(){
	 modal_open('List Fixed Asset','dialog-fixed-asset');
}

function choose_fd(){

    $("#dg_fd").datagrid('loadData', {"total":0,"rows":[]});
    $("#TotalAmount").numberbox('setValue',0);

    var fd          = $('#dg_fixed_asset').datagrid('getChecked');
    var totalAmount = 0;

    $.each(fd, function(ind, value) {
        var ReceiptNumber   = value.ReceiptNumber;
        var ReceiptDate     = value.ReceiptDate;
        var AssetCode       = value.AssetCode;
        var ItemAverage     = value.ItemAverage;
        var PaymentAmount   = value.PaymentAmount;
        var ItemCode        = value.ItemCode;
        var ItemDesc        = value.ItemDesc;
        var ItemQty         = value.ItemQty;
        var FixedAssetType  = value.FixedAssetType;
        var ItemCategoryID  = value.ItemCategoryID;

        // if(isNaN(PaymentAmount)){
        //     PaymentAmount = 0;
        // }

        var amount          = parseFloat(ItemAverage) - parseFloat(PaymentAmount);

        $('#dg_fd').edatagrid('addRow',{
            index:ind, 
            row:{ItemCode          : ItemCode,
                Description        : ItemDesc,
                Qty                : ItemQty,
                Amount             : amount,
                ItemAverage        : ItemAverage,
                ReceiptNumber      : ReceiptNumber,
                FixedAssetType     : FixedAssetType,
                AssetCode          : AssetCode,
                ItemCategoryID     : ItemCategoryID}
        });

        $('#dg_fd').edatagrid('saveRow');
        totalAmount += amount;
        form_click_aktiv++;
        
    });

    $("#TotalAmount").numberbox('setValue',totalAmount);
    modal_close('dialog-fixed-asset');
}

function remove_row(){
	var selectedrow     = $('#dg_fd').datagrid('getSelected');
	var row_index       = $('#dg_fd').datagrid('getRowIndex', selectedrow);
	
	$.messager.confirm('Confirmation :', 'Anda yakin, Anda ingin menghapus item "' + selectedrow.ItemCode + '"?',  
		function(r){
			if (r){
				/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
				$('.save-button').linkbutton('enable');
				/*=================================end============================================================*/
				$('#dg_fd').datagrid('deleteRow', row_index);
				calculate_sub();
			}
		}
	);
}

function calculate_sub(){
    var row         = $('#dg_fd').edatagrid('getRows');
    var totalAmount = 0;

    $.each(row, function(ind, value) {
        totalAmount += value.Amount;
    });

    $("#TotalAmount").numberbox('setValue',totalAmount);
}

function save_me(){
    $('#dg_fd').edatagrid('saveRow'); 

    var row         = $('#dg_fd').datagrid('getRows');
    var totalAmount = 0;

    $.each(row, function(ind, value) {
        totalAmount += value.Amount;
    });

    if(totalAmount <= 0){
        $.messager.alert('Unseccessfull','Total Amount tidak boleh nol','error',function(){
            return false;
        });
    }else{
        var detail = JSON.stringify(row);
        $('#detail').val(detail);
        save_transaction('<?= $menu_link?>','#fm_fixed_asset_dispose');
    }
}

</script>