<template>
  <div class="custom" style="font-size: 14px">
    <!-- 发送房源 -->

    <template v-if="isCustom.businessID === 'house_message'">
      <view>
        <view
          class="qzy-center item-main"
          @click="
            toDetail(
              isCustom.shopUserId,
              isCustom.publishType,
              isCustom.houseId
            )
          "
        >
          <image class="item-l" :src="isCustom.indoorImg" mode="scaleToFill" />
          <view class="item-r">
            <view class="text-14 text-bold item-title ellipsis-nowrap">
              {{ isCustom.publishType }}丨{{ isCustom.communityName }}
            </view>
            <view class="qzy-center text-12 text-500 item-row">
              <text>{{ isCustom.houseType }}</text>
              <text>丨</text>
              <text>{{ isCustom.acreage }}㎡</text>
              <text>丨</text>
              <text>{{ isCustom.orientation }}</text>
            </view>
            <view class="qzy-center text-500">
              <text class="text-18">{{ isCustom.price }}</text>
              <i class="text-12">{{ isCustom.unitName }}</i>
            </view>
            <view class="item-address text-10 text-500 ellipsis-nowrap">
              <text
                >{{ isCustom.cityName }}{{ isCustom.areaName
                }}{{ isCustom.address }}</text
              >
            </view>
          </view>
        </view>
      </view>
    </template>
    <!-- 发送邀请 -->
    <template v-else-if="isCustom.businessID === 'appointment_message'">
      <view class="invite-box">
        <view class="text-18 text-bold">{{
          formatterTitle(isCustom.status)
        }}</view>
        <view class="item-row text-16 text-500">{{ isCustom.houseName }}</view>
        <view class="item-row">
          <text>预约时间：</text>
          <view>{{ isCustom.time }}</view>
        </view>
        <view class="item-row">
          <text>小区地址：</text>
          <view>{{ isCustom.address }}</view>
        </view>
        <view class="item-row">
          <text>联系方式：</text>
          <view>{{ isCustom.contact }}</view>
        </view>
        <!-- <view class="item-row qzy-center"
          >信息费：<text class="text-bold">0</text>元</view
        > -->
        <view
          v-if="isCustom.status == 0 && isCustom.inviteeId == ownId"
          class="operate-box"
          @click="acceptHandle(isCustom.appointmentId)"
          >接受预约</view
        >
      </view>
    </template>
  </div>
</template>

<script lang="ts" setup>
import { watchEffect, ref } from "../../../../adapter-vue";
import {
  TUITranslateService,
  IMessageModel,
} from "@tencentcloud/chat-uikit-engine";
import { isUrl, JSONToObject } from "../../../../utils/index";
import { CHAT_MSG_CUSTOM_TYPE } from "../../../../constant";
import { ICustomMessagePayload } from "../../../../interface";
import Icon from "../../../common/Icon.vue";
import star from "../../../../assets/icon/star-light.png";
import api from "@/api/home/home.js";

const ownId = uni.getStorageSync("userId");

interface IEmits {
  (key: "scrollToLatestMessage"): void;
}
const emits = defineEmits<IEmits>();
interface Props {
  messageItem: IMessageModel;
  content: any;
}

const props = withDefaults(defineProps<Props>(), {
  messageItem: undefined,
  content: undefined,
});

const custom = ref();
const message = ref<IMessageModel>();
const extension = ref();
const isCustom = ref<ICustomMessagePayload>({
  businessID: "",
});

watchEffect(() => {
  custom.value = props.content;
  message.value = props.messageItem;
  const { payload } = props.messageItem;
  isCustom.value = payload.data || "";
  isCustom.value = JSON.parse(payload.data);
  if (payload.data === CHAT_MSG_CUSTOM_TYPE.SERVICE) {
    extension.value = JSONToObject(payload.extension);
  }
});

