<template>
  <div class="region-add">
    <el-card>
      <template slot="header">
        <div class="card-header">
          <span>添加区域</span>
        </div>
      </template>
      <el-form :model="regionForm" :rules="rules" ref="regionForm" label-width="100px" class="form-content">
        <el-form-item label="区域名称" prop="name">
          <el-input v-model="regionForm.name" placeholder="请输入区域名称"></el-input>
        </el-form-item>
        <el-form-item label="区域描述" prop="description">
          <el-input type="textarea" v-model="regionForm.description" placeholder="请输入区域描述" rows="4"></el-input>
        </el-form-item>
        <el-form-item class="form-actions">
          <el-button type="primary" @click="submitForm">提交</el-button>
          <el-button @click="resetForm">重置</el-button>
          <el-button @click="goBack">返回</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { regionApi } from '../../api/api'

export default {
  name: 'RegionAdd',
  data() {
    return {
      regionForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入区域名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async submitForm() {
      this.$refs.regionForm.validate(async (valid) => {
        if (valid) {
          try {
            await regionApi.create(this.regionForm)
            this.$message.success('添加成功')
            this.$router.push('/region/list')
          } catch (error) {
            this.$message.error('添加失败')
          }
        } else {
          return false
        }
      })
    },
    resetForm() {
      this.$refs.regionForm.resetFields()
    },
    goBack() {
      this.$router.push('/region/list')
    }
  }
}
</script>

<style scoped>
.region-add {
  padding: 0;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.form-content {
  max-width: 600px;
}

.form-actions {
  margin-top: 30px;
}

/* 移动端适配 */
@media screen and (max-width: 768px) {
  .form-content {
    max-width: 100%;
  }
  
  .form-content .el-form-item__label {
    float: none;
    display: block;
    text-align: left;
    margin-bottom: 5px;
  }
  
  .form-content .el-form-item__content {
    margin-left: 0 !important;
  }
  
  .form-actions {
    text-align: center;
  }
  
  .form-actions .el-button {
    margin-bottom: 10px;
  }
}
</style>
