<template>
  <view class="container">
    <view class="header">
      <button class="back" @click="back"><i class="icon icon-arrow_back" style="color:#05DCEF;font-size:36rpx"></i></button>
      <text class="title">通道三原材料回库</text>
    </view>

    <view class="content">
      <view class="tip">
        <view class="tip-icon"><i class="icon icon-info" style="color:#05DCEF"></i></view>
        <text class="tip-text">请输入母托号，系统会自动回库至固定入库口</text>
      </view>

      <view class="field">
        <text class="label">母托号 <text class="required">*</text></text>
        <view class="input-wrap">
          <i class="left-icon icon icon-qr_code_2" style="font-size:36rpx"></i>
          <input class="input" placeholder="请扫描或输入母托号" v-model="vehicleNo" />
        </view>
      </view>

      <view class="static-field">
        <text class="static-label">回库口</text>
        <text class="static-value">{{ fixedStand }}</text>
      </view>

      <button class="submit" :disabled="loading" @click="submit">
        <i class="icon icon-task_alt" style="margin-right:12rpx"></i> 确认回库
      </button>
    </view>
  </view>
</template>

<script>
import { WmsApiClient } from '@/common/wmsApi.js';
import { DialogUtils } from '@/utils/dialog.js';
export default {
  data() {
    return {
      vehicleNo: '',
      fixedStand: '104',
      loading: false,
    };
  },
  methods: {
    back() {
      uni.navigateBack();
    },
    resolveError(error, fallback = '请求失败') {
      if (!error) return fallback;
      if (typeof error === 'string') return error;
      if (typeof error.message === 'string') return error.message;
      if (typeof error.errMsg === 'string') return error.errMsg;
      if (typeof error.code === 'number') return `${fallback} (${error.code})`;
      return fallback;
    },
    async submit() {
      const vehicle = (this.vehicleNo || '').trim();
      if (!vehicle) {
        DialogUtils.showWarningMessage('提示', '请输入母托号');
        return;
      }
      if (this.loading) return;

      this.loading = true;
      uni.showLoading({ title: '正在请求', mask: true });
      try {
        const response = await WmsApiClient.rawStockIn({
          vehicleNo: vehicle,
          standId: this.fixedStand,
        });
        const code = Number(response.code);
        if (code === 0) {
          DialogUtils.showSuccessMessage('成功', '回库成功');
          this.vehicleNo = '';
        } else {
          DialogUtils.showWarningMessage('操作未成功', `${response.message || '提交失败'} (${response.code})`);
        }
      } catch (error) {
        DialogUtils.showErrorMessage('错误', this.resolveError(error));
      } finally {
        uni.hideLoading();
        this.loading = false;
      }
    },
  },
};
</script>

<style scoped>
.header {
  position: relative;
  height: 120rpx;
  padding: 0;
  background: linear-gradient(90deg, var(--grad-primary-start), var(--grad-primary-mid));
  display: flex;
  align-items: center;
  justify-content: center;
}

.container {
  min-height: 100vh;
  background: #F5F5F5;
}

.title {
  color: #fff;
  font-size: 32rpx;
  font-weight: 600;
}

.content {
  padding: 24rpx;
}

.tip {
  flex-direction: row;
  display: flex;
  align-items: center;
  background: #f2fdff;
  border-radius: 12rpx;
  padding: 24rpx;
  color: #333;
  margin-bottom: 28rpx;
}

.tip-icon {
  width: 40rpx;
  height: 40rpx;
  border-radius: 20rpx;
  background: #e6f7ff;
  text-align: center;
  line-height: 40rpx;
  margin-right: 12rpx;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tip-text {
  color: #333;
  font-size: 26rpx;
}

.field {
  margin-bottom: 24rpx;
}

.label {
  color: #333;
  font-size: 30rpx;
  margin-bottom: 12rpx;
  display: block;
}

.required {
  color: #ff4d4f;
}

.input-wrap {
  height: 96rpx;
  border-radius: 12rpx;
  border: 1px solid #e6e6e6;
  background: #fff;
  padding: 0 20rpx;
  display: flex;
  align-items: center;
}

.left-icon {
  color: #05DCEF;
  font-size: 36rpx;
  margin-right: 12rpx;
}

.input {
  flex: 1;
  height: 100%;
  font-size: 30rpx;
}

.static-field {
  background: #fff;
  border-radius: 12rpx;
  border: 1px solid #e6e6e6;
  padding: 24rpx 20rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 32rpx;
}

.static-label {
  color: #666;
  font-size: 28rpx;
}

.static-value {
  font-size: 32rpx;
  color: #111;
  font-weight: 600;
}

.submit {
  width: 100%;
  height: 92rpx;
  border: none;
  color: #fff;
  border-radius: 46rpx;
  background: linear-gradient(90deg, var(--grad-primary-start), var(--grad-primary-mid));
  font-size: 32rpx;
  box-shadow: 0 8rpx 22rpx rgba(52, 199, 89, .25);
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
}

.submit[disabled] {
  opacity: .6;
}

.back {
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 80rpx;
  height: 64rpx;
  line-height: 64rpx;
  border: none;
  color: #05DCEF;
  background: #fff;
  border-radius: 12rpx;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
