<script setup>
import AlertSearch from "./AlertSearch.vue";
import AlertTable from "./AlertTable.vue";
import AlertTableModal from "./AlertTableModal.vue";
import { ref, provide, onMounted } from "vue";
import useSearchParam from "@/hooks/useSearchParam";
import { getAlertLog } from "@/apis/alert";
import {
  getOperationDeviceList,
  getOperationCompanyList,
  getOperationEditRecord,
} from "@/apis/operation";
import { getAccountUserList } from "@/apis/account";
import useBuildingStore from "@/stores/useBuildingStore";

const { searchParams } = useSearchParam();
const store = useBuildingStore();

const tableLoading = ref(false);
const dataSource = ref([]);
const model_data = ref({
  model_userList: [],
  model_companyList: [],
});

const editRecord = ref(null);

const updateEditRecord = (data) => {
  if (data?.lorf) {
    editRecord.value = {
      ...data,
      lorf: data?.lorf?.map((file) => ({
        ...file,
        src: `${file.save_file_name}.${file.ori_file_name.split(".")[1]}`,
        name: file.ori_file_name,
        ext: file.ori_file_name.split(".")[1],
      })),
    };
  } else {
    editRecord.value = {
      ...data,
      lorf: [],
    };
  }
};

const getModalUserList = async () => {
  const res = await getAccountUserList({});
  return res.data.map((d) => ({ ...d, key: d.userinfo_guid }));
};

const getModalCompanyList = async () => {
  const res = await getOperationCompanyList();
  return res.data.map((d) => ({ ...d, key: d.id }));
};

const getAllOptions = async () => {
  Promise.all([getModalUserList(), getModalCompanyList()]).then(
    ([users, companies]) => {
      model_data.value.model_userList = users;
      model_data.value.model_companyList = companies;
    }
  );
};

const search = async () => {
  tableLoading.value = true;
  if (Object.keys(searchParams.value).length !== 0) {
    const res = await getAlertLog({
      ...searchParams.value,
      isRecovery: Number(searchParams.value.isRecovery),
    });
    dataSource.value = (res.data || []).map((d) => ({ ...d, key: d.id }));
    tableLoading.value = false;
  }
};

const openModal = async (record) => {
  try {
    if (record.formId) {
      const res = await getOperationEditRecord(record.formId);
      updateEditRecord({
        ...res.data,
        uuid: res.data.error_code,
        device_number: record.device_number
      });
    } else {
      updateEditRecord({
        ...record,
        uuid: record.id,
      });
    }
    alert_action_item.showModal();
  } catch (error) {
    console.error("Error opening modal:", error);
  }
};

onMounted(() => {
  getAllOptions();
  if (Object.keys(searchParams.value).length !== 0) {
    search();
  }
});

provide("alert_modal", { model_data, search, updateEditRecord });
provide("alert_table", {
  openModal,
  updateEditRecord,
  dataSource,
  search,
  tableLoading,
});
</script>

<template>
  <AlertTableModal :editRecord="editRecord" />
  <div>
    <AlertSearch />
    <AlertTable />
  </div>
</template>

<style lang="scss" scoped></style>
