import dayjs from "dayjs";
import { message } from "@/utils/message";
// import { getRoleList } from "@/api/system";
import { type PaginationProps } from "@pureadmin/table";
import { reactive, ref, onMounted } from "vue";

export function useWarnList() {
  const form = reactive({
    name: "",
    code: "",
    status: ""
  });
  const dataList = ref([]);
  const loading = ref(true);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });
  const columns: TableColumnList = [
    {
      label: "项目编号",
      prop: "id",
      minWidth: 100
    },
    {
      label: "项目名称",
      prop: "name",
      minWidth: 120
    },
    {
      label: "检测项",
      prop: "detection",
      minWidth: 150
    },
    {
      label: "告警任务",
      prop: "alarmTask",
      minWidth: 150
    },
    {
      label: "视频通道",
      prop: "videoChannel",
      minWidth: 150
    },
    {
      label: "视频通道",
      prop: "videoChannel",
      minWidth: 150
    },
    {
      label: "告警日期",
      minWidth: 180,
      prop: "createTime",
      formatter: ({ createTime }) =>
        dayjs(createTime).format("YYYY-MM-DD HH:mm:ss")
    },
    {
      label: "上报状态",
      prop: "reportingStatus",
      minWidth: 150
    },
    {
      label: "告警内容",
      prop: "alarmContent",
      minWidth: 150
    }
    // {
    //   label: "操作",
    //   fixed: "right",
    //   width: 240,
    //   slot: "operation"
    // }
  ];
  // const buttonClass = computed(() => {
  //   return [
  //     "!h-[20px]",
  //     "reset-margin",
  //     "!text-gray-500",
  //     "dark:!text-white",
  //     "dark:hover:!text-primary"
  //   ];
  // });

  const warnList = ref({
    list: [
      {
        createTime: 1605456000000, // 时间戳（毫秒ms）
        updateTime: 1684512000000,
        creator: "admin",
        id: 1,
        name: "零装线01",
        detection: "PCB板表面检测",
        alarmTask: "3",
        videoChannel: "10",
        reportingStatus: "已上报",
        alarmContent: "检测到表面缺陷"
      },
      {
        createTime: 1605456000000, // 时间戳（毫秒ms）
        updateTime: 1684512000000,
        creator: "admin",
        id: 2,
        name: "零装线02",
        detection: "人员违章违规",
        alarmTask: "4",
        videoChannel: "26",
        reportingStatus: "已上报",
        alarmContent: "检测到表面缺陷"
      },
      {
        createTime: 1605456000000, // 时间戳（毫秒ms）
        updateTime: 1684512000000,
        creator: "admin",
        id: 3,
        name: "零装线03",
        detection: "PCB板表面检测",
        alarmTask: "4",
        videoChannel: "31",
        reportingStatus: "已上报",
        alarmContent: "检测到表面缺陷"
      },
      {
        createTime: 1605456000000, // 时间戳（毫秒ms）
        updateTime: 1684512000000,
        creator: "admin",
        id: 4,
        name: "零装线04",
        detection: "PCB板表面检测",
        alarmTask: "3",
        videoChannel: "34",
        reportingStatus: "已上报",
        alarmContent: "检测到表面缺陷"
      },
      {
        createTime: 1605456000000, // 时间戳（毫秒ms）
        updateTime: 1684512000000,
        creator: "admin",
        id: 5,
        name: "零装线05",
        detection: "人员违章违规",
        alarmTask: "2",
        videoChannel: "21",
        reportingStatus: "已上报",
        alarmContent: "检测到表面缺陷"
      },
      {
        createTime: 1605456000000, // 时间戳（毫秒ms）
        updateTime: 1684512000000,
        creator: "admin",
        id: 6,
        name: "零装线06",
        detection: "PCB板表面检测",
        alarmTask: "4",
        videoChannel: "26",
        reportingStatus: "已上报",
        alarmContent: "检测到表面缺陷"
      },
      {
        createTime: 1605456000000, // 时间戳（毫秒ms）
        updateTime: 1684512000000,
        creator: "admin",
        id: 7,
        name: "零装线07",
        detection: "PCB板表面检测",
        alarmTask: "3",
        videoChannel: "23",
        reportingStatus: "已上报",
        alarmContent: "检测到表面缺陷"
      },
      {
        createTime: 1605456000000, // 时间戳（毫秒ms）
        updateTime: 1684512000000,
        creator: "admin",
        id: 8,
        name: "零装线08",
        detection: "人员违章违规",
        alarmTask: "5",
        videoChannel: "20",
        reportingStatus: "已上报",
        alarmContent: "检测到表面缺陷"
      },
      {
        createTime: 1605456000000, // 时间戳（毫秒ms）
        updateTime: 1684512000000,
        creator: "admin",
        id: 9,
        name: "零装线09",
        detection: "PCB板表面检测",
        alarmTask: "4",
        videoChannel: "23",
        reportingStatus: "已上报",
        alarmContent: "检测到表面缺陷"
      },
      {
        createTime: 1605456000000, // 时间戳（毫秒ms）
        updateTime: 1684512000000,
        creator: "admin",
        id: 10,
        name: "零装线10",
        detection: "PCB板表面检测",
        alarmTask: "4",
        videoChannel: "21",
        reportingStatus: "已上报",
        alarmContent: "检测到表面缺陷"
      },
      {
        createTime: 1605456000000, // 时间戳（毫秒ms）
        updateTime: 1684512000000,
        creator: "admin",
        id: 11,
        name: "零装线11",
        detection: "PCB板表面检测",
        alarmTask: "3",
        videoChannel: "15",
        reportingStatus: "已上报",
        alarmContent: "检测到表面缺陷"
      },
      {
        createTime: 1605456000000, // 时间戳（毫秒ms）
        updateTime: 1684512000000,
        creator: "admin",
        id: 12,
        name: "零装线12",
        detection: "人员违章违规",
        alarmTask: "3",
        videoChannel: "16",
        reportingStatus: "已上报",
        alarmContent: "检测到表面缺陷"
      },
      {
        createTime: 1605456000000, // 时间戳（毫秒ms）
        updateTime: 1684512000000,
        creator: "admin",
        id: 13,
        name: "零装线13",
        detection: "PCB板表面检测",
        alarmTask: "6",
        videoChannel: "19",
        reportingStatus: "已上报",
        alarmContent: "检测到表面缺陷"
      },
      {
        createTime: 1605456000000, // 时间戳（毫秒ms）
        updateTime: 1684512000000,
        creator: "admin",
        id: 14,
        name: "零装线14",
        detection: "人员违章违规",
        alarmTask: "4",
        videoChannel: "24",
        reportingStatus: "已上报",
        alarmContent: "检测到表面缺陷"
      },
      {
        createTime: 1605456000000, // 时间戳（毫秒ms）
        updateTime: 1684512000000,
        creator: "admin",
        id: 15,
        name: "零装线15",
        detection: "PCB板表面检测",
        alarmTask: "4",
        videoChannel: "35",
        reportingStatus: "已上报",
        alarmContent: "检测到表面缺陷"
      },
      {
        createTime: 1605456000000, // 时间戳（毫秒ms）
        updateTime: 1684512000000,
        creator: "admin",
        id: 16,
        name: "零装线16",
        detection: "人员违章违规",
        alarmTask: "2",
        videoChannel: "20",
        reportingStatus: "已上报",
        alarmContent: "检测到表面缺陷"
      },
      {
        createTime: 1605456000000, // 时间戳（毫秒ms）
        updateTime: 1684512000000,
        creator: "admin",
        id: 17,
        name: "零装线17",
        detection: "PCB板表面检测",
        alarmTask: "5",
        videoChannel: "37",
        reportingStatus: "已上报",
        alarmContent: "检测到表面缺陷"
      },
      {
        createTime: 1605456000000, // 时间戳（毫秒ms）
        updateTime: 1684512000000,
        creator: "admin",
        id: 18,
        name: "零装线18",
        detection: "PCB板表面检测",
        alarmTask: "4",
        videoChannel: "28",
        reportingStatus: "已上报",
        alarmContent: "检测到表面缺陷"
      },
      {
        createTime: 1605456000000, // 时间戳（毫秒ms）
        updateTime: 1684512000000,
        creator: "admin",
        id: 19,
        name: "零装线19",
        detection: "人员违章违规",
        alarmTask: "4",
        videoChannel: "40",
        reportingStatus: "已上报",
        alarmContent: "检测到表面缺陷"
      },
      {
        createTime: 1605456000000, // 时间戳（毫秒ms）
        updateTime: 1684512000000,
        creator: "admin",
        id: 20,
        name: "零装线20",
        detection: "PCB板表面检测",
        alarmTask: "4",
        videoChannel: "20",
        reportingStatus: "已上报",
        alarmContent: "检测到表面缺陷"
      }
    ],
    total: 20, // 总条目数
    pageSize: 20, // 每页显示条目个数
    currentPage: 1 // 当前页数
  });

  function handleDelete(row) {
    message(`您删除了角色名称为${row.name}的这条数据`, { type: "success" });
    onSearch();
  }

  function handleSizeChange(val: number) {
    console.log(`${val} items per page`);
  }

  function handleCurrentChange(val: number) {
    console.log(`current page: ${val}`);
  }

  function handleSelectionChange(val) {
    console.log("handleSelectionChange", val);
  }

  function getWarningList() {
    // http.request("post", "/login")
    return warnList;
  }

  async function onSearch() {
    loading.value = true;
    const data = getWarningList();
    dataList.value = data.value.list;
    pagination.total = data.value.total;
    pagination.pageSize = data.value.pageSize;
    pagination.currentPage = data.value.currentPage;
    console.log(dataList.value);
    setTimeout(() => {
      loading.value = false;
    }, 500);
  }

  const resetForm = formEl => {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  };

  /** 菜单权限 */
  function handleMenu() {
    message("等菜单管理页面开发后完善");
  }

  /** 数据权限 可自行开发 */
  // function handleDatabase() {}

  onMounted(() => {
    onSearch();
  });

  return {
    form,
    loading,
    columns,
    dataList,
    pagination,
    onSearch,
    resetForm,
    handleMenu,
    handleDelete,
    // handleDatabase,
    handleSizeChange,
    handleCurrentChange,
    handleSelectionChange
  };
}
