<template>
  <div class="layout-padding">
    <div class="engagement-time">
      <el-row :gutter="12" style="padding: 0 12px 12px; row-gap: 12px;">
        <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
          <SelectForm :type="type" :form="form" :title="'访问页面'" />
        </el-col>
      </el-row>
      <el-row :gutter="12" style="padding: 0 12px 12px; row-gap: 12px;">
        <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
          <View :type="type" :form="form" ref="viewRef" />
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script lang="ts" name="engagementTime" setup>
import { reactive } from 'vue'
import View from './View.vue';
import SelectForm from '../components/SelectForm.vue';
import { formatDate } from '/@/utils/formatTime';

const type = ref('daterange');

const form = reactive({
  dateArray: [new Date().getTime() - 1000 * 60 * 60 * 24 * 7, new Date().getTime()],
  date: new Date().getTime(),
  selectedDates: [] as string[], // 存储多个选中的日期
  timeCompare: 'time1',
  channel: 'all',
  version: 'all'
})

const viewRef = ref<InstanceType<typeof View>>();

watch(form, (newVal) => {
  console.log([`${formatDate(new Date(newVal.dateArray[0]), 'YYYY-mm-dd')} - ${formatDate(new Date(newVal.dateArray[1]), 'YYYY-mm-dd')}`]);
  nextTick(() => {
    viewRef.value?.handleDateChange([`${formatDate(new Date(newVal.dateArray[0]), 'YYYY-mm-dd')} - ${formatDate(new Date(newVal.dateArray[1]), 'YYYY-mm-dd')}`]);
  })
}, { deep: true, immediate: true })

onMounted(() => {
  nextTick(() => {
    viewRef.value?.handleDateChange([`${formatDate(new Date(form.dateArray[0]), 'YYYY-mm-dd')} - ${formatDate(new Date(form.dateArray[1]), 'YYYY-mm-dd')}`]);
  })
})

</script>
<style scoped lang="scss">
@import '/@/views/count/styles/common.scss';

svg {
  vertical-align: middle;
  margin: 0 0 0 12px;
}

.engagement-time {

}

</style>
