<?php
/*	
 *	Author 	: Wahyu / 29-08-2014	
 *	Revisi 	: Ferry / 03-01-2015 / Penambahan Preference Account
 */
?>

<div id="cc" class="easyui-layout" data-options="fit:true, border:false">
    <div data-options="region:'west',split:true, border:true" title="" style="width:230px; background-color:#F8F8F8;">
        <div title="" style="padding:10px;overflow:auto;" data-options="selected:true" >				
            <div class="preference_menu">
                <ul>
                    <!--<li><a href="<?php echo site_url('setup/preference_account'); ?>" style="border-top:none;"><img src="<?php echo base_url(); ?>assets/custom/icons/clipboard-invoice.png" alt=""/><span>P/L Account</span></a></li>-->	
                    <li><a href="<?php echo site_url('setup/preference_item'); ?>"><img src="<?php echo base_url(); ?>assets/custom/icons/clipboard-invoice.png" alt=""/><span>Item Account</span></a></li>	
                    <li><a href="<?php echo site_url('setup/preference_sales'); ?>"><img src="<?php echo base_url(); ?>assets/custom/icons/moneys.png" alt=""/><span>Sales Account</span></a></li>                   				   
                    <li><a href="<?php echo site_url('setup/preference_purchases'); ?>"><img src="<?php echo base_url(); ?>assets/custom/icons/sticky-notes.png" alt=""/><span>Purchases Account</span></a></li>	
                    <li><a href="<?php echo site_url('setup/preference_services'); ?>"><img src="<?php echo base_url(); ?>assets/custom/icons/calculator.png" alt=""/><span>Expense Account</span></a></li>
                    <li><a href="<?php echo site_url('setup/preference_advances'); ?>"><img src="<?php echo base_url(); ?>assets/custom/icons/calculator.png" alt=""/><span>Adv. Payment Account</span></a></li>
                    <li><a href="<?php echo site_url('setup/preference_expense'); ?>"><img src="<?php echo base_url(); ?>assets/custom/icons/calculator.png" alt=""/><span><b>Cash-Bon Account</b></span></a></li>
                    <li><a href="<?php echo site_url('setup/preference_others'); ?>"><img src="<?php echo base_url(); ?>assets/custom/icons/money-coin.png" alt=""/><span>Others Account</span></a></li> 
                    <li><a href="<?php echo site_url('setup/preference_approval'); ?>"><img src="<?php echo base_url(); ?>assets/custom/icons/property.png" alt=""/><span>Sales Approval Limit</span></a></li>
                    <li><a href="<?php echo site_url('setup/preference_miscellaneous'); ?>"><img src="<?php echo base_url(); ?>assets/custom/icons/folder-stand.png" alt=""/><span>Miscellaneous</span></a></li>
                </ul>
            </div>	
        </div>
    </div>
	
    <div data-options="region:'center',fit:false,plain:true, border:false" style="background:#eee;">
        <div id="wrapptabs" name="" class="easyui-tabs">
            <!--Awal View Service Category-->
            <div id="div_advance_group" title="Exp. Cashbon" border="false" style="padding:1px 0 1px 0;background:#EEE;">
                <div style="float:left;padding-left:5px;margin-bottom:5px;width:1116px;height:526px;background:#FFF;border:#95B8E7 1px solid;">
                    <div style="overflow:hidden;padding:1px;">
                        <form id="frm_category"  method="post" class="preference_form">
                            <label>								
                                <div style="margin:0px 0px 10px">
                                    <table id="dtg_category" class="easyui-edatagrid" title="" iconCls="icon-save" rownumbers="true" toolbar="#toolbar_cat" fitColumns="true" singleSelect="true" style="width:1090px;height:431px">
                                        
                                        <thead>
                                            <tr>
                                                <th field="ExpCategoryName" width="200" data-options="align:'left',halign:'center'">Group Name</th>
                                                <th field="ExpCategoryDesc" width="355" data-options="align:'left',halign:'center'">Description</th>
                                                <th field="ExpCategoryID" width="55"data-options="align:'center',halign:'center'" hidden>Category ID</th>							
                                                <th field="ExpCategoryType" width="55"data-options="align:'center',halign:'center'" hidden>ModusType</th>							
                                            </tr>
                                        </thead>
                                    </table>

                                    <div id="toolbar_cat">
                                        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="load_group_popup(1);">New </a>
                                        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="load_group_popup(2);">Edit</a>
                                        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="delete_category();">Delete </a>
                                    </div>                                    
                                </div>				
                            </label> <br />
                        </form>
                    </div>			
                </div>

                <div style="padding:5px;text-align:right;width:1110px;">
                    <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-save" onclick="save_category();">Save</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton  form-button" iconCls="icon-cancel" onclick="location.reload();">Cancel</a>
                </div>	
            </div>
            <!--Akhir View Service Category-->
			
            <div id="div_advance_group" title="Cashbon Detail" border="false" style="padding:1px 0 1px 0;background:#EEE;">
                <div style="float:left;padding-left:5px;margin-bottom:5px;width:1116px;height:526px;background:#FFF;border:#95B8E7 1px solid;">
                    <div style="overflow:hidden;padding:1px;">
                        <form id="frm_detail"  method="post" class="preference_form">
                               <table id="dtg_detail" class="easyui-edatagrid" title="" iconCls="icon-save" rownumbers="true" toolbar="#toolbar_det" fitColumns="true" singleSelect="true" style="width:1090px;height:431px"> 
                                    <thead>
                                        <tr>
                                            <th field="ExpDetailID" hidden="true" width="10" data-options="align:'left',halign:'center'">ID</th>
                                            <th field="ExpDetailName" width="100" data-options="align:'left',halign:'center'">Item</th>
                                            <th field="ExpDetailDesc" width="200"data-options="align:'left',halign:'center'">Item Description</th>	
                                            <th field="ExpDetailType" width="55"data-options="align:'center',halign:'center'" hidden>ModusType</th>	
                                            <th field="ExpCategoryName" width="200"data-options="align:'left',halign:'center'">Group Name</th>
                                            <th field="ExpCategoryID" width="200"data-options="align:'left',halign:'center'" hidden>ID Category</th>							
                                        </tr>
                                    </thead>
                                </table>
                                
                               <div id="toolbar_det">
                                    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="load_detail_popup(1);">New </a>
                                    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="load_detail_popup(2);">Edit </a>
                                    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="delete_detail();">Delete </a>
                               </div>			
                        </form>
                    </div>			
                </div>

                <div style="padding:5px;text-align:right;width:1110px;">
                    <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-save" onclick="save_new_detail();">Save</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton  form-button" iconCls="icon-cancel" onclick="location.reload();">Cancel</a>
                </div>	
            </div>
            
           <!--Awal View Service Detail-->
            <div id="div_advance_detail" title="Cashbon Account" style="padding:1px 0 1px 0;background:#EEE;">
                <div style="float:left;padding-left:5px;margin-bottom:5px;width:1116px;height:526px;background:#FFF;border:#95B8E7 1px solid;">
                    <div style="overflow:hidden;padding:1px;">
                        <form id="frm_detail" method="post" class="preference_form">
                            <table width="100%" cellspacing="1" cellpadding="1">
                                <tr>
                                    <td valign="middle"><b>Expense Group</b></td>
                                    
                                    <td width="650" height="15" align="left" valign="middle">
                                        <select id="select_expense_group" name="select_expense_group" class="easyui-combobox" style="width:651px;"
                                            data-options = "
                                                valueField:'ExpCategoryID',
                                                textField:'ExpCategoryName',
                                                url:'<?php echo base_url(); ?>setup/preference_expense/ajax_categori_expense_payment'
                                            ">
                                        </select>
                                    </td>
                                </tr>
                            </table>				
                            
                           <table width="100%" cellspacing="1" cellpadding="1">									
                                <tr>
                                    <td valign="middle"><b>Payable Account</b></td>
                                    
                                    <td width="650" height="15" align="left" valign="middle">
                                        <select id="select_expense_payable" name="select_expense_payable" class="easyui-combobox" style="width:651px;" editable="false" required='true'>
                                            <option value=""></option>;
                                            <?php foreach($PayableAkun as $row){
                                                echo"<option value='$row->AccountNumber'>$row->AccountName</option>";
                                            } ?>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td valign="middle"><b>Realized Gained / Loss</b></td>
                                    
                                    <td width="650" height="15" align="left" valign="middle">
                                        <select id="select_realized_account" name="select_realized_account" class="easyui-combobox" style="width:651px;" editable="false" required='true'>
                                            <option value=""></option>
                                            
                                            <?php foreach($RealizedAccount as $row){
                                                echo"<option value='$row->AccountNumber'>$row->AccountName</option>";
                                            } ?>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td valign="middle"><b>UnRealized Gained / Loss</b></td>
                                    
                                    <td width="650" height="15" align="left" valign="middle">
                                        <select id="select_unrealized_account" name="select_unrealized_account" class="easyui-combobox" style="width:651px;" editable="false" required='true'>
                                            <option value=""></option>
                                            
                                            <?php foreach($UnRealizedAccount as $row){
                                                echo"<option value='$row->AccountNumber'>$row->AccountName</option>";
                                            } ?>
                                        </select>
                                    </td>
                                </tr>

                               <tr>
                                    <td valign="middle"><b>Advance Expense</b></td>
                                    <td width="650" height="15" align="left" valign="middle">
                                        <select id="select_expense_account" name="select_expense_account" class="easyui-combobox" style="width:650px;" editable="false" required='true'>
                                            <option value=""></option>
                                            <?php foreach($ExpenseAkun as $row){
                                                echo"<option value='$row->AccountNumber'>$row->AccountName</option>";
                                            } ?>								   
                                        </select>
                                   </td>
                                </tr>
                            </table>
                        </form>
                    </div>			
                </div>
                                
                <div style="padding:5px;text-align:right;width:1110px;">
                    <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-save" onclick="save_detail();">Save</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-cancel" onclick="location.reload();">Cancel</a>
                </div>	
            </div>
            <!--Akhir View Service Detail-->
            
           
        </div>		
    </div>
