<template>
  <view class="container">
    <view class="input-container">
      <textarea 
        v-model="remark" 
        placeholder="请输入您的备注..." 
        class="remark-input"
      ></textarea>
    </view>
    <button @click="submitRemark" class="submit-button">提交</button>
  </view>
</template>

<script setup>
import { ref } from 'vue';
import { apiImageUrl } from '../../API/api';
import { onMounted } from 'vue';


const remark = ref('');
const orderItem = ref(null);

const getOrder = () => {
  const notPay = uni.getStorageSync("notPay");
  uni.request({
    url: apiImageUrl + '/api/orders/get/my',
    method: 'GET',
    data: {
      id: notPay
    },
    header: {
      'Content-Type': 'application/json', // 保设置正确的 Content-Type
      'cookie': uni.getStorageSync("cookie") || ''
    },
    success(res) {
      console.log("成功");
      if (res.data && res.data.data) {
        // 更新响应式变量
        orderItem.value = res.data.data;
        console.log(orderItem.value);
      }
    },
    fail() {
      console.log("失败");
    }
  });
}

onMounted(getOrder);

// 提交备注的方法
const submitRemark = () => {
  const notPay = uni.getStorageSync('notPay');
  
  if (!notPay) {
    uni.showToast({
      title: '订单ID未找到',
      icon: 'none'
    });
    return;
  }

  uni.showLoading({
    title: '正在提交备注...'
  });

  // 保 pickupTime 不为 null 或空字符串，并且是一小时后的时间
  let currentTime = new Date();
  let futureTime = new Date(currentTime.getTime() + 3600 * 1000); // 一小时后的时间
  let pickupTime = futureTime.toISOString();

  uni.request({
    url: apiImageUrl + '/api/orders/updateOrder',
    method: 'POST',
    data: {
      orderId: notPay,
      notes: remark.value,
      location: orderItem.value?.location || "",
      pickupMethod: orderItem.value?.pickupMethod || 0,
     pickupEndTime:orderItem.value?.pickupEndTime||"",
	 pickupStartTime:orderItem.value?.pickupStartTime||""
    },
	header: {
	  'Content-Type': 'application/json', // 确保设置正确的 Content-Type
	  'cookie': uni.getStorageSync("cookie") || ''
	},
    success(res) {
      console.log(res);
      if (res.data.code === 0) {
        uni.showToast({
          title: '备注提交成功',
          duration: 2000
        });
        
        // 返回上一页并传递备注信息
        uni.navigateBack({
          delta: 1, // 返回上一页
          success() {
            uni.$emit('updateRemark', remark.value); // 使用事件总线传递备注信息
          }
        });
      } else {
        uni.showToast({
          title: '备注提交失败',
          icon: 'none',
          duration: 2000
        });
      }
    },
    fail(err) {
      console.log(err);
    }
  });
}
</script>

<style scoped>
.container {
  padding: 20px;
}

.input-container {
  margin-bottom: 20px;
}

.remark-input {
  width: 100%;
  height: 150px;
  padding: 10px;
  font-size: 16px;
  border: 1px solid #ccc;
  border-radius: 8px;
  resize: none; /* 止用户调整大小 */
}

.submit-button {
  width: 100%;
  background-color: #4095e5;
  color: white;
  font-size: 16px;
  border: none;
  border-radius: 8px;
}
</style>