<template>
  <fs-page class="fs-page-custom">
    <fs-crud ref="crudRef" v-bind="crudBinding">
      <template #search-left>
        <DateRangePicker v-model="dateRange"></DateRangePicker>
      </template>
      <template #header-middle>
        <DataTendencyChart
          :query="queryParams"
          :fetchCard="getCardData"
          :fetchLine="getLineData">
        </DataTendencyChart>
      </template>
      <template #cell_adGroupName="scope">
        <el-link type="primary" :underline="false" @click="jumpAds(scope.row)">{{ scope.row.adGroupName }}</el-link>
      </template>
		</fs-crud>
	</fs-page>
</template>

<script lang="ts" setup>
import {onMounted, ref, watch} from 'vue'
import {FsPage, useFs} from '@fast-crud/fast-crud'
import {createCrudOptions} from './crud'
import {LocationQueryValue, useRouter} from 'vue-router'
import DateRangePicker from '/@/components/DateRangePicker/index.vue'
import {usePublicData} from '/@/stores/publicData'
import {storeToRefs} from 'pinia'
import DataTendencyChart from '/@/views/adManage/sp/chartComponents/dataTendency.vue'
import {getCardData, getLineData} from './api'

const publicData = usePublicData()
const router = useRouter()
interface Props {
  campaignId: LocationQueryValue | LocationQueryValue[]
}
const props = defineProps<Props>()
const { dateRange } = storeToRefs(publicData)
const queryParams = ref({
  campaignId: props.campaignId,
  dateRange
})
const { crudBinding, crudRef, crudExpose } = useFs({ createCrudOptions, context: queryParams })

onMounted(() => {
	crudExpose.doRefresh();
})
const jumpAds = (row: any) => {
  router.push({
    name: 'AdGroupDetail',
    query: { adGroupId: row.adGroupId, targetingType: row.targetingType, tagsViewName: row.adGroupName }
  })
}
watch(
  queryParams,
  async () => {
    crudExpose.doRefresh()
  },
  { deep: true }
)

</script>

<style scoped>

</style>