</div>

<!-- Batas Awal Dialog Services Category Adding -->
<div id="dialog-template-group-expense-add" class="easyui-dialog" style="width:650px; height:190px; padding: 10px" closed="true" buttons="#dlg_category_add_button" data-options="modal:true">
    <form id="frm_category_add" method="post" novalidate>
        <input type="hidden" name="ExpCategoryID" id="hdn_cat_id_add" value="0"/>
        
        <table width="100%" cellpadding="2" cellspacing="0">
            <tr>
                <td width='120'><b>Category Name</b></td>
                <td width='10'>:</td>
                
                <td align="left">
                    <input id="txt_cat_name_add" name="txt_cat_name_add" placeholder="< Item Exp Category Name >" style="width:470px; height:20px; border:1px solid #ccc" required>
                </td>
            </tr>

            <tr>
                <td width='120'><b>Description</b></td>
                <td width='10'>:</td>
                
                <td align="left">
                    <textarea id="txa_cat_desc_add" name="txa_cat_desc_add" placeholder="< Item Exp Category Description >" rows="4" style="resize:none;width:466px;white-space:normal;"></textarea>
                </td>
            </tr>            
        </table>
    </form>
</div>

<!-- Dialog Service Category Button -->
<div id="dlg_category_add_button" style="border:none;">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="modify_category_popup(1);">Add Item</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dialog-template-group-expense-add').dialog('close');">Cancel</a>
</div>
<!-- Batas Akhir Dialog Service Catagory -->

