<template>
  <div class="agent-login-container">
    <div class="login-card">
      <div class="login-header">
        <h2>区域代理登录</h2>
        <p>浙江省区域代理管理系统</p>
      </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"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            size="large"
            show-password
          />
        </el-form-item>
        
        <el-form-item>
          <el-button 
            type="primary" 
            size="large" 
            class="login-btn"
            :loading="loading"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="login-footer">
        <el-link type="primary" @click="showApplyDialog = true">
          申请成为区域代理
        </el-link>
      </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-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 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('/agents/regions')
    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) {
      // 保存代理信息到localStorage
      localStorage.setItem('agentInfo', JSON.stringify(data.data))
      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
    
    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>
.agent-login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 100%;
  max-width: 400px;
}

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

.login-header h2 {
  color: #333;
  margin-bottom: 8px;
  font-size: 24px;
  font-weight: 600;
}

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

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

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

.login-footer {
  text-align: center;
  padding-top: 20px;
  border-top: 1px solid #eee;
}

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

:deep(.el-button) {
  border-radius: 8px;
}
</style>