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

const selectedPortfolios = ref('sp')
const portfolios = [
  {
    value: 'sp/sb/sd',
    label: 'SP/SB/SD'
  },
  {
    value: 'sp',
    label: 'SP'
  },
  {
    value:'sb',
    label:'SB'
  },
  {
    value:'sd',
    label:'SD'
  },
  {
    value: 'dsp',
    label: 'DSP',
    disabled: true
  }
]
const tabActiveName = ref('dataTendency')
const shopInfo = useShopInfo()
const publicData = usePublicData()
const { dateRange } = storeToRefs(publicData)
const { profile } = storeToRefs(shopInfo)
const queryParams = ref({
  dateRange,
  profileId: profile.value.profile_id,
  campaignType: selectedPortfolios.value
})
const { crudBinding, crudRef, crudExpose } = useFs({ createCrudOptions, context: queryParams })
const router = useRouter()
const showCompare = ref(false)


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

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

<template>
  <div class="overview-tabs">
    <DateRangePicker v-model="dateRange"></DateRangePicker>
    <el-select
        v-model="selectedPortfolios"
        placeholder="SP"
        style="width: 200px"
        collapse-tags
        collapse-tags-tooltip
        :max-collapse-tags="3"
    >
      <el-option v-for="info of portfolios" :label="info.label" :value="info.value" :disabled="info.disabled"></el-option>
    </el-select>
  </div>
  <fs-page class="fs-page-custom" style="margin-top: -11px">
    <fs-crud ref="crudRef" v-bind="crudBinding">
      <template #header-middle>
        <el-tabs v-model="tabActiveName" class="chart-tabs" type="border-card">
          <DataTendencyChart
              v-if="tabActiveName === 'dataTendency'"
              :query="queryParams"
              :fetchCard="getCardData"
              :fetchLine="getLineData"
              :fetch-line-month="getLineMonthData"
              :fetch-line-week="getLineWeekData">
          </DataTendencyChart>
        </el-tabs>
      </template>
      <template #cell_percentTimeInBudget="scope">
        <el-progress :percentage="scope.row.percentTimeInBudget > 0 ? scope.row.percentTimeInBudget * 100 : 0" />
      </template>
      <template #cell_campaignName="scope">
        <el-link type="primary" :underline="false" @click="jumpGroup(scope.row)">{{ scope.row.campaignName }}</el-link>
      </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(BaseColumn)" #[`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>
    </fs-crud>
  </fs-page>
</template>

<style lang="scss" scoped>
.campare-switch {
  flex: none;
}
::v-deep(.el-table--border .el-table__footer-wrapper) {
  border: none;
}
::v-deep(.el-table .el-table__footer-wrapper .cell) {
  font-weight: 600;
}
</style>