<!-- Batas Awal Dialog Services Category Editing -->
<div id="dialog-template-group-expense-edit" class="easyui-dialog" style="width:650px; height:190px; padding: 10px" closed="true" buttons="#dlg_category_edit_button" data-options="modal:true">
    <form id="frm_category_edit" method="post" novalidate>
        <input type="hidden" name="ExpCategoryID" id="hdn_cat_id_edit"/>
        <input type="hidden" name="ExpCategoryType" id="hdn_cat_type_edit"/>
        
        <table width="100%" cellpadding="2" cellspacing="0">
            <tr>
                <td width='120'><b>Category Name</b></td>
                <td width='10'>:</td>
                
                <td align="left">
                    <input id="txt_cat_name_edit" name="ExpCategoryName" placeholder="< Item Exp Category Name >" style="width:470px; height:20px; border:1px solid #ccc" required>
                </td>
            </tr>

            <tr>
                <td width='120'><b>Description</b></td>
                <td width='10'>:</td>
                
                <td align="left">
                    <textarea id="txa_cat_desc_edit" name="ExpCategoryDesc" placeholder="< Item Exp Category Description >" rows="4" style="resize:none;width:466px;white-space:normal;"></textarea>
                </td>
            </tr>            
        </table>
    </form>
</div>

<!-- Dialog Service Category Editing Button -->
<div id="dlg_category_edit_button" style="border:none;">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="modify_category_popup(2);">Edit Item</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dialog-template-group-expense-edit').dialog('close');">Cancel</a>
</div>
<!-- Batas Akhir Dialog Service Catagory editing -->

