<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
      :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="90px">
            <el-form-item label="监控点名称" prop="pointName">
              <el-input v-model="queryParams.pointName" placeholder="请输入监控点名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="时间范围" prop="params">
              <el-date-picker v-model="queryParams.params.date" value-format="YYYY-MM-DD" type="daterange"
                range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间" unlink-panels :shortcuts="shortcuts"
                @change="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="cyclingList">
        <el-table-column label="监控点名称" align="center" width="150" show-overflow-tooltip prop="pointName" />
        <el-table-column label="非机动车占机动车道" align="center" prop="nonMotorVehicleOnLane" />
        <el-table-column label="非机动车不戴头盔" align="center" prop="nonMotorVehicleNoHelmet" />
        <el-table-column label="行人走机动车道" align="center" prop="pedestrianOnLane" />
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="cycling" lang="ts">
import { cycleEventStat } from '@/api/sense/events';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const cyclingList = ref([])
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const shortcuts = [
  {
    text: '上周',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
      return [start, end]
    },
  },
  {
    text: '上个月',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
      return [start, end]
    },
  },
  {
    text: '前三个月',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
      return [start, end]
    },
  },
]
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  pointName: undefined,
  params: {
    date: [],
    startDate: '',
    endDate: ''
  }
})
const getList = async () => {
  loading.value = true;
  queryParams.value.params.startDate = ''
  queryParams.value.params.endDate = ''
  if (queryParams.value.params.date && queryParams.value.params.date.length) {
    const [startDate, endDate] = queryParams.value.params.date
    queryParams.value.params.startDate = startDate
    queryParams.value.params.endDate = endDate
  }
  const res = await cycleEventStat(queryParams.value);
  cyclingList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}



/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}



onMounted(() => {
  getList();
});
</script>
