<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改推荐商品')" />
    <th:block th:include="include :: select2-css" />
    <th:block th:include="include :: bootstrap-select-css" />
    <th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-prod-edit" th:object="${prodVo}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">商品分类：</label>
                    <div class="col-sm-8">
                        <select name="prodClassId" class="form-control" th:with="prodClasses=${@product.getProdClasses()}" th:field="*{prodClassId}" >
                            <option value="">所有</option>
                            <option th:each="prodClass : ${prodClasses}" th:text="${prodClass.className}" th:value="${prodClass.prodClassId}"></option>
                        </select>
                    </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="prodName" class="form-control" type="text" th:field="*{prodName}">
                    </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">
                        <select name="ipId" class="form-control m-b" th:with="ipInfos=${@ipInfo.getIpInfos()}" th:field="*{ipId}" >
                            <option value="">所有</option>
                            <option th:each="ipInfo : ${ipInfos}" th:text="${ipInfo.ipName}" th:value="${ipInfo.ipId}"></option>
                        </select>
                    </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">
                        <select name="designerId" class="form-control m-b" th:with="designers=${@product.getDesigners()}" th:field="*{designerId}" >
                            <option value="">所有</option>
                            <option th:each="designer : ${designers}" th:text="${designer.designerName}" th:value="${designer.id}"></option>
                        </select>
                    </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="picUrlImg" width="30%" height="30%">
                        <input type="file" class="form-control" name="picUrlImgUrl" id="picUrlImgUrl" accept="image/*"/>
                        <input type="hidden" lass="form-control" name="picUrl" id="picUrl" />
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">商品描述：</label>
                    <div class="col-sm-10" style="margin-left: 80px">
                        <div class="summernote" style="border: red;" id="summernote"></div>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: select2-js" />
    <th:block th:include="include :: bootstrap-select-js" />
    <th:block th:include="include :: summernote-js" />
    <script th:inline="javascript">
        var prefix = ctx + "master/product/prod"
        $('#picUrlImgUrl').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) {
                            $('#picUrlImg').attr('src', result.imageUrl);
                            $('#picUrl').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();
                    }
                })
            }
        });

        $(".summernote").summernote({
            lang: 'zh-CN',
            callbacks:{
                onImageUpload: function (files) {
                    var fileSize = 0;
                    fileSize = 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(files[0].type)) {
                        layer.msg('上传的不是图片文件，请重新上传。', {time: 3000, icon:6});
                        return false;
                    }

                    var formData = new FormData();
                    formData.append('imageFile', files[0]);
                    $.ajax({
                        url : ctx + "common/upload/file/image",
                        type : 'POST',
                        data : formData,
                        processData : false,
                        contentType : false,
                        success : function(result) {
                            $('.summernote').summernote('insertImage', result.imageUrl);
                        },error:function(){
                            $.modal.alertError("上传失败");
                        }
                    });
                }
            },
            height: 600,
            toolbar: [
                ['style', ['fontname', 'bold', 'italic', 'underline', 'clear']],
                ['font', ['strikethrough', 'superscript', 'subscript']],
                ['fontsize', ['fontsize', 'undo', 'redo']],
                ['color', ['color']],
                ['para', ['ul', 'ol', 'paragraph']],
                ['height', ['height']],
                ['insert', ['picture', 'table']],
                ['search', ['findnreplace', 'changecolor']]
            ],
        });
        $('.summernote').summernote('code', [[${prodVo.description}]]);

        $("#form-prod-edit").validate({
            rules: {
                prodClassId: {
                    required: true
                },
                prodName: {
                    required: true
                },
            },
            messages: {
                prodClassId: icon + "请选择商品分类",
                prodName: icon + "请输入商品名称",
            },
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                var form = document.getElementById("form-prod-edit");
                var formData = new FormData(form);
                formData.append("description", $('#summernote').summernote('code'));
                $.ajax({
                    url: prefix + "/edit",
                    data: formData,
                    type: "post",
                    processData: false,
                    contentType: false,
                    success: function(result) {
                        $.operate.successCallback(result);
                    }
                });
            }
        }
    </script>
</body>
</html>