<!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-ipInfo-edit" th:object="${ipInfo}">
            <input name="ipId" th:field="*{ipId}" 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="ipName" th:field="*{ipName}" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">ip图片：</label>
                    <div class="col-sm-8">
                        <img id="ipUrlImg" width="30%" height="30%">
                        <input type="file" class="form-control" name="ipInfoUrl" id="ipInfoUrl" accept="image/*"/>
                        <input type="hidden" lass="form-control" name="ipUrl" id="ipUrl" />
                    </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="colorNo" th:field="*{colorNo}" 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">
                        <input name="sort" th:field="*{sort}" 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">
                        <input name="delFlag" th:field="*{delFlag}" class="form-control" type="text">
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "master/ipInfo";
        $('#ipInfoUrl').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) {
                            $('#ipUrlImg').attr('src', result.imageUrl);
                            $('#ipUrl').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-ipInfo-edit").validate({
            rules: {
                ipName: {
                    required: true
                },
                colorNo: {
                    required: true,
                    digits: true
                },
                sort: {
                    required: true,
                    digits: true
                }
            },
            messages: {
                ipName: icon + "请输入文创品牌名称",
                colorNo: {
                    required: icon + "请输入使用的色号"
                },
                sort: {
                    required: icon + "请输入排序号"
                }
            },
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                if ($("#ipUrl").val() === "") {
                    $.modal.alertError("请上传文创的简介图片");
                    return;
                }
                $.operate.save(prefix + "/edit", $('#form-ipInfo-edit').serialize());
            }
        }
    </script>
</body>
</html>