<!-- Dialog Template Group Detail -->
<div id="dialog-template-item-group-detail" class="easyui-dialog" style="width:450px;height:250px;padding:10px 20px" closed="true" buttons="#dialog-buttons-group-detail" data-options="modal:true">
    <form id="form_template_item_group_detail-add" method="post" novalidate>
        <input type="hidden" id="cat_id" name="cat_id" value="0"/>	
        <input type="hidden" name="ExpCategoryID" id="ExpCategoryID_Add"/>
        <input type="hidden" name="ExpCategoryName" id="ExpCategoryName_Add"/>
        	
        <table width="100%" cellpadding="2" cellspacing="0">
            <tr>
                <td><b>Group</b></td>
                <td>:</td>
                
                <td width="650" height="15" align="left" valign="middle">
                    <select id="select_expense_catgroup" name="select_expense_catgroup" class="easyui-combobox" style="width:250px;"
                        data-options = "
                            valueField:'ExpCategoryID',
                            textField:'ExpCategoryName',
                            url:'<?php echo base_url(); ?>setup/preference_expense/ajax_categori_expense_payment'
                        ">
                    </select>
                </td>
            </tr>
                                
            <tr>
                <td><b>Item Name</b></td>
                <td>:</td>
                <td align="right">
                    <input id="item_name_detail_dt" name="ExpenseDetName"  placeholder="< Item Name >" style="width:100%;height:20px; border:1px solid #ccc">
                </td>
            </tr>

            <tr>
                <td valign="top"><b>Description</b></td>
                <td valign="top">:</td>
                <td align="right">
                    <textarea class="textbox" name="ExpenseDetDesc" id="txa_desc_item_name_dt" rows="4" style="resize:none;width:100%;white-space:normal;"></textarea>
                </td>
            </tr>
        </table>
    </form>
</div>

<!-- Dialog Button -->
<div id="dialog-buttons-group-detail" style="border:none;">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="modify_category_popup_detail(1);">Save</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dialog-template-item-group-detail').dialog('close');">Cancel</a>
</div>

