<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/webpage/include/taglib.jsp"%>
<html>
<head>
	<title>编号模板管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">
		var validateForm;
		function doSubmit(index1){//回调函数，在编辑和保存动作时，供openDialog调用提交表单。
            var oldSerialNum = $("#oldSerialNum").val();
            var serialNum = $("#serialNum").val();
            if (oldSerialNum!=null&&typeof(oldSerialNum) != undefined&&serialNum!=null&&typeof(serialNum)!=undefined){
                if (serialNum<oldSerialNum){
                    parent.layer.msg("序列号不能小于原来的序列号", {icon: 5});
                    return false;
                }
            }
            if($("#serialTpl").val()==null||$("#serialTpl").val()==''){
                parent.layer.msg("编号模板为空，请先点击'生成模板'键", {icon: 5});
                return false;
            }
		  if(validateForm.form()){
              parent.layer.confirm("编号示例："+$("#serialTplEx").val()+"，确认继续提交吗？",{icon: 3, title:'系统提示'}, function(index){
                    //do something
                  $("#inputForm").submit();
                  parent.layer.close(index);
                  setTimeout(function(){parent.layer.close(index1)}, 100);
                });
              return true;
		  }
	
		  return false;
		}
		$(document).ready(function() {
			validateForm = $("#inputForm").validate({
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});

		});
		
		function setCompInfo(comId) {
            $.ajax({
                type:'post',
                url:'${ctx}/sys/office/compInfo',
                data:{
                    "id":comId
                },
                success:function(data){
                    $("#comSimpleName").val(data.simpleName);
                    $("#branchSimpleName").val(data.branchSimpleName);

                    var values = $("#serialTplVarList select");
                    for (var i=0;i<values.length;i++){
                        if($(values[i]).val()=="1"){
                            var valueId = "#"+$(values[i]).attr("id").replace("name","value");
                            $(valueId).val($("#comSimpleName").val());
                        }else if($(values[i]).val()=="2"){
                            var valueId = "#"+$(values[i]).attr("id").replace("name","value");
                            var valueId2 = "#"+$(values[i]).attr("id").replace("name","ex");
                            $(valueId).val($("#branchSimpleName").val());
                            $(valueId2).val($("#branchSimpleName").val());
                        }
                    }
                }
            })
        }
	</script>
