<!---- 
    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 <?=$menu_label?>', '<?=$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" />
                
                <table cellpadding="0" cellspacing="0" width="100%" height="141">
                <tr>
                	<td width="50%" valign="top">
                    	<table width="100%" cellpadding="0" cellspacing="1">
                        <tr>
                            <td width="120"><b>Prod No.</b></td>
                            <td width="10"><b>:</b></td>
                            <td>
                                <input name="txt_Prod_no" id="txt_Prod_no" type="text" class="easyui-validatebox" style="width:300px;height:18px" required="required" readonly />
                            </td>
                            <!--
                            <td width="100" height="20" align="center" valign="middle"><span style="color: #fff">Item Category</span></td>
                            -->
                        </tr>
                        <tr>
                            <td><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>
                            <!--
                            <td height="20" align="left" valign="top">
                                
                                <select name="cmb_item_category" id="cmb_item_category" class="easyui-combobox" style="width:122px;height:20px" 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_choose_item();										
                                    }
                                " >
                                </select>
                                
                            </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>
                            	<?php
//								$date1 = "2015-04-09";
//								$date2 = "2015-05-07";
//								
//								$diff = abs(strtotime($date2) - strtotime($date1));
//								
//								$years = floor($diff / (365*60*60*24));
//								$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
//								$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
//								
//								printf("%d years, %d months, %d days\n", $years, $months, $days);
								?>
                            </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_prod',options:{required:true}}" data-options="align:'left',halign:'center'">Item</th>
                                <th field="Description" width="185" editor="desc" data-options="align:'left',halign:'center'">Description</th>
                                <th field="ItemQty" width="20" editor="{type:'numberbox',options:{required:true,min:1}}" data-options="align:'center',halign:'center'">Qty</th>
                                <th field="ItemUnit" width="30" editor="unit" data-options="align:'left',halign:'center'">Unit</th>
                                <th hidden="true" field="warehouse_id" editor="warehouse_id" width="50" data-options="align:'left',halign:'center'">Warehouse ID</th>
                            </tr>
                            </thead>
                        </table>
                        <div id="tb_dg_item">
                            <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="javascript:$('#dg_item').edatagrid('addRow')">New</a>
                            <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="javascript:$('#dg_item').edatagrid('destroyRow')">Delete</a>
                        </div>
                    </div>
                </div>
                <!-- End of Datagrid -->
        	</form>
		</div>
	</div>
    <div id="form-footer" data-options="region:'south',border:false">
    	<table width="100%" cellpadding="0" cellspacing="0">
            <tr>
                <td>&nbsp;</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" 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 -->
<div id="production-dialog" 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="#productions-dialog">
    <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_item_category_production" id="cmb_item_category_production" class="easyui-combobox" style="width:502px;" editable="false" 
                        data-options = "
                            valueField:'ItemCategoryID',
                            textField:'ItemCategoryName',
                            url:'<?php echo base_url(); ?>list/daily_production/item_kategori',
                            onSelect: function(rec){
                                var id = rec.ItemCategoryID;
                                search_prod();	
                            }
                        ">
                    </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_item_code_production" id="txt_item_code_production" class="easyui-validatebox" placeholder="< Item Code >" onkeyup="search_prod();	" />
                </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_item_desc_production" id="txt_item_desc_production" class="easyui-validatebox" placeholder="< Item Name >" onkeyup="search_prod();	" />
                </td>
            </tr>

            <tr>
                <td colspan="3">
                    <table id="dg_choose_production" class="easyui-datagrid" 
                        url="<?Php echo base_url()?>list/daily_production/search_prod" 
                        singleSelect="true" fitColumns="true" 
                        pageSize="50" 
                        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="LockStatus" hidden='true'>Status</th>
                            </tr>
                        </thead>
                    </table>
                </td>
            </tr>
        </table>
    </form>
</div>

<div id="productions-dialog" style="border:none;">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onClick="choose_production();">OK</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="modal_close('production-dialog');">Cancel</a>
</div>


<script type="text/javascript">
$(document).ready(function(){	

	$('#txt_Prod_no').val('<?php echo $Prod_number; ?>');
	
	$('#txt_date').datebox({
		onChange:function(){
			url  = base_url + 'list/daily_production/get_prod_number';                
			var prod_af_date   = $('#txt_date').datebox('getValue');
			
			$.post(url, {prod_af_date:prod_af_date}, function(result){
				if (result.status === true){
					$('#txt_Prod_no').val(result.number);
				} 
			},'json');
		}
	});
	
	$("#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);
			
			qty.numberbox({
				onChange:function(){
					$('#dg_item').edatagrid('saveRow');
				}
			})
		}
	});
	
	$('#fm_daily_production').click(function(){
		$('#dg_item').edatagrid('saveRow');
	});
});	

	function search_prod(){
		var item_category=$('#cmb_item_category_production').combobox('getValue');
			if(item_category){
				$('#dg_choose_production').datagrid('load',{
					item_categori : item_category,
					item_code :  $('#txt_item_code_production').val(),	
					item_desc :  $('#txt_item_desc_production').val()	
				});	
			}   
	}
	
	function open_production() { 
	   modal_open('Item','production-dialog');
	}
	
	function choose_production() {
		var selected_data=$('#dg_choose_production').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(){	
					$('#dg_choose_production').datagrid('reload'); 	
				});
				
				flag = false;
			}
		});
		
		if(flag != false){
			$('.item_prod').val(selected_data.ItemCode);
			$('.desc').val(selected_data.ItemDesc);
			$('.unit').val(selected_data.ItemUnit);
			$('.warehouse_id').val(selected_data.ItemWHTypeID);
			modal_close('production-dialog');
		}
	}

	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').datagrid('getRows');
			var detail = JSON.stringify(i);
			$("#detail").val(detail);							
			save_transaction('<?php echo $menu_link?>','#fm_daily_production');								
		}								
	}
	
	function reset() {
		$('#txa_description').val("");
	}
	
	$.extend($.fn.datagrid.defaults.editors, {
            
		item_prod: {
			init: function(container, options){
				var input = $('<input type="text" class="item_prod" onClick="open_production()" 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);
			}
		},
		desc: {
			init: function(container, options){
				var input = $('<input type="text" class="desc" style="width:350px;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);
			}
		},
				
		unit: {
			init: function(container, options){
				var input = $('<input type="text" class="unit" style="width:82px;height:18px;text-align:center;" 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);
			}
		},
					
 
	});
</script>