<!---- 
    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";
			}
			
			if($group_user->DeletePrivillage == 1){
				$cek_autority_delete 		= "";
			}
			else{
				$cek_autority_delete 		= "disabled";
			}
			
			if($group_user->JournalPrivillage == 1){
				$cek_autority_journal 		= "";
			}
			else{
				$cek_autority_journal 		= "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">
						<input type="checkbox" class="easyui-checkbox" name="chk_suspended" id="chk_suspended" style="margin-top:0px;margin-right:1px;" onClick="suspend(1)" <?=(!in_array($detail->TransStatus,array(4,6)) || $detail->AppLevel1Status == 1) ? 'disabled' : ''?> <?php echo $cek_autority_delete;?>/>
                    	<span style="display:inline-block;margin-top:2px;<?=(($detail->AppLevel1Status!=0)?'color:gray;':'')?>"><?=(($detail->TransStatus!=3)?"Suspend":"Suspend")?></span>
                        
                        <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>
						 <a href="#" class="easyui-linkbutton" id="refresh-linkbutton" data-options="plain:true,iconCls:'icon-reload'" onclick="location.reload();">Refresh</a>
					</td>	
                    <td align="center" width="40%"><span id="approval_information">
					<?php
                    if($detail->TransStatus == 4){
                        echo get_status_approval_header($detail->TransStatus);
                    }
                    ?>
                    </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">
		<div id="form-wrapper" class="easyui-panel"  style="width:1180%;height:526px;padding:5px;" fit="true">
        <?php if($detail->TransStatus == 2){ ?>
            <div class="blanket"></div>
            <div class="finished-box"><b>Finished</b></div>
        <?php }else if($detail->TransStatus == 3){ ?>
             <div class="blanket"></div>
             <div class="suspend-box"><b>Suspended</b></div>
        <?php
        }
        ?>
        	<form id="fm_daily_production" name="fm_daily_production" method="post">
                <input type="hidden" name="detail" id="detail" />
                <input type="hidden" name="id" value="<?php echo $detail->ProdNumber; ?>"/>
                <input type="hidden" name="detail_id" id="detail_id">
                
                <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>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 height="30px">
                                <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" value="<?php echo convert_to_dmy($detail->ProdDate); ?>" required editable="false" readonly/>							
                                </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 $detail->ProdDesc; ?></textarea>
                                </td>
                            </tr>
                            
                            <tr height="30px">
                            <td valign="top" style="padding-top:4px;"><b>Branch</b></td>
                            <td valign="top" style="padding-top:4px;"><b>:</b></td>
                            <td>
                            	<select name="cmb_branch" id="cmb_branch" readonly
                                    class="easyui-combobox" 
                                    style="width:300px;"
                                    data-options="
                                        value:'<?php echo $detail->BranchFor;?>',
                                        panelWidth:300,
                                        valueField:'BranchCode',
                                        textField:'BranchName',
                                        url:'<?php echo base_url(); ?>list/daily_production_amount/get_branch_data'
                                    " required editable="false" 
                                >
                                </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-<?=(in_array($detail->TransStatus,array(3,1,2)))?'datagrid':'edatagrid'?>" title="" idField="id" url="<?php echo base_url().$menu_link."/ajax_item/".$detail->ProdNumber; ?>"
                             rownumbers="true" fitColumns="true" singleSelect="true" style="height:310px;">
                                <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" data-options="align:'left',halign:'center'">Item</th>
                                        <th field="ItemDesc" 	width="185"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="ItemAmount" 	width="50" editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',groupSeparator:'.',min:1,required:true,disabled:false}}" formatter="formatRp" data-options="align:'center',halign:'center'">Item Amount</th>
                                       <th field="flag" hidden="true" width="30" data-options="align:'left',halign:'center'" editor="{type:'numberbox'}">Flag</th>
                                       <th field="ItemCategoryID" hidden="true" width="30" data-options="align:'left',halign:'center'">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="javascript:$('#dg_item').edatagrid('addRow')">New</a>-->
                                <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="remove_row()">Delete</a>
                                <!--<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" plain="true" onclick="javascript:$('#dg_item').edatagrid('cancelRow')">Cancel</a>-->
                            </div>
                           
                        </div>
                    </div>		
                   
			</form>
        </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" plain="true">Help</a>
                        <a href="javascript:void(0)" class="easyui-linkbutton form-button app-button" iconCls="icon-ok" onclick="approval_prod()" <?=($this->session->userdata('jobtitle_status') != 1 || $detail->TransStatus != 4 || (date('Y-m-d') < date('Y-m-d', strtotime($detail->ProdDate))))?'disabled':''?>>App. Mgr</a>	
                        <?php
						if(($group_user->ReversePrivillage == 1)){
						?>
						<a href="javascript:void(0)" class="easyui-linkbutton form-button app-button" iconCls="icon-reload" onclick="app_reverse()" <?=(($this->session->userdata("jobtitle_status") == 0) && (in_array($detail->TransStatus,array(1,2,8))))?'':'disabled'?>>Reverse</a>  
						<?php
						}
						?>					
                        <a id="save_button" href="javascript:void(0)" class="easyui-linkbutton form-button save-button" iconCls="icon-save" onclick="update_data();" <?=(in_array($detail->TransStatus,array(0,1,2,3)))?'disabled':''?> <?php echo $cek_autority_save;?> >Save</a> 								
                        <a id="print_button" href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-print" onclick="parent.tab_add('Form <?=$menu_label?>', '<?=$menu_link?>', 'icon-print', 'to_print_detail/<?=$detail->ProdNumber?>');" <?=(in_array($detail->TransStatus,array(0,1,2,3,9,8)))?'':'disabled'?>>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\')'?>" <?=(!in_array($detail->TransStatus,array(0,4,6)))?'disabled':''?>><?=($this->session->userdata('jobtitle_status')==0)?'Cancel':'Close'?></a>
		
					</td>
				 </tr>
			</table>				
		</div>
</div>


<?php $this->load->view('v_custom_pop_up');?>
<script type="text/javascript">

/*========================deklar variabel array for remove===================*/
var array_item = [];
/*==============================end=========================================*/
function suspend(type){
	var created_by 		= '<?php echo $detail->CreatedBy;?>';
	var login_as 		= '<?php echo$this->session->userdata('username');?>';
	var job_title 		= '<?php echo $this->session->userdata('jobtitle_status');?>';
	var url             = '<?= base_url();?>custom/custom/checking_suspend';
	
	suspend_check_trans('F', type, '<?=$menu_link?>', $('#txt_Prod_no').val(), 'Initial Inventori Amount', url, created_by, login_as, job_title);
}

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 approval_prod() {
	var url_check           = '<?= base_url();?>custom/custom/checking_approval';
	var no_transaksi    	= $('#txt_Prod_no').val();
	
	checking_approval(no_transaksi,function(check){
		if(check.status == false){
			app_mgr();
		}else{
			$.messager.alert('Unseccessfull',check.message,'error',function(){
				location.reload();
			});
		}
	});
}

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(){	
	$('#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 update_data() {
	var url             = '<?= base_url();?>custom/custom/checking_update';
	var no_transaksi    = $('#txt_Prod_no').val();
	var trans_status 	= '<?=$detail->TransStatus?>';
	
	var created_by 		= '<?php echo $detail->CreatedBy;?>';
	var login_as 			= '<?php echo $this->session->userdata('username');?>';
	var job_title 			= '<?php echo $this->session->userdata('jobtitle_status');?>'; 
	
	$('#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 {
		var i=$('#dg_item').datagrid('getRows');
		var detail = JSON.stringify(i);
		$("#detail").val(detail);
		edit_transaction_check('<?php echo $menu_link;?>', '#fm_daily_production', no_transaksi, url, trans_status,created_by, login_as, job_title);
	}						
}

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

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){
					array_item.push(selectedrow.ProdDetailID);
                   	$("#detail_id").val(JSON.stringify(array_item));
                   	$('#dg_item').datagrid('deleteRow', row_index);
					/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
					$('.save-button').linkbutton('enable');
					/*=================================end============================================================*/
				}
			}
		);        
					
	}            
}

function app_reverse(){
	var no_transaksi    = $('#txt_Prod_no').val();
	checking_reverse(no_transaksi,function(check){
		
		if(check.status == true){
			var i       = $('#dg_item').datagrid('getRows');
			var detail  = JSON.stringify(i);
	
			$("#detail").val(detail);
			reverse_data_by_detail('<?php echo $menu_link?>','#fm_daily_production');
		}else{
		   $.messager.alert('Unseccessfull',check.message,'error',function(){
				location.reload();
			});
		}
	});
}
	
</script>