<template>
  <div class="house-edit">
    <el-card>
      <template slot="header">
        <div class="card-header">
          <span>编辑房源</span>
        </div>
      </template>
      <el-form :model="houseForm" :rules="rules" ref="houseForm" label-width="100px">
        <el-form-item label="区域" prop="regionId">
          <el-select v-model="houseForm.regionId" placeholder="请选择区域">
            <el-option v-for="region in regions" :key="region.id" :label="region.name" :value="region.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="houseForm.address" placeholder="请输入地址"></el-input>
        </el-form-item>
        <el-form-item label="面积(㎡)" prop="area">
          <el-input type="number" v-model="houseForm.area" placeholder="请输入面积"></el-input>
        </el-form-item>
        <el-form-item label="租金(元/月)" prop="price">
          <el-input type="number" v-model="houseForm.price" placeholder="请输入租金"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="houseForm.status" placeholder="请选择状态">
            <el-option label="可租" value="available"></el-option>
            <el-option label="已租" value="rented"></el-option>
            <el-option label="维护中" value="maintenance"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <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, houseApi } from '../../api/api'

export default {
  name: 'HouseEdit',
  data() {
    return {
      houseForm: {
        id: '',
        regionId: '',
        address: '',
        area: '',
        price: '',
        status: ''
      },
      regions: [],
      rules: {
        regionId: [
          { required: true, message: '请选择区域', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入地址', trigger: 'blur' },
          { min: 5, max: 100, message: '长度在 5 到 100 个字符', trigger: 'blur' }
        ],
        area: [
          { required: true, message: '请输入面积', trigger: 'blur' },
          { type: 'number', message: '请输入数字', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '请输入租金', trigger: 'blur' },
          { type: 'number', message: '请输入数字', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.loadRegions()
    this.loadHouseData()
  },
  methods: {
    async loadRegions() {
      try {
        const response = await regionApi.getAll()
        this.regions = response
      } catch (error) {
        this.$message.error('加载区域数据失败')
      }
    },
    async loadHouseData() {
      try {
        const id = this.$route.params.id
        const house = await houseApi.getById(id)
        if (house) {
          this.houseForm = house
        }
      } catch (error) {
        this.$message.error('加载房源数据失败')
      }
    },
    async submitForm() {
      this.$refs.houseForm.validate(async (valid) => {
        if (valid) {
          try {
            await houseApi.update(this.houseForm.id, this.houseForm)
            this.$message.success('编辑成功')
            this.$router.push('/house/list')
          } catch (error) {
            this.$message.error('编辑失败')
          }
        } else {
          return false
        }
      })
    },
    resetForm() {
      this.loadHouseData()
    },
    goBack() {
      this.$router.push('/house/list')
    }
  }
}
</script>

<style scoped>
.house-edit {
  padding: 20px 0;
}

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