<template>
  <view class="edit-container">
    <!-- 头部 -->
    <view class="header">
      <uni-icons type="back" size="28" color="#303133" @click="handleCancel" />
      <text class="title">修改分销员</text>
      <view class="header-right"></view>
    </view>
    
    <!-- 表单容器 -->
    <view class="form-container">
      <uni-forms :model="formData" ref="formRef" labelWidth="100rpx">
        <uni-forms-item label="姓名" required name="name">
          <uni-easyinput 
            v-model="formData.name" 
            placeholder="请输入姓名" 
            class="form-input"
          />
        </uni-forms-item>
        
        <uni-forms-item label="手机号" required name="phone">
          <uni-easyinput 
            v-model="formData.phone" 
            placeholder="请输入手机号" 
            class="form-input"
          />
        </uni-forms-item>
        
        <uni-forms-item label="OpenID" required name="openid">
          <uni-easyinput 
            v-model="formData.openid" 
            placeholder="请输入OpenID" 
            class="form-input"
          />
        </uni-forms-item>
        
        <uni-forms-item label="父ID" required name="parent_id">
          <uni-easyinput 
            v-model="formData.parent_id" 
            placeholder="请输入父ID" 
            type="number" 
            class="form-input"
          />
        </uni-forms-item>
        
        <uni-forms-item label="租户ID" required name="tenant_id">
          <uni-easyinput 
            v-model="formData.tenant_id" 
            placeholder="请输入租户ID" 
            type="number" 
            class="form-input"
          />
        </uni-forms-item>
        
        <uni-forms-item label="苹果URL" name="applet_url">
          <uni-easyinput 
            v-model="formData.applet_url" 
            placeholder="请输入苹果URL" 
            class="form-input"
          />
        </uni-forms-item>
        
        <uni-forms-item label="状态" name="is_active">
          <uni-data-checkbox 
            v-model="formData.is_active" 
            :localdata="statusOptions" 
            style="width: 100%"
          />
        </uni-forms-item>
      </uni-forms>
    </view>
    
    <!-- 按钮容器 -->
    <view class="btn-container">
      <button class="cancel-btn" @click="handleCancel">
        <uni-icons type="close" size="20" color="#fff" />
        <text class="btn-text">取消</text>
      </button>
      <button class="submit-btn" @click="handleSubmit">
        <uni-icons type="checkmark" size="20" color="#fff" />
        <text class="btn-text">提交</text>
      </button>
    </view>
  </view>
</template>

<script setup>
  import { ref, onMounted } from "vue"

  const formRef = ref(null)
  const distributorId = ref(uni.getStorageSync('__uni_route_query')?.id || '')
  
  const formData = ref({
    id: '',
    name: '',
    phone: '',
    openid: '',
    parent_id: '',
    tenant_id: '',
    applet_url: '',
    is_active: 'Y'
  })
  
  const statusOptions = [
    { text: '激活', value: 'Y' },
    { text: '未激活', value: 'N' }
  ]

  // 初始化加载数据
  onMounted(() => {
    fetchDistributorDetail()
  })

  // 获取分销员详情
  function fetchDistributorDetail() {
    // 这里应该调用后端API获取数据
    // 模拟数据
    setTimeout(() => {
      formData.value = {
        id: distributorId.value,
        name: '张三',
        phone: '13800138001',
        openid: 'openid123456',
        parent_id: '0',
        tenant_id: '1',
        applet_url: 'https://example.com',
        is_active: 'Y'
      }
    }, 500)
  }

  // 取消
  function handleCancel() {
    uni.navigateBack()
  }

  // 提交
  function handleSubmit() {
    formRef.value.validate().then(() => {
      // 这里应该调用后端API提交数据
      uni.showToast({ title: '修改成功' })
      uni.navigateTo({ url: '/pages/distributor/index' })
    }).catch(err => {
      console.log('表单验证失败', err)
    })
  }
</script>

<style lang="scss" scoped>
  .edit-container {
    padding: 20rpx;
    background-color: #f5f7fa;
    min-height: 100vh;
  }
  
  /* 头部样式 */
  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 24rpx;
    padding-bottom: 16rpx;
    border-bottom: 2rpx solid #ecf5ff;
  }
  
  .title {
    font-size: 32rpx;
    font-weight: 600;
    color: #303133;
  }
  
  .header-right {
    width: 28px;
  }
  
  /* 表单容器样式 */
  .form-container {
    background-color: #fff;
    border-radius: 12rpx;
    padding: 24rpx;
    margin-bottom: 24rpx;
    box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08);
  }
  
  /* 表单输入框样式 */
  .form-input {
    border-radius: 8rpx !important;
    border: 1rpx solid #dcdfe6 !important;
    transition: all 0.3s ease !important;
  }
  
  .form-input:focus {
    border-color: #409eff !important;
    box-shadow: 0 0 0 2rpx rgba(64, 158, 255, 0.2) !important;
  }
  
  /* 按钮容器样式 */
  .btn-container {
    display: flex;
    gap: 16rpx;
    margin-top: 32rpx;
  }
  
  .cancel-btn,
  .submit-btn {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    padding: 16rpx 0;
    border-radius: 8rpx;
    font-size: 28rpx;
    transition: all 0.3s ease;
    box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
  }
  
  .cancel-btn:active,
  .submit-btn:active {
    transform: translateY(2rpx);
    box-shadow: 0 1rpx 4rpx rgba(0, 0, 0, 0.1);
  }
  
  .cancel-btn {
    background-color: #909399;
    color: #fff;
  }
  
  .submit-btn {
    background-color: #409eff;
    color: #fff;
  }
  
  .btn-text {
    margin-left: 8rpx;
  }
  
  /* 响应式设计 */
  @media screen and (max-width: 750rpx) {
    .form-container {
      padding: 20rpx;
    }
    
    .btn-container {
      margin-top: 24rpx;
    }
  }
</style>