//提交表单
$("#finished_button").on("click", function(){
    var realname = $("#detail_form").find("input[name='realname']");
    if($(realname).val()==""){
      alert('请填写姓名');
      return false;
    }
    $.ajax({
        url: '/yidong/profile/basicok', 
        type: 'POST',
        dataType: "json",
        async: false, 
        data: $("#detail_form").serialize(),
        success: function(data){
          if(data.code=="success"){
              alert('提交成功');
          }else{
              alert(data.msg);
          }
        },
        error: function(){
          alert("系统错误，请联系管理员");
        }
    });
    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 : '/yidong/upload/logo',
  flash_swf_url : '/plugins/plupload/js/Moxie.swf',
  silverlight_xap_url : '/plugins/plupload/js/Moxie.xap',
  multi_selection: false,
  
  filters : {
    max_file_size : '2000mb'
  },

  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 = JSON.parse(info.response);
        if(json.error){
          document.getElementById("filelist").innerHTML = '上传失败';
          return false;
        }
        document.getElementById("console").innerHTML += json.info.name +',#,'+ json.info.path +',#,'+ json.info.size +',#,'+ json.info.extension +'<br>';
        $("#detail_form").find("input[name='logo']").val(json.info.path_s);
        $("#logopreview").html('<img src="'+ json.info.path_s +'" style="width: 40px; height: 40px; border-radius: 50%;">');
        setTimeout(function(){document.getElementById('filelist').innerHTML = '';},2000);
      }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();