<template>
  <fs-page class="fs-page-custom" v-loading='loading'>
    <fs-crud ref="crudRef" v-bind="crudBinding">
      <template #search-left>
        <DateRangePicker v-model="dateRange"></DateRangePicker>
      </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>
      <template #toolbar-left>
        <div>
          <span>数据对比 </span>
          <el-switch v-model="showCompare" size="small" />
        </div>
      </template>
    </fs-crud>
  </fs-page>
</template>

<script lang="ts" setup>
import { ref, onMounted, Ref, watch } from 'vue'
import { useFs, FsPage } from '@fast-crud/fast-crud'
import { createCrudOptions } from './crud'
import { usePublicData } from '/@/stores/publicData'
import { storeToRefs } from 'pinia'
import DateRangePicker from '/@/components/DateRangePicker/index.vue'
import { GetList } from './api'
import { BaseColumn } from '/@/views/adManage/utils/commonTabColumn.js'
import { LocationQueryValue } from 'vue-router'
import DataCompare from '/@/components/dataCompare/index.vue'

defineOptions({
  name: "Placement"
})
const publicData = usePublicData()
const { dateRange } = storeToRefs(publicData)
interface Props {
  campaignId: LocationQueryValue | LocationQueryValue[]
}
const props = defineProps<Props>()
const loading = ref(true)

const showCompare = ref(false)
const fetchData = async () => {
  loading.value = true
  const resp = await GetList({ 
    campaignId: props.campaignId,
    startDate: dateRange.value[0],
    endDate: dateRange.value[1]
  })
  crudExpose.setTableData(resp.data)
  loading.value = false
}

const context = { fetchData }

const { crudBinding, crudRef, crudExpose } = useFs({ createCrudOptions, context })
onMounted(async () => {
  await fetchData()
})



watch(
  dateRange, 
  async () => await fetchData()
)

</script>

<style scoped>
.red {
  color: red;
}
.green {
  color: #1cbc0e;
}
</style>