var app = new Vue({
  el: "#app",
  data: function () {
    var validmethod = (rule, value, callback) => {
      if (rule.field == "userName") {
        if (!value) {
          return callback(new Error('请输入手机号'));
        } else {
          callback();
        }
      } else {
        if (!value) {
          return callback(new Error('请输入密码'));
        }
        callback();
      }
    };
    return {
      domainUrl:"",
      loginForm: {
        userName: '',
        password: '',
      },
      rules: {
        userName: [
          { validator: validmethod, trigger: 'blur' }
        ],
        password: [
          { validator: validmethod, trigger: 'blur' }
        ],
      },
    }
  },
  created: function () {
  },
  mounted: function () {
    var harr = window.location.href.split("//");
    this.domainUrl = harr[0] + "//" + window.location.host;
  },

  methods: {
    postReq(path, data) {
      return axios.post(path, data).then(function (r) {
        return r.data ? r.data : null;
      })
    },
    btnclick: function (pfm, code) {
    },
    toforget() {
      window.location.href = this.domainUrl + "/forgetent";                
    },
    toreg() {
      window.location.href = this.domainUrl + "/regent";                
    },
    login() {
      var self = this;
      this.$refs['loginForm'].validate((valid) => {
        console.log(valid);
        if (valid) {
            //处理登录逻辑
            self.$root.postReq("/web/userCtl/loginent",{u:self.loginForm}).then(function(d){
              if(d.status==0) {
                window.location.href = self.domainUrl;                
                // self.$root.pushx({nane:"/home", notab:true});
              }  else {
                  self.$message.warning(d.msg || "登录失败, 请稍候重试");
              }
            });
        } else {
          return false;
        }
      });
    },
  },
});