<template>
  <div class="main">
    <div class="container" >
      <h3 class="title">测试平台</h3>
      <el-form style="margin-left:50px" :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" placeholder="请选择环境" @change="handleSelectChange" style="width: 100%;">
            <el-option v-for="item in nsOptions" :key="item.key" :label="item.desc" :value="item.key"></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>
</template>

<script>
import { getNamespaceList } from '@/api/getXyqbData/'
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)
                this.$router.push('/main')
              }
            })
            .catch((error) => {
              this.$message({ type: 'error', message: error })
            })
        }
      })
    },
    getNamespaceList() {
      getNamespaceList().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;
  }
}
.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>
