<template>
  <view class="manual-verify">
    <CustomNavbar title="" backgroundColor="#FFFFFF"></CustomNavbar>

    <view class="content">
      <view class="tip">请输入6位核销码</view>

      <view class="code-inputs" @tap="focusInput">
        <view
          class="input-box"
          :class="{ filled: code[i-1] }"
          v-for="i in 6"
          :key="i"
        >
          {{ code[i-1] || '' }}
        </view>
      </view>

      <input
        class="hidden-input"
        type="number"
        maxlength="6"
        :value="code"
        :focus="inputFocus"
        @input="onInput"
        @blur="resetFocus"
        confirm-type="done"
      />

      <button class="confirm-btn" :class="{ active: code.length === 6 }" @tap="submitCode">
        确认
      </button>
    </view>
  </view>
</template>

<script>
import CustomNavbar from "@/components/custom-navbar/custom-navbar.vue";
import request from "@/utils/request";

export default {
  components: { CustomNavbar },
  data() {
    return {
      code: '',
      inputFocus: false
    };
  },
  methods: {
    focusInput() {
      this.inputFocus = false;
      this.$nextTick(() => {
        this.inputFocus = true;
      });
    },
    resetFocus() {
      this.inputFocus = false;
    },
    onInput(e) {
      let value = e.detail.value;
      value = value.replace(/\D/g, '');
      if (value.length > 6) value = value.slice(0, 6);
      this.code = value;
    },
    submitCode() {
      if (this.code.length !== 6) {
        uni.showToast({ title: '请输入6位核销码', icon: 'none' });
        return;
      }
      
      uni.showLoading({ title: '查询订单中...', mask: true });
      
      // 统一调用查询接口，不要在这里调用核销接口
      request.post('/sj/poster/getOrderByCode', {
        server_code: this.code
      }).then(res => {
        uni.hideLoading();
        if (res.code === 200 && res.data) {
          // 查询成功，带上数据跳 detail 页
          const orderDataStr = encodeURIComponent(JSON.stringify(res.data));
          uni.navigateTo({
            url: `/pages/shop/verify/verify-order-detail?data=${orderDataStr}`
          });
        } else {
          // 查询不到，跳 fail 页
          uni.navigateTo({ url: '/pages/shop/verify/verify-fail' });
        }
      }).catch(() => {
        uni.hideLoading();
        uni.navigateTo({ url: '/pages/shop/verify/verify-fail' });
      });
    }
  }
};
</script>

<style lang="scss">
.manual-verify {
  padding-top: 150rpx;
  background-color: #fff;
  min-height: 100vh;

  .tip {
    font-weight: 500;
    font-size: 46rpx;
    color: #333333;
    line-height: 65rpx;
    text-align: left;
    margin-bottom: 80rpx;
    margin-left: 48rpx;
  }

  .code-inputs {
    display: flex;
    justify-content: center;
    gap: 24rpx;
    margin-bottom: 80rpx;
    padding: 0 30rpx;

    .input-box {
      width: 88rpx;
      height: 88rpx;
      border-radius: 12rpx;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 48rpx;
      font-weight: 500;
      color: #333;
      background-color: #F3F3F3;
    }
  }

  .confirm-btn {
    margin: 0 49rpx;
    background: #ff476685;
    color: #fff;
    border-radius: 88rpx;
    height: 96rpx;
    line-height: 96rpx;
    font-size: 36rpx;
    font-weight: 500;
    box-shadow: 0 8rpx 20rpx rgba(255, 71, 103, 0.2);
    transition: all 0.2s;

    &.active {
      background: #FF4767;
    }
  }

  .hidden-input {
    position: fixed;
    top: 0;
    left: 0;
    width: 2px;
    height: 2px;
    opacity: 0;
    background: transparent;
  }
}
</style>