<template>
  <view class="transfer-container">
    <common-header title="酒厂转让" theme="distiller" @back="goBack" />
    
    <view class="transfer-content">
      <view class="distiller-info">
        <text class="info-title">转让酒厂</text>
        <text class="distiller-name">{{ distillerData.name }}</text>
        <text class="distiller-address">{{ distillerData.address }}</text>
      </view>
      
      <uni-forms ref="form" :model="formData" :rules="rules" label-width="120">
        <uni-forms-item label="转让给" required name="transferTo">
          <uni-easyinput v-model="formData.transferTo" placeholder="请输入接收人" />
        </uni-forms-item>
        
        <uni-forms-item label="转让价格" name="price">
          <uni-easyinput v-model="formData.price" placeholder="请输入转让价格" type="number" />
        </uni-forms-item>
        
        <uni-forms-item label="转让原因" required name="reason">
          <uni-easyinput 
            v-model="formData.reason" 
            type="textarea" 
            placeholder="请输入转让原因"
            :auto-height="true" />
        </uni-forms-item>
        
        <uni-forms-item label="转让时间" required name="transferTime">
          <uni-datetime-picker v-model="formData.transferTime" />
        </uni-forms-item>
        
        <uni-forms-item label="备注" name="remark">
          <uni-easyinput 
            v-model="formData.remark" 
            type="textarea" 
            placeholder="请输入备注"
            :auto-height="true" />
        </uni-forms-item>
      </uni-forms>
      
      <view class="form-actions">
        <uni-button type="default" @click="goBack">取消</uni-button>
        <uni-button type="primary" @click="submitTransfer" :loading="submitting">确认转让</uni-button>
      </view>
    </view>
  </view>
</template>

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

export default {
  name: 'DistillerTransfer',
  components: {
    CommonHeader
  },
  
  data() {
    return {
      distillerId: null,
      distillerData: {},
      formData: {
        transferTo: '',
        price: '',
        reason: '',
        transferTime: '',
        remark: ''
      },
      rules: {
        transferTo: {
          rules: [{ required: true, errorMessage: '请输入接收人' }]
        },
        reason: {
          rules: [{ required: true, errorMessage: '请输入转让原因' }]
        },
        transferTime: {
          rules: [{ required: true, errorMessage: '请选择转让时间' }]
        }
      },
      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}`)
        this.distillerData = response.data || {}
      } catch (error) {
        console.error('获取酒厂详情失败:', error)
        this.$modal.showToast('获取酒厂详情失败')
      }
    },
    
    async submitTransfer() {
      try {
        // 表单验证
        const valid = await this.$refs.form.validate()
        if (!valid) return
        
        // 确认转让
        const confirmed = await new Promise((resolve) => {
          uni.showModal({
            title: '确认转让',
            content: `确定要将酒厂"${this.distillerData.name}"转让给"${this.formData.transferTo}"吗？`,
            success: (res) => {
              resolve(res.confirm)
            }
          })
        })
        
        if (!confirmed) return
        
        this.submitting = true
        
        const transferData = {
          distillerId: this.distillerId,
          ...this.formData
        }
        
        // TODO: Replace with actual API call
        const response = await this.$http.post('/brewery/distiller/transfer', transferData)
        
        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>
.transfer-container {
  min-height: 100vh;
  background: #f5f7fa;
}

.transfer-content {
  padding: 20rpx;
}

.distiller-info {
  background: #fff;
  border-radius: 16rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
  
  .info-title {
    display: block;
    font-size: 24rpx;
    color: #999;
    margin-bottom: 16rpx;
  }
  
  .distiller-name {
    display: block;
    font-size: 32rpx;
    font-weight: bold;
    color: #333;
    margin-bottom: 8rpx;
  }
  
  .distiller-address {
    font-size: 26rpx;
    color: #666;
  }
}

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