<script setup>
import { getGraphData, delGraphData } from "@/apis/graph";
import { inject, ref, watch } from "vue";
import useSearchParams from "@/hooks/useSearchParam";
import GraphModal from "./GraphModal.vue";
import { downloadExcelByHref } from "@/util/downloadExcel";

const FILE_BASEURL = import.meta.env.VITE_FILE_API_BASEURL;
const { openToast, cancelToastOpen } = inject("app_toast");
const columns = [
  {
    title: "編號",
    key: "index",
    width: 100,
  },
  {
    title: "名稱",
    key: "name",
  },
  {
    title: "檔案",
    key: "oriOrgName",
  },
  {
    title: "功能",
    key: "operation",
    width: 350,
  },
];

const dataSource = ref([]);
const { searchParams } = useSearchParams();

const getData = async (id) => {
  const res = await getGraphData(id);
  if (res.isSuccess) {
    dataSource.value = res.data.map((d) => ({ ...d, key: d.id }));
  }
};

const { current_dir } = inject("current_dir");

const editRecord = ref(null);
const updateEditRecord = (data) => {
  editRecord.value = data;
};

const delRecord = async (id) => {
  openToast("warning", "是否確認刪除該項目?", "body", async () => {
    await cancelToastOpen();
    const res = await delGraphData(id);
    if (res.isSuccess) {
      getData(parseInt(searchParams.value.id));
      openToast("success", "刪除成功");
    } else {
      openToast("error", res.msg);
    }
  });
};

const openModal = () => {
  graph_add_item.showModal();
};

watch(
  () => searchParams,
  (newVal) => {
    if (newVal.value.id) {
      updateEditRecord(null);
      getData(parseInt(newVal.value.id));
    }
  },
  {
    immediate: true,
    deep: true,
  }
);

const downloadFile = async (record) => {
  console.log(record);
  downloadExcelByHref(
    `${FILE_BASEURL}/upload/graph_manage/${record.oriSavName}`,
    record.oriOrgName
  );
};
</script>

<template>
  <div>
    <div class="flex items-center mb-3">
      <GraphModal
        :editRecord="editRecord"
        :updateEditRecord="updateEditRecord"
        :getTableData="getData"
        :openModal="openModal"
      />
      <h1 class="text-xl">{{ current_dir?.remark }}</h1>
    </div>
    <Table :columns="columns" :dataSource="dataSource">
      <template #bodyCell="{ record, column, index }">
        <template v-if="column.key === 'index'">{{ index + 1 }}</template>
        <template v-else-if="column.key === 'oriOrgName'">
          <a
            :href="`${FILE_BASEURL}/upload/graph_manage/${record.oriSavName}`"
            target="_blank"
            class="btn-link no-underline hover:no-underline :focus:outline-0"
          >
            {{ record.oriOrgName }}
          </a>
        </template>
        <template v-else-if="column.key === 'operation'">
          <a v-if="record.url" :href="record.url" target="_blank" class="btn btn-success btn-sm me-2 ">
            網址
          </a>
          <button
            v-if="record.oriOrgName"
            class="btn btn-sm btn-success text-white mr-2"
            @click.stop.prevent="() => downloadFile(record)"
          >
            下載
          </button>
          <button
            class="btn btn-sm btn-info text-white mr-2"
            @click.stop.prevent="
              () => {
                updateEditRecord(record);
                openModal();
              }
            "
          >
            修改
          </button>
          <button
            class="btn btn-sm btn-error text-white"
            @click.stop.prevent="
              () => {
                delRecord(record.id);
              }
            "
          >
            刪除
          </button>
        </template>
        <template v-else>
          {{ record[column.key] }}
          <!-- <button class="text-secondary no-underline border-0">
          </button> -->
        </template>
      </template>
    </Table>
  </div>
</template>

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