{% extends "data_edit.html" %}
{% load i18n %}

{% block form %}
	{% if request.user|HasPerm:"iaccess.add_accwiegandfmt" or request.user|HasPerm:"iaccess.change_accwiegandfmt" %}
	{% autoescape off %}
	<table>
	<tr>
		<td><label class="required" for="id_wiegand_name">{% trans "名称" %}</label></td>
		<td>{{ form.wiegand_name.as_widget }}</td>
	</tr>		
	<tr id="tr_default_fmt">
		<td><label for="id_default_fmt">{% trans "自动匹配" %}</label></td>
		<td>{{ form.default_fmt.as_widget }}</td>
	</tr>		
	<tr>
		<td align="left"><label class="required" for="id_wiegand_count">{% trans "总位数" %}</label></td>
		<td>{{ form.wiegand_count.as_widget }}</td>
	</tr>
	<tr class="displayN">
		<td colspan="2">{{ form.wiegand_mode.as_widget }}</td>
	</tr>
	<tr height="30px">
		<td olspan="2">
			<div style="padding-top:10px">
				<input type="radio" name="wgMode" id="wgMode1" value="1" />{% trans "模式一" %}
			</div>
		</td>
	</tr>
	<tr>
		<td><label for="id_first_p">{% trans "第一个奇偶校验位(p)" %}</label></td>
		<td>{{ form.first_p.as_widget }}</td>
	</tr>
	<tr>
		<td><label for="id_second_p">{% trans "第二个奇偶校验位(p)" %}</label></td>
		<td>{{ form.second_p.as_widget }}</td>
	</tr>
	</table>
	<table>
		<tr>
			<th colspan="2">
				<table height="60px" id="tb_wiegand_mode_1" class="wg_table_inner_mode_one">
					<tr>
						<td colspan="2">{% trans "偶校验(e)" %}</td>
						<td colspan="2">{% trans "奇校验(o)" %}</td>
						<td colspan="2">{% trans "CID(c)" %}</td>
						<td colspan="2">{% trans "设备代码(f)" %}</td>
						<td colspan="2">{% trans "区位码(s)" %}</td>
						<td colspan="2">{% trans "厂商代码(m)" %}</td>
					</tr>
					<tr>
						<td>{% trans "起始位" %}</td>
						<td>{% trans "长度" %}</td>
						<td>{% trans "起始位" %}</td>
						<td>{% trans "长度" %}</td>
						<td>{% trans "起始位" %}</td>
						<td>{% trans "长度" %}</td>
						<td>{% trans "起始位" %}</td>
						<td>{% trans "长度" %}</td>
						<td>{% trans "起始位" %}</td>
						<td>{% trans "长度" %}</td>
						<td>{% trans "起始位" %}</td>
						<td>{% trans "长度" %}</td>
					</tr>
					<tr>
						{{ form.even_parity_start|field_as_td_h_special }}
						{{ form.even_parity_count|field_as_td_h_special }}
						{{ form.odd_parity_start|field_as_td_h_special }}
						{{ form.odd_parity_count|field_as_td_h_special }}
						{{ form.cid_start|field_as_td_h_special }}
						{{ form.cid_count|field_as_td_h_special }}
						{{ form.facility_code_start|field_as_td_h_special }}
						{{ form.facility_code_count|field_as_td_h_special }}
						{{ form.site_code_start|field_as_td_h_special }}
						{{ form.site_code_count|field_as_td_h_special }}
						{{ form.manufactory_code_start|field_as_td_h_special }}
						{{ form.manufactory_code_count|field_as_td_h_special }}
					</tr>
				</table>
			</th>
		</tr>
	</table>
	<table>
		<tr height="30px">
			<td olspan="2">
				<div style="padding-top:10px">
					<input type="radio" name="wgMode" id="wgMode2" value="2" />{% trans "模式二" %}
				</div>
			</td>
		</tr>
	</table>
	<table width="750px" class="wg_table_inner_mode_two">
		<tr>
			<td width="150px"><label for="id_before_fmt">{% trans "卡校验格式" %}</label></td>
			<td>{{ form.before_fmt.as_widget }}</td>
		</tr>
		<tr>
			<td><label for="id_after_fmt">{% trans "奇偶校验格式" %}</label></td>
			<td>{{ form.after_fmt.as_widget }}</td>
		</tr>
	</table>
	
	{% if form.non_field_errors %}
		<tr><td>{{ form.non_field_errors }}</td></tr>
	{% endif %}

	{% endautoescape %}
	{% endif %}<!--add/change_accwiegandfmt-->
{% endblock %}

{% block addjs %}
	//新增默认韦根格式
	if($("#id_model_pk").val() == "None" || $("#id_model_pk").val() > 10)
	{
		$("#tr_default_fmt").hide();
	}
	else
	{
		$("#id_wiegand_name").attr("readonly", "true");//名称不让更改
		$("#id_wiegand_count").attr("readonly", "true");//总位数不让更改
	}
	
	// 限制只能是数字
	$("#id_wiegand_count").attr("maxlength", "2");
	$("#id_first_p").attr("maxlength", "2");
	$("#id_second_p").attr("maxlength", "2");
	$("#id_first_p").attr("class", "wZBaseIntegerField");
	$("#id_second_p").attr("class", "wZBaseIntegerField");
	$("#tb_wiegand_mode_1").find("input").each(function(){
	    $(this).attr("class", "wZBaseIntegerField");
		$(this).attr("maxlength", "2");
	});
	
	
	// 调整长度
	$("#id_before_fmt").attr("style", "width:550px");
	$("#id_after_fmt").attr("style", "width:550px");
	
	$("input[name^='wiegand_mode']").each(function(){
		if($(this).attr("checked"))
		{
			if($(this).val() == 1)
			{
				$("#wgMode1").click();
			}
			else
			{
				$("#wgMode2").click();
			}
		}
	});
	
	$("input[name^='wgMode']").click(function(){
		if($(this).val() == 1)
		{
			$("#id_wiegand_mode_0").click();
		}
		else
		{
			$("#id_wiegand_mode_1").click();
		}
	});
	
	var before_submit = function(){
		var wgMode = "";
		$("input[name^='wgMode']").each(function(){
			if($(this).attr("checked"))
			{
				wgMode = $(this).val();
			}
		});
	
		if(wgMode == 1)
		{
			// 模式一暂不做判断，后续需要补充
		}
		else
		{
			if($("#id_before_fmt").val().length != $("#id_wiegand_count").val())
			{
				$("#id_info").html("").append('<ul class="errorlist"><li>{% trans "卡校验格式长度必须等于总位数!" %}</li></ul>');
				return false;
			}
			if($("#id_after_fmt").val().length != $("#id_wiegand_count").val())
			{
				$("#id_info").html("").append('<ul class="errorlist"><li>{% trans "奇偶校验格式长度必须等于总位数!" %}</li></ul>');
				return false;
			}
		}
		return true;
	}
	
{% endblock %}

