<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="objectId">
              <el-input v-model="queryParams.objectId" 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 fixed="left" prop="objectId" />
        <el-table-column label="品牌" align="center" width="100" prop="objectAttr" />
        <el-table-column label="车辆类型" align="center" prop="objectType" />
        <el-table-column label="违法变道" align="center" prop="eventCount5" />
        <el-table-column label="压线行驶" align="center" prop="eventCount6" />
        <el-table-column label="机动车逆行" align="center"width="100" prop="eventCount7" />
        <el-table-column label="车辆闯禁" align="center" prop="eventCount8" />
        <el-table-column label="违法停车" align="center" prop="eventCount9" />
        <el-table-column label="占用非机动车道" align="center" width="120" prop="eventCount10" />
        <el-table-column label="不按规定车道行驶" align="center" width="130" prop="eventCount11" />
        <el-table-column label="货车右转必停" align="center" width="120" prop="eventCount12" />
      </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 { keyVehicleEventStat } 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,
  objectId: 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 keyVehicleEventStat(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>
