<template>
  <div class="agent-login-page">
    <div class="login-container">
      <div class="login-card">
        <div class="login-header">
          <h2>区域代理登录</h2>
          <p>浙江省区域代理管理系统</p>
        </div>
        
        <div class="image">
          <img src="/imgs/login-agent.png" alt="区域代理管理系统">
        </div>
        
        <el-form 
          ref="loginFormRef" 
          :model="loginForm" 
          :rules="loginRules" 
          class="login-form"
          @submit.prevent="handleLogin"
        >
          <el-form-item prop="phone">
            <el-input
              v-model="loginForm.phone"
              placeholder="请输入手机号"
              prefix-icon="Phone"
              size="large"
              clearable
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              prefix-icon="Lock"
              size="large"
              show-password
              clearable
              @keyup.enter="handleLogin"
            />
          </el-form-item>
          
          <el-form-item>
            <el-button 
              type="primary" 
              size="large" 
              class="login-button"
              :loading="loading"
              @click="handleLogin"
            >
              {{ loading ? '登录中...' : '登录' }}
            </el-button>
          </el-form-item>
        </el-form>
        
        <div class="login-footer">
          <el-link type="primary" @click="showApplyDialog = true">
            申请成为区域代理
          </el-link>
        </div>
      </div>
    </div>
    
    <!-- 背景装饰 -->
    <div class="background-decoration">
      <div class="decoration-circle circle-1"></div>
      <div class="decoration-circle circle-2"></div>
      <div class="decoration-circle circle-3"></div>
    </div>
    
    <!-- 申请代理对话框 -->
    <el-dialog
      v-model="showApplyDialog"
      title="申请成为区域代理"
      width="500px"
      :close-on-click-modal="false"
    >
      <el-alert
        title="重要提示"
        type="info"
        :closable="false"
        style="margin-bottom: 20px"
      >
        <template #default>
          <p>• 每个区域只能有一个代理账号</p>
          <p>• 每个用户只能申请一个区域的代理</p>
          <p>• 申请提交后需要等待管理员审核</p>
        </template>
      </el-alert>
      
      <el-form
        ref="applyFormRef"
        :model="applyForm"
        :rules="applyRules"
        label-width="100px"
      >
        <el-form-item label="选择区域" prop="region_id">
<!--          <el-select-->
<!--            v-model="applyForm.region_id"-->
<!--            placeholder="请选择代理区域"-->
<!--            style="width: 100%"-->
<!--            filterable-->
<!--          >-->
<!--            <el-option-group-->
<!--              v-for="city in groupedRegions"-->
<!--              :key="city.name"-->
<!--              :label="city.name"-->
<!--            >-->
<!--              <el-option-->
<!--                v-for="region in city.districts"-->
<!--                :key="region.id"-->
<!--                :label="region.district_name"-->
<!--                :value="region.id"-->
<!--              />-->
<!--            </el-option-group>-->
<!--          </el-select>-->

          <el-cascader v-model="applyForm.region_id"
                       popper-class="custom-cascader"
                       :show-all-levels="false"
                       placeholder="请选择代理区域"
                       :options="regions"
                       :props="regionsProp"
                       clearable />

        </el-form-item>
        
        <el-form-item label="真实姓名" prop="real_name">
          <el-input v-model="applyForm.real_name" placeholder="请输入真实姓名" />
        </el-form-item>
        
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="applyForm.phone" placeholder="请输入手机号" />
        </el-form-item>
        
        <el-form-item label="身份证号" prop="id_card">
          <el-input v-model="applyForm.id_card" placeholder="请输入身份证号" />
        </el-form-item>
        
        <el-form-item label="联系地址" prop="contact_address">
          <el-input
            v-model="applyForm.contact_address"
            type="textarea"
            placeholder="请输入联系地址"
            :rows="3"
          />
        </el-form-item>
      </el-form>
      
      <template #footer>
        <el-button @click="showApplyDialog = false">取消</el-button>
        <el-button type="primary" :loading="applyLoading" @click="handleApply">
          提交申请
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import api from '@/utils/api'

const router = useRouter()

// 响应式数据
const loading = ref(false)
const applyLoading = ref(false)
const showApplyDialog = ref(false)
const regions = ref([])
const loginFormRef = ref()
const applyFormRef = ref()

// 登录表单
const loginForm = reactive({
  phone: '',
  password: ''
})

// 申请表单
const applyForm = reactive({
  region_id: '',
  real_name: '',
  phone: '',
  id_card: '',
  contact_address: ''
})

