<script setup>
import Table from "@/components/customUI/Table.vue";
import Input from "@/components/customUI/Input.vue";
import { getAccountRoleList, delRole } from "@/apis/account";
import RoleAuthModal from "./RoleAuthModal.vue";
import { ref, onMounted, inject } from "vue";

const { openToast, cancelToastOpen } = inject("app_toast");
const columns = [
  {
    title: "編號",
    key: "index",
  },
  {
    title: "名稱",
    key: "full_name",
  },
  {
    title: "建立時間",
    key: "created_at",
  },
  {
    title: "操作",
    key: "operation",
    width: 400,
  },
];

const dataSource = ref([]);
const loading = ref(false);

const getRole = async (searchData = {}) => {
  loading.value = true;
  const res = await getAccountRoleList(searchData);
  dataSource.value = res.data;
  loading.value = false;
};

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

const selectedRole = ref({
  full_name: "",
});
const disabled = ref(true);

const view = (record) => {
  selectedRole.value = { ...record, Name: record.full_name };
};

const edit = (record) => {
  disabled.value = false;
  view(record);
};
const add = () => {
  disabled.value = false;
  selectedRole.value = { Name: "" };
  role_auth_modal.showModal();
};

const remove = async (Id) => {
  openToast("warning", "是否確認刪除該項目?", "body", async () => {
    await cancelToastOpen();
    const res = await delRole(Id);
    if (res.isSuccess) {
      dataSource.value = dataSource.value.filter(
        ({ role_guid }) => role_guid !== Id
      );
      openToast("success", "刪除成功");
    } else {
      openToast("error", res.msg);
    }
  });
};

const cancelModal = () => {
  selectedRole.value = null;
  disabled.value = true;
  role_auth_modal.close();
};

const searchRole = ref({
  Full_name: "",
});

const onSearch = () => {
  getRole(searchRole.value);
};
</script>

<template>
  <RoleAuthModal
    :selectedRole="selectedRole"
    :cancelModal="cancelModal"
    :disabled="disabled"
    :update="getRole"
  />
  <Table
    :columns="columns"
    :dataSource="dataSource"
    rowKey="role_guid"
    :loading="loading"
  >
    <template #beforeTable>
      <div class="flex items-center mb-8">
        <Input
          placeholder="請輸入角色名稱"
          name="Full_name"
          :value="searchRole"
        />
        <button
          class="btn btn-outline-info mx-3"
          @click.stop.prevent="onSearch"
        >
          搜尋
        </button>
        <button class="btn btn-success ml-10" @click.stop.prevent="add">
          <font-awesome-icon :icon="['fas', 'plus']" />新增
        </button>
      </div>
    </template>
    <template #bodyCell="{ record, column, index }">
      <template v-if="column.key === 'index'">{{ index + 1 }}</template>
      <template v-else-if="column.key === 'operation'">
        <button
          class="btn btn-sm btn-info text-white mr-2"
          @click.stop.prevent="() => view(record)"
        >
          查看
        </button>
        <button
          class="btn btn-sm btn-success text-white mr-2"
          @click.stop.prevent="() => edit(record)"
        >
          修改
        </button>
        <button
          class="btn btn-sm btn-error text-white"
          @click.stop.prevent="() => remove(record.role_guid)"
        >
          刪除
        </button>
      </template>
      <template v-else>
        {{ record[column.key] }}
      </template>
    </template>
  </Table>
</template>

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