PSTW_CentralizeSystem/Views/Admin/CreateModule.cshtml
2024-11-27 10:34:32 +08:00

180 lines
8.3 KiB
Plaintext

@{
ViewData["Title"] = "Create Module";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="row" id="app">
<div class="col-md-12 col-lg-12">
<div class="card">
<div class="card-body">
<div class="col-md-12 col-lg-12">
<form v-on:submit.prevent="">
<div class="card">
<div class="card-body">
<h5 class="card-title">Settings</h5>
<div class="form-group row">
<label class="col-md-3 mt-3">Module Name</label>
<div class="col-md-6">
<select class="form-select shadow-none mt-3" name="ModuleName" v-model="moduleData.ModuleName" style="height: 36px; width: 100%">
<optgroup label="Module">
<option v-for="(prefix, index) in classList" :key="index" :value="prefix">
{{ prefix }}
</option>
</optgroup>
</select>
</div>
</div>
<div class="form-group row">
<label class="col-md-3 mt-3">Module Access</label>
<div class="col-md-6">
<select class="form-select shadow-none mt-3" name="AllowedUserType" v-model="moduleData.AllowedUserType" style="height: 36px; width: 100%">
<optgroup label="General">
<option value="Public">Public</option>
<option value="Registered User">Registered User</option>
</optgroup>
<optgroup label="Role">
<option v-for="(roleType, index) in roleData" :key="index" :value="roleType.name">{{ roleType.name }}</option>
</optgroup>
</select>
</div>
</div>
<div class="form-group row">
<label class="col-md-3 mt-3">Module Status</label>
<div class="col-md-6">
<select class="form-select shadow-none mt-3" name="ModuleStatus" v-model="moduleData.ModuleStatus" style="height: 36px; width: 100%">
<optgroup label="General">
<option value="1">Enabled</option>
<option value="0">Disabled</option>
</optgroup>
</select>
</div>
</div>
</div>
<div class="border-top">
<div class="card-body">
<button type="button" class="btn btn-primary" v-on:click="saveData">
Add
</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@section Scripts {
@{
await Html.RenderPartialAsync("_ValidationScriptsPartial");
}
<script>
</script>
<script>
const app = Vue.createApp({
data() {
return {
moduleData:{
ModuleName : null,
AllowedUserType : null,
MethodAllowedUserType: [],
ModuleStatus : null,
Description : null,
},
classList: null,
roleData: null,
moduleList: null
};
},
mounted(){
this.classMethod();
this.fetchRoleList();
this.fetchModuleList();
},
methods: {
fetchRoleList() {
fetch('/RoleAPI/GetRoleList', {
method: 'POST'
})
.then(response => response.json())
.then(data => {
console.log(data);
if (data != null) {
this.roleData = data;
this.$nextTick(() => {
$(".select2").select2().on("change", (event) => {
const index = $(event.target).closest('.tab-pane').index();
this.moduleData.methodAllowedUserType[index].allowedUserTypesArray = $(event.target).val();
});
});
}
})
.catch(error => {
console.error('There was a problem with the fetch operation:', error);
});
},
saveData() {
if (!this.moduleData.ModuleName || !this.moduleData.ModuleStatus || !this.moduleData.AllowedUserType) {
alert("Please select a valid module name or status or module access.");
return;
}
fetch('/ModuleAPI/AddModule', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(this.moduleData)
})
.then(response => {
if (!response.ok) {
throw new Error('Name module already exists');
}
location.reload();
alert('Module information saved successfully');
window.location.href = '/Admin/ModuleAdmin';
})
.catch(error => {
console.error('There was a problem with the update operation:', error);
alert('Failed to save data: ' + error.message);
});
},
classMethod(){
fetch('/AdminAPI/GetListClassAndMethodInformation', {
method: 'POST'
})
.then(response => response.json())
.then(data => {
if (data != null) {
this.classList = data.map(data => data.controller.replace("Controller", ""));
}
})
.catch(error => {
console.error('There was a problem with the fetch operation:', error);
});
},
fetchModuleList() {
fetch('/ModuleAPI/GetModuleInformation', {
method: 'POST'
})
.then(response => response.json())
.then(data => {
console.log(data);
if (data != null) {
this.moduleList = data;
}
})
.catch(error => {
console.error('There was a problem with the fetch operation:', error);
});
}
}
})
$(function () {
app.mount('#app');
});
</script>
}