<template>
  <div class="search-key-words" @click="onBrowseDetail">
    <div class="card">
      <div class="title" v-if="search_engine_desc">
        <LogoOrDot point="icon" :icon_url="search_engine_icon" :need_arrow="true" :need_line="false"
          line_style="dotted" />
        <span>在{{ search_engine_desc }}</span>
      </div>
      <!-- 搜索词 -->
      <div class="searchword-wrap" v-if="searchword">
        <SearchWord :searchword="searchword" :search_engine="search_engine" />
      </div>
      <!-- 点击广告 -->
      <div class="adv-text" v-if="is_adv">
        <LogoOrDot point="dot" />
        <span>点击了 {{ adv_title }}</span>
      </div>
      <!-- 关键词 -->
      <!-- 如果关键词没有，但是有创意的话也要用Keyword组件 -->
      <div class="keyword-wrap" v-if="is_creative">
        <Keyword :text="keyword" :data="creative_data" />
      </div>
      <div class="frame-wrap" v-else-if="!is_creative && keyword">
        <DefaultFrame :text="keyword" />
      </div>
    </div>
    <!-- 浏览记录 -->
    <BrowseSteps :data="data" />
    <Device :data="device_info" />
  </div>
</template>

<script>
/**
 * 关键词
 * 有四种情况
 * 有关键词并且有创意
 * 有关键词没创意
 * 有创意没关键词
 * 没创意并且没关键词
 */
import { cdn_domain } from '@RecordCard/utils/cdn'
import Keyword from './components/Keyword/index.vue'
import SearchWord from './components/SearchWord/index.vue'
import LogoOrDot from '@RecordCard/components/Common/LogoOrDot/index.vue'
import Device from '@RecordCard/components/Common/Device/index.vue'
import BrowseSteps from '@RecordCard/components/Common/BrowseSteps/index.vue'
import DefaultFrame from '@RecordCard/components/Common/DefaultFrame/index.vue'
import {
  adv_dict,
  search_engine_dict,
  search_engine_desc_dict,
} from '@RecordCard/utils/dict'
export default {
  name: 'SearchKeyWords',
  components: {
    BrowseSteps,
    SearchWord,
    Device,
    LogoOrDot,
    Keyword,
    DefaultFrame,
  },
  props: {
    data: {
      type: Object,
      default: () => {
        return {}
      },
    },
  },
  data() {
    return {
      is_creative: false,
      is_need_user: false,
      user_name: '',
      search_engine: '',
      search_engine_desc: '',
      keyword: '',
      searchword: '',
      creative_data: {},
      equipment: '',
      is_adv: false,
      adv_title: "",
      device_info: {}
    }
  },
  watch: {
    data: {
      handler(val) {
        this.handleData(val)
      },
      immediate: true,
    },
  },
  computed: {
    search_engine_icon() {
      return `${cdn_domain}/frontend/weilingRecordCard/${this.search_engine}_icon.png`
    },
  },
  methods: {
    onBrowseDetail() {
      this.$emit('onBrowseDetail', this.data)
    },
    handleData(val) {
      const { web_site_his = {}, root_user = {}, user_info = {} } = val
      const {
        platform_id = '',
        keywords = '',
        searchword = '',
        creative = {},
        ad_type,
        is_ad,
        ip_location,
        use_environment = '',
      } = web_site_his
      if (root_user.type === 4) {
        this.is_need_user = true
        this.user_name = user_info?.nick_name
      }
      this.is_adv = is_ad;
      this.adv_title = adv_dict[ad_type];
      this.keyword = keywords
      this.searchword = searchword
      this.search_engine = search_engine_dict[platform_id]
      this.search_engine_desc = search_engine_desc_dict[platform_id]
      this.is_creative = Object.keys(creative).length > 0
      this.creative_data = creative
      this.device_info = {
        ip_location,
        equipment_name: use_environment,
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.search-key-words {
  width: 100%;
  cursor: pointer;

  .card {
    color: var(--wl-text-color-title-1, #0e0e0e);
    margin: $up-down-spacing 0;

    .title {
      color: #000;
      font-size: j(15);
      line-height: j(24);
      @extend %flex-middle;
      margin-bottom: $up-down-spacing;
    }

    .adv-text {
      @extend %flex-middle;
    }

    .searchword-wrap {
      margin-bottom: $up-down-spacing;
    }


    .keyword-wrap,
    .frame-wrap {
      margin-top: $up-down-spacing;
    }
  }
}
</style>
