<script setup>
import { getGraphData, delGraphData } from "@/apis/graph";
import { inject, ref, watch, computed } from "vue";
import useSearchParams from "@/hooks/useSearchParam";
import GraphModal from "./GraphModal.vue";
import { downloadExcelByHref } from "@/util/downloadExcel";
import { useI18n } from "vue-i18n";
const { t } = useI18n();
const FILE_BASEURL = import.meta.env.VITE_FILE_API_BASEURL;

const columns = computed(() => [
  {
    title: t("graphManagement.index"),
    key: "index",
    width: 100,
  },
  {
    title: t("graphManagement.oriOrgName"),
    key: "oriOrgName",
    width: "60%",
  },
  {
    title: t("graphManagement.operation"),
    key: "operation",
  },
]);

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) => {
  const res = await delGraphData(id);
  getData(parseInt(searchParams.value.id));
};

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"
      />
      <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'">
          <button
            class="btn btn-sm btn-success text-white mr-2"
            @click.stop.prevent="() => downloadFile(record)"
          >
            {{ $t("button.download") }}
          </button>
          <button
            class="btn btn-sm btn-info text-white mr-2"
            @click.stop.prevent="
              () => {
                updateEditRecord(record);
              }
            "
          >
            {{ $t("button.edit") }}
          </button>
          <button
            class="btn btn-sm btn-error text-white"
            @click.stop.prevent="
              () => {
                delRecord(record.id);
              }
            "
          >
            {{ $t("button.delete") }}
          </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>
