<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="${pageContext.request.contextPath }/common/admin/css/H-ui.min.css" rel="stylesheet" type="text/css" />
<link href="${pageContext.request.contextPath }/common/admin/css/H-ui.admin.css" rel="stylesheet" type="text/css" />
<link href="${pageContext.request.contextPath }/common/admin/lib/Hui-iconfont/1.0.1/iconfont.css" rel="stylesheet" type="text/css" />
<title>添加返利节点</title>
</head>
<body>
	<div class="pd-20">
		<div id="tab_demo" class="HuiTab">
		  <div class="tabBar cl">
		  	<c:forEach items="${types }" var="type">
				<span>${type.typeName }</span>		  		
		  	</c:forEach>
		  </div>
		  <c:forEach items="${types }" var="type">
		 	<div class="tabCon" lang="${type.typeId }">
		 		<form action="${pageContext.request.contextPath }/admin/templateFlow/updateTemplateFlowInfo" method="post" class="form form-horizontal" lang="${type.typeId }" id="form-member-add${type.typeId }">
		  			<input type="hidden" id="templateId" name="templateId" value="${templateId }"/>
		  			<input type="hidden" class="count" name="count" vlaue="">
		  			<div id="createTable${type.typeId }" class="mt-20"></div>
		  			<div class="col-9" style="padding-top: 10px;margin-left: 50%">
		        		<input id="submit${type.typeId }" class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
		      		</div>
		  		</form>
		 	</div>
		  </c:forEach>
		</div>
  	</div>
