<!---- 
    Author 	: Ladur
    Tanggal : 12-08-2014	
    Revisi 	:
-->
	
	<?php
			//---------Untuk privellege jika dia ada tombol save----------
			if($group_user->SavePrivillage == 1){
				$cek_autority_save 		= "";
			}
			else{
				$cek_autority_save 		= "disabled";
			}
			//-------------end----------------------------
	?>

<div class="easyui-layout" data-options="fit:true,border:false">
	<!-- 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" iconCls="icon-document" plain="true" onclick="parent.tab_add('New Inventories Initial Amount', '<?=$menu_link?>', 'icon-add', '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">
							<?php echo $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'>
        	<form id="fm_daily_production" name="fm_daily_production" method="post">
				<input type="hidden" name="detail" id="detail" />
                <input type="hidden" name="status_combo_relod" id="status_combo_relod" />
                <input type="hidden" name="status_new" id="status_new" />
                <table cellpadding="0" cellspacing="0" width="100%" height="141">
                <tr>
                	<td width="50%" valign="top">
                    	<table width="100%" cellpadding="0" cellspacing="1">
                        
                        <tr height="30px">
                            <td width="120"><b>Date</b></td>
                            <td ><b>:</b></td>
                            <td>
                                <input name="txt_date" id="txt_date" type="text" class="easyui-datebox validBackward" style="width:302px;text-align:center;" panelWidth="302" required editable="false" value="<?php echo $date_now;?>"/>							
                            </td>
                            
                        </tr>
                        <tr>
                            <td valign="top" style="padding-top:4px;"><b>Description</b></td>
                            <td valign="top" style="padding-top:4px;"><b>:</b></td>
                            <td>
                            	<textarea style="width:296px;resize:none;white-space:normal;" name="txa_description" id="txa_description" rows="4"></textarea>
                            	
                            </td>
                        </tr>
                        <tr height="30px">
                            <td valign="top" style="padding-top:4px;"><b>Area</b></td>
                            <td valign="top" style="padding-top:4px;"><b>:</b></td>
                            <td>
                            	<select class="combo-grid" name="cmb_branch" id="cmb_branch" style="width:300px;height:22px;" editable="true" required
                                    data-options="
                                        panelWidth:400,
                                        idField: 'IDArea',
                                        textField: 'NamaArea',
                                        url:'<?php echo base_url(); ?>list/transfer_item/get_area_data',
                                        method: 'get',
                                        columns: [[
                                            {field:'IDArea',title:'Area No',width:100,halign:'center'},
                                            {field:'NamaArea',title:'Area Name',width:161,halign:'center'}
                                        ]],
                                        onSelect: function(index,row){
                                            $('#status_new').val(1);
                                        },
                                        fitColumns: true
                                    ">
                                </select>
                            </td>
                        </tr>
                        </table>
                	</td>
                </tr>
                <tr>
                	<td width="50%" valign="top">&nbsp;</td>
                </tr>
                </table>
                    
                <!-- Datagrid -->
                <div class="easyui-tabs" style="margin-top:5px" data-options="tabHeight:18">
                    <div id="tab_general" title="Items">
                        <table toolbar="#tb_dg_item" id="dg_item" 
                        	class="easyui-edatagrid" title="" 
                            idField="id" rownumbers="true" 
                            fitColumns="true" singleSelect="true" 
                            style="height:325px;"
                        >
                            <thead>
                            <tr>
                                <th field="ItemCode" 		width="50" 	editor="{type:'item_production_amount',options:{required:true}}" 	data-options="align:'left',halign:'center'">Item</th>
                                <th field="Description" 	width="185" editor="description" 												data-options="align:'left',halign:'center'">Description</th>
                                <th field="ItemQty" 		width="20" 	editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',groupSeparator:'.',required:true,min:1}}" data-options="align:'center',halign:'center'" formatter="formatRp">Qty</th>
                                <th field="ItemUnit" 		width="30" 	editor="unit" 														data-options="align:'left',halign:'center'">Unit</th>
                                
                                <th field="ItemCategoryID" 	width="30" 	editor="ItemCategoryID" 											data-options="align:'left',halign:'center'" hidden>Item Category ID</th>
                                <th field="ItemStatus" 		width="30" 	editor="ItemStatus" 												data-options="align:'left',halign:'center'" hidden>Item Status</th>
                                <th field="ItemType" 		width="30" 	editor="ItemType" 													data-options="align:'left',halign:'center'" hidden>Item Type</th>
                            </tr>
                            </thead>
                        </table>
                        <div id="tb_dg_item">
                            <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="new_grid();">New</a>
                            <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="remove_row();">Delete</a>
                        
                        </div>
                    </div>
                </div>
                <!-- End of Datagrid -->
                
               
        	</form>
		</div>
		<div id="form-footer">
				
		</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-document" onclick="show_legend();" plain="true">Legend</a>	                       
						 <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="insert_data()" <?php echo $cek_autority_save;?>>Save</a> 
                         <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-cancel" onclick="parent.tab_close_this('tt')">Cancel</a>
					</td>
				 </tr>
			</table>				
		</div>
</div>

<!-- Dialog Production Amount-->
<div id="dialog-production-amount" class="easyui-dialog" style="width:850px;height:523px;top:27px;background-color:#F8F8F8;padding:0px" closed="true" data-options="modal:true, iconCls:'icon-save'" buttons="#dialog-productions-amount">
    <form id="form_account" method="post" novalidate>
        <table width="100%" align="left" border="0">
            <tr>
                <td width="308" align="left" valign="middle"><b>Item Category</b></td>
                <td width="10"><b>:</b></td>

                <td colspan="2">
                    <select name="cmb_ItemCategoryID" id="cmb_ItemCategoryID" class="easyui-combobox" style="width:502px;" editable="false" 
                        data-options = "
                            valueField:'ItemCategoryID',
                            textField:'ItemCategoryName',
                            url:'<?php echo base_url(); ?>custom/get_data/itemcategory',
                            onSelect: function(rec){
                                var id = rec.ItemCategoryID;
                                search_production_amount();	
                            }
                        ">
                    </select>
                </td>
            </tr>
            <tr>
                <td width="50" align="left" valign="middle"><b>Item Code</b></td>
                <td><b>:</b></td>

                <td colspan="2">
                    <input type="text" style="width:500px;height:20px" name="txt_ItemCode" id="txt_ItemCode" class="easyui-validatebox" placeholder="< Item Code >" onkeyup="search_production_amount();" />
                </td>
            </tr>

            <tr>
                <td width="50" align="left" valign="middle"><b>Description</b></td>
                <td><b>:</b></td>

                <td colspan="2">
                    <input type="text" style="width:500px;height:20px" name="txt_ItemDesc" id="txt_ItemDesc" class="easyui-validatebox" placeholder="< Item Name >" onkeyup="search_production_amount();" />
                </td>
            </tr>

            <tr>
                <td colspan="3">
                    <table id="tt_choose_production_amount" class="easyui-datagrid" rownumbers="true"
                        url="<?Php echo base_url()?>list/transfer_item/search_production_not_in" 
                        singleSelect="true" fitColumns="true" 
                        pageSize="150" 
                        style="width:828px;height:360px"
                        data-options = "border:true,view:scrollview,autoRowheight:false"
                    >
                        <thead>
                            <tr>
                                <th field="ItemCode" width="150" sortable="true">Item Code</th>
                                <th field="ItemDesc" width="200" sortable="true">Name</th>
                                <th field="ItemCategoryName" width="200" sortable="true">Item Category Name</th>
                                <th field="ItemUnit" width="90" sortable="true">Item Unit</th>
                                
                            </tr>
                        </thead>
                    </table>
                </td>
            </tr>
        </table>
    </form>
</div>

<div id="dialog-productions-amount" style="border:none;">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onClick="choose_initial_amount();">OK</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="modal_close('dialog-production-amount');">Cancel</a>
</div>
<?php $this->load->view('v_custom_pop_up');?>
<script type="text/javascript">
$(document).ready(function(){	
	
	$("#print_button").click(function() { 
		link_print('list/daily_production/print_preview');
	});
	
	item = $('#dg_item').datagrid('getRows');
	$.each(item, function(index,value){
		if(value.ItemCode == choose_production){
			alert();
		}
	});
	
	$('#dg_item').datagrid({
		onBeginEdit:function(rowIndex){
			var editors = $('#dg_item').datagrid('getEditors', rowIndex);
			var qty = $(editors[2].target);
			
			/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
			$('.save-button').linkbutton('disable');
			/*===============================end===========================================================*/
			
			qty.numberbox({
				onChange:function(){
					$('#dg_item').edatagrid('saveRow');
				}
			})
		},
		onEndEdit:function(){
			/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
			$('.save-button').linkbutton('enable');
			/*=================================end============================================================*/
		}
	});
	
	$('#fm_daily_production').click(function(){
		$('#dg_item').edatagrid('saveRow');
	});
	
	datagrid_item_type("dg_item");//untuk warna grid
});	
	
function choose_initial_amount() {
	var selected_data=$('#tt_choose_production_amount').datagrid('getSelected');	

	flag = true;
	item = $('#dg_item').datagrid('getRows');

	$.each(item, function(index,value){
		if(selected_data.ItemCode == value.ItemCode){
			$.messager.alert('Unseccessfull','Item sudah tersedia.','error',function(){	
				$('#tt_choose_production_amount').datagrid('reload'); 	
			});
			
			flag = false;
		}
	});
	
	if(flag != false){
		if(selected_data.ItemTypeID == 1){
			ItemStatus = 1;
		}
		else{
			ItemStatus = 0;
		}
		
		$('.item_production_amount').val(selected_data.ItemCode);
		$('.description').val(selected_data.ItemDesc);
		$('.unit').val(selected_data.ItemUnit);
		$('.ItemCategoryID').val(selected_data.ItemCategoryID);
		$('.ItemStatus').val(ItemStatus);
		$('.ItemType').val(0);
		
		modal_close('dialog-production-amount');
	}
}

function pop_production(branch_hidden){
	$('#tt_choose_production_amount').datagrid('load',{
		branch:branch_hidden
	});
}

function new_grid(){
	
	var combo_branch = $('#status_new').val();
	if(combo_branch != 1){
		$.messager.alert('Unseccessfull','Area Belum Di Pilih','error',function(){	
			$('#dg_item').datagrid('reload');
		})
	}
	else{
		$('#status_combo_relod').val(1);
		$('#dg_item').edatagrid('addRow');
	}
}

function insert_data() {
	$('#dg_item').edatagrid('saveRow');
	var cek = $('#dg_item').datagrid('getRows');
	err_qty = 0;
	
	var category = "";
	
	$.each(cek, function( index, value ) {
		if(parseInt(value.ItemQty) == 0) {
			err_qty++;
		}
	});
	
	if(err_qty > 0) {
		$.messager.alert('Unseccessfull','Qty tidak Boleh bernilai 0.','error',function(){	
			$('#dg_item').datagrid('reload'); 	
		})
	}
	else {
		var i=$('#dg_item').edatagrid('getRows');
		var detail = JSON.stringify(i);
		$("#detail").val(detail);							
		save_transaction('<?php echo $menu_link?>','#fm_daily_production');								
	}								
}

function reset() {
	$('#txa_description').val("");
}

/*====================functiom remove row========================================================*/
function remove_row(){
	var selectedrow     = $('#dg_item').datagrid('getSelected');
	var row_index       = $('#dg_item').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_item').datagrid('deleteRow', row_index);
			}
		}
	);
}
/*================================end===============================================================*/

