<template>

  <div class="login-form" v-show="slient_connect_to_old_management != true">
    <el-form ref="loginForm" :model="user" :rules="userFormRules" label-width="80px" @keyup.enter.native="login">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="user.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="user.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login" :loading="login_sending">登录</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<script>
  import cookie from "js-cookie"
  import userUtil from "user-util-vkm"
  import axios from "axios"

  export default {

    beforeMount: function () {

      if(this.vkroute.query.logout_old_management == true || this.vkroute.query.logout_old_management == "true")
      {
        this.slient_connect_to_old_management = true;
        location.href = "/login?logout=true";
        return;
      }

      if(userUtil.isManualLogout() != true)
      {
        var token = localStorage.getItem("m_token");
        var userId = localStorage.getItem("m_userid");

        if(token && userId && token.length > 0 && userId.length > 0)
        {

          this.slient_connect_to_old_management = true;

          var xhr = axios.create({

            headers: {
              "Authorization": "management " + userId + " " + token
            }

          });

          xhr.get(CONFIG.pathPrefix + "/api/external/staff/v1/info").then(function (res) {

            var user = res.data.data;

            cookie.set("token", token);

            user.token = token;

            localStorage.setItem("user", JSON.stringify(user));

            userUtil.removeManualLogoutState();

            //next

            var nextUrl = CONFIG.pathPrefix + "/home";
            var query = location.search;
            if(query && query.length > 0)
            {
              query = query.replace("?", "");
              var kvs = query.split("&");

              for(var i = 0, l = kvs.length; i < l; ++ i)
              {
                var kv = kvs[i].split("=");
                if(kv[0] == "redirect_url" && kv[1] && kv[1].length > 0)
                {
                  nextUrl = decodeURIComponent(kv[1]);
                  break;
                }
              }

            }

            location.replace(nextUrl);

          });
        }

      }

    },

    methods: {
      login () {

        var $this = this;

        this.$refs["loginForm"].validate(function (valid) {

          if(valid)
          {
            $this.login_sending = true;

            userUtil.login({
              username: $this.user.username,
              password: $this.user.password
            }, function () {

              $this.login_sending = false;

            }, function () {

              $this.login_sending = false;

            });

          }

        });
      }
    },

    data() {
      return {
        login_sending: false,
        phoneLoginDialogShow: false,
        user: {
          username: "",
          password: ""
        },
        slient_connect_to_old_management: false,
        userFormRules: {
          password: [
            {
              required: true,
              message: "请输入密码",
              trigger: "blur"
            }
          ],
          username: [
            {
              required: true,
              message: "请输入用户名",
              trigger: "blur"
            }
          ]
        }
      }
    }
  }

</script>

<style lang="stylus" scoped>
  div.login-form
    padding-top: 64px;
    width: 320px;
    margin: 0 auto;
</style>
