{% load staticfiles %}
<div class="innerDiv">
    <form class="layui-form" action="" lay-filter="newsForm">
        <div class="layui-form-item">
            <label class="layui-form-label">视频标题：</label>
            <div class="layui-input-block">
                <input type="text" name="title" required lay-verify="required" placeholder="请输入视频标题" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">视频简介：</label>
            <div class="layui-input-block">
                <textarea name="intro" lay-verify="required" placeholder="请输入简介" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">视频封页：</label>
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="videoImg"><i class="layui-icon"></i>视频封页</button>
                <!-- 隐藏的input -->
                <input type="hidden" id="img_url" name="img_url" value="" />
                <div class="layui-upload-list">
                    <img class="layui-upload-img" id="demo1">
                    <p id="demoText1"></p>
                </div>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">视频文件：</label>
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="videoFile" style="margin-bottom:20px;"><i class="layui-icon"></i>视频文件</button>
                <!-- 隐藏的input -->
                <input type="hidden" id="video_url" name="video_url" value="" />      
                <br />
                <div class="layui-progress layui-progress-big" lay-showPercent="yes" lay-filter="progressBar" id="progressBar">
                    <div class="layui-progress-bar layui-bg-red" lay-percent="0%"></div>
                </div>    
            </div>
        </div>
        
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="newsForm">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>


<!-- <script src="{% static '/lib/js/jquery-2.1.4.min.js'%}"></script>
<script src="{% static '/lib/layui/layui.js'%}"></script>
<script src="{% static '/js/backstageNet/common.js'%}"></script> -->


<script>
    layui.use(['element', 'form', 'upload'], function () {
        var element = layui.element;
        var form = layui.form;
        var $ = layui.jquery;
        var upload = layui.upload;

        element.init();

        //新闻提交
        form.on('submit(newsForm)', function (data) {
            // console.log(data.field)
            if (data.field.img_url == '') {
                layer.msg('请上传视频封页', { icon: 2 });
                return false;
            } else  if (data.field.video_url == '') {
                layer.msg('请上传视频文件', { icon: 2 });
                return false;
            } else {
                $.ajax({
                    url: 'video_pub',
                    type: 'post',
                    data: data.field,
                    success: function (data) {
                        if (data) {
                            layer.msg('发布成功,等待管理员审核', { icon: 1 })

                        } else {
                            layer.msg('发布失败', { icon: 2 })
                        }
                    },
                    error: function (type) {
                        console.log(type)
                    }
                })
                return false;
            }

        });


        // 重置事件
        $("button[type='reset']").click(function () {
            $('#img_url').val('');
            $('#demo1').removeAttr('src');
        })


        //视频图片上传
        var uploadInst = upload.render({
            elem: '#videoImg'
            , url: 'video_photo'
            , field: 'video_img'
            , xhr:function(){}
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.code == 0) {
                    //上传成功
                    document.getElementById("img_url").value = res.data.src;
                } else {
                    return layer.msg('上传失败');
                }


            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText1');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });

        //创建监听函数
        var xhrOnProgress = function (fun) {
            xhrOnProgress.onprogress = fun; //绑定监听
            //使用闭包实现监听绑
            return function () {
                //通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
                var xhr = $.ajaxSettings.xhr();
                //判断监听函数是否为函数
                if (typeof xhrOnProgress.onprogress !== 'function')
                    return xhr;
                //如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
                if (xhrOnProgress.onprogress && xhr.upload) {
                    xhr.upload.onprogress = xhrOnProgress.onprogress;
                }
                return xhr;
            }
        }

        //视频文件上传
        var uploadInst = upload.render({
            elem: '#videoFile'
            , url: 'video_upload'
            , accept: 'video'              //video audio images
            , field: 'video'
            , xhr:xhrOnProgress
            ,progress:function(value){//上传进度回调 value进度值
                element.progress('progressBar', value+'%')//设置页面进度条
            }
            ,choose: function (obj) {
                $('#progressBar').show();
                obj.preview(function (index, file, result) {
                    // console.log(file);
                });
            }
            , before: function (obj) {
                console.log('文件上传中');
            }
            , done: function (res) {
                //如果上传失败
                if (res.code == 0) {
                    //上传成功
                    document.getElementById("video_url").value = res.data.src;
                } else {
                    return layer.msg('上传失败');
                }


            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText2');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });

    });

    scrollBar()

</script>
</body>

</html>