<!-- Dialog Template Group Detail -->
<div id="dialog-edit-item-group-detail-edit" class="easyui-dialog" style="width:450px;height:250px;padding:10px 20px;" closed="true" buttons="#edit-buttons-group-detail" data-options="modal:true">
    <form id="form_edit_item_group_detail" method="post" novalidate>
    <input type="hidden" name="ExpCategoryID" id="ExpCategoryID_Edit"/>
        <input type="hidden" name="ExpCategoryName" id="ExpCategoryName_Edit"/>
        <table width="100%" cellpadding="2" cellspacing="0">
        	<tr>
                <td><b>Group</b></td>
                <td>:</td>
                
                <td width="650" height="15" align="left" valign="middle">
                    <select id="ExpCategoryID" name="ExpCategoryID" class="easyui-combobox" style="width:250px;"
                        data-options = "
                            valueField:'ExpCategoryID',
                            textField:'ExpCategoryName',
                            url:'<?php echo base_url(); ?>setup/preference_expense/ajax_categori_expense_payment'
                        ">
                    </select>
                </td>
            </tr>
            
            <tr>
        	<td><b>Item Name</b></td>
                <td>:</td>
                <td align="right">
                    <input id="detail_id" type="hidden" name="ExpDetailID">
                    <input type="hidden" name="ExpDetailType" id="hdn_det_type_edit"/>
                    <input id="detail_name" name="ExpDetailName"  placeholder="Item Name" style="width:100%; height:20px; border:1px solid #ccc">
                </td>
            </tr>

            <tr>
        	<td valign="top"><b>Description</b></td>
                <td valign="top">:</td>
                <td align="right">
                    <textarea class="textbox" name="ExpDetailDesc" id="detail_desc" rows="3" style="width:100%;resize:none;white-space:normal;"></textarea>
                </td>
            </tr>
        </table>
    </form>
</div>

<!-- Dialog edit Button -->
<div id="edit-buttons-group-detail" style="border:none;">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="modify_category_popup_detail(2);">Save</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dialog-edit-item-group-detail-edit').dialog('close');">Cancel</a>
</div>
<!-- Batas Dialog edit Button -->

<script type="text/javascript">
$('#select_expense_catgroup').combobox({
	onSelect: function(rec){			
		$('#ExpCategoryID_Add').val(rec.ExpCategoryID);
		$('#ExpCategoryName_Add').val(rec.ExpCategoryName);
	}
});

$('#ExpCategoryID').combobox({
	onSelect: function(rec){			
		$('#ExpCategoryID_Edit').val(rec.ExpCategoryID);
		$('#ExpCategoryName_Edit').val(rec.ExpCategoryName);
	}
});
	
$(function(){
	var dg_group = $('#dtg_category').datagrid({remoteFilter:true,type:'post',url:'<?php echo base_url(); ?>setup/preference_expense/ajax_categori_expense'});
	$('#dtg_category').datagrid('enableFilter');
	
	var dg_detail = $('#dtg_detail').datagrid({remoteFilter:true,type:'post',url:'<?php echo base_url(); ?>setup/preference_expense/ajax_expense_detail'});
	$('#dtg_detail').datagrid('enableFilter');
});
	
$('#select_expense_group').combobox({
	onSelect: function(rec){			
		var category_id = rec.ExpCategoryID;
		
		if(category_id !== 0){
			var uri = '<?php echo base_url(); ?>setup/preference_expense/ajax_expense_detail/';

			$('#dtg_detail').datagrid({
				url: uri + category_id
			});
			
			$.ajax({
				type    : "POST",
				url     : "<?php echo base_url(); ?>setup/preference_expense/combo_list_payable_account",
				data    : "category_id=" + category_id,
				cache   : false,

				success : function(result){
					var result = eval('('+result+')');
					$("#select_expense_payable").combobox('select', result.AccountNumber);
				}
			});
			
			$.ajax({
				type    : "POST",
				url     : "<?php echo base_url(); ?>setup/preference_expense/combo_list_realized_account",
				data    : "category_id=" + category_id,
				cache   : false,

				success : function(result){
					var result = eval('('+result+')');
					$("#select_realized_account").combobox('select', result.AccountNumber);
				}
			});
			
			$.ajax({
				type    : "POST",
				url     : "<?php echo base_url(); ?>setup/preference_expense/combo_list_unrealized_account",
				data    : "category_id=" + category_id,
				cache   : false,

				success : function(result){
					var result = eval('('+result+')');
					$("#select_unrealized_account").combobox('select', result.AccountNumber);
				}
			});
			
			$.ajax({
				type    : "POST",
				url     : "<?php echo base_url(); ?>setup/preference_expense/combo_list_expense_account",
				data    : "category_id=" + category_id,
				cache   : false,

				success : function(result){
					var result = eval('('+result+')');
					$("#select_expense_account").combobox('select', result.AccountNumber);
				}
			});
		}
	}
});

