<!---- 
    Author 	: Ladur
    Tanggal : 12-08-2014	
    Revisi 	:
-->

<?php
			//---------Untuk privellege jika dia ada tombol save----------
			
			if($group_user->JournalPrivillage == 1){
				$cek_autority_journal 		= "";
			}
			else{
				$cek_autority_journal 		= "disabled";
			}
			
			if($group_user->SavePrivillage == 1){
				$cek_autority_save 		= "";
			}
			else{
				$cek_autority_save 		= "disabled";
			}
			
			if($group_user->DeletePrivillage == 1){
				$cek_autority_delete 		= "";
			}
			else{
				$cek_autority_delete 		= "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-journal" plain="true" onclick="parent.tab_add('Preview Journal <?php echo $menu_label; ?>', '<?php echo $menu_link; ?>', 'icon-print', 'to_preview_journal/<?php echo $detail->ProdNumber ?>')" <?php echo $cek_autority_journal;?>>Journal</a>
						<?php 
							if($detail->TransStatus == 6) {
								$checked_pembatalan = "";
								$class_datagrid = "class='easyui-edatagrid'";
								$btn_save = "";
								$btn_print = "";
								$btn_cancel = "";
								$disable_date = "";
								$disable_desc = "";
								$nilai_chek = 3;
							}
							else if($detail->TransStatus == 3){
								$checked_pembatalan = "checked";
								$class_datagrid = "class='easyui-datagrid'";
								$btn_save = "disabled";
								$btn_print = "";
								$btn_cancel = "disabled";
								$disable_date = "readonly";
								$disable_desc = "readonly";
								$nilai_chek = 6;
							}
							else {
								$checked_pembatalan = "disabled";
								$class_datagrid = "class='easyui-datagrid'";
								$btn_save = "disabled";
								$btn_print = "";
								$btn_cancel = "disabled";
								$disable_date = "readonly";
								$disable_desc = "readonly";
							}
							
							if($this->session->userdata("jobtitle_status")==1) {
								if($detail->TransStatus == 4 || $detail->TransStatus == 2 || $detail->TransStatus == 3 || date('Y-m-d') < date('Y-m-d', strtotime($detail->ProdDate))) {
									$btn_app_mgr = "disabled";
									$btn_app_dir = "disabled";
								}
								else {
									$btn_app_mgr = "";
									$btn_app_dir = "disabled";
								}	
							}
							else if($this->session->userdata("jobtitle_status")==0 ) {
								if($detail->TransStatus == 5 || $detail->TransStatus == 2 || $detail->TransStatus == 3 || date('Y-m-d') < date('Y-m-d', strtotime($detail->ProdDate))) {
									$btn_app_dir = "disabled";
									$btn_app_mgr = "disabled";
								}
								else {
									$btn_app_dir = "";
									$btn_app_mgr = "disabled";
								}
							}
							else {
								$btn_app_dir = "disabled";
								$btn_app_mgr = "disabled";
							}
						?>
						
                        <input type="checkbox" name="chk_suspended" id="chk_suspended" value="3" style="margin-top:0px;margin-right:1px;" <?php echo $checked_pembatalan; ?> <?php echo $btn_save; ?> onclick="suspend(2);"/>
						<span style="display:inline-block;margin-top:2px;<?=(($detail->AppLevel1Status!=6)?'color:gray;':'')?>" <?php echo $cek_autority_delete;?>>Suspend Transaction</span>
					</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">
     <?php if($detail->TransStatus==2){ ?>
        <div class="blanket"></div>
        <div class="paid-box"><b>Paid</b></div>
    <?php }else if($detail->TransStatus==3){ ?>
    	 <div class="blanket"></div>
         <div class="suspend-box"><b>Suspended</b></div>
	<?php
	}
	?>
		<div id="form-wrapper" class="easyui-panel"  style="width:1180%;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="id" id="id" value="<?php echo $detail->ProdNumber; ?>"/>
                <input type="hidden" name="branch_code" id="branch_code" value="<?php echo $detail->BranchCode; ?>"/>
                
                <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" value="<?php echo $detail->ProdNumber; ?>" 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" style="width:302px;text-align:center;" panelWidth="302" value="<?php echo convert_to_dmy($detail->ProdDate); ?>" required editable="false" readonly/>							
                                </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" <?php echo $disable_desc; ?>><?php echo $detail->ProdDesc; ?></textarea>
                                </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" <?php echo $class_datagrid; ?> title="" idField="id" url="<?php echo base_url().$menu_link."/ajax_item/".$detail->ProdNumber."/".$detail->BranchCode; ?>"
                             rownumbers="true" fitColumns="true" singleSelect="true" style="height:325px;">
                                <thead>
                                    <tr>
                                        <th hidden="true" width="10" halign='center' field="ProdDetailID" data-options="align:'center'">ID</th>
                                        <th hidden="true" width="10" halign='center' field="ProdNumber" data-options="align:'center'">Prod</th>
                                        <th field="ItemCode" width="50" editor="{type:'item_prod',options:{required:true}}" data-options="align:'left',halign:'center'">Item</th>
                                        <th field="ItemDesc" 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 field="Modus_Type"       width="20"  data-options="halign:'center',align:'center'" editor="{type:'numberbox'}" hidden>Modus Type</th>
                                        <th field="LockStatus"       width="20"  data-options="halign:'center',align:'center'" editor="{type:'numberbox'}" hidden>Lock Status</th>
                                    </tr>
                                </thead>
                            </table>
                            
                            <div id="tb_dg_item">
                                <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="remove_row()" <?php echo $btn_cancel; ?>>Delete</a>
                            </div>
                            
                        </div>
                    </div>	
			</form>
        </div>
		<div id="form-footer">
			
		</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" plain="true">Help</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton form-button app-button" iconCls="icon-ok" onclick="app_dir()" <?php echo $btn_app_dir; ?> >App. Dir</a>	
                    <a href="javascript:void(0)" class="easyui-linkbutton form-button app-button" iconCls="icon-ok" onclick="app_mgr()" <?php echo $btn_app_mgr; ?>>App. Mgr</a>						
                    <a id="save_button" href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-save" onclick="update_data();" <?php echo $btn_save; ?> <?php echo $cek_autority_save;?>>Save</a> 								
                    <a id="print_button" href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-print" <?php echo $btn_print; ?>>Preview & Print</a>   
                    <a href="javascript:void(0)" class="easyui-linkbutton  form-button" iconCls="icon-cancel" onclick="<?=($this->session->userdata('jobtitle_status')==0)?'suspend(2)':'parent.tab_close(\'tt\')'?>" <?php echo $btn_cancel; ?>>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">

function remove_row(){
	var item = $('#dg_item').datagrid('getData').total;

	if(item <= 1){
		$.messager.alert('Warning','Anda tidak dapat menghapus list item, minimum list item adalah satu (1)','error');
	}
	else {
		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){
					$('#dg_item').datagrid('updateRow',{
						index:row_index,
						row:{Modus_Type         : 3}
					});
					var i       = $('#dg_item').datagrid('getRows');
					var detail  = JSON.stringify(i);
					
					$("#detail").val(detail);
					delete_row('<?php echo $menu_link?>','#fm_daily_production');		
				}
			}
		);        
					
	}            
}
    
