<template>
  <div ref="main"
       class="login">
    <div class="login-container">
      <div class="login-box">
        <form @submit.prevent="handleSubmit"
              class="login-form">
          <div class="form-group">
            <label for="username">{{usernameLabel}}</label>
            <input type="text"
                   id="username"
                   v-model="username"
                   :placeholder="'请输入'+usernameLabel"
                   :class="{'input-error': errors.username}" />
            <span v-if="errors.username"
                  class="error-message">{{ errors.username }}</span>
          </div>

          <div class="form-group">
            <label for="password">{{ passwordLabel }}</label>
            <input type="password"
                   id="password"
                   v-model="password"
                   :placeholder="'请输入'+passwordLabel"
                   :class="{'input-error': errors.password}" />
            <span v-if="errors.password"
                  class="error-message">{{ errors.password }}</span>
          </div>

          <button type="submit"
                  class="login-btn">登录</button>
        </form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "login",
  props: {
    option: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data () {
    return {
      username: '',
      password: '',
      errors: {
        username: '',
        password: ''
      }
    }
  },
  computed: {
    usernameLabel () {
      return this.option.usernameLabel || '用户名'
    },
    passwordLabel () {
      return this.option.passwordLabel || '密码'
    }
  },
  created () {

  },
  methods: {
    handleSubmit () {
      let result = false;
      this.errors.username = '';
      this.errors.password = '';
      if (!this.username) {
        this.errors.username = this.usernameLabel + '不能为空';
      }

      if (!this.password) {
        this.errors.password = this.passwordLabel + '不能为空';
      }

      if (!this.errors.username && !this.errors.password) {
        let { userList } = this.option
        userList.forEach(ele => {
          const data = ele.split(',')
          const username = data[0]
          const password = data[1]
          if (this.username == username && this.password == password) {
            result = true
          }
        })
        if (result) {
          this.$message.success('登录成功');
          this.handleClick(true)
        } else {
          this.handleClick(false)
          this.$message.error('登录失败');
        }
      }
    }
  }
}
</script>
<style scoped>
.login-container {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100;
}

.login-box {
  width: 100%;
  height: 100%;
  padding: 60px 40px;
  box-shadow: var(--shadow-card);
  border-radius: 8px;
  box-sizing: border-box;
  text-align: center;
}

.form-group {
  margin-bottom: 20px;
  text-align: left;
}

label {
  display: block;
  font-size: 24px;
  margin-bottom: 8px;
  color: var(--text-primary);
}

input {
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
  font-size: 26px;
  border: 1px solid var(--border-light);
  border-radius: 4px;
  outline: none;
  background-color: var(--bg-primary);
  color: var(--text-primary);
}

input:focus {
  border-color: var(--color-primary);
}

.error-message {
  font-size: 12px;
  color: var(--color-danger);
  margin-top: 5px;
}

button {
  margin-top: 50px;
  width: 100%;
  padding: 30px;
  font-size: 24px;
  background-color: var(--color-primary);
  color: var(--text-on-primary);
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

button:hover {
  background-color: var(--color-primary-hover);
}

.input-error {
  border-color: var(--color-danger);
}
</style>