nhance/app/Views/gsheet_editor.php
2026-02-26 12:28:23 +05:30

96 lines
2.4 KiB
PHP

<!DOCTYPE html>
<html>
<head>
<title>Google Sheet Editor</title>
<style>
body { font-family: Arial; padding: 10px; }
table { border-collapse: collapse; width: 100%; }
td { border: 1px solid #ccc; padding: 6px; min-width: 80px; }
td[contenteditable] { background: #fffde7; }
button { margin-right: 10px; }
</style>
</head>
<body>
<h3>Google Sheet Editor</h3>
<button onclick="save()">💾 Save</button>
<button onclick="download()"> Download</button>
<button onclick="createRfq()">🚀 Create RFQ</button>
<div id="result" style="margin-top:15px;"></div>
<script>
function createRfq() {
fetch('<?php echo base_url() ?>' +'/sheet/create', {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: 'templateId=1efHU8EBkjTZTzOx115xRrtgz3kwvvVp0p5GbrvREfiw'
})
.then(r => r.json())
.then(res => {
if (res.status === 'success') {
document.getElementById('result').innerHTML =
`<a href="${res.url}" target="_blank">Open RFQ Sheet</a>`;
} else {
alert(res.message);
}
});
}
</script>
<hr>
<table id="sheet"></table>
<script>
alert('first');
const sheetId = "<?= esc($sheetId) ?>";
alert('second');
/* ---------- LOAD ---------- */
fetch('<?php echo base_url() ?>' + `sheet/${sheetId}/fetch`)
.then(r => r.json())
// .then(r => r.json())
.then(render);
function render(data) {
alert('data');
console.log('data');
console.log(data);
const table = document.getElementById('sheet');
table.innerHTML = '';
data.forEach(row => {
const tr = document.createElement('tr');
row.forEach(cell => {
const td = document.createElement('td');
td.contentEditable = true;
td.innerText = cell ?? '';
tr.appendChild(td);
});
table.appendChild(tr);
});
}
/* ---------- SAVE ---------- */
function save() {
const rows = [...document.querySelectorAll('tr')]
.map(tr => [...tr.children].map(td => td.innerText));
fetch(`/sheet/${sheetId}/save`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(rows)
})
.then(() => alert('Saved successfully'));
}
/* ---------- DOWNLOAD ---------- */
function download() {
window.location.href = '<?php echo base_url() ?>' + `/sheet/${sheetId}/download`;
}
</script>
</body>
</html>