355 lines
18 KiB
PHP
355 lines
18 KiB
PHP
<div class="row">
|
|
<div class="col-lg-12">
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<h4 class="header-title"><?= $page_name; ?></h4>
|
|
<!-- <?php if (session()->getFlashdata('success')) : ?>
|
|
<div class="alert alert-success"><?= session()->getFlashdata('success') ?></div>
|
|
<?php endif; ?> -->
|
|
<form class="parsley-examples" action="<?= base_url() . "insert_users"; ?>" method="post" enctype="multipart/form-data" id="myForm">
|
|
<div class="form-group">
|
|
<div class="form-row">
|
|
<div class="form-group col-md-4">
|
|
<label for="first_name" class="col-form-label">Name<span class="text-danger">*</span></label>
|
|
<input data-parsley-type="alphanum" type="text" class="form-control" id="first_name" name="first_name" placeholder="First Name" value="<?= isset($details['first_name']) ? $details['first_name'] : '' ?>" required />
|
|
</div>
|
|
<div class="form-group col-md-4">
|
|
<label for="mobile_no" class="col-form-label">Mobile Number<span class="text-danger">*</span></label>
|
|
<input type="text" class="form-control" id="mobile_no" name="mobile_no" placeholder="Mobile Number (Enter only numbers)" data-toggle="input-mask" data-mask-format="0000000000" value="<?= isset($details['mobile_no']) ? $details['mobile_no'] : '' ?>" autofocus required>
|
|
</div>
|
|
<div class="form-group col-md-4">
|
|
<label for="role" class="col-form-label">Role<span class="text-danger">*</span></label>
|
|
<select id="role" name="role" class="form-control" required>
|
|
<option value="">Select Role</option>
|
|
<option value="admin" <?= isset($details['role']) && $details['role'] === 'admin' ? 'selected' : '' ?>>Admin</option>
|
|
<option value="manager" <?= isset($details['role']) && $details['role'] === 'manager' ? 'selected' : '' ?>>Manager</option>
|
|
<option value="accounts" <?= isset($details['role']) && $details['role'] === 'accounts' ? 'selected' : '' ?>>Accounts Staff</option>
|
|
<option value="auditor" <?= isset($details['role']) && $details['role'] === 'auditor' ? 'selected' : '' ?>>Auditor</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
<div class="form-row">
|
|
<?php if ($loggedin_person_role === 'sadmin') { ?>
|
|
<div class="form-group col-md-3">
|
|
<label for="business_id" class="col-form-label">Organization<span class="text-danger">*</span></label>
|
|
<select id="business_id" name="business_id" class="form-control" required>
|
|
<?php foreach ($business_details as $business) { ?>
|
|
<option value="<?= $business['business_id']; ?>" <?= isset($details['business_id']) && ($details['business_id'] == $business['business_id']) ? 'selected' : '' ?>>
|
|
<?= $business['title']; ?>
|
|
</option>
|
|
<?php } ?>
|
|
</select>
|
|
</div>
|
|
<div class="form-group col-md-3">
|
|
<label for="F" class="col-form-label">Organization Branches<span class="text-danger">*</span></label>
|
|
<select id="branch_id" name="branch_id" class="form-control" required>
|
|
<?php if (!empty($branches)) { ?>
|
|
<?php foreach ($branches as $branch) { ?>
|
|
<option value="<?= $branch['id']; ?>" <?= isset($details['branch_id']) && ($details['branch_id'] == $branch['id']) ? 'selected' : '' ?>>
|
|
<?= $branch['branch_name']; ?>
|
|
</option>
|
|
<?php } ?>
|
|
<?php } else { ?>
|
|
<option value="">No branches available</option>
|
|
<?php } ?>
|
|
</select>
|
|
</div>
|
|
<?php } ?>
|
|
<div class="form-group col-md-3">
|
|
<label for="email" class="col-form-label">Email<span class="text-danger">*</span></label>
|
|
<input type="email" class="form-control" id="email" name="email" placeholder="Email" value="<?= isset($details['email']) ? $details['email'] : '' ?>" required />
|
|
</div>
|
|
<?php if (empty($details)) { ?>
|
|
<div class="form-group col-md-3">
|
|
<label for="password" class="col-form-label">Password<span class="text-danger">*</span></label>
|
|
<input type="text" class="form-control" id="password" name="password" placeholder="Password" value="<?= isset($details['password']) ? $details['password'] : '' ?>" required />
|
|
</div>
|
|
<?php } ?>
|
|
</div>
|
|
<div class="form-row">
|
|
<div class="form-group col-md-4">
|
|
<label for="address" class="col-form-label">Address<span class="text-danger"></span></label>
|
|
<input type="text" class="form-control" id="address" name="address" placeholder="Address (eg : 1234 Main St)" value="<?= isset($details['address']) ? $details['address'] : '' ?>" />
|
|
</div>
|
|
<div class="form-group col-md-4">
|
|
<label for="city" class="col-form-label">City<span class="text-danger"></span></label>
|
|
<input type="text" class="form-control" id="city" name="city" placeholder="City" value="<?= isset($details['city']) ? $details['city'] : '' ?>" />
|
|
</div>
|
|
<div class="form-group col-md-4">
|
|
<label for="state" class="col-form-label">State<span class="text-danger"></span></label>
|
|
<input type="text" class="form-control" id="state" name="state" placeholder="State" value="<?= isset($details['state']) ? $details['state'] : '' ?>" />
|
|
</div>
|
|
</div>
|
|
<div class="form-row">
|
|
<div class="form-group col-md-4">
|
|
<label for="postal_code" class="col-form-label">Postal Code<span class="text-danger"></span></label>
|
|
<input data-parsley-type="number" type="text" class="form-control" id="postal_code" name="postal_code" placeholder="Postal Code (PIN)" value="<?= isset($details['postal_code']) ? $details['postal_code'] : '' ?>" pattern="[0-9]{6}" maxlength="6" />
|
|
</div>
|
|
<div class="form-group col-md-4">
|
|
<label for="profile_picture" class="col-form-label">Profile Picture</label>
|
|
<input type="file" name="profile_picture" value="<?= isset($details['profile_picture']) ? $details['profile_picture'] : '' ?>" accept="image/*" onchange="validateFile(this)" />
|
|
<div id="error_message" style="color: red;"></div>
|
|
</div>
|
|
<?php if ($loggedin_person_role !== 'sadmin') : ?>
|
|
<div class="form-group col-md-4">
|
|
<label for="branch" class="col-form-label">Branch<span class="text-danger"></span></label>
|
|
<select id="branch" name="branch" class="form-control">
|
|
<option value="">Select Branch</option>
|
|
<?php
|
|
// Check if branches are available
|
|
if (!empty($branches)) {
|
|
foreach ($branches as $branch) {
|
|
?>
|
|
<option data-business-id="<?= $branch['business_id']; ?>" value="<?= $branch['id']; ?>" <?= isset($details['branch_id']) && ($details['branch_id'] == $branch['id']) ? 'selected' : '' ?>>
|
|
<?= $branch['branch_name']; ?>
|
|
</option>
|
|
<?php
|
|
}
|
|
}
|
|
?>
|
|
</select>
|
|
</div>
|
|
<?php endif; ?>
|
|
<?php if (!empty($details['profile_picture'])) { ?>
|
|
<div class="form-group col-md-4 mt-3">
|
|
<div class="media">
|
|
<img src="<?= base_url('public/uploads/' . $details['profile_picture']) ?>" alt="Business Logo" height="100" class="preview-image d-flex align-self-start rounded mr-2" style="position: relative; left: 40%;">
|
|
<div class="media-body">
|
|
<!-- <h5 class="mt-0">Existing Profile Picture</h5> -->
|
|
<!-- <p class="mb-1"><?= $details['profile_picture']; ?></p> -->
|
|
<input type="hidden" id="existing_profile_picture_name" name="existing_profile_picture_name" placeholder="hidden Field for profile picture name" readonly value="<?= $details['profile_picture']; ?>" />
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php } ?>
|
|
</div>
|
|
<input type="hidden" id="user_id" name="user_id" placeholder="hidden for user id" value="<?= isset($details['user_id']) ? $details['user_id'] : '' ?>" />
|
|
<?php if (!empty($details) && $loggedin_person_role !== 'manager') { ?>
|
|
<div class="form-group text-right m-b-0 checkbox checkbox-purple">
|
|
<input type="checkbox" id="isactive" name="isactive" class="form-control" <?= isset($details) && $details['isactive'] == 1 ? 'checked' : '' ?>>
|
|
<label for="isactive"> Is Active</label>
|
|
</div>
|
|
<br>
|
|
<?php } ?>
|
|
<div class="form-group text-right m-b-0">
|
|
<button class="btn btn-success waves-effect waves-light mr-1" type="submit" id="submitBtn">
|
|
Submit
|
|
</button>
|
|
<button type="reset" class="btn btn-primary waves-effect mr-1">
|
|
Reset
|
|
</button>
|
|
<?php if ($loggedin_person_role !== 'manager') : ?>
|
|
<a href="<?= base_url() . "user_list"; ?>" class="btn btn-secondary waves-effect">Cancel</a>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
<!-- end card-body-->
|
|
</div>
|
|
<!-- end card-->
|
|
</div>
|
|
<!-- end col-->
|
|
</div>
|
|
<!-- end row -->
|
|
<script>
|
|
$(function() {
|
|
role = "<?php echo get_user_role() ?>";
|
|
console.log(role);
|
|
if(role == "auditor")
|
|
{
|
|
// for input
|
|
var formElements = document.querySelectorAll('#myForm input, #myForm textarea');
|
|
formElements.forEach(function(element) {
|
|
element.setAttribute('readonly', true);
|
|
});
|
|
// for select
|
|
var formElements = document.querySelectorAll('#myForm select, #myForm button');
|
|
formElements.forEach(function(element) {
|
|
element.setAttribute('disabled', true);
|
|
});
|
|
}
|
|
});
|
|
</script>
|
|
<script>
|
|
// Get the current date in "yyyy-mm-dd" format
|
|
function getCurrentDate() {
|
|
const today = new Date();
|
|
const year = today.getFullYear();
|
|
let month = today.getMonth() + 1;
|
|
let day = today.getDate();
|
|
|
|
// Add leading zeros if month or day is less than 10
|
|
if (month < 10) {
|
|
month = '0' + month;
|
|
}
|
|
|
|
if (day < 10) {
|
|
day = '0' + day;
|
|
}
|
|
|
|
return `${year}-${month}-${day}`;
|
|
}
|
|
|
|
// Set the max attribute of the date input field to the current date
|
|
document.getElementById('date_of_birth').setAttribute('max', getCurrentDate());
|
|
|
|
// Set the min attribute of the date input field to "1965-01-01"
|
|
document.getElementById('date_of_birth').setAttribute('min', '1965-01-01');
|
|
|
|
function validateFile(input) {
|
|
const file = input.files[0];
|
|
const errorMessage = document.getElementById("error_message");
|
|
|
|
if (file) {
|
|
if (file.type.startsWith("image/")) {
|
|
// It's an image file, no error
|
|
errorMessage.textContent = "";
|
|
} else {
|
|
// Invalid file type
|
|
errorMessage.textContent = "Invalid file type. Please select an image.";
|
|
input.value = ""; // Clear the input field
|
|
}
|
|
}
|
|
}
|
|
</script>
|
|
<script>
|
|
document.getElementById('myForm').addEventListener('submit', function(event) {
|
|
var form = event.target;
|
|
if (form.checkValidity() === false) {
|
|
form.reportValidity(); // Display validation error messages
|
|
event.preventDefault(); // Prevent form submission if it's not valid
|
|
$('#submitBtn').prop('disabled', false);
|
|
} else {
|
|
$('#submitBtn').prop('disabled', true);
|
|
}
|
|
});
|
|
</script>
|
|
<script>
|
|
$(document).ready(function () {
|
|
// Function to fetch branches based on selected organization
|
|
function fetchBranches(selectedBusinessId) {
|
|
var businessBranchDropdown = $('#branch_id');
|
|
|
|
// Clear existing options
|
|
businessBranchDropdown.empty().append('<option value="">Choose Organization Branch</option>');
|
|
|
|
// Fetch branches via AJAX
|
|
$.ajax({
|
|
url: '<?= base_url('users/get_branches') ?>/' + selectedBusinessId,
|
|
method: 'GET',
|
|
dataType: 'json',
|
|
success: function (response) {
|
|
console.log('Response from server:', response); // Log the response for debugging
|
|
|
|
if (response && response.length > 0) {
|
|
console.log('Number of branches:', response.length); // Log the number of branches
|
|
|
|
$.each(response, function (index, branch) {
|
|
console.log('Appending option:', branch); // Log each branch before appending
|
|
|
|
var option = $('<option></option>').attr('value', branch.id).text(branch.branch_name);
|
|
businessBranchDropdown.append(option);
|
|
});
|
|
} else {
|
|
console.log('No branches available.'); // Log if no branches are available
|
|
businessBranchDropdown.append('<option value="">No branches available</option>');
|
|
}
|
|
},
|
|
error: function (error) {
|
|
console.error('Error fetching branches:', error);
|
|
}
|
|
});
|
|
}
|
|
|
|
// Change event handler for the organization dropdown
|
|
$('#business_id').change(function () {
|
|
var selectedBusinessId = $(this).val();
|
|
console.log('Selected Business ID:', selectedBusinessId); // Log the selected business ID
|
|
|
|
// Call the function to fetch branches
|
|
if (selectedBusinessId) {
|
|
fetchBranches(selectedBusinessId);
|
|
}
|
|
});
|
|
|
|
// Trigger the change event if the organization is pre-selected (e.g., during edit)
|
|
var preSelectedBusinessId = $('#business_id').val();
|
|
if (preSelectedBusinessId) {
|
|
fetchBranches(preSelectedBusinessId);
|
|
}
|
|
});
|
|
|
|
</script>
|
|
<script>
|
|
// JavaScript to show/hide branches based on selected business
|
|
$(document).ready(function () {
|
|
var branchDropdown = $('#branch');
|
|
var branches = <?= json_encode($branches); ?>; // Assuming $branches is available
|
|
|
|
// Initial branches
|
|
var initialBranches = branches.filter(branch => branch.business_id == '<?= $session_bid ?? '' ?>');
|
|
|
|
// Populate the dropdown with initial branches
|
|
populateBranchDropdown(initialBranches);
|
|
|
|
// Change event handler for the business dropdown
|
|
$('#business_id').change(function () {
|
|
var selectedBusinessId = $(this).val();
|
|
|
|
// Filter branches based on selected business
|
|
var filteredBranches = branches.filter(branch => branch.business_id == selectedBusinessId);
|
|
|
|
// Populate the dropdown with filtered branches
|
|
populateBranchDropdown(filteredBranches);
|
|
});
|
|
|
|
// Function to populate the branch dropdown
|
|
function populateBranchDropdown(branchesToDisplay) {
|
|
branchDropdown.empty().append('<option value="">Select Branch</option>');
|
|
|
|
if (branchesToDisplay.length > 0) {
|
|
branchesToDisplay.forEach(branch => {
|
|
var option = $('<option></option>').attr('value', branch.id).text(branch.branch_name);
|
|
branchDropdown.append(option);
|
|
});
|
|
}
|
|
}
|
|
});
|
|
</script>
|
|
<!-- <script>
|
|
// JavaScript to show/hide branches based on selected business
|
|
$(document).ready(function () {
|
|
var branchDropdown = $('#branch');
|
|
var branches = <?= json_encode($branches); ?>; // Assuming $branches is available
|
|
|
|
// Initial branches
|
|
var initialBranches = branches.filter(branch => branch.business_id == '<?= $details['business_id'] ?? '' ?>');
|
|
|
|
// Populate the dropdown with initial branches
|
|
populateBranchDropdown(initialBranches);
|
|
|
|
// Change event handler for the business dropdown
|
|
$('#business_id').change(function () {
|
|
var selectedBusinessId = $(this).val();
|
|
|
|
// Filter branches based on selected business
|
|
var filteredBranches = branches.filter(branch => branch.business_id == selectedBusinessId);
|
|
|
|
// Populate the dropdown with filtered branches
|
|
populateBranchDropdown(filteredBranches);
|
|
});
|
|
|
|
// Function to populate the branch dropdown
|
|
function populateBranchDropdown(branchesToDisplay) {
|
|
branchDropdown.empty().append('<option value="">Select Branch</option>');
|
|
|
|
if (branchesToDisplay.length > 0) {
|
|
branchesToDisplay.forEach(branch => {
|
|
var option = $('<option></option>').attr('value', branch.id).text(branch.branch_name);
|
|
branchDropdown.append(option);
|
|
});
|
|
}
|
|
}
|
|
});
|
|
</script> -
|