<script lang="ts" setup>
import { inject, onMounted, ref, watch } from 'vue'
import { FsPage, useFs } from '@fast-crud/fast-crud'
import { createCrudOptions } from './crud'
import DataTendencyChart from '/@/views/adManage/sp/chartComponents/dataTendency.vue'
import { getCardData, getLineData, getLineMonthData, getLineWeekData } from './api'
import { useShopInfo } from '/@/stores/shopInfo'
import { usePublicData } from '/@/stores/publicData'
import { storeToRefs } from 'pinia'
import { spCampaignPuchasedOtherProductsMetricsEnum } from '/@/views/adManage/utils/enum.js'
import { SpCampaignPuchasedOtherProductsColumn } from '/@/views/adManage/utils/commonTabColumn.js'
import DataCompare from '/@/components/dataCompare/index.vue'
import router from '/@/router'


const searchItem = inject('topSearchItem')
const searchType = inject('searchType')
const filterItem = inject('searchItem')

const tabActiveName = ref('dataTendency')
const shopInfo = useShopInfo()
const publicData = usePublicData()
const { dateRange } = storeToRefs(publicData)
const { profile } = storeToRefs(shopInfo)
const initMetric = [
  { metric: 'TotalPurchasesOtherSKU', color: '#0085ff', label: '其它商品订单数' },
  { metric: 'TotalSalesOtherSKU', color: '#3fd4cf', label: '其它商品销售额' },
  { metric: 'TotalUnitOrderedOtherSKU', color: '#ff9500', label: '其它商品销量' },
]
const showCompare = ref(false)
const queryParams = ref({
  profileId: profile.value.profile_id,
  dateRange,
  searchItem,
  searchType,
  filterItem,
})
const { crudBinding, crudRef, crudExpose } = useFs({ createCrudOptions, context: queryParams })
const jumpGroup = (row: any) => {
  router.push({
    name: 'CampaignDetail',
    query: { campaignId: row.campaignId, tagsViewName: row.campaignName },
  })
}

onMounted(async () => {
  crudExpose.doRefresh()
})
watch(
  queryParams,
  async () => {
    crudExpose.doRefresh()
  },
  { deep: true }
)
</script>

<template>
  <fs-page class="fs-page-custom">
    <fs-crud ref="crudRef" v-bind="crudBinding">
      <template #header-middle>
        <el-tabs v-model="tabActiveName" class="chart-tabs" type="border-card">
          <el-tab-pane label="数据趋势" name="dataTendency">
            <DataTendencyChart
              :query="queryParams"
              :fetchCard="getCardData"
              :fetchLine="getLineData"
              :fetch-line-month="getLineMonthData"
              :fetch-line-week="getLineWeekData"
              :init-metric="initMetric"
              :metric-enum="spCampaignPuchasedOtherProductsMetricsEnum">
            </DataTendencyChart>
          </el-tab-pane>
        </el-tabs>
      </template>
      <template #cell_adProduct="scope">
        <p>{{ scope.row.advertisedAsin }}</p>
        <p>{{ scope.row.advertisedSku }}</p>
      </template>
      <template #cell_purchasedProduct="scope">
        <p>{{ scope.row.purchasedAsin }}</p>
      </template>

      <template #cell_campaignName="scope">
        <el-tooltip effect="dark" :content="scope.row.campaignName" placement="top">
          <el-link type="primary" :underline="false" @click="jumpGroup(scope.row)">
            <div class="en-text">{{ scope.row.campaignName }}</div>
          </el-link>
        </el-tooltip>
      </template>
      <template #cell_adGroupName="scope">
        <el-tooltip effect="dark" :content="scope.row.adGroupName" placement="top">
          <el-link type="primary" :underline="false" @click="jumpGroup(scope.row)">
            <div class="en-text">{{ scope.row.adGroupName }}</div>
          </el-link>
        </el-tooltip>
      </template>
      <template v-for="field of Object.keys(SpCampaignPuchasedOtherProductsColumn)" #[`cell_${field}`]="scope">
        <DataCompare
          :field="field"
          :value="scope.row[field]"
          :prev-val="scope.row[`prev${field}`]"
          :gap-val="scope.row[`gap${field}`]"
          :date-range="dateRange"
          :show-compare="showCompare" />
      </template>
      <template #toolbar-left>
        <div class="campare-switch">
          <span>数据对比 </span>
          <el-switch v-model="showCompare" size="small" />
        </div>
      </template>
    </fs-crud>
  </fs-page>
</template>

<style scoped>
.campare-switch {
  flex: none;
}

::v-deep(.el-table__footer-wrapper td.el-table__cell:nth-child(n + 3):nth-child(-n + 6) .cell) {
  display: none;
}

.en-text {
  max-width: 100%;
  font-size: 13px;
  font-weight: 420;
  word-break: break-word;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: normal;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
}

::v-deep(.el-table__footer-wrapper) {
  border: 0;
}

::v-deep(.el-table .el-table__footer-wrapper .cell) {
  font-weight: 600;
}

.text-range {
  color: #808184;
}
</style>
