@extends('admin.master')

@section('top')
  <div class="top-breadcrumb pull-left">
    <h5>媒体库</h5>
  </div>
@endsection

@section('ordersearch')
    <div class="order-search mb-10">
      <div class="clearfix">
        <div class="pull-left">
            <a id="pickfile" href="javascript:;" class="btn btn-primary upload">
                <i class="iconfont icon-plus"></i><span>上传文件</span>
            </a>
            <a id="importImg" href="javascript:;" class="btn btn-outline-primary upload ml-10">
              <span>导入产品图片</span>
            </a>
        </div>
        <div class="pull-right">
          {!! Form::model($where, ['method'=>'get', 'class'=>'form-inline']) !!}
          <div class="form-group pr-10">
            {!! Form::text('word', null, ['class'=>'form-control len wd-search', 'id'=>'kw', 'placeholder'=>'请输入图片名称']) !!}
          </div>
          <button type="submit" class="btn btn-primary"><i class="iconfont icon-sousuo2"></i></button>
          {!! Form::close() !!}
        </div>
      </div>
    </div>
@endsection

@section('content')
  {!! Form::open(['route' => 'admin.collect-media.index', 'method' => 'post', 'class'=>'form-ajax']) !!}
  <table class="table table-hover">
    <thead>
    <tr>
      <th width="60" class="text-center">
        <div class="pretty p-default p-smooth">
          <input name="chkAll" type="checkbox" id="chkAll" onclick="checkAll(this, 'id[]')" value="checkbox">
          <div class="state p-success"><label></label></div>
        </div>
      </th>
      <th width="100">图片</th>
      <th>地址</th>
      <th>创建时间</th>
      <th width="110" class="text-center">操作</th>
    </tr>
    </thead>
    <tbody>
    @if(!$data->isEmpty())
      @foreach ($data as $value)
        <tr>
          <td align="center">
            <div class="pretty p-default p-smooth">
              <input name="id[]" class="id" type="checkbox" value="{{ $value['id'] }}" onclick="checkItem(this, 'chkAll')">
              <div class="state p-success"><label></label></div>
            </div>
          </td>
          <td>
            <a href="{{ $value->path }}" target="_blank"><img src="{{ $value->path . '?x-oss-process=image/resize,m_fixed,h_60,w_80' }}"></a>
          </td>
          <td>{{ $value->path }}</td>
          <td>
            <span class="text-success">{{ $value->created_at }}</span>
          </td>
          <td>
            <div class="toolbar">
              <a href="javascript:void(0);" data-id="{{ $value->id }}" class="btn-link delete-item">删除</a>
            </div>
          </td>
        </tr>
      @endforeach
      <tr class="footer">
        <td align="center">
          <div class="pretty p-default p-smooth">
            <input name="chkAll" type="checkbox" id="chkAll" onclick="checkAll(this, 'id[]')" value="checkbox">
            <div class="state p-success"><label></label></div>
          </div>
        </td>
        <td class="text-left" colspan="5">
          <div class="foot-wrap">
            <div class="pull-left">
              <input name="btn_del" type="button" value="删 除" class="btn btn-default delet-select" >&nbsp;&nbsp;共[ <b class="text-warning">{{ $data->total() }}</b> ]条记录&nbsp;&nbsp;
            </div>
            @if(!empty($data->render()))
              <div class="pull-right">
                <div class="pagination-info">共有{{ $data->total() }}条， 每页显示：{!! $data->perPage() !!}条</div>
                {!! $data->appends($where)->render() !!}
              </div>
            @endif
          </div>
        </td>
      </tr>
    @endif
    </tbody>
  </table>
  @if($data->isEmpty())
    <div class="nodata-wrap">
      <div class="notice-null"><span class="inline-block">暂无数据</span></div>
    </div>
  @endif
  {!! Form::close() !!}
@endsection