function load_detail_popup(modus_type){
	if(modus_type == 1){
		modal_open('New Item Expense Detail', 'dialog-template-item-group-detail');
	}
	else {
		var row = $('#dtg_detail').datagrid('getSelected');

		if(row){                    
			
			$('#dialog-edit-item-group-detail-edit').dialog('open').dialog('setTitle', 'Edit Item Expense Detail');
			$('#form_edit_item_group_detail').form('load',row);
		}
	}
}

function modify_category_popup_detail(modus_type){   
	if(modus_type == 1){
		category_id     = $('#cat_id').val();
		category_name   = $('#item_name_detail_dt').val().trim();
		category_desc   = $('#txa_desc_item_name_dt').val();
		group_name      = $('#ExpCategoryName_Add').val().trim();
		group_id      	= $('#ExpCategoryID_Add').val().trim();
			
		action_type     = modus_type;
	}
	else{
		category_id     = $('#detail_id').val();
		category_name   = $('#detail_name').val().trim();
		category_desc   = $('#detail_desc').val();
		group_name      = $('#ExpCategoryName_Edit').val().trim();
		group_id      	= $('#ExpCategoryID_Edit').val().trim();
		action_type     = $('#hdn_cat_type_edit').val();
	}

	if((category_name != '') && (category_desc != '') && (group_name != '')){
		confirm_category_detail(category_id, category_name, category_desc, modus_type, action_type,group_name,group_id);            
	}
	else{
		$.messager.alert('Warning','Item Item Name,Description Dan Group tidak boleh kosong','error');
	}
}

function confirm_category_detail(category_id, category_name, category_desc, modus_type, action_type,group_name,group_id){
	var url             = base_url + 'setup/preference_expense/check_category_name_detail';
	
	var i               = $('#dtg_detail').datagrid('getRows');
	var detail          = JSON.stringify(i);

	var selectedrow     = $('#dtg_detail').datagrid('getSelected');
	var row_index       = $('#dtg_detail').datagrid('getRowIndex', selectedrow);

	$.post(url, {detail:detail, category_id:category_id, category_name:category_name, modus_type:modus_type}, function(result){
		if (result.status == true){
			if(result.flag == true){
				$.messager.alert('Warning', result.message, 'error');
			}
			else {
				$.messager.confirm('Confirmation :', result.message, function(r){
					if(r){
						modify_category_detail(row_index, category_name, category_desc, modus_type, action_type,group_name,group_id);
					}
				});
			}                    
		} 
		else {                
			modify_category_detail(row_index, category_name, category_desc, modus_type, action_type,group_name,group_id);
		}
	},'json');            
}

function modify_category_detail(row_index, category_name, category_desc, modus_type, action_type,group_name,group_id){
	if(modus_type == 1){
		$('#dtg_detail').datagrid('appendRow',{
			ExpDetailName : category_name,
			ExpDetailDesc : category_desc,
			ExpDetailType : modus_type,
			ExpCategoryName : group_name,
			ExpCategoryID : group_id,
			ExpDetailID   : 0
		});

		$('#item_name_detail_dt').val('');
		$('#txa_desc_item_name_dt').val('');
		modal_close('dialog-template-item-group-detail');
	}
	else{
		modus_type = action_type == 1 ? 1 : modus_type;
		
		$('#dtg_detail').datagrid('updateRow',{
		   index:row_index,
		   row: {ExpDetailName  : category_name,
				 ExpDetailDesc  : category_desc,
				 ExpCategoryName : group_name,
			ExpCategoryID : group_id,
				 ExpDetailType  : modus_type}
		});

		$('#detail_name').val('');
		$('#detail_desc').val('');
		modal_close('dialog-edit-item-group-detail-edit');
	}
}

