<template>
  <el-dialog :title="$t('roomRenovationManage.add')" :visible.sync="visible" width="50%" @close="resetForm">
    <el-form :model="form" ref="form" label-width="120px">
      <el-form-item :label="$t('roomRenovationManage.room')" prop="roomName" required>
        <el-input v-model.trim="form.roomName"  :placeholder="$t('roomRenovationManage.roomNumberPlaceholder')"
          @blur="queryRoom" />
      </el-form-item>

      <el-form-item :label="$t('roomRenovationManage.contactPerson')" prop="personName" required>
        <el-input v-model.trim="form.personName" :placeholder="$t('roomRenovationManage.contactPersonPlaceholder')"
          disabled />
      </el-form-item>

      <el-form-item :label="$t('roomRenovationManage.phone')" prop="personTel" required>
        <el-input v-model.trim="form.personTel" :placeholder="$t('roomRenovationManage.phonePlaceholder')" disabled />
      </el-form-item>

      <el-form-item :label="$t('roomRenovationManage.renovationStartTime')" prop="startTime" required>
        <el-date-picker v-model="form.startTime" type="date"
          :placeholder="$t('roomRenovationManage.startTimePlaceholder')" value-format="yyyy-MM-dd" />
      </el-form-item>

      <el-form-item :label="$t('roomRenovationManage.renovationEndTime')" prop="endTime" required>
        <el-date-picker v-model="form.endTime" type="date" :placeholder="$t('roomRenovationManage.endTimePlaceholder')"
          value-format="yyyy-MM-dd" />
      </el-form-item>

      <el-form-item :label="$t('roomRenovationManage.renovationCompany')" prop="renovationCompany" required>
        <el-input v-model.trim="form.renovationCompany"
          :placeholder="$t('roomRenovationManage.renovationCompanyPlaceholder')" />
      </el-form-item>

      <el-form-item :label="$t('roomRenovationManage.renovationManager')" prop="personMain" required>
        <el-input v-model.trim="form.personMain" :placeholder="$t('roomRenovationManage.renovationManagerPlaceholder')" />
      </el-form-item>

      <el-form-item :label="$t('roomRenovationManage.managerPhone')" prop="personMainTel" required>
        <el-input v-model.trim="form.personMainTel" :placeholder="$t('roomRenovationManage.managerPhonePlaceholder')" />
      </el-form-item>

      <el-form-item :label="$t('roomRenovationManage.remark')" prop="remark">
        <el-input v-model.trim="form.remark" type="textarea"
          :placeholder="$t('roomRenovationManage.remarkPlaceholder')" />
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        {{ $t('roomRenovationManage.cancel') }}
      </el-button>
      <el-button type="primary" @click="saveRoomRenovation">
        {{ $t('roomRenovationManage.save') }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addRoomRenovation } from '@/api/community/roomRenovationManageApi'

export default {
  name: 'AddRoomRenovation',
  data() {
    return {
      visible: false,
      form: {
        roomName: '',
        personName: '',
        personTel: '',
        startTime: '',
        endTime: '',
        renovationCompany: '',
        personMain: '',
        personMainTel: '',
        remark: '',
        isPostpone: 'N',
        communityId: ''
      }
    }
  },
  methods: {
    open() {
      this.visible = true
    },

    resetForm() {
      this.form = {
        roomName: '',
        personName: '',
        personTel: '',
        startTime: '',
        endTime: '',
        renovationCompany: '',
        personMain: '',
        personMainTel: '',
        remark: '',
        isPostpone: 'N',
        communityId: this.getCommunityId()
      }
      this.$refs.form && this.$refs.form.resetFields()
    },

    async saveRoomRenovation() {
      try {
        this.form.communityId = this.getCommunityId()
        await addRoomRenovation(this.form)
        this.$message.success(this.$t('common.operationSuccess'))
        this.visible = false
        this.$emit('success')
      } catch (error) {
        console.error('添加装修信息失败:', error)
        this.$message.error(error.message || this.$t('common.operationFailed'))
      }
    },

    async queryRoom() {
      if (!this.form.roomName) return

      try {
        // 这里需要根据实际API实现查询房屋信息
        // const roomInfo = await getRoomInfo({ roomName: this.form.roomName })
        // this.form.personName = roomInfo.ownerName
        // this.form.personTel = roomInfo.link
      } catch (error) {
        console.error('查询房屋信息失败:', error)
      }
    }
  }
}
</script>