<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>上传详情图片</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../layuiadmin/style/admin.css" media="all">
    <style>
        .input {
            border-radius: 2px;
            height: 38px;
            border-width: 1px;
            border-color: lightgray;
            border-style: solid;
            background-color: white;
            padding-left: 3px;
        }

        .processText {
            margin-top: 6px;
        }

        .layui-upload-drag {
            padding: 10px;
            margin-right: 15px;
        }

        .imgsContent {
            display: flex;
            justify-content: flex-start;
            flex-direction: row;
            flex-wrap: wrap;
        }

        .image {
            width: 150px;
            height: auto;
        }

        .imgItem {
            margin: 3px 10px;
        }

        .option {
            display: flex;
            justify-content: space-between;
            flex-direction: row;
        }

        .optionItem {
            padding: 0 13px;
            margin: 10px;
            border: 1px solid rgba(0, 0, 0, 0.1);
            background: #009688;
            color: #ffffff;
        }

    </style>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-card">

            <div class="layui-card-body" pad15>
                <form class="layui-form" action="" lay-filter="component-form-element">
                    <div class="layui-row layui-col-space10 layui-form-item">
                        <div>
                            <label class="layui-form-label"><font color='red' size="4">*</font>上传图片：</label>
                            <div class="imgsContent">
                                <div class="layui-upload-drag" id="upload_img">
                                    <div>
                                        <p style="padding-bottom: 10px">多选，最多选10张</p>
                                        <i class="layui-icon"></i>
                                        <p>点击上传，或将图片拖到此处</p>
                                    </div>
                                </div>
                                <div id="imgsList" class="imgsContent"></div>
                            </div>
                        </div>

                        <button id="id_save" class="layui-btn" lay-submit lay-filter="component-form-element"
                                style="display: none">保存
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script src="../../layuiadmin/layui/layui.js"></script>
<script>
    //即将上传的文件数组
    var filesList = [];
    //渲染的图片列表
    var imgsList = [];

    //渲染待上传的图片列表
    var renderImgsList = function (list) {
        var html = "";
        list.forEach((item, index) => {
            var src = item.src
            html += "<div class='imgItem'>\n";
            html += "<img src='" + src + "' class='image'>\n";
            html += '<div class="option">\n';
            html += '<div onclick="showImg(' + index + ')" class="optionItem">查看</div>\n';
            html += '<div onclick="deleteImg(' + index + ')" class="optionItem">删除</div>\n';
            html += '</div>\n';
            html += '</div>\n';
        })
        layui.$('#imgsList').html(html);
    }

    var showImg = function (index) {
        var newPage = window.open();
        newPage.document.write("<img src=" + imgsList[index].src + " />")
    };

    var deleteImg = function (index) {
        var id = imgsList[index].id
        if (id) {
            // 删除此id
            layer.confirm('确定要删除此图片吗？', function (conf_index) {
                layer.close(conf_index);
                layui.admin.req({
                    notice: true
                    , url: '/commodity/del_images/' + id + '/'
                    , type: 'delete'
                    , done: function (res) {
                        imgsList.splice(index, 1);
                        renderImgsList(imgsList);
                    }
                });
            });
        } else {
            filesList.splice(index, 1);
            imgsList.splice(index, 1);
            renderImgsList(imgsList);
        }
    };

    layui.link('../../../layuiadmin/style/autocomplete.css');
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
        , autocomplete: 'autocomplete'
    }).extend({
        index: 'lib/index',
    }).use(['index', 'form', 'autocomplete', 'upload'], function () {
        var $ = layui.$
            , admin = layui.admin
            , upload = layui.upload
            , form = layui.form;

        var id = layui.view.getParameterByName('id');
        var type = layui.view.getParameterByName('type');

        admin.req({
            url: '/commodity/' + id + '/query_images/'
            , data: {type}
            , type: 'post'
            , done: function (res) {
                imgsList = res.data;
                renderImgsList(imgsList)
            }
        });

        //拖拽上传
        upload.render({
            elem: '#upload_img'
            , accept: 'file'
            , exts: 'jpg|png|jpeg|bmp'
            , acceptMime: '.jpg,.png,.jpeg,.bmp'
            , data: {}
            , multiple: true
            , number: 10
            , auto: false
            , choose: function (obj) {
                if (imgsList.length > 10) {
                    layer.msg("图片最多只允许上传10张", {icon: 2});
                    return
                }
                //预读本地文件，如果是多文件，则会遍历。(不支持ie8/9)
                obj.preview(function (index, file, result) {
                    filesList.push(file);
                    imgsList.push({
                        id: '',
                        src: result,
                    });
                    // 有图片列表的话渲染图片
                    if (imgsList.length > 0) {
                        renderImgsList(imgsList)
                    }
                });
            }
        });


        form.on('submit(component-form-element)', function (data) {
            if (filesList.length === 0) {
                layer.msg("请先选择图片！", {icon: 2});
                return false
            }
            var formData = new FormData();
            formData.append("type", type);
            filesList.forEach((item, index) => {
                formData.append(`file${index}`, item)
            });
            var url = '/commodity/' + id + '/upload_images/';
            admin.req({
                url: url
                , data: formData
                , contentType: false
                , processData: false
                , type: 'post'
                , done: function (res) {
                    parent.layui.onSubmitChild(res);
                }
            });

            return false;
        });

        parent.layui.submitChild = function () {
            $("#id_save").click();
        };
    });
</script>
</body>
</html>
