<?php include $this->admin_tpl('meta');?>
<title>添加字段 - YzmCMS内容管理系统</title>
</head>
<body>
<nav class="breadcrumb"><i class="yzm-iconfont yzm-iconai-home"></i> 首页 <span class="c-gray en">&gt;</span> 内容管理 <span class="c-gray en">&gt;</span> <?php echo get_model($modelid, 'name');?> <span class="c-gray en">&gt;</span> 字段管理 <span class="c-gray en">&gt;</span> 添加字段 <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新" ><i class="yzm-iconfont yzm-iconshuaxin"></i></a></nav>
<article class="page-container">
<div class="formdoby_info"> 
	  <fieldset>
		<legend><?php echo $modelname ?></legend>
		    <form action="" method="post" onsubmit="return check_all()">
			<table class="table table-border yzmcms_table">			
				<tr>
					<td width="180"><span class="c-red">*</span>字段类型</td> 
					<td>
					<span class="select-box inline">
					<select name="fieldtype" id="fieldtype" class="select" onchange="javascript:field_setting(this.value);">
					<option value='0' selected>请选择字段类型</option>
					<option value="input" >单行文本</option>
					<option value="textarea" >多行文本</option>
					<option value="number" >整数类型</option>
					<option value="decimal" >金额类型</option>
					<option value="datetime" >时间类型</option>
					<option value="image" >图片上传</option>					
					<option value="images" >多图片上传</option>					
					<option value="attachment" >附件上传</option>					
					<option value="attachments" >多附件上传</option>					
					<option value="select" >option下拉框</option>
					<option value="radio" >radio选项卡</option>
					<option value="checkbox" >checkbox多选框</option>
					<option value="editor" >编辑器</option>
					<option value="editor_mini" >编辑器(Mini版)</option>
					</select>
					</span>
                    </td>
				</tr>
				<tr>
					<td><span class="c-red">*</span>字段名</td> 
					<td><input type="text" name="field" value="" class="input-text w_300" id="field" onblur="check_field()"><span class="tip">只能由英文字母、数字和下划线组成，例如：name</span></td>
				</tr>
				<tr>
					<td><span class="c-red">*</span>字段别名</td> 
					<td><input type="text" name="name" value="" class="input-text w_300" id="name" onblur="check_name()"><span class="tip">例如：姓名</span></td>
				</tr>				
				<tr>
					<td>字段提示</td> 
					<td><input type="text" name="tips"  class="input-text w_300"><span class="c-red"></span><span class="tip">显示在字段别名下方作为表单输入提示</span></td>
				</tr>
				<tr style="display:none" id="options">
					<td>选项列表</td> 
					<td><textarea style="height:100px;width:400px;float:left;margin-right:5px" name="setting" id="textarea" class="textarea"></textarea><span style="color:#888">多个选项用 “|” 分开，如：<br>男|女|未知<br><i class="mt-5" style="display:block;">也支持自定义key值，如：<br>0:男|1:女|2:未知</i></span></td>
				</tr>	
				<tr style="display:none" id="dates">
					<td>时间格式</td> 
					<td> 
					 <label><input type="radio" name="dateset" value="0" checked> 日期（<?php echo date('Y-m-d')?>）</label>
					 <br><label><input type="radio" name="dateset" value="1"> 日期+时间（<?php echo date('Y-m-d H:i:s')?>）</label>
					</td>
				</tr>				
				<tr>
					<td>默认值</td> 
					<td><input class="input-text w_300" type="text" value="" name="defaultvalue"></td>
				</tr>				
				<tr>
					<td>字符长度取值范围</td> 
					<td class="yzm-explain-box">最小值：<input class="input-text w_100" type="text" value="0" name="minlength">
                        最大值：<input class="input-text w_100" type="text" value="250" name="maxlength">
                        <span class="yzm-explain">最小值为1则为必填项，为0则非必填项<br>最大值指的是数据库表字段的最大长度</span>
                    </td>
				</tr>	
				<tr>
					<td>数据校验未通过的提示信息</td> 
					<td><input type="text" name="errortips" value="" class="input-text w_300"></td>
				</tr>	
				<tr>
					<td>在会员投稿中显示</td> 
					<td>
					 <label><input type="radio" name="isadd" value="1"> 是</label>
					 &nbsp;&nbsp;<label><input type="radio" name="isadd" value="0" checked> 否</label>
					</td>
				</tr>				
				<tr>
					<td>禁用</td> 
					<td>
					 <label><input type="radio" name="disabled" value="1"> 是</label>
					 &nbsp;&nbsp;<label><input type="radio" name="disabled" value="0" checked> 否</label>
					</td>
				</tr>
				<?php if(isset($select_page)){?>
				<tr>
					<td>应用栏目</td> 
					<td>
						<span class="select-box inline">
					 	<?php echo $select_page; ?>
						</span>
						<span class="c-red ml-5" style="vertical-align:bottom">(按 Ctrl 可以进行多选)</span>
					</td>
				</tr>
				<?php }?>		
                <tr>
					<td></td>
					<td>
					<input class="dosubmit w_100 btn-primary radius" name="dosubmit" type="submit" value="提交">
					<input class="btn w_100 btn-default radius h35" type="button" value="后退" onclick="window.history.go(-1)">
					</td>
				</tr>				
			</table>
            </form>			
	  </fieldset>
