var autoLogin = {
  setCookie: function (cname, cvalue, exdays,path) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toGMTString();
    path = path ? ';path='+path+'' : '';
    document.cookie = cname + "=" + cvalue + "; " + expires + path;
  },
  getCookie: function (cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
      var c = ca[i].trim();
      if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
    }
    return "";
  },
  login: function () {
    var _self = this;

    var flag = parseInt(_self.getCookie('autoLogin'));

    if (flag) {
      var inputdata = {};
      inputdata['4dk_userName'] =  unescape(_self.getCookie('autoLoginUser'));
      inputdata['4dk_userPassword'] =  unescape(_self.getCookie('autoLoginPas'));
      inputdata['4dk_auto'] = 'on';

      $.ajax({
        url: '/user/login',
        type: 'POST',
        contentType: 'application/x-www-form-urlencoded; charset=utf-8',
        dataType: 'json',
        data: inputdata,
        success: function (data) {
          if (data.state == 1) {
            window.location.reload();
          }
        },
        error: function (msg) {
          console.log('error');
        }
      });

    };

  },
  login2: function (fail) {//许钟文 因为ajax需要时间所以要得到后才能执行后续
    var _self = this;

    var flag = parseInt(_self.getCookie('autoLogin'));
    console.log(flag)
    if (flag) {
      var inputdata = {};
      inputdata['4dk_userName'] =  unescape(_self.getCookie('autoLoginUser'));
      inputdata['4dk_userPassword'] =  unescape(_self.getCookie('autoLoginPas'));
      inputdata['4dk_auto'] = 'on';

      $.ajax({
        url: '/user/login',
        type: 'POST',
        contentType: 'application/x-www-form-urlencoded; charset=utf-8',
        dataType: 'json',
        data: inputdata,
        success: function (data) {
          if (data.state == 1) {
            window.location.reload();
          }else{
			  console.log("data.state" + data.state)
			  fail && fail();
		  }
        },
        error: function (msg) {
          console.log('error');
		  fail && fail();
        }
      });

    }else{
		fail && fail();
	}	 

  },
  clear: function () {
    this.setCookie('autoLogin', 0, 180, '/');
  }
};