// 表单验证规则
const loginRules = {
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const applyRules = {
  region_id: [
    { required: true, message: '请选择代理区域', trigger: 'change' }
  ],
  real_name: [
    { required: true, message: '请输入真实姓名', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
  ],
  id_card: [
    { required: true, message: '请输入身份证号', trigger: 'blur' },
    { pattern: /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/, message: '身份证号格式不正确', trigger: 'blur' }
  ]
}

// 设置省市区级联选择器配置
const regionsProp = {
  value: 'code',
  label: 'label',
  children: 'children'
}

// 计算属性
const groupedRegions = computed(() => {
  const grouped = {}
  regions.value.forEach(region => {
    if (!grouped[region.city_name]) {
      grouped[region.city_name] = {
        name: region.city_name,
        districts: []
      }
    }
    grouped[region.city_name].districts.push(region)
  })
  return Object.values(grouped)
})

// 方法
const loadRegions = async () => {
  try {
    const { data } = await api.get('/regions/provinces')
    regions.value = data.data
  } catch (error) {
    ElMessage.error('加载区域列表失败')
  }
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
    loading.value = true
    
    const { data } = await api.post('/agents/login', loginForm)
    
    if (data.success) {
      // 保存代理信息和token到localStorage
      localStorage.setItem('agentInfo', JSON.stringify(data.data))
      localStorage.setItem('token', data.data.token)
      ElMessage.success('登录成功')
      router.push('/agent/dashboard')
    }
  } catch (error) {
    if (error.response?.data?.message) {
      ElMessage.error(error.response.data.message)
    } else {
      ElMessage.error('登录失败')
    }
  } finally {
    loading.value = false
  }
}

const handleApply = async () => {
  if (!applyFormRef.value) return
  try {
    await applyFormRef.value.validate()
    applyLoading.value = true

    // 将区id设置为最终id
    applyForm.region_id = applyForm.region_id[2]
    const { data } = await api.post('/agents/apply', applyForm)
    
    if (data.success) {
      ElMessage.success(data.message)
      showApplyDialog.value = false
      // 重置表单
      Object.keys(applyForm).forEach(key => {
        applyForm[key] = ''
      })
    }
  } catch (error) {
    if (error.response?.data?.message) {
      ElMessage.error(error.response.data.message)
    } else {
      ElMessage.error('申请失败')
    }
  } finally {
    applyLoading.value = false
  }
}

// 生命周期
onMounted(() => {
  loadRegions()
})
</script>

<style scoped lang="scss">
/* 使用与主登录页面一致的样式 */
.agent-login-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #a2f6ff 0%, #f3f3f3 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}

.login-container {
  width: 100%;
  max-width: 400px;
  padding: 20px;
  position: relative;
  z-index: 10;
}

.login-card {
  backdrop-filter: blur(10px);
  border-radius: 16px;
  padding: 40px 30px;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  color: #4784ff;
  margin-bottom: 8px;
  font-weight: 600;
}

.login-header p {
  color: #666;
  font-size: 14px;
}

/* 图片容器样式 */
.image {
  width: 375px;
  height: 200px;
  margin: 0 auto 20px;
  overflow: hidden;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.image img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

/* 响应式适配 */
@media (max-width: 375px) {
  .image {
    width: 100%;
    height: auto;
    aspect-ratio: 375 / 200;
  }
}

.login-form {
  margin-bottom: 20px;
}

.login-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  font-weight: 600;
}

.login-footer {
  text-align: center;
  margin-bottom: 20px;
}

.login-footer .el-link {
  font-size: 14px;
}

.background-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.decoration-circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 6s ease-in-out infinite;
}

.circle-1 {
  width: 200px;
  height: 200px;
  top: 10%;
  left: 10%;
  animation-delay: 0s;
}

.circle-2 {
  width: 150px;
  height: 150px;
  top: 60%;
  right: 10%;
  animation-delay: 2s;
}

.circle-3 {
  width: 100px;
  height: 100px;
  bottom: 20%;
  left: 20%;
  animation-delay: 4s;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
  }
}

/* 输入框样式 */
:deep(.el-input__wrapper) {
  background: transparent !important;
  border-radius: 8px;
}

:deep(.el-input__wrapper:hover),
:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #409eff inset;
}

:deep(.el-form-item.is-error .el-input__wrapper) {
  box-shadow: 0 0 0 1px #f56c6c inset;
}

:deep(.el-form-item.is-success .el-input__wrapper) {
  box-shadow: 0 0 0 1px #67c23a inset;
}

:deep(.el-button) {
  border-radius: 8px;
}

/* 对话框样式调整 */
:deep(.el-dialog) {
  border-radius: 12px;
}

:deep(.el-dialog__header) {
  border-bottom: 1px solid #eee;
  padding: 15px 20px;
  margin-right: 0;
}

:deep(.el-dialog__title) {
  font-weight: 600;
  color: #333;
}

:deep(.el-dialog__body) {
  padding: 20px;
}

:deep(.el-dialog__footer) {
  border-top: 1px solid #eee;
  padding: 15px 20px;
}

/* 动画效果 */
.login-card {
  animation: slideInUp 0.6s ease-out;
}

@keyframes slideInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-container {
    padding: 15px;
  }
  
  .login-card {
    padding: 30px 20px;
  }
}
</style>

<style>
.custom-cascader{
  .el-cascader-panel{
    .el-cascader-menu{
      min-width: 100px;
    }
  }
}
</style>