<script>

$(function () {
	
	$('#managegoods').datagrid({
		url : '<?php echo $url?>manage/goods/get_goods',
		fit : true,
		fitColumns : true,
		striped : true,
		rownumbers : true,
		border : false,
		pagination : true,
	    pageSize:10,
	    pageList: [10, 20, 30,50], 
		toolbar : '#goods_tool',
        singleSelect:true,
		columns : [[
			{
				field : 'goods_id',
				title : '自动编号',
				width : 100,
				checkbox : true,
			},
			{
				field : 'goods_name',
				title : '货品名',
				width : 100,
			},
			{
				field : 'goods_code',
				title : '货品编号',
				width : 100,
			},
			{
				field : 'goods_unit',
				title : '基本单位',
				width : 100,
			},
			{
				field : 'goods_type',
				title : '货品类别',
				width : 100,
				formatter: function(value,row,index){
			        <?php foreach ($type as $key=>$value): ?>               
					if (row.goods_type==<?= $value->id?>){
						return '<?=$value->text?>';
					} 		        
		        <?php endforeach; ?>         						
				}					
			},
			{
				field : 'goods_weight',
				title : '货品重量g',
				width : 100,
			}										
		]]
	});

	<?php foreach ($button as $item): ?>
	<?php if($item->button_way=='look'){ ?>				
	$('#managegoods_look').dialog({
		width : 350,
		title : '查看货品',
		modal : true,
		closed : true,
		iconCls : 'icon-tip',
		buttons : [{
			text : '取消',
			iconCls : 'icon-redo',
			handler : function () {
				$('#managegoods_look').dialog('close').form('reset');
			}
		}],
	});
	<?php }?>	
	<?php if($item->button_way=='add'){ ?>				
	$('#managegoods_add').dialog({
		width : 350,
		title : '新增货品',
		modal : true,
		closed : true,
		iconCls : 'icon-add',
		buttons : [{
			text : '提交',
			iconCls : 'icon-ok',
			handler : function () {
				if ($('#managegoods_add').form('validate')) {
					var obj=$('#managegoods_aproperty').propertygrid('groups');
					var adata={}
					$.each( obj, function(i, n){
						console.log (  n );

						$.each(n.rows,function(r,m){
    							if(m.value==null||m.value==undefined||m.value==""){
    								eval("adata."+m.name+"="+undefined);								
    							}else{
    								eval("adata."+m.name+"='"+m.value+"'");					
    							}
						});
					});
					console.log ( adata );
					
					$.ajax({
						url : '<?php echo $url?>manage/goods/add_goods',
						type : 'post',
						data : adata,
						beforeSend : function () {
							$.messager.progress({
								text : '正在新增中...',
							});
						},
						success : function (data, response, goods) {
							$.messager.progress('close');
							 var data = eval('(' + data + ')');     
							if (data.success) {
								$.messager.show({
									title : '提示',
									msg : '新增货品成功',
								});
								$('#managegoods_add').dialog('close').form('reset');
								$('#managegoods').datagrid('reload');
							} else {
// 								$.messager.alert('新增失败！', '货品名重复或者数据不完整导致失败，请重试！', 'warning');
								$.messager.alert('新增失败！', data.message, 'warning');
							}
						}
					});
				}
			},
		},{
			text : '取消',
			iconCls : 'icon-redo',
			handler : function () {
				$('#managegoods_add').dialog('close').form('reset');
			},
		}],
	});
	
	<?php }?>
	<?php if($item->button_way=='edit'){ ?>				
	
	$('#managegoods_edit').dialog({
		width : 350,
		title : '货品修改',
		modal : true,
		closed : true,
		iconCls : 'icon-edit',
		buttons : [{
			text : '提交',
			iconCls : 'icon-ok',
			handler : function () {
				var obj=$('#managegoods_eproperty').propertygrid('groups');
				var edata={}
				$.each( obj, function(i, n){
					console.log (  n );
					$.each(n.rows,function(r,m){
						if(m.value==null||m.value==undefined||m.value==""){
							eval("edata."+m.name+"="+undefined);								
						}else{
							eval("edata."+m.name+"='"+m.value+"'");					
						}
					});
				});
				console.log ( edata );

				
				$.ajax({
					url : '<?php echo $url?>manage/goods/update_goods',
					type : 'post',
					data : edata,
					beforeSend : function () {
						$.messager.progress({
							text : '正在修改中...',
						});
					},
					success : function (data, response, goods) {
						$.messager.progress('close');
						 var data = eval('(' + data + ')');     
// 							console.log(data);
						if (data.success) {
							$.messager.show({
								title : '提示',
								msg : '修改货品成功',
							});
							$('#managegoods_edit').dialog('close').form('reset');
							$('#managegoods').datagrid('reload');
						} else {
// 							$.messager.alert('修改失败！', '未知错误或没有任何修改，请重试！', 'warning');
							$.messager.alert('修改失败！', data.message, 'warning');
							
						}
					}
				});
			}
		},{
			text : '取消',
			iconCls : 'icon-redo',
			handler : function () {
				$('#managegoods_edit').dialog('close').form('reset');
			},
		}],
	});

	<?php }?>
    <?php endforeach; ?>		
	
	goods_tool = {
		reload : function () {
			$('#managegoods').datagrid('reload');
		},
		remove : function () {
			$('#managegoods').datagrid('unselectAll');
		},
		<?php foreach ($button as $item): ?>
		<?php if($item->button_way=='look'){ ?>			
		look : function () {
			var rows = $('#managegoods').datagrid('getSelections');
			console.log(rows);
			if (rows.length > 1) {
				$.messager.alert('警告操作！', '编辑记录只能选定一条数据！', 'warning');
			} else if (rows.length == 1) {
				$('#managegoods_look').dialog('open');
				$('#managegoods_lproperty').propertygrid({    
				    url: '<?php echo $url?>manage/goods/get_one_goods/'+rows[0].goods_id+'/l',    
				    showGroup: true,    
				    scrollbarSize: 0,	
					columns : [[
								{
									field : 'name',
									title : '字段',
									width : 100,
									sortable:true
								},
								{
									field : 'value',
									title : '值',
									width : 100,
									resizable:false,
									formatter: function(value,rowData,rowIndex){
				         				if(rowData.name=='货品类别'){
				         						function agoodsType(){
	    			         					    var result = true;
	    			         					    $.ajax({
	    			         					    	url : '<?php echo $url?>manage/goods/get_goods_type',
	    			         					        type : "post",
	    			         					        data : {},
	    			         					        async : false,
	    			         					        success : function(data) {
	    			         					        	result = eval(data);
	    			         					        }
	    			         					    });
	    			         					    return result;
	    			         					}			
												var agoodstype=agoodsType();
												for(var o in agoodstype){
													if(value == agoodstype[o].id){
														return agoodstype[o].text;
													}
												}	         					
				         					}else{ 
				         						return value;
					         				}
				         			} 																									
								} 
							]]
				});  								
				
			} else if (rows.length == 0) {
				$.messager.alert('警告操作！', '编辑记录至少选定一条数据！', 'warning');
			}
		},
		<?php }?>		
		<?php if($item->button_way=='add'){ ?>					
		add : function () {
			$('#managegoods_add').dialog('open');
			$('#managegoods_aproperty').propertygrid({    
			    url: '<?php echo $url?>manage/goods/get_goods_attr',    
			    showGroup: true,    
			    scrollbarSize: 0,	
				columns : [[
							{
								field : 'name',
								title : '字段',
								width : 100,
								sortable:true
							},
							{
								field : 'value',
								title : '值',
								width : 100,
								resizable:false,
								formatter: function(value,rowData,rowIndex){
			         				if(rowData.name=='货品类别'){
			         						function agoodsType(){
    			         					    var result = true;
    			         					    $.ajax({
    			         					    	url : '<?php echo $url?>manage/goods/get_goods_type',
    			         					        type : "post",
    			         					        data : {},
    			         					        async : false,
    			         					        success : function(data) {
    			         					        	result = eval(data);
    			         					        }
    			         					    });
    			         					    return result;
    			         					}			
											var agoodstype=agoodsType();
											for(var o in agoodstype){
												if(value == agoodstype[o].id){
													return agoodstype[o].text;
												}
											}	         					
			         					}else{ 
			         						return value;
				         				}
			         			} 																									
							} 
						]]
			});  
		},
		<?php }?>
		<?php if($item->button_way=='edit'){ ?>			
		edit : function () {
			var rows = $('#managegoods').datagrid('getSelections');
			console.log(rows);
			if (rows.length > 1) {
				$.messager.alert('警告操作！', '编辑记录只能选定一条数据！', 'warning');
			} else if (rows.length == 1) {

				$('#managegoods_edit').dialog('open');
				$('#managegoods_eproperty').propertygrid({    
				    url: '<?php echo $url?>manage/goods/get_one_goods/'+rows[0].goods_id+'/e',    
				    showGroup: true,    
				    scrollbarSize: 0,	
					columns : [[
								{
									field : 'name',
									title : '字段',
									width : 100,
									sortable:true
								},
								{
									field : 'value',
									title : '值',
									width : 100,
									resizable:false,
									formatter: function(value,rowData,rowIndex){
				         				if(rowData.name=='货品类别'){
				         						function agoodsType(){
	    			         					    var result = true;
	    			         					    $.ajax({
	    			         					    	url : '<?php echo $url?>manage/goods/get_goods_type',
	    			         					        type : "post",
	    			         					        data : {},
	    			         					        async : false,
	    			         					        success : function(data) {
	    			         					        	result = eval(data);
	    			         					        }
	    			         					    });
	    			         					    return result;
	    			         					}			
												var agoodstype=agoodsType();
												for(var o in agoodstype){
													if(value == agoodstype[o].id){
														return agoodstype[o].text;
													}
												}	         					
				         					}else{ 
				         						return value;
					         				}
				         			} 																									
								} 
							]]
				});  				
				
			} else if (rows.length == 0) {
				$.messager.alert('警告操作！', '编辑记录至少选定一条数据！', 'warning');
			}
		},
		<?php }?>
	    <?php endforeach; ?>		
	};
	
});
</script>
<table id="managegoods"></table>

<div id="goods_tool" style="padding:5px;">
	<div style="margin-bottom:5px;">
	<?php foreach ($button as $item): ?>
    	<a href="#" class="easyui-linkbutton" iconCls="icon-<?=$item->button_icon?>" plain="true" onclick="goods_tool.<?=$item->button_way ?>()"><?php echo $item->button_name?></a>
    <?php endforeach; ?>
		<a href="#" class="easyui-linkbutton" iconCls="icon-reload" plain="true"  onclick="goods_tool.reload();">刷新</a>
		<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="goods_tool.remove();">取消选择</a>		
	</div>
</div>

<form id="managegoods_look" style="margin:0;padding:3px;color:#333;">
	<table id="managegoods_lproperty" style="width:330px"></table>
</form>

<form id="managegoods_add" style="margin:0;padding:3px;color:#333;">
	<table id="managegoods_aproperty" style="width:330px"></table>
</form>


<form id="managegoods_edit" style="margin:0;padding:3px;color:#333;">
		<table id="managegoods_eproperty" style="width:330px"></table>
</form>

