<?php
/*	
 * Author : Ferry / 31-03-2015	
 */
?>

<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>Cash-Bon Account</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><b>Sales Approval Limit</b></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 style="float:left;padding:0px;width:1125px;height:540px;">
            <div style="overflow:hidden;padding:0px;">
                <table id="tt" class="easyui-datagrid" 
                    url="<?php echo base_url(); ?>setup/preference_approval/list_approval_limit" title="Sales Approval" 
                    iconCls="" rownumbers="true" toolbar="#toolbar" fitColumns="true" singleSelect="true" style="width:auto;height:583px"
                >
                    <thead frozen="true">
                        <tr>
                            <th rowspan="2" field="JobTitleCode" width="380" halign='center'>Job Title Name</th>
                            <th colspan="2">Range Limit</th>
                        </tr>

                        <tr>							
                            <th field="LimitBegin" width="360" formatter="formatRp" align="center">From</th>
                            <th field="LimitEnd" width="360" formatter="formatRp" align="center">To</th>
                        </tr>
                    </thead>								
                </table>
				
                <!-- Toolbar -->
                <div id="toolbar">
                    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="popup_approval_limit();">New </a>
                    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="update();">Edit </a>
                    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="delete_approval_limit();">Delete </a>
                </div>				
            </div>			
        </div>
    </div>
</div>

<!-- Dialog Template approval limit -->
<div id="dialog-template-approval-limit" class="easyui-dialog" style="width:370px;height:175px;padding:10px 20px;" closed="true" buttons="#dialog-buttons-app" data-options="modal:true">
    <form id="form_template_approval-limit" method="post" novalidate>
        <table width="100%" cellpadding="2" cellspacing="0">
            <tr>
                <td><b>Job Title Name</b></td>
                <td>:</td>
        
                <td align="left">
<!--                    <select id="JobTitleCode_id" class="easyui-validatebox" name="JobTitleCode" style="width:101%;height:20px;">
                        <option value="MGR">MGR</option>
                        <option value="SPV">SPV</option>	 						   
                    </select>           -->
                    
                    <select class="easyui-combogrid" id="cmb_jobtitle" name="cmb_jobtitle" style="width:207px; height:23px;" 
                       data-options="
                           panelWidth: 350,
                           idField: 'JobTitleCode',
                           textField: 'JobTitleName',
                           url: '<?=base_url()?>custom/get_data/jobtitle',
                           fitColumns: true,
                           method: 'get',
                           columns: [[
                               {field:'JobTitleCode',title:'Job Title Code',width:110,halign:'center'},
                               {field:'JobTitleName',title:'Job Title Name',width:190,halign:'center'}
                           ]]
                       ">
                    </select>
                </td>
            </tr>
        
            <tr>
        	<td><b>Range Limit</b></td>
                <td>:</td>
                
                <td align="left">
                    <input id="LimitBegin" name="LimitBegin" class="currency" placeholder="< From >" style="width:99.5%; height:20px; border:1px solid #ccc">
                    <input id="LimitBegin_angka" name="LimitBegin_angka" type="hidden">
                </td>
            </tr>
        
            <tr>
        	<td>&nbsp;</td>
                <td>&nbsp;</td>
            
                <td align="left">
                    <input id="LimitEnd" name="LimitEnd" class="currency" placeholder="< To >" style="width:99.5%; height:20px; border:1px solid #ccc">
                    <input id="LimitEnd_angka" name="LimitEnd_angka" type="hidden">
                </td>
            </tr>
        </table>
    </form>
</div>

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

<!-- Dialog edit approval limit -->
<div id="dialog-edit-approval-limit" class="easyui-dialog" style="width:370px;height:175px;padding:10px 20px;" closed="true" buttons="#edit-buttons-app" data-options="modal:true">
    <form id="form_edit_approval-limit" method="post" novalidate>
        <table width="100%" cellpadding="2" cellspacing="0">
            <tr>
        	<td><b>Job Title Name</b></td>
                <td>:</td>

                <td align="right">
                    <select class="easyui-combogrid" id="cmb_jobtitle" name="cmb_jobtitle" editable="false" readonly style="width:207px; height:23px;" 
                        data-options="
                            panelWidth: 350,
                            idField: 'JobTitleCode',
                            textField: 'JobTitleName',
                            url: '<?=base_url()?>custom/get_data/jobtitle',
                            fitColumns: true,
                            method: 'get',
                            columns: [[
                                {field:'JobTitleCode',title:'Job Title Code',width:110,halign:'center'},
                                {field:'JobTitleName',title:'Job Title Name',width:190,halign:'center'}
                            ]]
                       ">
                    </select>
                </td>
            </tr>
        
            <tr>
        	<td><b>Range Limit</b></td>
                <td>:</td>
            
                <td align="right">
                    <input id="LimitBegin_edit" name="LimitBegin" class="currency" style="width:99.5%;height:20px;border:1px solid #ccc">
                    <input id="LimitBegin_angka_edit" name="LimitBegin_angka_edit" type="hidden">
                </td>
            </tr>
        
            <tr>
        	<td>&nbsp;</td>
                <td>&nbsp;</td>
            
                <td align="right">
                    <input id="LimitEnd_edit" name="LimitEnd" class="currency" style="width:99.5%;height:20px;border:1px solid #ccc">
                    <input id="LimitEnd_angka_edit" name="LimitEnd_angka_edit" type="hidden">
                </td>
            </tr>
        </table>
    </form>
