Merge pull request 'raymart_branch' (#34) from raymart_branch into main

Reviewed-on: #34
This commit is contained in:
MarkHipe 2024-03-07 10:40:49 +08:00
commit 64c06a749f
2 changed files with 452 additions and 49 deletions

View File

@ -83,6 +83,8 @@ if (isset($customer_data[0]["address"]) && is_array($customer_data[0]["address"]
<link rel="stylesheet" href="assets/css/responsive.css" />
<!-- Background css -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha512-DTOQO9RWCH3ppGqcWaEA1BIZOC6xxalwEsw9c2QQeAIftl+Vegovlnee1c9QX4TctnWMn13TZye+giMm8e2LwA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" id="bg-switcher-css" href="assets/css/backgrounds/bg-4.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/select2@latest/dist/css/select2.min.css" crossorigin="anonymous" referrerpolicy="no-referrer" />
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
@ -105,6 +107,13 @@ if (isset($customer_data[0]["address"]) && is_array($customer_data[0]["address"]
});
}
</script>
<style>
#sidebar-category:hover{
color: #ffaa00;
}
</style>
</head>
<body onload="updateCartItemCount(); updateWishItemCount()" class="checkout_page">
@ -252,19 +261,29 @@ if (isset($customer_data[0]["address"]) && is_array($customer_data[0]["address"]
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#secondModal">Add New Address</button>
<!-- Display a list of addresses and let the user choose -->
<form id="addressForm">
<form id="addressForm" style="min-width: 500px;">
<label>Select Address:</label>
<?php foreach ($customer_data as $customer_index => $customer) { ?>
<?php foreach ($customer['address'] as $address_index => $address) { ?>
<div class="form-check">
<!-- added new id for filter delete -->
<div class="form-check" id="form-check-<?php echo $address_index ?>">
<input type="radio" class="form-check-input" name="selectedAddress" id="address_<?php echo $customer_index; ?>_<?php echo $address_index; ?>" value="<?php echo $customer_index; ?>_<?php echo $address_index; ?>">
<label class="form-check-label" for="address_<?php echo $customer_index; ?>_<?php echo $address_index; ?>">
<label class="form-check-label" style="max-width: 80%; word-wrap: break-word;" for="address_<?php echo $customer_index; ?>_<?php echo $address_index; ?>">
<?php echo $address['first_name']; ?> <?php echo $address['last_name']; ?><br>
<?php echo $address['address_1']; ?> <?php echo $address['address_2']; ?>
<?php echo $address['barangay']; ?>, <?php echo $address['city']; ?>,
<?php echo $address['province']; ?> <?php echo $address['country']; ?><br>
Phone: <?php echo $address['phone']; ?>
</label>
<!-- raymart added icon march 4 2024 -->
<?php
$address['id'] =$address_index;
$jsonAddress = json_encode($address)?>
<a onclick="deleteAddress('<?php echo $customer['_id']; ?>', <?php echo $address_index; ?>, true)"
class="fa-regular fa-trash-can" id="sidebar-category" style="float: right; font-size: 20px;"></a>
<a data-bs-toggle="modal" data-bs-target="#thirdModal" data-value=' <?php echo $jsonAddress; ?>'
class="fa-regular fa-pen-to-square" id="sidebar-category" style="float: right; font-size: 20px; padding-right: 50px;"></a>
<!-- raymart added icon march 4 2024 -->
</div>
<?php } ?>
<?php } ?>
@ -278,6 +297,50 @@ if (isset($customer_data[0]["address"]) && is_array($customer_data[0]["address"]
<script>
var customerData = <?php echo json_encode($customer_data); ?>;
// raymart/sir jun added delete function for customer id march 4 2024
function deleteAddress(customerId, addressIndex) {
fetch('https://<?php echo $_SESSION["data_endpoint"]; ?>/api/v1/customers/' + customerId)
.then(response => response.json())
.then(data => {
let existingAddresses = data.address || [];
if (addressIndex >= 0 && addressIndex < existingAddresses.length) {
existingAddresses.splice(addressIndex, 1);
fetch('https://<?php echo $_SESSION["data_endpoint"]; ?>/api/v1/customers/' + customerId, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
address: existingAddresses
}),
})
.then(response => {
if (response.ok) {
// filter the delete action
document.getElementById('form-check-' + addressIndex).remove();
// location.reload();
} else {
console.error('Failed to submit data');
alert('Failed to submit data');
}
})
.catch(error => {
console.error('Error:', error);
alert('Error submitting data');
});
} else {
console.log("Invalid address index.");
}
})
.catch(error => {
console.error('Error:', error);
alert('Error fetching customer data');
});
}
// raymart/sir jun added delete function for customer id march 4 2024
function useSelectedAddress() {
// Get the selected address radio button
var selectedAddress = document.querySelector('input[name="selectedAddress"]:checked');
@ -355,7 +418,89 @@ if (isset($customer_data[0]["address"]) && is_array($customer_data[0]["address"]
<input type="text" class="form-control" id="addressCountry">
</div>
<button type="button" class="btn btn-primary" id="submitBtn">Submit</button>
</form>
</div>
</div>
</div>
</div>
<!-- raymart added 3rd modal march to show the customer address 04 2024 -->
<div class="modal fade" id="thirdModal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-sm" role="document" style="max-width: 800px;">
<div class="modal-content">
<div class="modal-body" style="overflow-y: auto; max-height: 90vh;">
<form>
<div class="form-group">
<label for="addressFirstName" class="text-dark font-weight-medium pt-3 mb-2">First Name</label>
<input type="text" class="form-control" id="addressFirstName2" value="<?php echo $address['first_name']; ?>">
<!-- <label for="addressFirstName" class="text-dark font-weight-medium pt-3 mb-2">First Name</label>-->
<input type="hidden" class="form-control" id="addressId" >
</div>
<div class="form-group">
<label for="addressLastName" class="text-dark font-weight-medium pt-3 mb-2">Last Name</label>
<input type="text" class="form-control" id="addressLastName2" value="<?php echo $address['last_name']; ?>">
<!-- <label for="addressLastName" class="text-dark font-weight-medium pt-3 mb-2">Last Name</label>
<input type="text" class="form-control" id="addressLastName"> -->
</div>
<div class="form-group">
<label for="addressContact" class="text-dark font-weight-medium pt-3 mb-2">Contact Number</label>
<input type="text" class="form-control" id="addressContact2" value="<?php echo $address['phone']; ?>">
<!-- <label for="addressContact" class="text-dark font-weight-medium pt-3 mb-2">Contact Number</label>
<input type="number" class="form-control" id="addressContact"> -->
</div>
<div class="form-group">
<label for="addressBuilding" class="text-dark font-weight-medium pt-3 mb-2">Building,Number</label>
<input type="text" class="form-control" id="addressBuilding2" value="<?php echo $address['address_1']; ?>">
<!-- <label for="addressBuilding" class="text-dark font-weight-medium pt-3 mb-2"> Building,Number </label>
<input type="text" class="form-control" id="addressBuilding"> -->
</div>
<div class="form-group">
<label for="addressStreet" class="text-dark font-weight-medium pt-3 mb-2">Street </label>
<input type="text" class="form-control" id="addressStreet2" value="<?php echo $address['address_2']; ?>">
<!-- <label for="addressStreet" class="text-dark font-weight-medium pt-3 mb-2"> Street </label>
<input type="text" class="form-control" id="addressStreet"> -->
</div>
<div class="form-group">
<label for="provinceSelect2">Province</label>
<select class="form-select" id="provinceSelect2">
<option id="provinceSelect2Opt" value="<?php echo $address['province']; ?>" >Select Province</option>
</select>
<!-- <label for="provinceSelect">Province</label>
<select class="form-select" id="provinceSelect">
<option value="" disabled selected hidden>Select Province</option>
</select> -->
</div>
<div class="form-group">
<label for="citySelect2">Municipality/City</label>
<select class="form-select" id="citySelect2" required>
<option value="" id="citySelect2Opt" selected hidden>Select Municipality/City</option>
</select>
</div>
<div class="form-group">
<label for="barangaySelect2">Barangay</label>
<select class="form-select" id="barangaySelect2" required>
<option value="" id="barangaySelect2Opt" selected hidden>Select Barangay</option>
</select>
</div>
<div class="form-group">
<label for="addressCountry2" class="text-dark font-weight-medium pt-3 mb-2">Country</label>
<input type="text" class="form-control" id="addressCountry2">
</div>
<button type="button" class="btn btn-primary" id="submitBtn2">Submit</button>
<!-- raymart added 3rd modal march to show the customer address 04 2024 -->
</form>
</div>
@ -363,8 +508,163 @@ if (isset($customer_data[0]["address"]) && is_array($customer_data[0]["address"]
</div>
</div>
<script>
const customerId = '<?php echo $customerId ?>';
<script>
// raymart/sir mark added function to show the addresses of the customer in the third modal march 04 2024
$('#thirdModal').on('shown.bs.modal', function (event) {
var jsonString = $(event.relatedTarget).data('value');
var jsonObject = JSON.parse(jsonString);
console.log(jsonObject)
// document.getElementById("addressFirstName2").value('mmmm');
// $('#addressFirstName2').setAttribute('value','My default value');
document.querySelector('input[id="addressFirstName2"]').value = jsonObject.first_name;
document.querySelector('input[id="addressLastName2"]').value = jsonObject.last_name;
document.querySelector('input[id="addressContact2"]').value = jsonObject.phone;
document.querySelector('input[id="addressBuilding2"]').value = jsonObject.address_1;
document.querySelector('input[id="addressStreet2"]').value = jsonObject.address_2;
document.querySelector('option[id="provinceSelect2Opt"]').text = jsonObject.province;
document.querySelector('option[id="provinceSelect2Opt"]').value = jsonObject.province;
document.querySelector('option[id="citySelect2Opt"]').text = jsonObject.city;
document.querySelector('option[id="citySelect2Opt"]').value = jsonObject.city;
document.querySelector('option[id="barangaySelect2Opt"]').text = jsonObject.barangay;
document.querySelector('option[id="barangaySelect2Opt"]').value = jsonObject.barangay;
document.querySelector('input[id="addressCountry2"]').value = jsonObject.country;
document.querySelector('input[id="addressId"]').value = jsonObject.id;
console.log('id ' + jsonObject.id)
const provinceSelect = $('#provinceSelect2'); // Use jQuery to select the element
const citySelect = $('#citySelect2'); // Use jQuery to select the element
const barangaySelect = $('#barangaySelect2'); // Use jQuery to select the element
// const provinceSelect = $('#provinceSelect2'); // Use jQuery to select the element
// const citySelect = $('#citySelect2'); // Use jQuery to select the element
// const barangaySelect = $('#barangaySelect2'); // Use jQuery to select the element
// Initialize Select2 on the provinceSelect, citySelect, and barangaySelect elements
provinceSelect.select2({
dropdownParent: $('#thirdModal'),
containerCssClass: 'select2-zindex-high' // Optional, add a custom class for styling
});
citySelect.select2({
dropdownParent: $('#thirdModal'),
containerCssClass: 'select2-zindex-high' // Optional, add a custom class for styling
});
barangaySelect.select2({
dropdownParent: $('#thirdModal'),
containerCssClass: 'select2-zindex-high' // Optional, add a custom class for styling
});
// Fetch provinces data
fetch('https://psgc.gitlab.io/api/provinces')
.then(response => {
if (response.ok) {
return response.json();
} else {
console.error('Failed to fetch provinces');
throw new Error('Failed to fetch provinces');
}
})
.then(provincesData => {
// Iterate through the provinces data and add options
provincesData.forEach(province => {
const option = new Option(province.name, province.code);
provinceSelect.append(option);
});
// Add an extra option manually
const extraOption = new Option('Metro Manila', '130000000');
provinceSelect.append(extraOption);
// Add event listener to provinceSelect
provinceSelect.on('change', function() {
// Clear existing options in citySelect and barangaySelect
citySelect.html('<option value="" disabled selected hidden>Select City</option>');
barangaySelect.html('<option value="" disabled selected hidden>Select Barangay</option>');
// Fetch and update cities/municipalities based on the selected province
updateCities();
});
})
.catch(error => {
console.error('Error:', error);
});
// Function to update city/municipality options based on the selected province
function updateCities() {
const selectedProvinceCode = provinceSelect.val(); // Use val() to get the selected value with Select2
if (selectedProvinceCode) {
let citiesEndpoint;
if (selectedProvinceCode === '130000000') {
// Check if Metro Manila is selected
citiesEndpoint = 'https://psgc.gitlab.io/api/regions/130000000/cities-municipalities/';
} else {
citiesEndpoint = `https://psgc.gitlab.io/api/provinces/${selectedProvinceCode}/cities-municipalities/`;
}
fetch(citiesEndpoint)
.then(response => {
if (response.ok) {
return response.json();
} else {
console.error('Failed to fetch cities/municipalities');
throw new Error('Failed to fetch cities/municipalities');
}
})
.then(citiesData => {
// Iterate through the cities data and add options
citiesData.forEach(city => {
const option = new Option(city.name, city.code);
citySelect.append(option);
});
})
.catch(error => {
console.error('Error:', error);
});
}
}
// Add event listener to citySelect
citySelect.on('change', function() {
// Clear existing options in barangaySelect
barangaySelect.html('<option value="" disabled selected hidden>Select Barangay</option>');
// Fetch and update barangays based on the selected city/municipality
const selectedCityCode = citySelect.val();
if (selectedCityCode) {
fetch(`https://psgc.gitlab.io/api/cities-municipalities/${selectedCityCode}/barangays/`)
.then(response => {
if (response.ok) {
return response.json();
} else {
console.error('Failed to fetch barangays');
throw new Error('Failed to fetch barangays');
}
})
.then(barangaysData => {
// Iterate through the barangays data and add options
barangaysData.forEach(barangay => {
const option = new Option(barangay.name, barangay.code);
barangaySelect.append(option);
});
})
.catch(error => {
console.error('Error:', error);
});
}
});
// document.querySelector('input[id="addressFirstName2"]').value = jsonObject.last_name;
});
// raymart/sir mark added function to show the addresses of the customer in the third modal march 04 2024
const customerId = '<?php echo $customerId ?>';
document.addEventListener('DOMContentLoaded', function() {
// Get the select elements
const provinceSelect = $('#provinceSelect'); // Use jQuery to select the element
@ -552,20 +852,95 @@ if (isset($customer_data[0]["address"]) && is_array($customer_data[0]["address"]
});
});
// raymart/sir mark added submitbtn2 for edited customerId march 04 2024
$('#submitBtn2').on('click', function() {
// function updateAddress(){
// Retrieve existing addresses from the API
// fetch('https://api.obanana.shop/api/v1/customers/65482e8d209ff8d348bd30fd')
fetch('https://<?php echo $_SESSION["data_endpoint"]; ?>/api/v1/customers/' + customerId)
.then(response => response.json())
.then(data => {
// Get the existing addresses array
const existingAddresses = data.address || [];
// Get the new address details
const firstName = $('#addressFirstName2').val();
const lastName = $('#addressLastName2').val();
const contact = $('#addressContact2').val();
const buildingNumber = $('#addressBuilding2').val();
const street = $('#addressStreet2').val();
const province = $('#provinceSelect2 :selected').text();
const city = $('#citySelect2 :selected').text();
const barangay = $('#barangaySelect2 :selected').text();
const country = $('#addressCountry2').val();
const id = $('#addressId').val();
console.log('clickkkkkkkk'+ id)
// Create a new address object
const newAddress = {
first_name: firstName,
last_name: lastName,
phone: contact,
address_1: buildingNumber,
address_2: street,
city: city,
province: province,
barangay: barangay,
country: country,
};
// Add the new address to the existing addresses
// existingAddresses.push(newAddress);
// var newAddresses= existingAddresses.filter((e,i)=>i!==id)
let existingAddress = existingAddresses
existingAddress[id]={
first_name: firstName,
last_name: lastName,
phone: contact,
address_1: buildingNumber,
address_2: street,
city: city,
province: province,
barangay: barangay,
country: country,
};
console.log( existingAddress[id])
// Make a PATCH request to update the addresses array
return fetch('https://<?php echo $_SESSION["data_endpoint"]; ?>/api/v1/customers/' + customerId, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
address: existingAddress
}),
});
})
.then(response => {
if (response.ok) {
// Handle success (e.g., show a success message)
location.reload();
} else {
// Handle error
console.error('Failed to submit data');
alert('Failed to submit data');
}
})
.catch(error => {
// Handle network or other errors
console.error('Error:', error);
alert('Error submitting data');
});
});
});
</script>
<span class="ec-check-order-btn">
<?php
if (empty($cartItems)) {
echo '<!-- $cartItems is empty, "Place Order" button will not be displayed -->';
} else {
echo '
<span class="ec-check-order-btn">
<a class="btn btn-primary" id="submitButton">Place Order</a>
</span>';
}
?>
<a class="btn btn-primary" id="submitButton">Place Order</a>
</span>
</div>
</div>
@ -1026,30 +1401,6 @@ if (isset($customer_data[0]["address"]) && is_array($customer_data[0]["address"]
const randomIndex = Math.floor(Math.random() * refchar.length);
uniqueRef += refchar.charAt(randomIndex);
}
const selectedFName = document.getElementById('selectedFName').innerText;
const selectedLName = document.getElementById('selectedLName').innerText;
const selectedContact = document.getElementById('selectedContact').innerText;
const sBuilding = document.getElementById('sBuilding').innerText;
const sStreet = document.getElementById('sStreet').innerText;
const sCity = document.getElementById('sCity').innerText;
const sBarangay = document.getElementById('sBarangay').innerText;
const sProvince = document.getElementById('sProvince').innerText;
const sCountry = document.getElementById('sCountry').innerText;
if (
selectedFName.trim() === "" ||
selectedLName.trim() === "" ||
selectedContact.trim() === "" ||
sBuilding.trim() === "" ||
sStreet.trim() === "" ||
sCity.trim() === "" ||
sBarangay.trim() === "" ||
sProvince.trim() === "" ||
sCountry.trim() === ""
) {
alert("Please select address.");
return; // Stop further execution
}
newArray.forEach(async (orderId) => {
console.log(orderId)
const token = '<?php echo $_SESSION["token"] ?>';
@ -1103,7 +1454,7 @@ if (isset($customer_data[0]["address"]) && is_array($customer_data[0]["address"]
'Authorization': "Bearer " + token,
},
});
// added marhc 7 2024
if (!patchResponse.ok) {
throw new Error(`Error updating payment status: ${patchResponse.status} ${patchResponse.statusText}`);
}

View File

@ -64,6 +64,8 @@ $products = productList();
<!-- Background css -->
<link rel="stylesheet" id="bg-switcher-css" href="assets/css/backgrounds/bg-4.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha512-DTOQO9RWCH3ppGqcWaEA1BIZOC6xxalwEsw9c2QQeAIftl+Vegovlnee1c9QX4TctnWMn13TZye+giMm8e2LwA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
<body class="shop_page">
@ -138,9 +140,12 @@ $products = productList();
<div class="ec-vendor-dashboard-card space-bottom-30">
<div class="ec-vendor-card-header">
<h5>Product List</h5>
<div class="ec-header-btn">
<div class="ec-header-btn" >
<!-- <a class="btn btn-lg btn-primary" href="vendor-uploads-add-product-action.php">Add</a> -->
<a class="btn btn-lg btn-primary" onclick="addProduct();" href="">Add</a>
<a class="btn btn-lg btn-primary" onclick="deleteSelectedProduct('<?php echo $product['_id'] ?>');">Delete Selected</a>
<a class="btn btn-lg btn-primary" href="vendor-uploads-add-product-action.php" onclick="addProduct('<?php echo $product['_id'] ?>');">Add</a>
</div>
</div>
<div class="ec-vendor-card-body">
@ -166,8 +171,10 @@ $products = productList();
for ($i = 0; $i <= $totalProducts - 1; $i++) {
$product = $products[$i];
?>
<tr>
<tr id="<?php echo $product['_id']; ?>">
<td>
<!-- raymart added function feb 21 2024 -->
<?php
if (isset($product['images'])) {
@ -197,12 +204,15 @@ $products = productList();
<td><span><?php echo $product['sale_price']; ?></span></td>
<td><span><?php echo $product['minimum_order']; ?></span></td>
<td><span><?php echo $product['stock']; ?></span></td>
<td style="display: flex; justify-content: space-between;">
<span onclick="editProduct('<?php echo $product['_id'] ?>');">
<i class="mdi mdi-circle-edit-outline"></i>
<td style="display: flex; justify-content: space-around;">
<span>
<input type="checkbox" name="product_checkbox[]" style="width: 20px; height: 33px; " value="<?php echo $product['_id']; ?>">
</span>
<span onclick="editProduct('<?php echo $product['_id'] ?>');">
<a class="mdi mdi-circle-edit-outline" style="font-size: 20px;"></a>
</span>
<span onclick="deleteProduct('<?php echo $product['_id'] ?>');">
<i class="mdi mdi mdi-delete-outline"></i>
<a class="mdi mdi mdi-delete-outline" style="font-size: 20px;"></a>
</span>
</td>
</tr>
@ -223,6 +233,7 @@ $products = productList();
var email = '<?php echo isset($_SESSION["email"]) ? $_SESSION["email"] : ""; ?>';
var password = '<?php echo isset($_SESSION["password"]) ? $_SESSION["password"] : ""; ?>';
function deleteProduct(productId) {
let text = "Please confirm delete action!\nClick OK or Cancel button.";
if (confirm(text) == true) {
@ -295,13 +306,54 @@ $products = productList();
// function editProduct(productId) {
// window.open("vendor-uploads.php?id=" + productId, "_self");
// }
function addProduct() {
function addProduct(productId) {
console.log("Session Token:", sessionToken);
login(email, password, function() {
// Removed the call to updateSessionToken
window.open("vendor-uploads-add-product-action.php", "_self");
window.open("vendor-uploads-add-product-action.php" + productId, "_self");
});
}
// multiple delete when checking the checkbox
function deleteSelectedProduct() {
var checkboxes = document.querySelectorAll('input[name="product_checkbox[]"]:checked');
if (checkboxes.length === 0) {
alert('Please select at least one product to delete.');
return;
}
var selectedProductIds = [];
checkboxes.forEach(function (checkbox) {
selectedProductIds.push(checkbox.value);
});
var text = "Please confirm delete action!\nClick OK or Cancel button.";
if (confirm(text) == true) {
selectedProductIds.forEach(function(productId) {
fetch("https://<?php echo $_SESSION['data_endpoint']; ?>/api/v1/products/" + productId, {
method: "DELETE"
})
.then(response => {
if (response.ok) {
console.log("Product with ID " + productId + " deleted successfully.");
// Remove the deleted product row from the table
var productRow = document.getElementById(productId);
if (productRow) {
productRow.remove();
} else {
console.error("Product row with ID " + productId + " not found in the DOM.");
}
} else {
throw new Error("Error deleting product with ID " + productId + ": " + response.statusText);
}
})
.catch(error => {
console.error(error.message);
});
});
}
}
</script>
</section>
<!-- End Vendor dashboard section -->