<script lang="ts" setup>
import { nextTick, onMounted, ref, watch } from 'vue'
import { FsPage, useFs } from '@fast-crud/fast-crud'
import { createCrudOptions } from './crud'
import { useRoute, useRouter } from 'vue-router'
import DataTendencyChart from '/@/views/adManage/sd/chartComponents/dataTendency.vue'
import { useShopInfo } from '/@/stores/shopInfo'
import { usePublicData } from '/@/stores/publicData'
import AdStructChart from './chartComponents/adStruct.vue'
import { getCardData, getLineData, getLineMonthData, getLineWeekData } from './api'
import { storeToRefs } from 'pinia'
import { SdBaseColumn } from '/@/views/adManage/utils/commonTabColumn'
import DataCompare from '/@/components/dataCompare/index.vue'
import { sdTypeMap, sdtargetMap } from '/@/views/adManage/utils/enum'

const tabActiveName = ref('dataTendency')
const shopInfo = useShopInfo()
const publicData = usePublicData()
const { dateRange } = storeToRefs(publicData)
const { profile } = storeToRefs(shopInfo)
const queryParams = ref({
  profileId: profile.value.profile_id,
  dateRange,
})

const { crudBinding, crudRef, crudExpose } = useFs({ createCrudOptions, context: queryParams })
const route = useRoute()
const router = useRouter()
const adStructChartRef = ref()
const dataTendencyRef = ref()
const showCompare = ref(false)

onMounted(() => {
  crudExpose.doRefresh()
})

const jumpGroup = (row: any) => {
  router.push({
    name: 'CampaignDetail',
    query: { campaignId: row.campaignId, tagsViewName: row.campaignName },
  })
}

const resizeTabChart = () => {
  if (tabActiveName.value === 'dataTendency') {
    dataTendencyRef.value.resizeChart()
  } else if (tabActiveName.value === 'adStruct') {
    adStructChartRef.value.resizeChart()
  }
}
const changeTab = () => {
  nextTick(() => {
    resizeTabChart()
  })
}
defineExpose({ resizeTabChart })

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" @tab-change="changeTab">
          <el-tab-pane label="数据趋势" name="dataTendency">
            <DataTendencyChart
              :query="queryParams"
              v-if="tabActiveName === 'dataTendency'"
              :fetchCard="getCardData"
              :fetch-line-month="getLineMonthData"
              :fetch-line-week="getLineWeekData"
              :fetchLine="getLineData">
            </DataTendencyChart>
          </el-tab-pane>
          <el-tab-pane label="广告结构" name="adStruct">
            <AdStructChart v-if="tabActiveName === 'adStruct'" />
          </el-tab-pane>
          <el-tab-pane label="散点视图" name="scatterView"></el-tab-pane>
        </el-tabs>
      </template>
      <template #cell_percentTimeInBudget="scope">
        <el-progress :percentage="scope.row.percentTimeInBudget > 0 ? scope.row.percentTimeInBudget * 100 : 0" />
      </template>

      <template #cell_expression="scope">
        <div>
          <!-- 预处理以确保 expression 是数组且第一个元素存在且有 value -->
          <div v-if="Array.isArray(scope.row.expression) && scope.row.expression.length > 0 && scope.row.expression[0]?.value">
            <!-- 渲染除了第一个 lookback 以外的第一个元素 -->
            <div v-if="scope.row.expression[0].value[0].type !== 'lookback'" style="font-weight: 550; color: #505968">
              {{ scope.row.expression[0].value[0].value ?? '--' }}
            </div>
            <!-- 渲染其他所有元素（包括可能跳过的第一个 lookback） -->
            <div>
              <template v-for="(item, index) in scope.row.expression[0].value" :key="index">
                <span v-if="index > 0 || scope.row.expression[0].value[0].type === 'lookback'">
                  <span v-if="['asinPriceGreaterThan', 'asinPriceLESSThan', 'asinReviewRatingGreaterThan', 'asinReviewRatingLessThan'].includes(item.type)">
                    {{ sdtargetMap[item.type] }}
                  </span>
                  <span v-else>
                    {{ item.type ? sdtargetMap[item.type] + ': ' : '--' }}
                  </span>
                  <span style="color: #000;">
                    {{ item.value ?? '--' }}
                  </span>
                  <span v-if="index < scope.row.expression[0].value.length - 1">&nbsp;</span>
                </span>
              </template>
            </div>
          </div>
          <!-- 如果 expression 不存在或为空数组，显示暂无数据 -->
          <div v-else style="font-weight: 500; color: #000;">暂无数据</div>
        </div>
      </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 ? scope.row.campaignName : '--' }}</div>
          <!-- </el-link> -->
        </el-tooltip>
      </template>
      <!-- 类型 -->
      <template #cell_resolvedExpression="scope">
        <div>
          {{ sdTypeMap[scope.row.resolvedExpression[0].type as string] }}
        </div>
      </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 #cell_suggestedBid="scope">
        <div>{{ scope.row.suggestedBid ? `$${scope.row.suggestedBid}` : '--' }}</div>
        <div class="text-range">
          {{ scope.row.suggestedBid_lower ? `$${scope.row.suggestedBid_lower}` : '--' }} ~
          {{ scope.row.suggestedBid_upper ? `$${scope.row.suggestedBid_upper}` : '--' }}
        </div>
      </template>
      <template #cell_MissedImpressions="scope"> {{ scope.row.MissedImpressionsLower ?? '0' }} ~ {{ scope.row.MissedImpressionsUpper ?? '0' }} </template>
      <template #cell_MissedClicks="scope"> {{ scope.row.MissedClicksLower ?? '0' }} ~ {{ scope.row.MissedClicksUpper ?? '0' }}</template>
      <template #cell_MissedSales="scope"> {{ scope.row.MissedSalesLower ?? '0' }} ~ {{ scope.row.MissedSalesUpper ?? '0' }}</template>

      <template v-for="field of Object.keys(SdBaseColumn)" #[`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 lang="scss" 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;
}

.ellipsis {
  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;
}

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

::v-deep(span.el-link__inner) {
  white-space: nowrap;
  text-overflow: ellipsis;
  display: inline;
}
</style>
