{% extends "special_menu.html" %}
{% load i18n %}
{% block content %}
<form id="id_edit_form" action="" method="post">
    <table class="content_tbl" style='width:97%; margin-left:5px;'>
        <tbody>
        <tr><td><div class='div_box1 floatL' style='width:98%'><h2>{% trans '邮箱设置' %}</h2>
            <table id='tbl1'><tbody>
                <tr>
                    <th width="200px"><label for="id_email_address" class="required">{% trans '邮箱地址' %}:</label></th>
                    <td width="140px"><input id="id_email_address" name="email_address" class="required" type="text" value="{{att_cfg.email_address}}"/></td>
                    <td></td>
                </tr>
                <tr>
                    <th width="200px"><label for="id_email_host" class="required">{% trans '邮件服务器' %}(SMTP):</label></th>
                    <td width="140px"><input id="id_email_host" name="email_host" class="required" type="text" value="{{att_cfg.email_host}}"/></td>
                    <td></td>
                </tr>
                <tr>
                    <th width="200px"><label for="id_email_port" class="required">{% trans '服务器端口' %}:</label></th>
                    <td width="140px"><input id="id_email_port" name="email_port" class="required" type="text" value="{{att_cfg.email_port}}"/></td>
                    <td></td>
                </tr>
                <tr>
                    <th width="200px"><label for="id_email_host_user" class="required">{% trans '账户' %}:</label></th>
                    <td width="140px"><input id="id_email_host_user" name="email_host_user" class="required" type="text" value="{{att_cfg.email_host_user}}"/></td>
                    <td></td>
                </tr>
                <tr>
                    <th width="200px"><label for="id_email_host_password" class="required">{% trans '密码' %}:</label></th>
                    <td width="140px"><input id="id_email_host_password" name="email_host_password" class="required" type="password" value="{{att_cfg.email_host_password}}"/></td>
                    <td></td>
                </tr>
                <tr>
                    <th width="200px"><label for="id_email_use_tls" class="required">{% trans '是否使用安全连接' %}(TLS):</label></th>
                    <td width="140px"><select name="email_use_tls" id="id_email_use_tls" class="required">
                        <option value="0" {% ifequal att_cfg.email_use_tls  0 %}selected{% endifequal %}>{% trans '否' %}</option>
                        <option value="1" {% ifequal att_cfg.email_use_tls  1 %}selected{% endifequal %}>{% trans '是' %}</option>
                    </select></td>
                    <td></td>
                </tr>
            </tbody></table>
        </div></td></tr>
        <tr><td><div class='div_box1 floatL' style='width:98%'><h2>{% trans '报警设置' %}</h2>
            <table id="tbl2"><tbody>
                <tr>
                    <th><label for="id_late_count" class="required">{% trans '迟到超过' %}</label></th>
                    <td><input id="id_late_count" name="late_count"  type="text"  value="{{ att_cfg.late_count }}"/></td>
                    <td style="margin-left: -10px"><span>{% trans '次,邮件提醒' %}</span></td>
                </tr>
                <tr>
                    <th><label for="id_early_count" class="required">{% trans '早退超过' %}</label></th>
                    <td><input id="id_early_count" name="early_count" type="text" value="{{ att_cfg.early_count }}"/></td>
                    <td><span>{% trans '次,邮件提醒' %}</span></td>
                </tr>
                <tr>
                    <th><label for="id_absent_count" class="required">{% trans '旷工超过' %}</label></th>
                    <td><input id="id_absent_count" name="absent_count" type="text" value="{{ att_cfg.absent_count }}"/></td>
                    <td><span>{% trans '次,邮件提醒' %}</span></td>
                </tr>
            </tbody> </table>
        </div></td></tr>
        <tr><td colspan="3"><span id="id_error" style="display:none;"></span></td></tr>
        <tr>
            <td colspan="3">
                <div class='editformbtn'>
                    <div class='lineH22'>
                        <span class='action_OK'>&nbsp;</span>
                        <a id='id_submit'class='Link_blue1' href='javascript:void(0)' >{%trans '确定' %}</a>
                        <div class='clear'></div>
                    </div>
                </div>
            </td>
        </tr>
        </tbody>
    </table>
</form>
{% endblock %}
{% block getdatalist %}
{% endblock %}
{% block addjs %}
    $('#id_edit_form').validate({
        rules:{
            "email_port":{required:true,digits:true,min:1,max:65535},
            "late_count":{required:true,digits:true,min:0},
            "early_count":{required:true,digits:true,min:0},
            "absent_count":{required:true,digits:true,min:0},
        },
        errorPlacement: function(error, element) {
            error.appendTo(element.parent());
        }
    });
    function formValid(){
        var email_check = ($("#id_email_address").val()).match("^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$");
        if($("#id_email_address").val() != '' && email_check == null)
        {
            alert(gettext("邮箱地址格式不正确"));
            return false;
        }
        //var email_user_check = ($("#id_email_host_user").val()).match("^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$");
        //if($("#id_email_host_user").val() != '' && email_user_check == null)
        //{
        //    alert(gettext("邮箱地址格式不正确"));
        //    return false;
        //}
        return true;
    };
    $('#id_submit').click(function(){
        $("#id_error").empty();
        if($("#id_edit_form").valid()){
            if(!formValid()){
                return ;
            }
            $("#id_edit_form").ajaxSubmit({
                url:"/att/set_att_cfg_options/",
                type:"POST",
                dataType:"json",
                async: true,
                success:function(callback){
                    $("#id_error").attr("style", "display:block");
                    if (callback.ret == 'ok')
                    {
                        $("#id_error").html('<ul class="successlist"><li><font color="blue">'+gettext("保存成功")+'</font></li></ul>');
                    }else
                    {
                        $("#id_error").html('<ul class="successlist"><li><font color="red">'+callback.ret+'</font></li></ul>');
                    }
                },
                error:function(XMLHttpRequest, textStatus, errorThrown){
                    alert(gettext("服务器处理数据失败，请重试！"));
                }
            })
        }
    });
{% endblock %}
