<template>
  <drawer-page-container
    v-model="show"
    title="订单发货"
    height="70%"
    tall
    :show-close="true"
    :closeable="false"
    @close="onClose"
  >
    <view class="popup-wrap">
      <view class="order-no" v-if="orderNo">订单号：{{ orderNo }}</view>
      <view class="form">
        <view v-if="deliveryMethod === 0" class="field">
          <text class="label">快递单号</text>
          <input
            class="input"
            v-model="expressNo"
            placeholder="请输入快递单号"
            placeholder-style="color:#BDBDBD"
          />
        </view>
        <view v-if="deliveryMethod === 0" class="field">
          <text class="label">快递公司</text>
          <express-company-picker
            v-model="expressCompanyCode"
            :options="companyOptions"
          />
        </view>
        <view class="field">
          <text class="label">发货备注</text>
          <textarea
            class="textarea"
            v-model="introduce"
            placeholder="选填"
            placeholder-style="color:#BDBDBD"
          />
        </view>
      </view>
      <view class="footer">
        <button class="btn-cancel" @click="onClose">取消</button>
        <button class="btn-submit" :loading="submitting" @click="submit">确认发货</button>
      </view>
    </view>
  </drawer-page-container>
</template>

<script>
import DrawerPageContainer from '@/subPackages/sub_business_shared/components/DrawerPageContainer.vue'
import ExpressCompanyPicker from './ExpressCompanyPicker.vue'
import { withWxKey } from '@/utils/wxListKey.js'

export default {
  name: 'ShipOrderPopup',
  components: { DrawerPageContainer, ExpressCompanyPicker },
  props: {
    bizApi: { type: Object, required: true },
  },
  data() {
    return {
      show: false,
      orderId: 0,
      orderNo: '',
      deliveryMethod: 0,
      expressNo: '',
      expressCompanyCode: '',
      introduce: '',
      companyOptions: [],
      submitting: false,
    }
  },
  methods: {
    open(order) {
      this.orderId = order.id
      this.orderNo = order.order_no || ''
      this.deliveryMethod = Number(order.delivery_method)
      this.expressNo = ''
      this.expressCompanyCode = ''
      this.introduce = ''
      this.companyOptions = []
      this.show = true
      if (this.deliveryMethod === 0) {
        this.loadCompanies()
      }
    },
    loadCompanies() {
      this.bizApi.getExpressCompaniesOption().then(w => {
        const items = (w.data && (w.data.items || w.data)) || []
        this.companyOptions = withWxKey(Array.isArray(items) ? items : [], 'id', 'ec')
      })
    },
    onClose() {
      this.show = false
    },
    submit() {
      if (this.deliveryMethod === 0) {
        if (!this.expressNo.trim()) {
          uni.showToast({ title: '请输入快递单号', icon: 'none' })
          return
        }
        if (!this.expressCompanyCode) {
          uni.showToast({ title: '请选择快递公司', icon: 'none' })
          return
        }
      }
      this.submitting = true
      const payload = {
        order_id: this.orderId,
        express_no: this.expressNo.trim(),
        express_company_code: this.expressCompanyCode,
        introduce: this.introduce.trim(),
      }
      this.bizApi.wareSendOrder(payload).then(w => {
        if (w.ok) {
          uni.showToast({ title: '发货成功' })
          this.show = false
          this.$emit('success')
        }
      }).finally(() => { this.submitting = false })
    },
  },
}
</script>

<style lang="scss" scoped>
.popup-wrap {
  padding: 0 32rpx 32rpx;
  padding-bottom: calc(32rpx + env(safe-area-inset-bottom));
}
.order-no { font-size: 26rpx; color: #86909C; margin-bottom: 24rpx; }
.field { margin-bottom: 24rpx; }
.label { display: block; font-size: 28rpx; color: #4E5969; margin-bottom: 12rpx; }
.input {
  width: 100%;
  box-sizing: border-box;
  background: #F7F8FA;
  border-radius: 12rpx;
  min-height: 88rpx;
  height: 88rpx;
  line-height: 88rpx;
  padding: 0 24rpx;
  font-size: 28rpx;
}
.textarea {
  width: 100%;
  box-sizing: border-box;
  background: #F7F8FA;
  border-radius: 12rpx;
  min-height: 160rpx;
  padding: 24rpx;
  line-height: 1.5;
  font-size: 28rpx;
}
.footer { display: flex; gap: 24rpx; margin-top: 32rpx; }
.footer button {
  flex: 1;
  margin: 0;
  height: 88rpx;
  line-height: 88rpx;
  border-radius: 44rpx;
  font-size: 30rpx;
}
.footer button::after { display: none; }
.btn-cancel { background: #fff; color: #4E5969; border: 1rpx solid #E5E6EB; }
.btn-submit { background: #6acdbb; color: #fff; }
</style>
