<!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-class-edit" th:object="${prodClass}">
            <input name="prodClassId" th:field="*{prodClassId}" type="hidden">
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">商品分类名称：</label>
                    <div class="col-sm-8">
                        <input name="className" th:field="*{className}" 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="classImg" width="30%" height="30%" th:src="*{classUrl}">
                        <input type="file" class="form-control" name="classImgUrl" id="classImgUrl" accept="image/*"/>
                        <input type="hidden" class="form-control" name="classUrl" id="classUrl" th:field="*{classUrl}" />
                    </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">
                        <input name="sort" th:field="*{sort}" class="form-control" type="text">
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "master/product/class"

        $('#classImgUrl').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/image",
                    data: formdata,
                    type: "post",
                    processData: false,
                    contentType: false,
                    beforeSend: function () {
                        $.modal.loading("正在处理中，请稍后...");
                        $.modal.disable();
                    },
                    success: function (result) {
                        if (result.code === web_status.SUCCESS) {
                            $('#classImg').attr('src', result.imageUrl);
                            $('#classUrl').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();
                    }
                })
            }
        });

        $("#form-class-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-class-edit').serialize());
            }
        }
    </script>
</body>
</html>