function search_production_amount(){
	
	searchParameter = {};
		
	searchParameter['cmb_ItemCategoryID'] 	= $('#cmb_ItemCategoryID').combobox('getValue');
	searchParameter['txt_ItemCode'] 		= $('#txt_ItemCode').val();
	searchParameter['txt_ItemDesc'] 		= $('#txt_ItemDesc').val();
	searchParameter['txt_Branch'] 			= $('#cmb_branch').combobox('getValue');
	
	$('#tt_choose_production_amount').datagrid('load',{
		param: searchParameter
	});
}

/*=============onclick saat colom item di klik ======================================================*/
$.extend($.fn.datagrid.defaults.editors, {
	ItemCategoryID: {
        init: function(container, options){
            var input = $('<input type="text" class="ItemCategoryID" style="height:18px;" readonly/>').appendTo(container);
            return input.validatebox(options);
        },
        destroy: function(target){
            $(target).remove();
        },
        getValue: function(target){
            return $(target).val();
        },
        setValue: function(target, value){
            $(target).val(value);
        },
        resize: function(target, width){
            $(target)._outerWidth(width);
        }
    },
	ItemStatus: {
        init: function(container, options){
            var input = $('<input type="text" class="ItemStatus" style="height:18px;" readonly/>').appendTo(container);
            return input.validatebox(options);
        },
        destroy: function(target){
            $(target).remove();
        },
        getValue: function(target){
            return $(target).val();
        },
        setValue: function(target, value){
            $(target).val(value);
        },
        resize: function(target, width){
            $(target)._outerWidth(width);
        }
    },
	ItemType: {
        init: function(container, options){
            var input = $('<input type="text" class="ItemType" style="height:18px;" readonly/>').appendTo(container);
            return input.validatebox(options);
        },
        destroy: function(target){
            $(target).remove();
        },
        getValue: function(target){
            return $(target).val();
        },
        setValue: function(target, value){
            $(target).val(value);
        },
        resize: function(target, width){
            $(target)._outerWidth(width);
        }
    }
});
/*============================================end================================================================*/
</script>