<template>
  <view class="add-container">
    <common-header title="新增发货" theme="shipping" @back="goBack" />
    
    <view class="add-content">
      <uni-forms ref="form" :model="formData" :rules="rules" label-width="120">
        <uni-forms-item label="关联订单" required name="orderNo">
          <uni-easyinput v-model="formData.orderNo" placeholder="请输入关联订单号" />
        </uni-forms-item>
        
        <uni-forms-item label="客户名称" required name="customerName">
          <uni-easyinput v-model="formData.customerName" placeholder="请输入客户名称" />
        </uni-forms-item>
        
        <uni-forms-item label="收货地址" required name="address">
          <uni-easyinput v-model="formData.address" placeholder="请输入收货地址" />
        </uni-forms-item>
        
        <uni-forms-item label="物流公司" name="logistics">
          <uni-data-select 
            v-model="formData.logistics" 
            :localdata="logisticsOptions"
            placeholder="请选择物流公司" />
        </uni-forms-item>
        
        <uni-forms-item label="物流单号" name="trackingNo">
          <uni-easyinput v-model="formData.trackingNo" placeholder="请输入物流单号" />
        </uni-forms-item>
        
        <uni-forms-item label="发货时间" name="shipTime">
          <uni-datetime-picker v-model="formData.shipTime" />
        </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="submitForm" :loading="submitting">保存</uni-button>
      </view>
    </view>
  </view>
</template>

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

export default {
  name: 'ShippingAdd',
  components: {
    CommonHeader
  },
  
  data() {
    return {
      formData: {
        orderNo: '',
        customerName: '',
        address: '',
        logistics: '',
        trackingNo: '',
        shipTime: '',
        remark: ''
      },
      rules: {
        orderNo: {
          rules: [{ required: true, errorMessage: '请输入关联订单号' }]
        },
        customerName: {
          rules: [{ required: true, errorMessage: '请输入客户名称' }]
        },
        address: {
          rules: [{ required: true, errorMessage: '请输入收货地址' }]
        }
      },
      logisticsOptions: [
        { value: 'sf', text: '顺丰快递' },
        { value: 'ems', text: '中国邮政' },
        { value: 'zt', text: '中通快递' },
        { value: 'yd', text: '韵达快递' },
        { value: 'sto', text: '申通快递' },
        { value: 'other', text: '其他' }
      ],
      submitting: false
    }
  },
  
  methods: {
    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.post('/brewery/shipping', 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>
.add-container {
  min-height: 100vh;
  background: #f5f7fa;
}

.add-content {
  padding: 20rpx;
}

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