</div>
</article>
<?php include $this->admin_tpl('footer');?> 
<script type="text/javascript">
	function field_setting(fieldtype) {
		var isOptionField = ['select', 'radio', 'checkbox'].includes(fieldtype);
		$('#options').css('display', isOptionField ? '' : 'none');
		
		var isDatetimeField = fieldtype === 'datetime';
		$('#dates').css('display', isDatetimeField ? '' : 'none');
		
		var defaultValue = 
		    fieldtype === 'number' ? '0' : 
		    fieldtype === 'decimal' ? '0.00' : 
		    '';
		$('input[name="defaultvalue"]').val(defaultValue);
	}

	function check_name(){ 
		if($("input[name='name']").val()==''){
			layer.msg('字段别名不能为空！', {icon:2});
			return false;
		}else{
			return true;
		}		
	}
	
	function check_field(){ 
		var res = false;	
		var reg = /^[a-zA-Z]{1}([a-zA-Z0-9]|[_]){0,29}$/;	
		var filed = $("input[name='field']").val();
		if(filed.length<1 || filed.length>30){
			layer.msg('字段名长度必须为1-30位！', {icon:2});
			return false;
		}
        if(!reg.test(filed)){
		   layer.msg('字段名必须为英文、数字或下划线！', {icon:2});
		   return false;
		}
		
		$.ajax({   
			type: "get",   
			url: '<?php echo U('public_check_field');?>',   
			dataType: "html",
			async:false,
			data: "field="+$('#field').val()+"&modelid=<?php echo intval($_GET['modelid']);?>",   
			success: function(msg){
			if(msg){
				if(msg==0){
					layer.msg('字段名已经存在！', {icon:2});
				}else if(msg==1){
					res = true;	
				}else{
					layer.alert('Ajax通信失败！');
				}
			}  
			}  
		});
        return res;		
	}
	
	function check_all(){
		var fieldtype = $("select[name='fieldtype']").val();
	    if(fieldtype =='0'){
		   layer.msg('请选择字段类型！', {icon:2});
		   return false;
		}else if(fieldtype=='select' || fieldtype=='radio' || fieldtype=='checkbox'){
			if($('#textarea').val() == ''){
				layer.msg('选项列表不能为空！', {icon:2});
				return false;
			}
		}
		
	    if(!check_field() || !check_name()){
		    return false;
		}
		
	    if(isNaN($("input[name='minlength']").val()) || isNaN($("input[name='maxlength']").val())){
		    layer.msg('字符长度取值范围 只能为数字类型！', {icon:2});
		    return false;
		}		
        return true;			
	}
</script> 
</body>
</html>