<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" :style="{ marginTop: '10px', height: '60px' }">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="70px">
            <el-form-item label="设备名称" prop="deviceName">
              <el-input v-model="queryParams.deviceName" placeholder="请输入设备名称" clearable @keyup.enter="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">
      <div class="table-content">
        <el-table v-loading="loading" :data="flowRateList" @selection-change="handleSelectionChange">
          <el-table-column label="" align="left" prop="" width="10"/>
          <el-table-column label="设备ID" align="left" prop="deviceId" width="180"/>
          <el-table-column label="设备名称" align="left" prop="deviceName" width="250"/>
          <el-table-column label="设备SN" align="left" prop="deviceSn" width="250"/>
          <el-table-column label="设备MAC" align="left" prop="deviceMac" width="180"/>
          <el-table-column label="总人数" align="center" prop="totalNum" width="100"/>
          <el-table-column label="今日" align="center" prop="todayNum" width="80"/>
          <el-table-column label="昨日" align="center" prop="yesterdayNum" width="80"/>
          <el-table-column label="上周" align="center" prop="lastWeekNum" width="80"/>
          <el-table-column label="本周" align="center" prop="weekNum" width="80"/>
          <el-table-column label="本月" align="center" prop="monthNum" width="80"/>
          <el-table-column label="备注" align="left" prop="remark" :show-overflow-tooltip="true"/>
          <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-tooltip content="30天人流记录" placement="top">
                <el-button link type="primary" icon="View" @click="handleMonthInfo(scope.row)"
                           v-hasPermi="['device:chatRecord:query']"></el-button>
              </el-tooltip>
              <el-tooltip content="人流列表" placement="top">
                <el-button link type="primary" icon="Position" @click="handleInfo(scope.row)"
                           v-hasPermi="['device:visitorsFlowRate:query']"></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
      </div>

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

    <el-dialog :title="dialog.title" v-model="dialog.visible" append-to-body>
      <div ref="chartRef" style="width: 100%; height: 400px;"></div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ChatRecord" lang="ts">
import {getVisitorsFlowRateMonthInfo, listVisitorsFlowRateCount,} from '@/api/smsb/device/visitors_flow_rate';
import {VisitorsFlowRateVO,VisitorsFlowRateForm,VisitorsFlowRateQuery} from "@/api/smsb/device/visitors_flow_rate_types";
import {ref} from "vue";
import {ChatRecordVO} from "@/api/smsb/device/device_chat_record_type";
import * as echarts from "echarts";

const {proxy} = getCurrentInstance() as ComponentInternalInstance;

const flowRateList = ref<VisitorsFlowRateVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const chartRef = ref();

const queryFormRef = ref<ElFormInstance>();
const chatRecordFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: VisitorsFlowRateForm = {
  id: undefined,
  deviceId: undefined,
  deviceName: undefined,
}
const data = reactive<PageData<VisitorsFlowRateForm, VisitorsFlowRateQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    difyId: undefined,
    name: undefined,
    deviceId: undefined,
    deviceName: undefined,
    params: {}
  },
  rules: {}
});

const {queryParams, form, rules} = toRefs(data);

/** 查询问答记录列表 */
const getList = async () => {
  loading.value = true;
  const res = await listVisitorsFlowRateCount(queryParams.value);
  flowRateList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}
const handleInfo = async (row?: VisitorsFlowRateVO) => {
  const deviceName = row.deviceName;
  // 跳转页面 /source/push/review?id=1898991996092481537&type=approval&taskId=1898992031169445891
  proxy.$router.push('/device/pc/visitorsFlowRate?deviceName=' + deviceName);
}

const handleMonthInfo = async (row?: ChatRecordVO) => {
  const res = await getVisitorsFlowRateMonthInfo(row.deviceId);
  dialog.title = '30天人流记录';
  dialog.visible = true;
  // 使用 nextTick 确保 DOM 更新后再初始化图表
  await nextTick();
  const chart = echarts.init(chartRef.value, 'macaroons');
  chart.setOption({
    tooltip: {
      trigger: 'axis',
    },
    xAxis: {
      type: 'category',
      data: res.data.dates,
    },
    yAxis: {
      type: 'value',
    },
    series: [{
      name: '人流量',
      type: 'line',
      data: res.data.counts,
      smooth: true,
    }],
  });
}


/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  chatRecordFormRef.value?.resetFields();
}

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

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

/** 多选框选中数据 */
const handleSelectionChange = (selection: VisitorsFlowRateVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}


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