CviLux_fe/src/views/accountManagement/components/RoleAuthModal.vue
2024-10-11 17:10:47 +08:00

208 lines
5.4 KiB
Vue

<script setup>
import Modal from "@/components/customUI/Modal.vue";
import Table from "@/components/customUI/Table.vue";
import Input from "@/components/customUI/Input.vue";
import Checkbox from "@/components/customUI/Checkbox.vue";
import {
getAccountRoleAuthPageList,
getAccountRoleAuthList,
postAccountRole,
} from "@/apis/account";
import { defineProps, onMounted, ref, watch, computed } from "vue";
import useActiveBtn from "@/hooks/useActiveBtn";
import { useI18n } from "vue-i18n";
const { t } = useI18n();
const props = defineProps({
selectedRole: String,
cancelModal: Function,
disabled: Boolean,
update: Function,
});
const { items, changeActiveBtn, setItems, selectedBtn } = useActiveBtn();
const columns = computed(() => [
{
title: t("accountManagement.choose"),
key: "SaveCheckAuth",
},
{
title: t("accountManagement.index"),
key: "index",
},
{
title: t("accountManagement.index"),
key: "subName",
},
]);
const rawData = ref([]);
const dataSource = ref([]);
const getAllAuth = async () => {
const res = await getAccountRoleAuthPageList();
rawData.value = res.data;
dataSource.value = res.data;
};
watch([rawData, selectedBtn], ([newData, newSelectedBtn]) => {
dataSource.value = newData.filter(({ authCode }) =>
authCode.startsWith(newSelectedBtn.authCode)
);
});
// 角色所有權限
const loading = ref(false);
const SaveCheckAuth = ref([]);
watch(SaveCheckAuth, (newValue, oldValue) => {
if (newValue.includes("PF10") && !oldValue.includes("PF10")) {
setItems([
{
title: t("accountManagement.basic_permissions"),
key: "PF",
active: selectedBtn.value.authCode === "PF",
authCode: "PF",
},
{
title: t("accountManagement.production_permissions"),
key: "PS",
active: selectedBtn.value.authCode === "PS",
authCode: "PS",
},
]);
} else if (!newValue.includes("PF10") && oldValue.includes("PF10")) {
console.log(newValue.filter((authCode) => authCode.startsWith("PS")));
SaveCheckAuth.value = newValue.filter(
(authCode) => !authCode.startsWith("PS")
);
setItems([
{
title: t("accountManagement.basic_permissions"),
key: "PF",
active: true,
authCode: "PF",
},
]);
}
});
const getRoleAuth = async (id) => {
const res = await getAccountRoleAuthList(id);
SaveCheckAuth.value = res.data
// .filter(({ authCode }) => authCode.startsWith("PF"))
.map(({ authCode }) => authCode);
role_auth_modal.showModal();
};
const onChange = (value, checked) => {
if (checked) {
SaveCheckAuth.value = [...SaveCheckAuth.value, value];
} else {
SaveCheckAuth.value = SaveCheckAuth.value.filter((v) => v !== value);
}
};
const form = ref(null);
const onCancel = () => {
console.log("onCancel", items.value[0]);
changeActiveBtn(items.value[0]);
props.cancelModal();
};
const onOk = async () => {
let postData = { SaveCheckAuth: SaveCheckAuth.value };
const res = await postAccountRole({
Id: props.selectedRole.role_guid || "0",
...props.selectedRole,
...postData,
});
props.update();
onCancel();
};
watch(
() => props.selectedRole,
(newValue) => {
Boolean(newValue?.role_guid) && getRoleAuth(newValue.role_guid);
}
);
onMounted(() => {
setItems([
{
title: t("accountManagement.basic_permissions"),
key: "PF",
active: true,
authCode: "PF",
},
]);
getAllAuth();
});
</script>
<template>
<Modal
id="role_auth_modal"
:title="t('accountManagement.role_permissions_setting')"
:onCancel="onCancel"
>
<template #modalContent>
<form class="modal_form" ref="form">
<Input
v-if="!disabled"
:label="t('accountManagement.role_name')"
:placeholder="t('accountManagement.role_placeholder')"
class="mt-5"
name="Name"
:value="selectedRole"
/>
<p v-else class="text-xl mt-5">{{ selectedRole?.Name }}</p>
<ButtonGroup
:items="items"
:withLine="false"
class="my-4"
:onclick="(e, item) => changeActiveBtn(item)"
/>
<Table :columns="columns" :dataSource="dataSource" :withStyle="false">
<template #bodyCell="{ record, column, index }">
<template v-if="column.key === 'index'">
{{ index + 1 }}
</template>
<template v-else-if="column.key === 'SaveCheckAuth'">
<Checkbox
:checked="SaveCheckAuth.includes(record.authCode)"
:disabled="disabled"
name="SaveCheckAuth"
:value="record.authCode"
:onChange="onChange"
/>
<!-- <label class="cursor-pointer label justify-center">
<input
type="checkbox"
:disabled="disabled"
:checked="SaveCheckAuth.includes(record.authCode)"
class="checkbox checkbox-success"
/>
</label> -->
</template>
</template>
</Table>
</form>
</template>
<template #modalAction>
<button class="btn btn-outline-success mr-2" @click="onCancel">
{{ $t("button.cancel") }}
</button>
<button v-if="!disabled" class="btn btn-outline-success" @click="onOk">
{{ $t("button.submit") }}
</button>
</template>
</Modal>
</template>
<style lang="scss" scoped></style>