<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增设计师')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-designer-add">
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">昵称：</label>
                    <div class="col-sm-8">
                        <input name="designerName" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">设计师头像：</label>
                    <div class="col-sm-8">
                        <img id="designerAvatarImg" width="30%" height="30%">
                        <input type="file" class="form-control" name="designerAvatarUrl" id="designerAvatarUrl" accept="image/*"/>
                        <input type="hidden" lass="form-control" name="designerAvatar" id="designerAvatar" />
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">设计师描述：</label>
                    <div class="col-sm-8">
                        <textarea name="description" class="form-control" style="width: 455px; height: 300px; resize: none;"></textarea>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "master/designer"
        $('#designerAvatarUrl').change(function () {
            var fileSize = this.files[0].size;
            fileSize = Math.round(fileSize/1024*100)/100;

            if(fileSize>2048){
                layer.msg('上传文件不得大于2M，请重新上传。', {time: 3000, icon:6});
                return false;
            }

            if (!/image\/\w+/.test(this.files[0].type)) {
                layer.msg('上传的不是图片文件，请重新上传。', {time: 3000, icon:6});
                return false;
            }

            if ($(this).val()) {
                var formdata = new FormData();
                formdata.append("imageFile", this.files[0]);
                $.ajax({
                    url: ctx + "common/upload/file/designerAvatar",
                    data: formdata,
                    type: "post",
                    processData: false,
                    contentType: false,
                    beforeSend: function () {
                        $.modal.loading("正在处理中，请稍后...");
                        $.modal.disable();
                    },
                    success: function (result) {
                        if (result.code === web_status.SUCCESS) {
                            $('#designerAvatarImg').attr('src', result.imageUrl);
                            $('#designerAvatar').val(result.imageUrl);
                            $.modal.alertSuccess(result.msg)
                        } else if (result.code === web_status.WARNING) {
                            $.modal.alertWarning(result.msg)
                        } else {
                            $.modal.alertError(result.msg);
                        }
                        $.modal.closeLoading();
                        $.modal.enable();
                    }
                })
            }
        });

        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#form-designer-add").validate({
            rules: {
                designerName: {
                    required: true
                },
                description: {
                    maxlength: 4000
                }
            },
            messages: {
                designerName: icon + "请输入设计者昵称",
                description: icon + "设计者描述最多输入4000字"
            },
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                if ($("#designerAvatar").val() === "") {
                    $.modal.alertError("请上传设计师的头像");
                    return;
                }
                $.operate.save(prefix + "/add", $('#form-designer-add').serialize());
            }
        }
    </script>
</body>
</html>