<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="flowDetailList" >
        <el-table-column label="任务编号" align="center" prop="taskId" />
        <el-table-column label="监控点名称" align="center" width="150" show-overflow-tooltip prop="pointName" />
        <el-table-column label="车道号" align="center" prop="laneNumber" />
        <el-table-column label="车流量" align="center" prop="vehicleCount" />
        <el-table-column label="平均数据" align="center" prop="averageSpeed" />
        <el-table-column label="排队长度" align="center" prop="queueTime" />
        <el-table-column label="空间占有率" align="center" width="120" prop="spaceRate" />
        <el-table-column label="时间占有率" align="center" width="120" prop="timeRate" />
        <el-table-column label="车头时距" align="center" prop="headwayTime" />
        <el-table-column label="车头间距" align="center" prop="headwayDistance" />
        <el-table-column label="车辆类型与行驶方向数" align="center" width="160" prop="vehicleRemark" />
      </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="FlowDetail" lang="ts">
import { listFlowDetail, getFlowDetail, delFlowDetail, addFlowDetail, updateFlowDetail } from '@/api/sense/flowDetail';
import { FlowDetailVO, FlowDetailQuery, FlowDetailForm } from '@/api/sense/flowDetail/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const flowDetailList = ref<FlowDetailVO[]>([]);
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 listFlowDetail(queryParams.value);
  flowDetailList.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>