</head>
<body>
<div class="single-form">
	<div class="container">
		<form:form id="inputForm" modelAttribute="serialNumTpl" action="${ctx}/serialnum/serialNumTpl/save" method="post" class="form-horizontal">
		<form:hidden path="id"/>
		<sys:message content="${message}"/>
			<div class="form-group layui-row first lw9">
				<div class="form-group-label"><h2>基础信息</h2></div>
				<div class="layui-item layui-col-sm6">
					<label class="layui-form-label"><span class="require-item">*</span>模板名称：</label>
					<div class="layui-input-block">
						<form:input path="tplName" htmlEscape="false"  maxlength="64"  class="form-control layui-input required"/>
					</div>
				</div>
				<div class="layui-item layui-col-sm6">
					<label class="layui-form-label"><span class="require-item">*</span>公司：</label>
					<div class="layui-input-block with-icon">
						<sys:treeselectComp id="company" name="company.id" value="${serialNumTpl.company.id}" labelName="parent.name" labelValue="${serialNumTpl.company.name}"
											cssStyle="required" title="公司" url="/sys/office/treeDataCompany?type=2" extId="${serialNumTpl.company.id}"  cssClass="form-control layui-input " allowClear="${serialNumTpl.currentUser.admin}"/>
					</div>
				</div>
				<%--<div class="layui-item layui-col-sm6">--%>
					<%--<label class="layui-form-label">公司简称：</label>--%>
					<%--<div class="layui-input-block">--%>
						<%--<input id="comSimpleName" htmlEscape="false" readonly="true" class="form-control layui-input " value="${fns:getSysParamByCompany("company_shortname", serialNumTpl.company.id)}"/>--%>
					<%--</div>--%>
				<%--</div>--%>
				<%--<div class="layui-item layui-col-sm6">--%>
					<%--<label class="layui-form-label">分公司简称：</label>--%>
					<%--<div class="layui-input-block">--%>
						<%--<input id="branchSimpleName" htmlEscape="false" readonly="true"  class="form-control layui-input "--%>
								<%--value="${fns:getSysParamByCompany("branch_shortname", serialNumTpl.company.id)}"--%>
						<%--/>--%>
					<%--</div>--%>
				<%--</div>--%>
				<div class="layui-item layui-col-sm6">
					<label class="layui-form-label"><span class="require-item">*</span>业务标识：</label>
					<div class="layui-input-block">
						<form:select path="bizCode" htmlEscape="false"    class="form-control simple-select required">
							<form:option value="" label=""/>
							<form:options items="${fns:getDictList('serial_biztype')}"  itemLabel="label" itemValue="value" htmlEscape="false"/>
						</form:select>
					</div>
				</div>
				<div class="layui-item layui-col-sm6">
					<label class="layui-form-label">起始序列号：</label>
					<div class="layui-input-block">
						<input type="hidden" name="oldSerialNum" id="oldSerialNum" value="${serialNumTpl.serialNum}">
						<form:input path="serialNum" htmlEscape="false" class="form-control digits layui-input "/>
					</div>
				</div>
				<div class="layui-item layui-col-sm6">
					<label class="layui-form-label"><span class="require-item">*</span>序列号长度：</label>
					<div class="layui-input-block">
						<form:input path="serialNumLen" htmlEscape="false" maxlength="1" class="form-control required digits layui-input "/>
					</div>
				</div>
				<div class="layui-item layui-col-sm6">
					<label class="layui-form-label"><span class="require-item">*</span>编号模板：</label>
					<div class="layui-input-block">
						<form:input path="serialTpl" htmlEscape="false"  readonly="true" class="form-control required layui-input "/>
					</div>
				</div>
				<div class="layui-item layui-col-sm6">
					<label class="layui-form-label"><span class="require-item">*</span>编号示例：</label>
					<div class="layui-input-block">
						<form:input path="serialTplEx" htmlEscape="false"  readonly="true" maxlength="64" class="form-control required layui-input"/>
					</div>
				</div>
			</div>
			<div class="form-group layui-row">
				<div class="form-group-label"><h2>生成编号模板</h2></div>
				<div class="layui-item nav-btns">
					<a class="nav-btn nav-btn-add" onclick="addRow('#serialTplVarList', serialTplVarRowIdx, serialTplVarTpl);" title="新增"><i class="fa fa-plus"></i> 新增</a>
				</div>
				<div class="layui-item layui-col-xs12" style="padding:0 16px;">
					<table id="contentTable" class="table table-striped table-bordered table-condensed can-edit">
						<thead>
						<tr>
							<th class="hide"></th>
							<%--<th width="50" style="text-align: center">序号</th>--%>
							<th style="text-align: center">变量名称</th>
							<th style="text-align: center">变量值</th>
							<th width="80">操作</th>
						</tr>
						</thead>
						<tbody id="serialTplVarList">
						</tbody>
					</table>
					<script type="text/template" id="serialTplVarTpl">//<!--
							<tr id="serialTplVarList{{idx}}">
							<td style="text-align: center">
								<div class="serialTpl_div">
									<select id="serialTplVar{{idx}}_name" class="form-control" onchange="changeVar(this)">
										<option value=""></option>
										<c:forEach items="${fns:getDictList('serial_element')}" var="item">
											<option value="${item.value}">${item.label}</option>
										</c:forEach>
									</select>
								</div>
							</td>
							<td style="text-align: center">
								<input type="text" id="serialTplVar{{idx}}_ex" disabled="true" class="form-control"/>
								<input type="text" id="serialTplVar{{idx}}_value" disabled="true" style="display:none"class="form-control"/>
							</td>
							<td class="text-center op-td">
								{{#delBtn}}<span class="op-btn op-btn-delete" onclick="delRow(this, '#serialTplVarList{{idx}}')" title="删除"><i class="fa fa-trash"></i>&nbsp;删除</span>{{/delBtn}}
							</td>
						</tr>//-->
					</script>
					<script type="text/javascript">
						var serialTplVarRowIdx = 0, serialTplVarTpl = $("#serialTplVarTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
						$(document).ready(function() {
							/*var data = <%--${fns:toJson(workReimbursement.reimbursementVATTaxes)};--%>
							for (var i=0; i<data.length; i++){
								addRow('#reimbursementVATTaxes', reimbursementVATTaxesRowIdx, reimbursementVATTaxesTpl, data[i]);
								reimbursementVATTaxesRowIdx = reimbursementVATTaxesRowIdx + 1;
							}*/
						});

						function changeVar(obj) {
							var val = $(obj).val();
							var thisId = $(obj).attr("id");
							var valueId = "#"+thisId.replace("name","value");
							var exId = "#"+thisId.replace("name","ex");
							$(valueId).attr("disabled",true);
							$(exId).attr("disabled",true);
							switch (val){
								case "1": {
									$(valueId).val("{spnm}");
									$(exId).val($("#comSimpleName").val());
									break;
								}
								case "2": {
									$(valueId).val("{brspnm}");
									$(exId).val($("#branchSimpleName").val());
									break;
								}
								case "3":
								case "4":
								case "5":
								case "6": {
									$(exId).attr("disabled",false);
									break;
								}
								case "7": {
									$(valueId).val("{year}");
									$(exId).val(new Date().getFullYear());
									break;
								}
								case "8": {
									var numLen = $("#serialNumLen").val();
									if (typeof(numLen)!=undefined&&numLen!=null&&numLen>0){
										var exNum = '';
										for (var j=0;j<numLen-1;j++){
											exNum+='0';
										}
										exNum+="1";
										$(exId).val(exNum);
									}else {
										$(exId).val("001");
									}
									$(valueId).val("{serialNum}");
									break;
								}
							}

						}

						function genTpl(obj) {
							var serialTplVal = "";
							var exTpl = "";
							var values = $("#serialTplVarList select");
							if(values==null||values.length==0){
								parent.layer.msg("请先点击'新增'添加模板信息", {icon: 5});
								return;
							}
							for (var i=0;i<values.length;i++){
								var val1 = $(values[i]).val();
								var id = "#"+ $(values[i]).attr("id").replace("name","value");
								var exId = "#"+ $(values[i]).attr("id").replace("name","ex");
								var tplStr = $(id).val();
								var exStr = $(exId).val();
								if (val1==3||val1==4||val1==5||val1==6){
									tplStr = $(exId).val();
								}
								/*switch (val1){
									case "1": {
										tplStr = "{spnm}"
										exTpl =
										break;
									}
									case "2": {
										tplStr = "{brspnm}"
										break;
									}
									case "3":
									case "4":
									case "5":
									case "6": {
										var id = "#"+ $(values[i]).attr("id").replace("name","value");
										tplStr = $(id).val();
										break;
									}
									case "7": {
										tplStr ="{year}";
										break;
									}
									case "8": {
										tplStr ="{serialNum}";
										break;
									}
								}*/
								if(tplStr!=null){
									serialTplVal += tplStr;
								}
								if (exStr!=null){
									exTpl += exStr;
								}
							}
							$("#serialTpl").val(serialTplVal);
							$("#serialTplEx").val(exTpl);
						}

						function addRow(list, idx, tpl, row){
							if (idx==0){
								if($("#companyId").val()==null||$("#companyId").val()==''){
									parent.layer.msg("请先选择公司信息", {icon: 5});
									return;
								}
								if($("#serialNumLen").val()==null||$("#serialNumLen").val()==0){
									parent.layer.msg("请先确认序列号长度", {icon: 5});
									return;
								}
							}

							bornTemplete(list, idx, tpl, row, idx);
							serialTplVarRowIdx +=1;
						}

						function bornTemplete(list, idx, tpl, row, idx1){
							var idx1 = $("#serialTplVarList tr").length +1;
							$(list).append(Mustache.render(tpl, {
								idx: idx, delBtn: true, row: row,
								order:idx1 + 1, idx1:idx1
							}));
							$(list+idx).find("select").each(function(){
								$(this).val($(this).attr("data-value"));
							});
							$(list+idx).find("input[type='checkbox'], input[type='radio']").each(function(){
								var ss = $(this).attr("data-value").split(',');
								for (var i=0; i<ss.length; i++){
									if($(this).val() == ss[i]){
										$(this).attr("checked","checked");
									}
								}
							});
						}

						function delRow(obj, prefix){
							$(obj).parent().parent().remove();
						}
					</script>
				</div>
			</div>
		</form:form>
	</div>
</div>
</body>
</html>