@section('notice')
  <!-- Modal -->
  <div class="modal fade" id="dialog" tabindex="-1" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="true"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title">提示</h4>
        </div>
        <div class="modal-body clearfix">
          <div class='col-sm-1 text-center'><i class='iconfont icon-zhuyi'></i></div>
          <div class='col-sm-11 breakall'><p>删除后您将无法恢复，请谨慎操作</p><p class='text-muted'>您确认要删除吗？</p></div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-primary btn-modal-ok">确定</button>
          <button class="btn btn-default btn-modal-cancel btn-modal-close" data-dismiss="modal">取消</button>
        </div>
      </div>
    </div>
  </div>
  <!-- Modal End -->
  <!-- Modal -->
  <div class="modal fade" id="dialog-tip" tabindex="-1" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="true"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title">提示</h4>
        </div>
        <div class="modal-body clearfix">
          <div class='col-sm-1 text-center'><i class='fa fa-times-circle text-size-32 text-warning'></i></div>
          <div class='col-sm-11 breakall'><p class="delete-error">删除失败！</p></div>
        </div>
        <div class="modal-footer">
          <button class="btn btn-primary btn-modal-close" data-dismiss="modal">确定</button>
        </div>
      </div>
    </div>
  </div>
  <!-- Modal End -->
  <!-- Modal -->
  <div class="modal fade" id="dialog-common" tabindex="-1" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="true"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title">提示</h4>
        </div>
        <div class="modal-body clearfix">
          <div class='col-sm-1 text-center'><i class='iconfont icon-zhuyi'></i></div>
          <div class='col-sm-11 breakall'><p class="common">请选择要删除的内容!</p></div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-primary btn-modal-common">确定</button>
        </div>
      </div>
    </div>
  </div>
  <!-- Modal End -->
  <!-- Modal -->
  <div class="modal fade" id="importDialog" tabindex="-1" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="true"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title">图片导入</h4>
        </div>
        <div class="modal-body  clearfix">
          <div class="form-group form-horizontal">
            <label class="col-sm-3 control-label">产品分类：</label>
            <div class="col-sm-9">
              {!! Form::select('pid', ['' => '请选择'] + $nests, null, ['class'=>'form-control', 'id'=>'category']) !!}
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-primary import-ok">确定</button>
          <button class="btn btn-default btn-modal-cancel btn-modal-close" data-dismiss="modal">取消</button>
        </div>
      </div>
    </div>
  </div>
  <!-- Modal End -->
@endsection

@section('foot')
  <script type="text/javascript">
      var urlUpload = "{{ url('admin/media/upload') }}";
      // 图片上传
      var settings = {
          runtimes : 'html5',
          browse_button : 'pickfile',
          url : urlUpload,
          dragdrop: true,
          multi_selection: true,
          multipart_params: {'type': 1, 'id': 0},
          headers: {
              'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
          },
          init: {
              FilesAdded: function(up, files) {
                  swal({
                      title: "图片上传中...",
                      text: "请不要关闭当前窗口！",
                      imageUrl: "{{ asset('/data/assets/images/loading6.gif') }}",
                      showConfirmButton: false,
                  });
                  up.start();
              },
              UploadComplete: function(up, file) {
                  swal({
                      title: "",
                      text: "图片上传成功！",
                      type: "success",
                      showConfirmButton: false,
                      timer: 1200,
                  });
                  setTimeout("location.reload()", 1200);
              },
              Error: function(up, err) {
                  alert('文件上传失败');
              },
          }
      };

      var ossUploader = function(config) {
          var options = {};
          $.extend(options, settings, config);

          var up = new plupload.Uploader(options);
          up.init();
      }
      ossUploader();

      // 删除
      $('.delete-item').on('click', function(e) {
          var id = $(this).data('id');
          url = "{{ url('admin/collect-media') }}" + '/' + id;

          $('#dialog').modal('show');
          return false;
      });

      //批量删除
      $('.delet-select').on('click', function(){
          var ids = [];
          $('.id:checked').each(function(){
              ids.push(this.value);
          });
          ids = ids.join('-');
          if (!ids) {
              $('.common').text('请选择要删除的内容!');
              $('#dialog-common').modal('show');
              return false;
          }
          url = location.pathname + '/' + ids;

          $('#dialog').modal('show');
          return false;
      });

      $('body').on('click', '.btn-modal-common', function(){
          $(this).parents('#dialog-common').modal('hide');
      });

      $('body').on('click', '.btn-modal-ok', function() {
          $(this).parents('#dialog').modal('hide');
          $.ajax({
              type: "DELETE",
              url: url,
              success: function(data) {
                  if (data.status == 'success') {
                      swal({
                          title: "",
                          text: "信息删除成功！",
                          type: "success",
                          showConfirmButton: false,
                          timer: 1200,
                      });
                      setTimeout("window.location.reload()", 1200);
                  } else {
                      $('.delete-error').html(data.error);
                      $('#dialog-tip').modal('show');
                  }
              }
          });
      });

      $('#importImg').on('click', function() {
          $('#importDialog').modal('show');
      });

      $('body').on('click', '.import-ok', function() {
          $(this).parents('#importDialog').modal('hide');
          $.ajax({
              url: HOME_URL + '/admin/collect-media/import',
              type: "post",
              data: {'pid': $("#category").val()},
              success: function(data) {
                  if (data.status == 'success') {
                      swal({
                          title: "",
                          text: "导入成功！",
                          type: "success",
                          showConfirmButton: false,
                          timer: 1200,
                      });
                      setTimeout("location.reload()", 1200);
                  } else {
                      $('.delete-error').html('导入失败！');
                      $('#dialog-tip').modal('show');
                  }
              }
          });
      });
  </script>
@endsection