<template>
  <div>
    <div :class="['bottom-quick-order-container']">
      <div @click="closeOrder">
        <image v-if="isTikTok" class="close-icon-in-tt" :src="cardToolbarCloseIcon"></image>
        <Icon v-else class="close-icon" :file="cardToolbarCloseIcon" width="12px" height="12px"/>
      </div>
      <div v-for="(item, index) in topCustomField" :key="index" class="quick-order-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="quick-order-main">
        <img
          v-if="props.bottomQuickOrder.pic"
          class="quick-order-img"
          :src="props.bottomQuickOrder.pic"
        >
        <div class="quick-order-information">
          <div class="quick-order-title">
            {{ props.bottomQuickOrder.header }}
          </div>
          <div class="quick-order-description-block">
            <div class="quick-order-description">
              {{ props.bottomQuickOrder.desc }}
            </div>
            <div v-if="bottomCustomField.length === 0" class="quick-order-link"  @click="sendOrder">
              {{TUITranslateService.t("发送")}}
            </div>
          </div>
        </div>
      </div>
      <div v-for="(item, index) in bottomCustomField" :key="index" class="quick-order-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="bottomCustomField.length !== 0" class="quick-order-link"  @click="sendOrder">
        {{TUITranslateService.t("发送")}}
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import vue from '../../../../adapter-vue';
import { TUITranslateService } from '../../../../@aidesk/uikit-engine';
import cardToolbarCloseIcon from '../../../../assets/dialog-close.png';
import Icon from '../../../common/Icon.vue';
import { QuickOrderModel } from '../../../../interface';
import { isTikTok } from "../../../../utils/env";

const { computed } = vue;
interface IProps {
  bottomQuickOrder: QuickOrderModel;
}
const emits = defineEmits(['closeOrder','sendOrder']);

const props = withDefaults(defineProps<IProps>(), {
  bottomQuickOrder: () => ({} as QuickOrderModel),
});

const customField = computed(() => {
  return Array.isArray(props.bottomQuickOrder?.customField) ? props.bottomQuickOrder.customField : [];
});

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

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

const sendOrder = () => {
  emits('sendOrder');
};

const closeOrder = () => {
  emits('closeOrder');
};
</script>
<style scoped lang="scss">
.bottom-quick-order-line {
  display: flex;
  align-items: flex-end;
}
.bottom-quick-order-container {
  display: flex;
  flex-direction: column;
  background: #fff;
  padding: 10px 30px 10px 10px;
  border-radius: 8px;
  margin: 0 10px 0 10px;
  position: relative;
}
.quick-order-custom-field {
  font-size: 12px;
  max-width: 100%;
  overflow: hidden;
}
.custom-field-line {
  display: flex;
  gap: 5px;
  font-size: 12px;
  line-height: 22px;
}
.order-name {
  color: #878787;
  margin-right: 3px;
  white-space: nowrap;
  flex-shrink: 0;
}
.order-value {
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.quick-order-main {
  display: flex;
  margin: 10px 0 10px 0;
}
.quick-order-img {
  width: 60px;
  height: 60px;
  border-radius: 10px;
  margin-right: 15px;
  flex-shrink: 0;
  object-fit: cover;
}

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

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

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

  .quick-order-description {
    font-size: 16px;
    max-width: 120px;
    color: #1C66E5;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-weight: 500;
    flex: 0 1 120px;
  }
}
.quick-order-link {
  cursor: pointer;
  background-color: #1c66e5;
  color: #ffffff;
  font-size: 12px;
  padding: 2px 18px;
  line-height: 22px;
  border-radius: 23px;
  flex: 0 0 auto;
  margin-left: auto;
}
.close-icon {
  position: absolute;
  right: 10px;
  top: 10px;
  cursor: pointer;
}
.close-icon-in-tt {
  position: absolute;
  right: 10px;
  top: 10px;
  cursor: pointer;
  width: 12px;
  height: 12px;
}
</style>
