<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改企业微信门户首页banner图')" />
    <th:block th:include="include :: cropbox-css" />
    <style>
        .bg-lg{
            width: 365px;
            height: 195px;
            font-size: 125px;
            color: black;
            text-align: center
        }
        .thumbBox_L{
            position: absolute;
            top: 50%;
            left: 50%;
            width: 730px;
            height: 390px;
            margin-left: -365px;
            margin-top: -195px;
            box-sizing: border-box;
            border: 1px solid rgb(102, 102, 102);
            box-shadow: 0 0 0 1000px rgba(0, 0, 0, 0.5);
            background: none repeat scroll 0% 0% transparent;
        }
        .imageBox_L{
            margin: 0 auto;
            width: 800px;
            height: 450px;
        }
    </style>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content" id="div1">
        <form class="form-horizontal m" id="form-banner-edit" th:object="${wxBanner}">
            <input name="bannerId" th:field="*{bannerId}" type="hidden">

            <div class="form-group">    
                <label class="col-sm-3 control-label">标题：</label>
                <div class="col-sm-8">
                    <input name="title" th:field="*{title}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">内容地址：</label>
                <div class="col-sm-8">
                    <input name="contextUrl" th:field="*{contextUrl}" class="form-control" type="text">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">背景图片：</label>
                <div class="col-sm-8">
                    <input id="input_imgUrl" name="imgUrl" th:field="*{imgUrl}" type="hidden"/>
                    <div class="thumbnail slideshow_span bg-lg" id="div-bgUrl" onclick="javascript:imgUpload()">
                        <img th:src="@{*{imgUrl}}" style="width:100%;height:100%;">
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">内容摘要：</label>
                <div class="col-sm-8">
                    <textarea name="des" class="form-control">[[*{des}]]</textarea>
                </div>
            </div>
        </form>
    </div>

    <div class="wrapper wrapper-content animated fadeInRight ibox-content" id="div2" style="display: none">
        <div class="imageBox imageBox_L" style="margin: 0 auto;">
            <div class="thumbBox_L" ></div>
            <div class="spinner" style="display: none">Loading...</div>
        </div>
        <div class="action" style="margin: 10px auto;">
            <div class="new-contentarea tc">
                <a href="javascript:void(0)" class="upload-img"> <label for="avatar">上传图像</label> </a>
                <input type="file" class="" name="avatar" id="avatar" accept="image/*"/>
            </div>
            <input type="button"  onclick="upBg()" class="btn-info Btnsty_peyton"  value="上传" />
            <input type="button"  onclick="back()" class="btn-info Btnsty_peyton "  value="返回" />
        </div>
    </div>

    <th:block th:include="include :: footer" />
    <th:block th:include="include :: cropbox-js" />
    <script type="text/javascript">
        var prefix = ctx + "wxportal/banner";
        var imgUrl = '[[${imgUrl}]]';
        var options = {
            thumbBox: '.thumbBox_L',
            spinner: '.spinner',
            imgSrc: $.common.isEmpty(imgUrl) ? ctx + 'img/profile.jpg' : ctx +imgUrl
        }
        $(function(){
            $("#form-banner-add").validate({
                focusCleanup: true
            });

            cropper = $('.imageBox').cropbox(options);
            $('#avatar').on('change', function() {
                var reader = new FileReader();
                reader.onload = function(e) {
                    options.imgSrc = e.target.result;
                    //根据MIME判断上传的文件是不是图片类型
                    if((options.imgSrc).indexOf("image/")==-1){
                        $.modal.alertWarning("文件格式错误，请上传图片类型,如：JPG，PNG后缀的文件。");
                    } else {
                        cropper = $('.imageBox').cropbox(options);
                    }
                }
                reader.readAsDataURL(this.files[0]);
            })


            $('#btnZoomIn').on('click', function(){
                cropper.zoomIn();
            })

            $('#btnZoomOut').on('click', function(){
                cropper.zoomOut();
            })
        })

        function back() {
            $("#div1").show();
            $("#div2").hide();
        }




        /*上传背景图片*/
        function imgUpload() {

            $("#div1").hide();
            $("#div2").show();
            var imgUrl =$("#input_imgUrl").val();
            if(imgUrl==null||imgUrl==''){
                imgUrl =  ctx + 'img/profile.jpg';
            }else{
                imgUrl = ctx+$("#input_imgUrl").val();
            }
            options = {
                thumbBox: '.thumbBox_L',
                spinner: '.spinner',
                imgSrc:imgUrl
            }
            cropper = $('.imageBox').cropbox(options);
        }
        function upBg() {
            var img =cropper.getBlob();
            var formdata = new FormData();
            formdata.append("file", img);
            $.ajax({
                url: ctx + "common/upload",
                data: formdata,
                type: "post",
                processData: false,
                contentType: false,
                success: function(result) {
                    if(result.code==0){
                        $("#input_imgUrl").val(result.data.fileName);
                        $("#div-bgUrl").html("<img src='"+result.data.url+"' style='width；100%;height:100%;'>");
                        $("#div1").show();
                        $("#div2").hide();
                    }else{
                        $("#div1").show();
                        $("#div2").hide();
                        $.modal.alertWarning(result.msg);
                    }

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