/**
 * Created by Weitools.com on 2015/6/25.
 */
require(["jquery", "jqform", "validate","layer"],
    function ($, jqform, validate,layer) {
        $(function () {



            $(".table-content-title").click(function () {
                $(".table-content").attr("data-open", 'off');
                $(this).parents("li").find(".table-content").attr("data-open", 'on');
            });

            $("#checkcode").click(function () {
                $(this).attr('src', '/app-checkcode?t=' + (new Date().getTime()));
                return false;
            });
            $.validator.addMethod("isPhone", function (value, element) {
                var length = value.length;
                var mobile = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})(17[0-9]{1})||(18[0-9]{1})|(19[0-9]{1}))+\d{8})$/;
                return this.optional(element) || (length == 11 && mobile.test(value));
            }, "请填写正确的手机号码");//可以自定义默认提示信息

            $("#MyForm").validate({
                rules: {
                    xingming: {
                        required: true
                    },
                    phone: {
                        isPhone: true,
                    },
                    imgcode: {
                        required: true,
                    },
                    phonecode: {
                        required: true
                    },
                    msg: {
                        required: true,
                        minlength: 6,
                        maxlength: 10000
                    }

                },

                messages: {
                    xingming: {
                        required: '请填写姓名'
                    },
                    phone: {
                        required: '请输入手机号',
                    },
                    imgcode: {
                        required: '输入验证码',
                    },
                    phonecode: {
                        required: '输入验证码',
                    },
                    msg: {
                        required: '留言内容不能为空',
                        minlength: '内容长度不能少于6个字符',
                        maxlength: '内容长度不能超过200个字符'
                    }
                },
                errorElement: "em",
                errorClass: "error",
                errorPlacement: function (e, t) {
                 //   t.parents("li").append(e);
                }
            });





            $("#MyForm").ajaxForm({
                dataType: "json",
                beforeSerialize: function () {
                   /* layer.open({
                        type: 1,
                        title: false,
                        shadeClose: false,
                        closeBtn:false,
                        content: '数据正在提交'
                    });*/
                },
                beforeSubmit: function () {
                    /*layer.open({
                        type: 1,
                        title: false,
                        shadeClose: false,
                        closeBtn:false,
                        content: '数据正在提交'
                    });*/
                    // _alert.loading(2);
                },
                success: function (data) {
                    //   layerm.closeAll();
                    if ($("#MyForm").data("lang")=="cn"){
                        var btn_text=["确定"];
                        var title_text="信息";
                    }else {
                        var btn_text=["OK"];
                        var title_text="Information";
                    }
                    //  alert(returndata);
                    if (data.status == 0) {
                        layer.alert(data.msg,{
                            title:title_text,
                            btn:btn_text
                        });
                    } else if (data.status == 1) {
                        layer.alert(data.msg, {
                            title:title_text,
                            closeBtn: 0,
                            btn:btn_text
                        }, function(){
                            location.href = window.location.href;
                        });
                    }

                    // _alert.loading();

                }
            });
        });
    }
)