<template>
  <view class="create-service-order">
    <view
      class="w-full bg-white border-box border-ff overflow-hidden rounded-20"
    >
      <view
        class="border-box order-header w-vw flex flex-items-center flex-justify-between bg-EEF8FF"
      >
        <text class="font-size-18 font-500 color-171717 text-left ml-12">
          {{ isCallSuccess ? "服务已创建" : "呼叫服务" }}
        </text>
        <image
          class="header-icon"
          src="https://oss.nianxx.cn/mp/static/version_101/home/feedback.png"
        />
      </view>

      <view v-if="!isCallSuccess" class="order-content border-box p-12">
        <view
          class="bg-F5F7FA border-box flex flex-items-center p-12 rounded-10 font-size-14 color-171717 mb-12"
        >
          <text class="font-500 line-height-22 mr-20">房间号</text>
          <input placeholder="请填写房间号" v-model="roomId" />
        </view>

        <view
          class="bg-F5F7FA border-box flex flex-items-center p-12 rounded-10 font-size-14 color-171717 mb-12"
        >
          <text class="font-500 line-height-22 mr-20">联系电话</text>
          <input placeholder="请填写联系电话" v-model="contactPhone" />
        </view>

        <view
          class="bg-F5F7FA border-box p-12 rounded-10 font-size-14 font-500 color-171717 mb-12"
        >
          <view class="font-500 line-height-22 mb-12">需求信息描述</view>
          <textarea
            class="h-80"
            placeholder="请输入需求信息描述"
            placeholder-class="font-size-14 font-400"
            maxlength="100"
            v-model="contactText"
          />
        </view>

        <view
          class="bg-F5F7FA border-box p-12 rounded-10 font-size-14 font-500 color-171717 mb-12"
        >
          <view class="font-500 line-height-22 mb-12">照片上传</view>
          <view
            class="bg-white p-24 rounded-5 inline-block"
            @click="handleChooseImage"
          >
            <uni-icons fontFamily="znicons" size="24" color="#6A717F">
              {{ zniconsMap["zn-camera"] }}
            </uni-icons>
          </view>
        </view>
      </view>

      <view v-else class="border-box card-content flex flex-items-center p-12">
        <view class="border-box left flex-full pr-20">
          <view class="font-size-12 color-525866 line-height-20 mb-4"
            >房间号：{{ roomId }}</view
          >
          <view class="font-size-12 color-525866 line-height-20 mb-4"
            >联系方式: {{ contactPhone }}</view
          >
          <view class="font-size-12 color-525866 line-height-20 ellipsis-2"
            >需求描述: {{ contactText }}</view
          >
        </view>

        <image
          class="right rounded-6"
          src="https://picsum.photos/300/300"
          mode="aspectFill"
        />
      </view>

      <view
        class="btn rounded-50 color-white bg-button flex flex-items-center flex-justify-center ml-12 mr-12 mb-12"
        @click="handleCall"
      >
        {{ isCallSuccess ? "查看服务" : "立即呼叫" }}
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted, nextTick } from "vue";
import { SCROLL_TO_BOTTOM } from "@/constant/constant";
import { createWorkOrder } from "@/request/api/OrderApi";
import { updateImageFile } from "@/request/api/UpdateFile";
import { zniconsMap } from "@/static/fonts/znicons.js";

const workOrderTypeId = ref("");
const roomId = ref("");
const contactPhone = ref("");
const contactText = ref("");
const contentImgUrl = ref("");
const isCallSuccess = ref(false); // 呼叫成功状态
const workOrderId = ref(0); // 工单ID

// 处理图片上传
const handleChooseImage = () => {
  console.log("选择图片");
  uni.chooseImage({
    count: 1,
    success: (res) => {
      const file = res.tempFilePaths[0];
      updateImagehandle(file);
    },
    fail: (err) => {
      console.error("选择图片失败:", err);
      uni.showToast({
        title: "选择图片失败",
        icon: "none",
        duration: 2000,
      });
    },
  });
};

const updateImagehandle = (file) => {
  if (!file) {
    return;
  }
  updateImageFile(file).then((res) => {
    contentImgUrl.value = res.data?.url || "";
  });
};

const handleCall = async () => {
  if (isCallSuccess.value) {
    // 查看工单
    viewWorkOrder();
    return;
  }

  if (!contactPhone.value.trim()) {
    uni.showToast({
      title: "请填写联系电话",
      icon: "none",
      duration: 2000,
    });
    return;
  }

  if (!contactText.value.trim()) {
    uni.showToast({
      title: "请填写需求信息描述内容",
      icon: "none",
      duration: 2000,
    });
    return;
  }

  sendCreateWorkOrder();
};

/// 创建工单
const sendCreateWorkOrder = async () => {
  try {
    const res = await createWorkOrder({
      workOrderTypeId: workOrderTypeId.value,
      roomId: roomId.value,
      contactPhone: contactPhone.value,
      content: contactText.value,
      contentImgUrl: contentImgUrl.value,
    });

    if (res.code === 0) {
      // 保存工单ID
      workOrderId.value = res.data?.id || "";

      // 设置成功状态
      isCallSuccess.value = true;

      uni.showToast({
        title: "呼叫成功",
        icon: "success",
        duration: 2000,
      });
    } else {
      uni.showToast({
        title: res.message || "呼叫失败",
        icon: "none",
        duration: 2000,
      });
    }
  } catch (error) {
    console.error("呼叫失败:", error);
    uni.showToast({
      title: "网络错误，请重试",
      icon: "none",
      duration: 2000,
    });
  }
};

// 查看工单
const viewWorkOrder = () => {
  console.log("查看工单:", workOrderId.value);
  // 这里可以跳转到工单详情页面
  uni.navigateTo({
    url: `/pages-order/order/list?id=${workOrderId.value}`,
  });
};

onMounted(() => {
  nextTick(() => {
    setTimeout(() => {
      uni.$emit(SCROLL_TO_BOTTOM, true);
    }, 200);
  });
});
</script>

<style scoped lang="scss">
@import "./styles/index.scss";
</style>
