<template>
  <div class="dialog-overlay" @click="handleOverlayClick">
    <div class="dialog" @click.stop>
      <div class="dialog-header">
        <h3>用户登录</h3>
        <button v-if="allowClose" class="close-btn" @click="$emit('close')">×</button>
      </div>
      <div class="dialog-body">
        <div class="form-group">
          <label>用户名：</label>
          <input type="text" v-model="username" placeholder="请输入用户名" />
        </div>
        <div class="form-group">
          <label>密码：</label>
          <input type="password" v-model="password" placeholder="请输入密码" />
        </div>
        <div v-if="errorMessage" class="error-message">
          {{ errorMessage }}
        </div>
      </div>
      <div class="dialog-footer">
        <button v-if="allowClose" class="btn-secondary" @click="$emit('close')">取消</button>
        <button class="btn-primary" @click="handleLogin" :disabled="isLogging">
          {{ isLogging ? '登录中...' : '登录' }}
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { AuthService } from '../../bindings/videoconcat/services/index.js'

const props = defineProps({
  allowClose: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['close', 'login-success'])

const username = ref('')
const password = ref('')
const isLogging = ref(false)
const errorMessage = ref('')

// 处理遮罩层点击
const handleOverlayClick = () => {
  if (props.allowClose) {
    emit('close')
  }
}

const handleLogin = async () => {
  if (!username.value || !password.value) {
    errorMessage.value = '请输入用户名和密码'
    return
  }

  isLogging.value = true
  errorMessage.value = ''

  try {
    const response = await AuthService.Login(username.value, password.value)
    if (response && response.Code === 200) {
      emit('login-success')
      emit('close')
    } else {
      errorMessage.value = (response && response.Msg) || '登录失败'
    }
  } catch (error) {
    errorMessage.value = '登录失败: ' + (error.message || error)
  } finally {
    isLogging.value = false
  }
}
</script>

<style scoped>
.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.dialog {
  background: white;
  border-radius: 8px;
  width: 400px;
  max-width: 90vw;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #eee;
}

.dialog-header h3 {
  margin: 0;
  color: #333;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: #999;
  padding: 0;
  width: 30px;
  height: 30px;
  line-height: 30px;
}

.close-btn:hover {
  color: #333;
}

.dialog-body {
  padding: 20px;
}

.form-group {
  margin-bottom: 15px;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
  color: #555;
  font-weight: 500;
}

.form-group input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
}

.form-group input:focus {
  outline: none;
  border-color: #7163ba;
}

.error-message {
  color: #dc3545;
  font-size: 14px;
  margin-top: 10px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 20px;
  border-top: 1px solid #eee;
}

.btn-primary {
  padding: 8px 16px;
  background: #7163ba;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.btn-primary:hover:not(:disabled) {
  background: #5a5080;
}

.btn-primary:disabled {
  background: #ccc;
  cursor: not-allowed;
}

.btn-secondary {
  padding: 8px 16px;
  background: #6c757d;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.btn-secondary:hover {
  background: #5a6268;
}
</style>

