<template>
  <view class="edit-container">
    <common-header title="编辑酒厂" theme="distiller" @back="goBack" />
    
    <view class="edit-content">
      <uni-forms ref="form" :model="formData" :rules="rules" label-width="120">
        <uni-forms-item label="酒厂名称" required name="name">
          <uni-easyinput v-model="formData.name" placeholder="请输入酒厂名称" />
        </uni-forms-item>
        
        <uni-forms-item label="酒厂地址" name="address">
          <uni-easyinput v-model="formData.address" placeholder="请输入酒厂地址" />
        </uni-forms-item>
        
        <uni-forms-item label="联系人" name="contact">
          <uni-easyinput v-model="formData.contact" placeholder="请输入联系人" />
        </uni-forms-item>
        
        <uni-forms-item label="联系电话" name="phone">
          <uni-easyinput v-model="formData.phone" placeholder="请输入联系电话" />
        </uni-forms-item>
        
        <uni-forms-item label="酒厂描述" name="description">
          <uni-easyinput 
            v-model="formData.description" 
            type="textarea" 
            placeholder="请输入酒厂描述"
            :auto-height="true" />
        </uni-forms-item>
        
        <uni-forms-item label="状态" name="status">
          <uni-data-select 
            v-model="formData.status" 
            :localdata="statusOptions"
            placeholder="请选择状态" />
        </uni-forms-item>
      </uni-forms>
      
      <view class="form-actions">
        <uni-button type="default" @click="goBack">取消</uni-button>
        <uni-button type="primary" @click="submitForm" :loading="submitting">保存</uni-button>
      </view>
    </view>
  </view>
</template>

<script>
import CommonHeader from '@/components/common-header/common-header.vue'

export default {
  name: 'DistillerEdit',
  components: {
    CommonHeader
  },
  
  data() {
    return {
      distillerId: null,
      formData: {
        name: '',
        address: '',
        contact: '',
        phone: '',
        description: '',
        status: 1
      },
      rules: {
        name: {
          rules: [{ required: true, errorMessage: '请输入酒厂名称' }]
        }
      },
      statusOptions: [
        { value: 1, text: '正常' },
        { value: 0, text: '停用' }
      ],
      submitting: false
    }
  },
  
  onLoad(option) {
    const { id } = option
    if (id) {
      this.distillerId = id
      this.getDistillerDetail(id)
    }
  },
  
  methods: {
    async getDistillerDetail(id) {
      try {
        // TODO: Replace with actual API call
        const response = await this.$http.get(`/brewery/distiller/${id}`)
        const data = response.data || {}
        this.formData = {
          name: data.name || '',
          address: data.address || '',
          contact: data.contact || '',
          phone: data.phone || '',
          description: data.description || '',
          status: data.status || 1
        }
      } catch (error) {
        console.error('获取酒厂详情失败:', error)
        this.$modal.showToast('获取酒厂详情失败')
      }
    },
    
    async submitForm() {
      try {
        // 表单验证
        const valid = await this.$refs.form.validate()
        if (!valid) return
        
        this.submitting = true
        
        // TODO: Replace with actual API call
        const response = await this.$http.put(`/brewery/distiller/${this.distillerId}`, this.formData)
        
        this.$modal.showToast('保存成功')
        
        // 返回上一页
        setTimeout(() => {
          uni.navigateBack()
        }, 1500)
        
      } catch (error) {
        console.error('保存失败:', error)
        this.$modal.showToast('保存失败')
      } finally {
        this.submitting = false
      }
    },
    
    goBack() {
      uni.navigateBack()
    }
  }
}
</script>

<style lang="scss" scoped>
.edit-container {
  min-height: 100vh;
  background: #f5f7fa;
}

.edit-content {
  padding: 20rpx;
}

.form-actions {
  display: flex;
  gap: 20rpx;
  margin-top: 60rpx;
  padding: 0 20rpx;
}
</style> 