<template>
  <div class="quota-apply-container">
    <div class="page-header">
      <el-button type="primary" link @click="handleBack"><el-icon><ArrowLeft /></el-icon>返回</el-button>
      <span class="page-title">额度申请</span>
    </div>

    <el-form ref="formRef" :model="formData" :rules="rules" label-position="top" class="apply-form">
      <div class="form-row">
        <el-form-item label="月度采购金额" prop="monthAmount">
          <el-input v-model="formData.monthAmount" placeholder="请输入">
            <template #suffix>元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="年度采购金额" prop="yearAmount">
          <el-input v-model="formData.yearAmount" placeholder="请输入">
            <template #suffix>元</template>
          </el-input>
        </el-form-item>
      </div>

      <div class="form-row">
        <el-form-item label="申请信用金额" prop="creditAmount">
          <el-input v-model="formData.creditAmount" placeholder="请输入">
            <template #suffix>元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="账期时间" prop="accountPeriod">
          <el-select v-model="formData.accountPeriod" placeholder="请选择" style="width: 100%">
            <el-option label="30天" value="30" />
            <el-option label="60天" value="60" />
            <el-option label="90天" value="90" />
          </el-select>
        </el-form-item>
      </div>

      <el-form-item label="主要办公采购类目" prop="category">
        <div class="category-tags">
          <div v-for="item in categoryList" :key="item" :class="['tag-item', { active: formData.category === item }]" @click="formData.category = item">
            {{ item }}
          </div>
        </div>
      </el-form-item>

      <el-form-item label="其他采购类目" prop="otherCategory">
        <el-input v-model="formData.otherCategory" type="textarea" :rows="2" placeholder="请输入" maxlength="50" show-word-limit />
      </el-form-item>

      <el-form-item label="上传合作协议" prop="agreement">
        <el-upload action="#" :auto-upload="false" :limit="1" list-type="picture-card">
          <div class="upload-content">
            <el-icon><Plus /></el-icon>
            <span>上传</span>
          </div>
        </el-upload>
      </el-form-item>

      <div class="form-actions">
        <el-button type="danger" @click="handleSave">保存</el-button>
        <el-button @click="handleBack">取消</el-button>
      </div>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ArrowLeft, Plus } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const router = useRouter()
const formRef = ref()

const formData = reactive({
  monthAmount: '',
  yearAmount: '',
  creditAmount: '',
  accountPeriod: '',
  category: '办公耗材',
  otherCategory: ''
})

const categoryList = ['办公耗材', '办公设备', '数码设备', '办公日用', '办公日用', '办公日用', '办公日用', '办公日用', '办公日用']

const rules = {
  monthAmount: [{ required: true, message: '请输入月度采购金额', trigger: 'blur' }],
  creditAmount: [{ required: true, message: '请输入申请信用金额', trigger: 'blur' }]
}

const handleBack = () => { router.back() }

const handleSave = async () => {
  const valid = await formRef.value?.validate()
  if (!valid) return
  ElMessage.success('申请提交成功')
  router.back()
}
</script>

<style scoped lang="scss">
.quota-apply-container {
  padding: 20px;
  background: #fff;
  min-height: 100%;
}

.page-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 25px;
  .page-title { font-size: 16px; font-weight: bold; color: #333; }
}

.apply-form {
  max-width: 900px;

  .form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px 40px;
  }

  :deep(.el-form-item__label) {
    font-size: 14px;
    color: #333;
    padding-bottom: 8px;
  }

  :deep(.el-input__wrapper),
  :deep(.el-select__wrapper),
  :deep(.el-textarea__inner) {
    background: #f5f5f5;
    box-shadow: none;
  }
}

.category-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;

  .tag-item {
    padding: 6px 16px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 13px;
    color: #666;
    background: #f5f5f5;
    transition: all 0.2s;

    &:hover { color: #e60012; }
    &.active { background: #e60012; color: #fff; }
  }
}

.upload-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #999;
  font-size: 12px;
  .el-icon { font-size: 20px; margin-bottom: 5px; }
}

.form-actions {
  margin-top: 30px;
  display: flex;
  gap: 15px;
}
</style>
