<template>
  <div class="message-product-card">
    <div v-for="(item, index) in topCustomField" :key="index" class="product-card-custom-field">
      <div class="custom-field-line">
        <div v-if="item.name" class="order-name">
          {{ item.name }}:
        </div>
        <div class="order-value">
          {{ item.value }}
        </div>
      </div>
    </div>
    <div class="product-card-main">
      <img
        v-if="props.payload.content.pic"
        class="product-img"
        :src="props.payload.content.pic"
      >
      <div class="product-card-information">
        <div class="product-card-title">
          {{ props.payload.content.header }}
        </div>
        <div class="product-card-description-block">
          <div class="product-card-description">
            {{ props.payload.content.desc }}
          </div>
        </div>
      </div>
    </div>
    <div v-for="(item, index) in bottomCustomField" :key="index" class="product-card-custom-field">
      <div class="custom-field-line">
        <div v-if="item.name" class="order-name">
          {{ item.name }}:
        </div>
        <div class="order-value">
          {{ item.value }}
        </div>
      </div>
    </div>
    <div v-if="!isWeChat" class="product-card-link"  @click="jumpProductCard">
      {{ TUITranslateService.t("AIDesk.跳转") }}
    </div>
  </div>
</template>

<script lang="ts">
import vue from '../../../../../../adapter-vue';
import { customerServicePayloadType } from '../../../../../../interface';
import { isApp, isWeChat } from '../../../../../../utils/env';
import { openSafeUrl } from '../../../../../../utils/utils';
import { TUITranslateService } from '../../../../../../@aidesk/uikit-engine';
import Log from '../../../../../../utils/logger';

// eslint-disable-next-line
declare var uni: any;

const { computed } = vue;

interface Props {
  payload: customerServicePayloadType;
}

export default {
  props: {
    payload: {
      type: Object as () => customerServicePayloadType,
      default: () => ({}),
    },
  },
  emits: ['sendMessage'],
  setup(props: Props) {
    const jumpProductCard = () => {
      const { url } = props.payload.content;
      if (url) {
        // 支持打开网页或 app 内页面间跳转
        // 小程序内打开网页有安全限制，请开发者参考 https://developers.weixin.qq.com/miniprogram/dev/component/web-view.html 实现
        if (url.startsWith('https://') || url.startsWith('http://')) {
          // android/ios/harmony app
          // #ifdef APP-PLUS | APP-HARMONY
          // @ts-ignore
          plus.runtime.openURL(url);
          // #endif
          // #ifdef H5
          // @ts-ignore
          openSafeUrl(url);
          // #endif
        } else {
          uni.navigateTo({
            url,
          });
        }
      } else {
        Log.w('Missing required url');
      }
    };
    const customField = computed(() => {
      return Array.isArray(props.payload.content.customField) ? props.payload.content.customField : [];
    });

    const topCustomField = computed(() => {
      return customField.value.filter((item) => item.position === 'top');
    });

    const bottomCustomField = computed(() => {
      return customField.value.filter((item) => item.position !== 'top');
    });
    return {
      props,
      isApp,
      isWeChat,
      jumpProductCard,
      TUITranslateService,
      topCustomField,
      bottomCustomField,
    };
  },
};
</script>
<style lang="scss" scoped>
.message-product-card {
  min-width: 100%;
  max-width: 400px;
  margin-top: 2px;
  display: flex;
  flex-direction: column;
  .product-card-main {
    display: flex;
    margin: 10px 0 10px 0;
  }
}
.product-img {
  width: 60px;
  height: 60px;
  border-radius: 10px;
  flex-shrink: 0;
  object-fit: cover;
  margin-right: 15px;
  align-self: center;
}

.product-card-information {
  width:100%;
  margin-right: 5px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;

  .product-card-title {
    color: #000000;
    font-size: 12px;
    display: -webkit-box;
    overflow: hidden;
    text-overflow: ellipsis;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    word-break: break-word;
    overflow-wrap: anywhere;
  }

  .product-card-description-block {
    display:flex;
    justify-content: space-between;
    align-items: center;
    gap: 5px;
  }

  .product-card-description {
    font-size: 14px;
    max-width: 100px;
    color: #1C66E5;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-weight: 600;
    flex: 0 1 100px;
  }
}
.product-card-custom-field {
  color: #000;
  margin: 0 0 5px 0;
}
.custom-field-line {
  display: flex;
  font-size: 12px;
  gap: 5px;
  overflow: hidden;
  .order-name {
    color: #878787;
    margin-right: 3px;
    white-space: nowrap;
    flex-shrink: 0;
  }
  .order-value {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
.product-card-link {
  cursor: pointer;
  background-color: #1c66e5;
  color: #ffffff;
  padding: 2px 18px;
  border-radius: 23px;
  font-size: 12px;
  line-height: 22px;
  flex: 0 0 auto;
  margin-left: auto;
}
</style>
