{% extends "special_menu.html" %}
{% load i18n %}
{% block content %}
<form method="post" onkeydown="if(event.keyCode==13){return false;}" id="id_edit_form" enctype="multipart/form-data">
    <table class="content_tbl" style='width:97%; margin-left:5px;margin-top: 20px'>
        <tbody>
        <tr><td><div class='floatL' style='width:98%'>
            <table id='tbl1'><tbody>
                <tr>
                    <th width="200px" height="30"><label for="id_company_name" >{% trans 'Company Name' %}:</label></th>
                    <td width="140px" height="30"><input id="id_company_name" name="company_name" type="text" value=""/></td>
                    <td width="140px"></td>
                    <td rowspan="7">
                        <div id="image_area" style="border: 1px solid #d5d8d2; width: 200px; height: 75px;">
                        <div class="div_img floatL">
                            <div>
                                <div id="photo_show_area">
                                    <div>
                                        <div id='localImage'>
                                            <img id='id_img_logo' width="200px" height="75px" src="/file/company_logo/company_logo.jpg" onerror="this.src=''"/>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="clear"></div>
                        <div style="position:absolute;">
                            <span class="color_orange" style="padding-right:10px;">{% trans '(最佳尺寸为200×75像素)'%}</span>
                            <br />
                            <div class="file-uploader-wrap">
                                <input id="id_fileUpload" name="fileUpload" type="file" onchange="javascript:setImagePreview();" class="file-uploader"/>
                                <div class="file-uploader-wrap-fake">
                                    <input id="imageLogoButton" value="{% trans '浏览' %}" name="imageLogoButton" type="button" style="display:inline-block;width:120px;height:24px;">
                                </div>
                            </div>
                        </div>
                    </div>
                    </td>
                    <td width="10"></td>
                    <td>
                        <input type="checkbox" id="show_report">
                        <span>{% trans 'Show In Report' %}</span>
                    </td>
                </tr>
                <tr>
                    <th width="200px" height="30"><label for="id_company_email" class="">{% trans 'Email' %}:</label></th>
                    <td width="140px" height="30"><input id="id_company_email" name="company_email" type="text" value=""/></td>
                    <td></td>
                </tr>
                <tr>
                    <th width="200px" height="30"><label for="id_phone_num" class="">{% trans 'Phone Number' %}:</label></th>
                    <td width="140px" height="30"><input id="id_phone_num" name="phone_num" type="text" value=""/></td>
                    <td></td>
                </tr>
                <tr>
                    <th width="200px" height="30"><label for="id_company_country" class="">{% trans 'Country' %}:</label></th>
                    <td width="140px" height="30"><input id="id_company_country" name="company_country" type="text" value=""/></td>
                    <td></td>
                </tr>
                <tr>
                    <td height="10"></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>
    <input id="id_show_in_report" name="show_in_report" type="hidden"/>

</form>
{% endblock %}
{% block getdatalist %}
{% endblock %}
{% block addjs %}
    function setImagePreview(){
        $("#id_error").empty();
        var docObj=document.getElementById("id_fileUpload");
        $(docObj).valid();
        var docObjButton=document.getElementById("imageLogoButton");
        $(docObjButton).valid();
        var imgObjPreview=document.getElementById("id_img_logo");
        if(docObj.files && docObj.files[0])
        {
            if(docObj.value.match(/.jpg|.gif|.png|.bmp/i))
            {
                imgObjPreview.style.display = 'block';
                if (window.navigator.userAgent.indexOf("Chrome") >= 1 || window.navigator.userAgent.indexOf("Safari") >= 1) {
                    imgObjPreview.src = window.webkitURL.createObjectURL(docObj.files[0]);
                }
                else {
                    imgObjPreview.src = window.URL.createObjectURL(docObj.files[0]);
                }
            }else{
                $("#id_error").attr("style", "display:block");
                $("#id_error").html('<ul class="successlist"><li><font color="red">'+gettext("上传的图片格式不正确，请重新选择！")+'</font></li></ul>');
                return false;
            }
        }
        else
        {
             //IE下，使用滤镜
            docObj.select();
            docObj.blur();
            var imgSrc = document.selection.createRange().text;
            if(imgSrc.match(/.jpg|.gif|.png|.bmp/i)) {
                var localImagId = document.getElementById("localImage");
                localImagId.style.width = "80px";
                localImagId.style.height = "80px";
                //图片异常的捕捉，防止用户修改后缀来伪造图片
                try {
                    localImagId.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
                    localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc;
                }
                catch (e) {
                    $("#id_error").attr("style", "display:block");
                    $("#id_error").html('<ul class="successlist"><li><font color="red">'+gettext("上传的图片格式不正确，请重新选择！")+'</font></li></ul>');
                    return false;
                }
                imgObjPreview.style.display = 'none';
                document.selection.empty();
            }else{
                $("#id_error").attr("style", "display:block");
                $("#id_error").html('<ul class="successlist"><li><font color="red">'+gettext("上传的图片格式不正确，请重新选择！")+'</font></li></ul>');
                return false;
            }
         }
         return true;
    }
    function formValid(){
        var email_check = ($("#id_company_email").val()).match("^[a-zA-Z0-9]+([._\\-]*[a-zA-Z0-9])*@([a-zA-Z0-9]+[-a-zA-Z0-9]*[a-zA-Z0-9]+.){1,63}[a-zA-Z0-9]+$");
        if($("#id_company_email").val() != '' && email_check == null)
        {
            $("#id_error").attr("style", "display:block");
            $("#id_error").html('<ul class="successlist"><li><font color="red">'+gettext("邮箱地址格式不正确")+'</font></li></ul>');
            return false;
        }
        return true;
    };
    function initVal(){
        $("#id_error").empty();
        $.ajax({
            url:"/base/get_company_info/",
            type:"Get",
            dataType:"json",
            async: true,
            success: function(callback){
                if(callback){
                    var company_info = callback.company_info;
                    if(company_info.show_in_report == 1){
                        $("#show_report").attr('checked', true);
                    }
                    $("#id_company_name").val(company_info.company_name);
                    $("#id_company_email").val(company_info.company_email);
                    $("#id_phone_num").val(company_info.phone_number);
                    $("#id_company_country").val(company_info.company_country);
                    $("#id_show_in_report").val(company_info.show_in_report);
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                $("#id_error").attr("style", "display:block");
                $("#id_error").html('<ul class="successlist"><li><font color="red">'+gettext("服务器处理数据失败，请重试！")+'</font></li></ul>');
            }
        });
    }
    function initFunction(){
        $('#id_edit_form').validate({
            rules:{
                //"company_name":{required:true},
            },
            errorPlacement: function(error, element) {
                error.appendTo(element.parent());
            }
        });
        $("#show_report").click(function(){
            if($(this).attr('checked')== true){
                $("#id_show_in_report").val(1);
            }else{
                $("#id_show_in_report").val(0);
            }
        });
         $('#id_submit').click(function(){
            $("#id_error").empty();
            if($("#id_edit_form").valid()){
                if(!formValid()){
                    return ;
                }
                $("#id_edit_form").ajaxSubmit({
                    url:"/base/set_company_info/",
                    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){
                        $("#id_error").attr("style", "display:block");
                        $("#id_error").html('<ul class="successlist"><li><font color="red">'+gettext("服务器处理数据失败，请重试！")+'</font></li></ul>');
                    }
                })
            }
        });
        initVal();
    }
    initFunction();
{% endblock %}
{% block add_contents %}
<script>
    $('.file-uploader').focus(function(){
        this.blur();
        this.click();
    })
</script>
{% endblock %}
