<!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">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body" pad15>
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <form class="layui-form" action="" lay-filter="component-form-element">
                        <div class="layui-upload">
                          <button type="button" class="layui-btn" id="upload_image">上传图片</button>
                          <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                            已上传图片：
                            <div class="layui-upload-list" id="show_image" style="display: flex;flex-wrap: wrap;"></div>
                         </blockquote>
                        </div>

                        <button class="layui-btn" id="upload_save" lay-submit lay-filter="component-form-element" style="display: none">开始上传</button>
                    </form>

                </div>
            </div>
        </div>
    </div>
</div>

  <script src="../../layuiadmin/layui/layui.js"></script>
  <script>
   var main_id = null;
  layui.link('../../../layuiadmin/style/autocomplete.css');
  layui.config({
    base: '../../../layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index',
    autocomplete: 'autocomplete'
  }).use(['index','utils', 'form', 'element', 'upload'], function(){
    var $ = layui.$
    ,admin = layui.admin
    ,element = layui.element
    ,form = layui.form
    ,upload = layui.upload;

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

    form.render(null, 'component-form-element');
    element.render('breadcrumb', 'breadcrumb');
    refresh();

    //多图片上传
  upload.render({
    elem: '#upload_image'
    ,url:  '/usedvehicle/usedvehicle/'+id+'/set_vehicle_img/'
    ,field: 'images'
    ,exts: 'jpg|png|jpeg|bmp'
    ,acceptMime: '.jpg,.png,.jpeg,.bmp'
    ,multiple: true
    ,done: function (res) {
        if (res.code != 0) {
            layer.msg(res.msg);
        }else {
            refresh();
        }
    },
    error: function () {
        layer.msg('保存失败');
    }
  });
    parent.layui.submitChild = function () {
      $("#upload_save").click();
    };
  });

  function refresh () {
      var $ = layui.$;
      layui.admin.req({
        url: '/usedvehicle/images/?main=' + main_id,
        done: function (res) {
           $('#show_image').html('');
           var data = res.data;
           for (var i in data){
               var html = '<div style="margin-top: 10px;"><a href="'+data[i].img_url+'" target="_blank"><img src="'+ data[i].img_url +'" class="layui-upload-img" style="margin-left: 5px;min-width: 80px; height: 100px;max-width: 200px;"></a>' +
                      '<div align="center" style="margin-top: 3px;" ><button type="button" class="layui-btn layui-btn-danger layui-btn-xs" onclick="delImage('+data[i].id+')">删除</button></div></div>';
                $('#show_image').append(html)
           }
        }
    });
  }

  function delImage(id) {

      layui.admin.req({
         url: '/usedvehicle/images/'+id + '/'
         ,type: 'delete'
         ,done: function(res){
             refresh();
         }
      });
  }

  </script>
</body>
</html>