</div>

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

<script>	
    function popup_approval_limit(){
        modal_open('Approval Limit','dialog-template-approval-limit');		
    }	
	
    function formatRp(a,b,c,d,e){
        e=function(f){return f.split('').reverse().join('');};b=e(parseInt(a,10).toString());for(c=0,d='';c<b.length;c++){d+=b[c];if((c+1)%3===0&&c!==(b.length-1)){d+='.';}}return'\t'+e(d)+',00';
    }
	
    function save_data(){
        var jobtitle            = $('#cmb_jobtitle').combogrid('getValue');
        var LimitBegin_angka    = $('#LimitBegin_angka').val();
        var LimitEnd_angka      = $('#LimitEnd_angka').val();		
		
        $.ajax({
            type: "POST",
            url: "<?php echo base_url(); ?>setup/preference_approval/proses_post_data_approval_limit",
            data: $.param({ jobtitle:jobtitle, LimitBegin_angka:LimitBegin_angka, LimitEnd_angka:LimitEnd_angka }),
            cache: false,
            success: function(msg){
                msg = eval('('+msg+')');
                if (msg.status == true){
                    $.messager.alert('Success',msg.message,'info',function(){
                        $('#tt').datagrid('reload'); 
                        $('#dialog-template-approval-limit').dialog('close');
                    });
                }else {            
                    $.messager.alert('Error',msg.message,'error',function(){                    
                        $('#dialog-template-approval-limit').dialog('close');
                    });
                }
            }
        });
    }
	
    function save_edit_data(){
        var jobtitle_edit           = $('#form_edit_approval-limit #cmb_jobtitle').combogrid('getValue');
        var LimitBegin_angka_edit   = $('#LimitBegin_edit').val();
        var LimitEnd_angka_edit     = $('#LimitEnd_edit').val();
        $.ajax({
            type: "POST",
            url: "<?php echo base_url(); ?>setup/preference_approval/edit_data_approval_limit",
            data: $.param({ jobtitle_edit:jobtitle_edit, LimitBegin_angka_edit:LimitBegin_angka_edit, LimitEnd_angka_edit:LimitEnd_angka_edit }),
            cache: false,
            success: function(msg){
                msg = eval('('+msg+')');
                if (msg.status == true){
                    $.messager.alert('Success',msg.message,'info',function(){
                        $('#tt').datagrid('reload'); 
                        $('#dialog-edit-approval-limit').dialog('close');
                    });
                }else {            
                    $.messager.alert('Error',msg.message,'error',function(){      
                        $('#dialog-edit-approval-limit').dialog('close');
                    });
                }
            }
        });
    }
	
    function toAngka(rp){return parseInt(rp.replace(/,.*|\D/g,''),10)}

    $(document).ready(function() {        
        $('#LimitBegin').keyup(function() { 
            var LimitBegin = $(this).val();
            var LimitBegin_angka = toAngka(LimitBegin);
	
            if(LimitBegin_angka > 0){
                $('#LimitBegin_angka').val(LimitBegin_angka);
            }
        });
		
        $('#LimitBegin_edit').keyup(function() { 
            var LimitBegin_edit = $(this).val();
            var LimitBegin_angka_edit = toAngka(LimitBegin_edit);
    
            if(LimitBegin_angka_edit > 0){
               $('#LimitBegin_angka_edit').val(LimitBegin_angka_edit);
            }
        });
		
        $('#LimitEnd').keyup(function() { 
            var LimitEnd = $(this).val();
            var LimitEnd_angka = toAngka(LimitEnd);
    
            if(LimitEnd_angka > 0){
               $('#LimitEnd_angka').val(LimitEnd_angka);
            }
        });
		
        $('#LimitEnd_edit').keyup(function() { 
            var LimitEnd_edit = $(this).val();
            var LimitEnd_angka_edit = toAngka(LimitEnd_edit);

            if(LimitEnd_angka_edit > 0){
               $('#LimitEnd_angka_edit').val(LimitEnd_angka_edit);
            }
        });
    });
	
    function update(){
        var row = $('#tt').datagrid('getSelected');

        if(row){
            $('#dialog-edit-approval-limit').dialog('open').dialog('setTitle','Edit Detail');
            $('#form_edit_approval-limit').form('load',row);
			
			job_title = row.JobTitleCode;
			grid = $('#cmb_jobtitle').combogrid('grid').datagrid('getRows');
			$.each(grid, function(index, value){
				if(value.JobTitleCode == job_title){
					$('#form_edit_approval-limit #cmb_jobtitle').combogrid('grid').datagrid('selectRow',index);
				}
			});
        }
    }
	
    function delete_approval_limit(){
        var id = $('#tt').datagrid('getSelected').JobTitleCode;
	
        if(id){
            $.ajax({
                type: "POST",
                url: "<?php echo base_url(); ?>setup/Preference_approval/delete_approval_limit",
                data: $.param({ id:id }),
                cache: false,
                success: function(msg){
                    $('#tt').datagrid('reload'); 
                }
            });
        }
    }
</script>