var form;

$(document).ready(function(){ 
  form = layui.form;
});

//提交表单
function saveUsers(){
  $("#detail_form").ajaxSubmit({
    url: '/gluser/import/saveok', 
    type: 'POST',
    dataType: "json", 
    beforeSend: function(){
      $("#waitingtips").show();
    },
    success: function(data){
      if(data.code=="success"){
        if(data.msg==""){
          layer.msg("成功", {icon:1}, function(){parent.location.reload();});
        }else{
          $("#errmsgtips").html(data.msg).show();
        }
      }else{
        layer.msg(data.msg, {icon:2});
      }
    },
    error: function(){
      layer.msg("系统错误，请联系管理员", {icon:2});
    },
    complete: function(){
      $("#waitingtips").hide();
    }
  });
  return false;
}

//上传插件
var uploader = new plupload.Uploader({
  runtimes : 'html5,flash,silverlight,html4',
  browse_button : 'pickfiles', // you can pass an id...
  container: document.getElementById('uploadcontainer'), // ... or DOM Element itself
  url : '/gluser/import/upload',
  flash_swf_url : '/plugins/plupload/js/Moxie.swf',
  silverlight_xap_url : '/plugins/plupload/js/Moxie.xap',
  multi_selection: false,
  
  filters : {
    max_file_size : '1000mb',
    mime_types: [
      {title : "Excel files", extensions : "xls,xlsx"}
    ]
  },

  init: {
    PostInit: function() {
      document.getElementById('filelist').innerHTML = '';
      document.getElementById('uploadfiles').onclick = function() {
        uploader.start();
        return false;
      };
    },

    FilesAdded: function(up, files) {
      plupload.each(files, function(file) {
        document.getElementById('filelist').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b><div class="progress"><div class="progress-bar" style="width: 0%"></div></div></div>';
      });
      uploader.start(); //自动开始上传
      return false;
    },

    UploadProgress: function(up, file) {
      var d = document.getElementById(file.id);
      d.getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
            var prog = d.getElementsByTagName('div')[0];
      var progBar = prog.getElementsByTagName('div')[0]
      progBar.style.width= 2*file.percent+'px';
      progBar.setAttribute('aria-valuenow', file.percent);
    },

    FileUploaded: function(up, file, info) {
      //console.log(info.response);
      if (info.status == 200){
        var json = $.parseJSON(info.response);
        if(json.error){
          document.getElementById("filelist").innerHTML = '上传失败';
          return false;
        }
        document.getElementById("console").innerHTML += json.info.path +'<br>';
        $("#detail_form").find("input[name='filepath']").val(json.info.path);
        saveUsers();
      }else{
        document.getElementById("console").innerHTML += file.name +'上传失败<br>';
      }
    },

    UploadComplete: function(up, files) {
      console.log('complete');
    },

    Error: function(up, err) {
      document.getElementById('console').appendChild(document.createTextNode("\nError #" + err.code + ": " + err.message));
    }
  }
});

uploader.init();