<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" :lazy="true">
            <AdStructChart ref="adStructChartRef"/>
          </el-tab-pane>
          <el-tab-pane label="散点视图" name="scatterView" :lazy="true"></el-tab-pane>
        </el-tabs>
      </template>
    </fs-crud>
  </fs-page>
</template>

<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/sp/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 '/@/views/adManage/sp/campaigns/api'
import {storeToRefs} from 'pinia'


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()


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

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>

<style lang="scss">
.chart-tabs {
  margin: 5px 0;

  .el-tabs__nav {
    padding-left: 0 !important;
  }
}

</style>