const acceptHandle = async (id) => {
  const { code, data } = await api.changeUserSubscribe({ id: id, status: 1 });
  if (code === 200) {
    uni.$u.toast("已接受预约~");
    emits("scrollToLatestMessage");
  }
};
// 发送邀请标题文字格式化
const formatterTitle = (status) => {
  if (status == 0) {
    return "待确认预约看房邀请";
  } else if (status == 1) {
    return "已接受预约看房邀请";
  } else if (status == 2) {
    return "已看房";
  } else if (status == 3) {
    return "已成交";
  } else if (status == 4) {
    return "未按时看房";
  } else {
    return "已取消预约看房邀请";
  }
};

const toDetail = (shopUserId, publishType, houseId) => {
  // 自己发布的房源不跳转详情
  if (shopUserId == uni.getStorageSync("userId")) {
    return false;
  }
  let path = "";
  if (publishType == "新房") {
    path = "/packageHouse/home/house/new-house-detail";
  } else if (publishType == "二手房") {
    path = "/packageHouse/home/house/used-house-detail";
  } else {
    path = "/packageHouse/home/house/renting-house-detail";
  }
  webUni.webView.navigateTo({ url: path + "?id=" + houseId });
};
</script>
<style lang="scss" scoped>
@import "../../../../assets/styles/common";
.phone {
  min-width: 180px;
  height: 65px;
  border-radius: 10px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  padding: 2px;
}
.xian {
  width: 408rpx;
  height: 1rpx;
  background: #000000;
  opacity: 0.1;
}
.phone_footer {
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  position: relative;
  top: 8px;
  height: 25px;
  align-items: center;
}
a {
  color: #00b693;
}

.custom {
  font-size: 14px;

  h1 {
    font-size: 14px;
    color: #000;
  }

  h1,
  a,
  p {
    font-size: 14px;
  }

  .evaluate {
    ul {
      display: flex;
      padding: 10px 0;
    }

    &-list {
      display: flex;
      flex-direction: row;

      &-item {
        padding: 0 2px;
      }
    }
  }

  .order {
    display: flex;
    min-height: 250px;
    min-width: 215px;
    flex-direction: column;
    padding: 5px;
    .header {
      width: 100%;
      height: 55px;
      display: flex;
      flex-direction: row;
      justify-content: space-between;
    }
    .contont {
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      align-items: flex-start;
      height: 190px;
      .title {
        font-family: PingFang SC;
        font-weight: 500;
        font-size: 26rpx;
        color: #a1a1a1;
        line-height: 44rpx;
      }
      .value {
        font-family: PingFang SC;
        font-weight: 500;
        font-size: 30rpx;
        color: #666666;
        line-height: 44rpx;
      }
    }
    .main {
      padding-left: 5px;
      width: 90%;
      line-height: 25px;
    }

    img {
      width: 45px;
      height: 45px;
    }
  }

  .data-time {
    background: #f4f4f4;
    padding: 10upx 24upx;
  }

  .item-l {
    width: 60px;
    height: 60px;
    border-radius: 6upx;
    margin-right: 22upx;
  }
  .item-r {
    width: calc(100% - 70px);
    padding: 10upx 0;
  }
  .item-row {
    text {
      display: block;
    }
  }
  i {
    font-style: normal;
  }
  .invite-box {
    .item-row {
      padding: 10upx 0;
    }
  }
}
.operate-box {
  margin-top: 10px;
  padding-top: 10px;
  text-align: center;
  font-weight: bold;
  font-size: 32upx;
}
.in {
  .operate-box {
    color: #009f82;
    border-top: 1px solid #f4f4f4;
  }

  .item-main {
    background: #f4f4f4;
    padding: 0 14upx;
    border-radius: 16rpx;
  }
}
.out {
  .operate-box {
    border-top: 1px solid rgba(255, 255, 255, 0.5);
    color: #fff;
  }
  .item-main {
    background: #009f82;
    padding: 0 14upx;
    border-radius: 16rpx;
  }
}
</style>