function delete_detail(){        
	var url             = base_url + 'setup/preference_expense/delete_detail';

	var selectedrow     = $('#dtg_detail').datagrid('getSelected');
	var row_index       = $('#dtg_detail').datagrid('getRowIndex', selectedrow);
	
	var exp_detail_id   = selectedrow.ExpDetailID;

	$.messager.confirm('Confirmation :', 'Anda yakin, Anda ingin menghapus item "' + selectedrow.ExpDetailName + '"',  
		function(r){
			if (r){
				$('#dtg_detail').datagrid('deleteRow', row_index);
				
				$.post(url, {exp_detail_id:exp_detail_id}, function(result){
					if (result.status == true){
						$.messager.alert('Success',result.message,'info',function(){
//                                location.reload();						                    
						});				
					} 
					else {
						$.messager.alert('Failed',result.message,'error',function(){
							location.reload();						                    
						});				
					}
				},'json');
			}
		}
);        
}
	
/*===============================================Group=======================================================================*/	
function load_group_popup(modus_type){
	if(modus_type == 1){
    	modal_open('Expense Group','dialog-template-group-expense-add');
    }
    else {
    	var row = $('#dtg_category').datagrid('getSelected');
		 if(row){
			$('#dialog-template-group-expense-edit').dialog('open').dialog('setTitle', 'Edit Expense Category');
			$('#frm_category_edit').form('load',row);
		}       
    }
}

function modify_category_popup(modus_type){   
	if(modus_type == 1){
		category_id     = $('#hdn_cat_id_add').val();
		category_name   = $('#txt_cat_name_add').val().trim();
		category_desc   = $('#txa_cat_desc_add').val();
		action_type     = modus_type;
	}
	else{
		category_id     = $('#hdn_cat_id_edit').val();
		category_name   = $('#txt_cat_name_edit').val().trim();
		category_desc   = $('#txa_cat_desc_edit').val();
		action_type     = $('#hdn_cat_type_edit').val();
	}

	if((category_name != '') && (category_desc != '')){
		confirm_category(category_id, category_name, category_desc, modus_type, action_type);            
	}
	else{
		$.messager.alert('Warning','Item Advance Category Name tidak boleh kosong','error');
	}
}

function confirm_category(category_id, category_name, category_desc, modus_type, action_type){
	var url             = base_url + 'setup/preference_expense/check_category_name';
	
	var i               = $('#dtg_category').datagrid('getRows');
	var detail          = JSON.stringify(i);

	var selectedrow     = $('#dtg_category').datagrid('getSelected');
	var row_index       = $('#dtg_category').datagrid('getRowIndex', selectedrow);

	$.post(url, {detail:detail, category_id:category_id, category_name:category_name, modus_type:modus_type}, function(result){
		if (result.status == true){
			if(result.flag == true){
				$.messager.alert('Warning', result.message, 'error');
			}
			else {
				$.messager.confirm('Confirmation :', result.message, function(r){
					if(r){
						modify_category(row_index, category_name, category_desc, modus_type, action_type);
					}
				});
			}                    
		} 
		else {                
			modify_category(row_index, category_name, category_desc, modus_type, action_type);
		}
	},'json');            
}

