<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="monthPurchase">
          <el-input v-model="formData.monthPurchase" placeholder="请输入">
            <template #suffix>元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="年度采购金额" prop="yearPurchase">
          <el-input v-model="formData.yearPurchase" placeholder="请输入">
            <template #suffix>元</template>
          </el-input>
        </el-form-item>
      </div>

      <div class="form-row">
        <el-form-item label="申请信用金额" prop="applyAmount">
          <el-input v-model="formData.applyAmount" placeholder="请输入">
            <template #suffix>元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="账期时间" prop="amountPeriod">
          <el-select v-model="formData.amountPeriod" 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="mainPurchase">
        <div class="mainPurchase-tags">
          <div
            v-for="item in categoryList"
            :key="item.value"
            :class="['tag-item', { active: formData.mainPurchase === item.value }]"
            @click="formData.mainPurchase = item.value"
          >
            {{ item.label }}
          </div>
        </div>
      </el-form-item>

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

      <el-form-item label="上传合作协议" prop="cooperationImage">
        <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';
import { addCreditApply } from '@/api/pc/cost/creditApply';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { purchase_item } = toRefs<any>(proxy?.useDict('purchase_item'));
const router = useRouter();
const formRef = ref();

const formData = reactive({
  monthPurchase: '',
  yearPurchase: '',
  applyAmount: '',
  amountPeriod: '',
  mainPurchase: '0',
  otherPurchase: '',
  cooperationImage: ''
});

const categoryList = computed(() => purchase_item.value || []);
const rules = {
  monthPurchase: [{ required: true, message: '请输入月度采购金额', trigger: 'blur' }],
  applyAmount: [{ required: true, message: '请输入申请信用金额', trigger: 'blur' }]
};

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

const handleSave = async () => {
  const valid = await formRef.value?.validate();
  if (!valid) return;
  try {
    await addCreditApply(formData);
    ElMessage.success('保存成功');
    handleBack();
  } catch (error) {
    ElMessage.error('保存失败');
  }
};
</script>

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

.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;
  }
}

.mainPurchase-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>
