{% 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">
                <textarea name="info" 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">
                <input type="text" name="mobile" required lay-verify="phone" 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">
                <button type="button" class="layui-btn" id="productUpImg">
                    <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="demoText"></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"><i class="layui-icon"></i>产品介绍图</button> -->
                <div class="uploadParent">
                    <div class="" id="UpProductImg1"></div>
                    <!-- 隐藏的input -->
                    <input type="hidden" id="img_url1" name="img_url1" value="" />
                    <div class="" id="UpProductImg2"></div>
                    <!-- 隐藏的input -->
                    <input type="hidden" id="img_url2" name="img_url2" value="" />
                    <div class="" id="UpProductImg3"></div>
                    <!-- 隐藏的input -->
                    <input type="hidden" id="img_url3" name="img_url3" value="" />
                </div>
                <div class="uploadShowParent">
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" id="productImg1">
                        <p id="demoText"></p>
                    </div>
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" id="productImg2">
                        <p id="demoText"></p>
                    </div>
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" id="productImg3">
                        <p id="demoText"></p>
                    </div>
                </div>

                <!-- <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                    预览图：
                    <div class="layui-upload-list" id="demo2"></div>
                </blockquote> -->
            </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(['form', 'upload'], function () {
        var form = layui.form;
        var $ = layui.jquery;
        var upload = layui.upload;

        //发布提交
        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.img_url1 == '' && data.field.img_url2 == '' && data.field.img_url3 == '') {
                layer.msg('请至少上传一张产品介绍', { icon: 2 });
                return false;
            } else {
                $.ajax({
                    url: 'equip_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: '#productUpImg'
            , url: 'product_photo'
            , field: '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;
                    console.log(res)
                } else {
                    return layer.msg('上传失败');
                }


            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                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();
                });
            }
        });

        //产品介绍图
        upload.render({
            elem: '#UpProductImg1'
            , url: 'product_photo'
            , multiple: true
            , field: 'img1'
            , xhr: function () { } //上传视频进度条
            , before: function (obj) {
                $('#UpProductImg1').css('background', 'none')
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#productImg1').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.code == 0) {
                    //上传成功
                    document.getElementById("img_url1").value = res.data.src;
                    console.log(res)
                } else {
                    return layer.msg('上传失败');
                }


            }
        });
        upload.render({
            elem: '#UpProductImg2'
            , url: 'product_photo'
            , multiple: true
            , field: 'img2'
            , xhr: function () { } //上传视频进度条
            , before: function (obj) {
                $('#UpProductImg2').css('background', 'none')
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#productImg2').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.code == 0) {
                    //上传成功
                    document.getElementById("img_url2").value = res.data.src;
                    console.log(res)
                } else {
                    return layer.msg('上传失败');
                }


            }
        });
        upload.render({
            elem: '#UpProductImg3'
            , url: 'product_photo'
            , multiple: true
            , field: 'img3'
            , xhr: function () { } //上传视频进度条
            , before: function (obj) {
                $('#UpProductImg3').css('background', 'none')
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#productImg3').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.code == 0) {
                    //上传成功
                    document.getElementById("img_url3").value = res.data.src;
                    console.log(res)
                } else {
                    return layer.msg('上传失败');
                }


            }
        });


    });

    scrollBar()

</script>
</body>

</html>