<script type="text/javascript" src="${pageContext.request.contextPath }/common/admin/lib/jquery/1.9.1/jquery.min.js"></script> 
<script type="text/javascript" src="${pageContext.request.contextPath }/common/admin/lib/icheck/jquery.icheck.min.js"></script> 
<script type="text/javascript" src="${pageContext.request.contextPath }/common/admin/lib/Validform/5.3.2/Validform.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/common/admin/lib/layer/1.9.3/layer.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/common/admin/lib/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/common/admin/js/H-ui.js"></script> 
<script type="text/javascript" src="${pageContext.request.contextPath }/common/admin/js/H-ui.admin.js"></script>
<script type="text/javascript">
	var count = 0;
	var templateName = '';
	var nameArray = new Array();
	function accDiv(arg1, arg2) {
	    var t1 = 0, t2 = 0, r1, r2;
	    try {
	        t1 = arg1.toString().split(".")[1].length;
	    }
	    catch (e) {
	    }
	    try {
	        t2 = arg2.toString().split(".")[1].length;
	    }
	    catch (e) {
	    }
	    with (Math) {
	        r1 = Number(arg1.toString().replace(".", ""));
	        r2 = Number(arg2.toString().replace(".", ""));
	        return (r1 / r2) * pow(10, t2 - t1);
	    }
	}
  	$(function($) {
  		var templateId = $('#templateId').val();
  		$.ajax({
  		   type: "POST",
  		   url: "/watero/admin/templateFlow/getFlowInfo",
  		   data: {templateId:templateId},
  		   dataType:"json",
  		   success: function(data){
  		   		count = data.count;
  		   		$('.count').val(count);
  		   		templateName = data.templateName;
  		   		nameArray = templateName.split('+');
  		   		createTable(count,5,data);
  		   }
  		});
  	});
  	
  	$(function(){
  		$.Huitab("#tab_demo .tabBar span","#tab_demo .tabCon","current","click","0");
  		<c:forEach items="${types }" var="type">
	  		$("#form-member-add${type.typeId }").Validform({
	  			btnSubmit:"#submit${type.typeId}",
	  			tiptype:2,
	  			beforeCheck:function(curform){
	  				try {
	  					var typeId = $(curform).attr("lang");
		  				var url = $(curform).attr("action");
		  				url = url + "?productType=" + typeId;
		  				$(curform).attr("action", url);
	  			  	}catch(e){
	  				}
	  				return true;
	  			}
	  		});
  		</c:forEach>
  	});
  	
  	//动态创建table
  	function createTable(rowCount, cellCount ,data) {
  		var tabCons = $(".tabCon");
  		for (var k = 0; k < tabCons.length; k=k+1) {
  			// 多个控制面板
  			var key = $(tabCons[k]).attr("lang");
  			// console.log("lang:" + key);
  			var table=$('<table class="table table-border table-bordered table-hover table-bg table-sort">');
			table.appendTo($('#createTable' + key));
			
			// 一行的数据
			for (var i = 0; i < rowCount; i++) {
				var tr = $('<tr></tr>');
				tr.appendTo(table);
				// 一列的数据
				for (var j = 0; j < cellCount; j++) {
					createTd(tr, i, j, data, key);
				}
			}
			tr.appendTo(table);
			$("#createTable" + key).append('</table>');
		}
	}

	//创建table中的td
	function createTd(tr, i, j, data, key) {
		// console.log(i+":" +j + ":" + key + ":" + getJsonValue(getJsonValue(data, key), i+''+j));
		var td = null;
		var str = '';
		// 第一列数据
		if (j == 0) {
			switch (i) {
			case 0:
				str = '<td style="width:100px;text-align:center">一层</td>';
				break;
			case 1:
				str = '<td style="width:100px;text-align:center">二层</td>';
				break;
			case 2:
				str = '<td style="width:100px;text-align:center">三层</td>';
				break;
			case 3:
				str = '<td style="width:100px;text-align:center">四层</td>';
				break;
			case 4:
				str = '<td style="width:100px;text-align:center">五层</td>';
				break;
			case 5:
				str = '<td style="width:100px;text-align:center">六层</td>';
				break;
			case 6:
				str = '<td style="width:100px;text-align:center">七层</td>';
				break;
			case 7:
				str = '<td style="width:100px;text-align:center">八层</td>';
				break;
			}
			td = $(str);
			td.appendTo(tr);
		} else if (j == 1) {
			// 第二列数据
			td = $('<td style="width:100px;text-align:center"><select id="templateName'+key+i+j+'" name="templateName' + i + j + '"></select></td>');
			td.appendTo(tr);
			var num = i + '' + j;
			var property = 0;
			property = getData(num, property, data, key);
			// console.log(i + "," + j + ",p：" + property);
			for (var m = 0; m < nameArray.length; m++) {
				if (nameArray[m] != null && nameArray[m] != '') {
					var roleId = nameArray[m].split(':')[0];
					var roleName = nameArray[m].split(':')[1];
					// console.log(roleId + "," + roleName);
					if (roleId == property) {
						$('<option value="'+ roleId +'" selected="selected">' + roleName + '</option>').appendTo($('#templateName' + key + i + j));
					} else {
						$('<option value="'+ roleId +'">' + roleName  + '</option>').appendTo($('#templateName' + key + i + j));
					}
				}
			}
		} else {
			// 其余都是输入框
			if (((parseInt(count) + parseInt(2)) - (parseInt(i) + parseInt(j))) > 0) {
				var money = 0;
				var num = i + '' + j;
				money = getData(num, money, data, key);
				td = $('<td style="width:100px;text-align:center"><input style="width:50px;text-align:center" type="text" name="flow'
						+ i
						+ j
						+ '" value="'
						+ accDiv(money, 100)
						+ '" datatype="*"  /></td>');
			} else {
				td = $('<td style="width:100px;text-align:center"><input style="width:50px;text-align:center" type="text" value="0" readonly="readonly" name="flow'+ i+j +'" /></td>');
			}
			td.appendTo(tr);
		}
	}

	function getJsonValue(json, name){
	   return json[name];
	}
	
	function getData(num, property, data, key) {
		switch (num) {
		case '01':
			property = getJsonValue(data, key).templateName01;
			break;
		case '11':
			property = getJsonValue(data, key).templateName11;
			break;
		case '21':
			property = getJsonValue(data, key).templateName21;
			break;
		case '31':
			property = getJsonValue(data, key).templateName31;
			break;
		case '41':
			property = getJsonValue(data, key).templateName41;
			break;
		case '51':
			property = getJsonValue(data, key).templateName51;
			break;
		case '61':
			property = getJsonValue(data, key).templateName61;
			break;
		case '71':
			property = getJsonValue(data, key).templateName71;
			break;
		case '02':
			property = getJsonValue(data, key).flow02;
			break;
		case '03':
			property = getJsonValue(data, key).flow03;
			break;
		case '04':
			property = getJsonValue(data, key).flow04;
			break;
		case '12':
			property = getJsonValue(data, key).flow12;
			break;
		case '13':
			property = getJsonValue(data, key).flow13;
			break;
		case '14':
			property = getJsonValue(data, key).flow14;
			break;
		case '22':
			property = getJsonValue(data, key).flow22;
			break;
		case '23':
			property = getJsonValue(data, key).flow23;
			break;
		case '24':
			property = getJsonValue(data, key).flow24;
			break;
		case '32':
			property = getJsonValue(data, key).flow32;
			break;
		case '33':
			property = getJsonValue(data, key).flow33;
			break;
		case '34':
			property = getJsonValue(data, key).flow34;
			break;
		case '42':
			property = getJsonValue(data, key).flow42;
			break;
		case '43':
			property = getJsonValue(data, key).flow43;
			break;
		case '44':
			property = getJsonValue(data, key).flow44;
			break;
		case '52':
			property = getJsonValue(data, key).flow52;
			break;
		case '53':
			property = getJsonValue(data, key).flow53;
			break;
		case '54':
			property = getJsonValue(data, key).flow54;
			break;
		case '62':
			property = getJsonValue(data, key).flow62;
			break;
		case '63':
			property = getJsonValue(data, key).flow63;
			break;
		case '64':
			property = getJsonValue(data, key).flow64;
			break;
		case '72':
			property = getJsonValue(data, key).flow72;
			break;
		case '73':
			property = getJsonValue(data, key).flow73;
			break;
		case '74':
			property = getJsonValue(data, key).flow74;
			break;
		}
		return property;
	}
</script>
</body>
</html>