function modify_category(row_index, category_name, category_desc, modus_type, action_type){
	if(modus_type == 1){
		$('#dtg_category').datagrid('appendRow',{
			ExpCategoryName : category_name,
			ExpCategoryDesc : category_desc,
			ExpCategoryType : modus_type,
			ExpCategoryID   : 0
		});

		$('#txt_cat_name_add').val('');
		$('#txa_cat_desc_add').val('');
		modal_close('dialog-template-group-expense-add');
	}
	else{
		modus_type = action_type == 1 ? 1 : modus_type;
		
		$('#dtg_category').datagrid('updateRow',{
		   index:row_index,
		   row: {ExpCategoryName  : category_name,
				 ExpCategoryDesc  : category_desc,
				 ExpCategoryType  : modus_type}
		});

		$('#txt_cat_name_edit').val('');
		$('#txa_cat_desc_edit').val('');
		modal_close('dialog-template-group-expense-edit');
	}
}
	
function save_category() {
	var url         = base_url + 'setup/preference_expense/modify_category';

	var i           = $('#dtg_category').datagrid('getRows');
	var detail      = JSON.stringify(i);
	
	$.post(url, {detail:detail}, function(result){
		if (result.status == true){
			$.messager.alert('Success',result.message,'info',function(){
				location.reload();
			});
		} 
		else {                
			$.messager.alert('Error',result.message,'error',function(){
				location.reload();
			});
		}
	},'json');            
}

function delete_category(){        
	var url             = base_url + 'setup/preference_expense/delete_category';

	var selectedrow     = $('#dtg_category').datagrid('getSelected');
	var row_index       = $('#dtg_category').datagrid('getRowIndex', selectedrow);
	
	var exp_category_id = selectedrow.ExpCategoryID;

	$.messager.confirm('Confirmation :', 'Anda yakin, Anda ingin menghapus item "' + selectedrow.ExpCategoryName + '"',  
		function(r){
			if (r){
				$('#dtg_category').datagrid('deleteRow', row_index);
				
				$.post(url, {exp_category_id:exp_category_id}, function(result){
					if (result.status == true){
						$.messager.alert('Success',result.message,'info',function(){
							location.reload();						                    
						});				
					} 
					else {
						$.messager.alert('Failed',result.message,'error',function(){
							location.reload();						                    
						});				
					}
				},'json');
			}
		}
);        
}	

function save_new_detail() {
	var url     = base_url + 'setup/preference_expense/modify_detail_pref';

	var i       = $('#dtg_detail').datagrid('getRows');
	var detail  = JSON.stringify(i);
	
	
	
	$.post(url, {detail:detail}, function(result){
		if (result.status == true){
			$.messager.alert('Success',result.message,'info',function(){
				location.reload();
			});
		} 
		else {                
			$.messager.alert('Error',result.message,'error',function(){
				location.reload();
			});
		}
	},'json'); 
}

function save_detail() {
	var url     = base_url + 'setup/preference_expense/modify_detail';

	var i       = $('#dtg_detail').datagrid('getRows');
	var detail  = JSON.stringify(i);
	
	var categori_expense    = $('#select_expense_group').combobox('getValue');
	
	var payable_expense     = $('#select_expense_payable').combobox('getValue');
		
    var realized_account    = $('#select_realized_account').combobox('getValue');
    var unrealized_account  = $('#select_unrealized_account').combobox('getValue');
	var expense_account  	= $('#select_expense_account').combobox('getValue');
	
	if(payable_expense == '' || realized_account == '' || unrealized_account == '' || expense_account == ''){
		$.messager.alert('Warning','Expense Detail Account tidak boleh kosong','info');
	}
	else{	
		$.post(url, {detail:detail, categori_expense:categori_expense, payable_expense:payable_expense, realized_account:realized_account, unrealized_account:unrealized_account, expense_account:expense_account}, function(result){
			if (result.status == true){
				$.messager.alert('Success',result.message,'info',function(){
					location.reload();
				});
			} 
			else {                
				$.messager.alert('Error',result.message,'error',function(){
					location.reload();
				});
			}
		},'json'); 
	}
}		
</script>