<template>
  <div class="main">
    <div class="container">
      <div class="loginForm">
        <h3 class="title">测试平台</h3>
        <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef">
          <el-form-item prop="username" class="input-style">
            <el-input v-model="loginForm.username" placeholder="请输入用户名" prefix-icon="el-icon-user-solid"></el-input>
          </el-form-item>
          <el-form-item prop="password" class="input-style">
            <el-input type="password" @keyup.enter.native="login" v-model="loginForm.password" placeholder="请输入用户名" prefix-icon="el-icon-unlock" show-password></el-input>
          </el-form-item>
          <el-form-item class="input-style" prop="namespace">
            <el-select v-model="loginForm.namespace" filterable placeholder="请选择环境" @change="handleSelectChange" style="width: 100%;">
              <el-option v-for="item in nsOptions" :key="item.name" :label="item.name" :value="item.name"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item class="input-style">
            <el-button type="primary" style="width: 100%; margin-top: 20px;" @click="login">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { getNamespaceList } from '@/api/k8s'
import { login } from '@/api/Login/'
export default {
  data() {
    return {
      namespace: '',
      nsOptions: [],
      loginForm: {
        username: '',
        password: '',
        namespace: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '请输入用户名' }
        ],
        password: [{ required: true, trigger: 'blur', message: '请输入密码' }],
        namespace: [{ required: true, trigger: 'blur', message: '请选择环境' }]
      },
      token: ''
    }
  },
  methods: {
    // 给store.state.env赋值
    handleSelectChange() {
      window.sessionStorage.setItem('env', this.loginForm.namespace)
    },
    login() {
      this.$refs.loginFormRef.validate((valid) => {
        if (!valid) {
          return false
        } else {
          // 调取登陆接口
          login(this.loginForm)
            .then((resp) => {
              if (resp.data.data === false) {
                this.$message.error('用户名或密码错误,请重新登陆！')
              } else {
                // 将token放到Application中
                window.sessionStorage.setItem('token', resp.data.data.token)
                // 设置当前选中的菜单
                window.sessionStorage.setItem('activePath', '/index/list')
                this.$router.push('/index/list')
              }
            })
            .catch((error) => {
              this.$message({ type: 'error', message: error })
            })
        }
      })
    },
    getNamespaceList() {
      getNamespaceList({ env: 'all' }).then((resp) => {
        this.nsOptions = resp.data.data
      })
    }
  },
  created() {
    this.getNamespaceList()
  }
}
</script>

<style lang="less" scoped>
.main {
  position: fixed;
  height: 100%;
  width: 100%;
  // background-color: #2d3a4b;
  background-image: url('../../assets/image/quant.png');
  background-size: contain;
  .container {
    height: 300px;
    width: 400px;
    margin: 200px auto;
    // padding: 20px;
  }
}
.loginForm {
  padding: 20px;
  background: rgba(48, 49, 51, 0.43);
  box-shadow: 4px 4px 4px 0px rgb(36, 41, 41);
}
.title {
  text-align: center;
  font-size: 26px;
  color: #ffffff;
}
.area {
  margin-top: 40px;
}
.input-style {
  width: 350px;
  height: 50px;
}
/deep/.el-input__inner {
  height: 45px !important;
}
</style>