function delete_row(url, form_id){
	url = base_url + url;

	$(form_id).form('submit',{
		url: url + '/delete_row',
		
		onSubmit: function(){
			return $(this).form('validate');
		},
		success: function(result){
			var result = eval('('+result+')');

			if (result.status == true){
				$.messager.alert('Success',result.message,'info',function(){						
					$('#dg_item').datagrid('reload');								
				});				
			} 
			else {
				$.messager.alert('Failed',result.message,'error',function(){						
					location.reload();								
				});				
			}		
		}
	});
}


function delete_data(){
	$('#chk_suspended').val('3');
	cancel_data('<?php echo $menu_link; ?>/delete','#fm_daily_production');
}


function app_dir(){
	var i=$('#dg_item').datagrid('getRows');
	var detail = JSON.stringify(i);
	$("#detail").val(detail);
	custom_data('<?php echo $menu_link?>/approval_dir','#fm_daily_production');
}

function app_mgr(){
	var i=$('#dg_item').datagrid('getRows');
	var detail = JSON.stringify(i);
	$("#detail").val(detail);
	custom_data('<?php echo $menu_link?>/approval_mgr','#fm_daily_production');
}

$(document).ready(function(){	
	$("#print_button").click(function() { 
		link_print('list/daily_production/print_preview');
	});
	
	$('#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');
	});
});	
</script>

<script type="text/javascript">
	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');
		}
	}
	
	$.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);
			}
		},
							
		warehouse_id: {
			init: function(container, options){
				var input = $('<input type="text" class="warehouse_id" 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);
			}
		}
 
	});
	
	function update_data() {
		$('#dg_item').edatagrid('saveRow');
		var cek = $('#dg_item').datagrid('getRows');
		
		err = 0;
		var category = "";
		
		$.each(cek, function( index, value ) {
			if(category != value.ItemCode){
				err++;
				category = value.ItemCode;
			}
			
			if(value.ItemQty == 0) {
				$.messager.alert('Error','Tidak Boleh Qty Bernilai 0','error',function(){	
				});
			}
		});
		
		if(err == 0) {
			$.messager.alert('Unseccessfull','Harap di pilih satu Item.','error',function(){	
				$('#dg').datagrid('reload'); 	
			});
		}/*
		else if(err > 1) {
			$.messager.alert('Unseccessfull','Tidak bisa digunakan Untuk Lebih Dari Satu Category Item.','error',function(){	
				$('#dg').datagrid('reload'); 	
			})
		}*/
		else {
			var i=$('#dg_item').datagrid('getRows');
			var detail = JSON.stringify(i);
			$("#detail").val(detail);
			edit_data('<?php echo $menu_link?>','#fm_daily_production');								
		}						
	}
	
	function reset() {
		$('#txa_description').val("");
	}
	
	function suspend(type){
		suspend_data_new('F', type, '<?=$menu_link?>', $('#id').val(), 'Daily Production');
	}
</script>