<!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" id="div_upload">
                        </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;
   var options = 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');

    admin.req({
         url: '/option/search/?type=3'
         ,done: function(res){
             var data = res.data;
             options = res.data;
             for (var m in data){
                 var button_id='upload_image_'+data[m].id;
                 var div_id='show_image_'+data[m].id;
                 var html  = '<button type="button" class="layui-btn" id="'+button_id+'">上传'+data[m].name+'图片</button>' +
                         '<blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">已上传图片：' +
                         '<div class="layui-upload-list" id="'+div_id+'" style="display: flex;flex-wrap: wrap;"></div></blockquote>';
                 $('#div_upload').append(html)
             }
             loadUpload();
            refresh();
         }
    });
    //refresh();

    //多图片上传
    var loadUpload = function () {
        for (var i in options){
          upload.render({
            elem: '#upload_image_' + options[i].id
            ,url:  '/vehicle/model/'+id+'/set_model_img/'
            ,field: 'images'
            ,exts: 'jpg|png|jpeg|bmp'
            ,acceptMime: '.jpg,.png,.jpeg,.bmp'
            ,multiple: true
            ,data: {type: options[i].id}
            ,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: '/vehicle/model_images/?vehicle_model=' + main_id,
        done: function (res) {
         for (var i in options){
              $('#show_image_' + options[i].id).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_'+ data[i].type).append(html)
           }
        }
    });
  }

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

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