// Shop11 API Functions
// ページビルダーで作成したページで使用するAPI呼び出し関数
document.addEventListener('DOMContentLoaded', function() {
console.log('✅ Shop11 API Functions loaded');
// すべてのAPIボタンにイベントリスナーを追加
document.addEventListener('click', function(e) {
const action = e.target.getAttribute('data-action');
if (!action) return;
switch(action) {
case 'goldCheckDelete':
goldCheckDelete(e.target);
break;
case 'createSateiTitle':
createSateiTitle(e.target);
break;
case 'sendNotification':
sendNotification(e.target);
break;
case 'searchProduct':
searchProduct(e.target);
break;
case 'loadProductTable':
loadProductTable(e.target);
break;
case 'clearProductTable':
clearProductTable(e.target);
break;
case 'loadCustomerTable':
loadCustomerTable(e.target);
break;
case 'clearCustomerTable':
clearCustomerTable(e.target);
break;
case 'loadSateiTable':
loadSateiTable(e.target);
break;
case 'clearSateiTable':
clearSateiTable(e.target);
break;
case 'registerProduct':
registerProduct(e.target);
break;
}
});
});
// 地金チェック削除
function goldCheckDelete(button) {
const block = button.closest('.shop11-api-block');
const productId = block.querySelector('.gold-product-id').value;
const resultDiv = block.querySelector('.gold-check-result');
if (!productId) {
alert('商品IDを入力してください');
return;
}
resultDiv.style.display = 'block';
resultDiv.innerHTML = '処理中...';
const requestData = { product_id: productId };
fetch('/api/shouhin/gold_check_id_delete', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.content || ''
},
body: JSON.stringify(requestData)
})
.then(res => res.json())
.then(data => {
resultDiv.innerHTML = '
' + JSON.stringify(data, null, 2) + '
';
// Supabaseにログ保存
if (window.saveApiLogToSupabase) {
saveApiLogToSupabase(
'地金チェック削除',
'/api/shouhin/gold_check_id_delete',
requestData,
data,
'success'
);
}
})
.catch(err => {
resultDiv.innerHTML = 'エラー: ' + err.message + '';
// Supabaseにエラーログ保存
if (window.saveApiLogToSupabase) {
saveApiLogToSupabase(
'地金チェック削除',
'/api/shouhin/gold_check_id_delete',
requestData,
{ error: err.message },
'error'
);
}
});
}
// 査定タイトル生成
function createSateiTitle(button) {
const block = button.closest('.shop11-api-block');
const productId = block.querySelector('.satei-product-id').value;
const resultDiv = block.querySelector('.satei-title-result');
if (!productId) {
alert('商品IDを入力してください');
return;
}
resultDiv.style.display = 'block';
resultDiv.innerHTML = '生成中...';
const endpoint = '/api/satei_func/create_title/' + productId;
const requestData = { product_id: productId };
fetch(endpoint, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})
.then(res => res.json())
.then(data => {
resultDiv.innerHTML = 'タイトル:
' + (data.title || JSON.stringify(data, null, 2));
if (window.saveApiLogToSupabase) {
saveApiLogToSupabase('査定タイトル生成', endpoint, requestData, data, 'success');
}
})
.catch(err => {
resultDiv.innerHTML = 'エラー: ' + err.message + '';
if (window.saveApiLogToSupabase) {
saveApiLogToSupabase('査定タイトル生成', endpoint, requestData, { error: err.message }, 'error');
}
});
}
// メール送信
function sendNotification(button) {
const block = button.closest('.shop11-api-block');
const to = block.querySelector('.mail-to').value;
const subject = block.querySelector('.mail-subject').value;
const body = block.querySelector('.mail-body').value;
const resultDiv = block.querySelector('.mail-result');
if (!to || !subject || !body) {
alert('すべての項目を入力してください');
return;
}
resultDiv.style.display = 'block';
resultDiv.innerHTML = '送信中...';
const endpoint = '/api/notification/mail';
const requestData = { to, subject, body };
fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.content || ''
},
body: JSON.stringify(requestData)
})
.then(res => res.json())
.then(data => {
resultDiv.innerHTML = '✅ 送信完了
' + JSON.stringify(data, null, 2) + '
';
if (window.saveApiLogToSupabase) {
saveApiLogToSupabase('メール送信', endpoint, requestData, data, 'success');
}
})
.catch(err => {
resultDiv.innerHTML = '❌ エラー: ' + err.message + '';
if (window.saveApiLogToSupabase) {
saveApiLogToSupabase('メール送信', endpoint, requestData, { error: err.message }, 'error');
}
});
}
// 商品検索
function searchProduct(button) {
const block = button.closest('.shop11-api-block');
const keyword = block.querySelector('.search-keyword').value;
const resultDiv = block.querySelector('.search-result');
if (!keyword) {
alert('検索キーワードを入力してください');
return;
}
resultDiv.style.display = 'block';
resultDiv.innerHTML = '検索中...';
const endpoint = '/api/auto_complete_refasta/ResultSearch?q=' + encodeURIComponent(keyword);
const requestData = { keyword };
fetch(endpoint, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})
.then(res => res.json())
.then(data => {
if (Array.isArray(data) && data.length > 0) {
let html = '';
data.forEach(item => {
html += '- ' +
(item.label || item.name || JSON.stringify(item)) + '
';
});
html += '
';
resultDiv.innerHTML = html;
} else {
resultDiv.innerHTML = '検索結果なし';
}
if (window.saveApiLogToSupabase) {
saveApiLogToSupabase('商品検索', endpoint, requestData, data, 'success');
}
})
.catch(err => {
resultDiv.innerHTML = 'エラー: ' + err.message + '';
if (window.saveApiLogToSupabase) {
saveApiLogToSupabase('商品検索', endpoint, requestData, { error: err.message }, 'error');
}
});
}
// 商品データテーブル読込
function loadProductTable(button) {
const block = button.closest('.shop11-api-block');
const keyword = block.querySelector('.product-search-keyword').value;
const tableBody = block.querySelector('.product-table-body');
const infoDiv = block.querySelector('.product-table-info');
if (!keyword) {
alert('検索キーワードを入力してください');
return;
}
tableBody.innerHTML = '| 🔍 検索中... |
';
const endpoint = '/api/auto_complete_refasta/ResultSearch?q=' + encodeURIComponent(keyword);
const requestData = { keyword };
fetch(endpoint, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})
.then(res => res.json())
.then(data => {
if (Array.isArray(data) && data.length > 0) {
let html = '';
data.forEach((item, index) => {
html += '';
html += '| ' + (item.product_id || item.id || index + 1) + ' | ';
html += '' + (item.label || item.name || item.title || '-') + ' | ';
html += '' + (item.brand || item.maker || '-') + ' | ';
html += '' + (item.category || item.category_name || '-') + ' | ';
html += '' + (item.price ? '¥' + parseInt(item.price).toLocaleString() : '-') + ' | ';
html += '' + getStatusBadge(item.status) + ' | ';
html += '' +
'' +
'' +
' | ';
html += '
';
});
tableBody.innerHTML = html;
infoDiv.innerHTML = '✅ ' + data.length + '件の商品が見つかりました';
if (window.saveApiLogToSupabase) {
saveApiLogToSupabase('商品データテーブル読込', endpoint, requestData, { count: data.length }, 'success');
}
} else {
tableBody.innerHTML = '| 📭 検索結果なし |
';
infoDiv.innerHTML = '検索結果: 0件';
if (window.saveApiLogToSupabase) {
saveApiLogToSupabase('商品データテーブル読込', endpoint, requestData, { count: 0 }, 'success');
}
}
})
.catch(err => {
tableBody.innerHTML = '| ❌ エラー: ' + err.message + ' |
';
infoDiv.innerHTML = 'データ読込エラー';
if (window.saveApiLogToSupabase) {
saveApiLogToSupabase('商品データテーブル読込', endpoint, requestData, { error: err.message }, 'error');
}
});
}
// 商品テーブルクリア
function clearProductTable(button) {
const block = button.closest('.shop11-api-block');
const keyword = block.querySelector('.product-search-keyword');
const tableBody = block.querySelector('.product-table-body');
const infoDiv = block.querySelector('.product-table-info');
keyword.value = '';
tableBody.innerHTML = '| 検索キーワードを入力して検索してください |
';
infoDiv.innerHTML = '';
}
// ステータスバッジ生成
function getStatusBadge(status) {
if (!status) return '-';
const statusMap = {
'1': { label: '在庫あり', color: 'success' },
'2': { label: '予約中', color: 'warning' },
'3': { label: '売約済', color: 'danger' },
'4': { label: '出品中', color: 'info' },
'5': { label: '取り下げ', color: 'secondary' }
};
const statusInfo = statusMap[status] || { label: status, color: 'secondary' };
return '' + statusInfo.label + '';
}
// 顧客データテーブル読込
function loadCustomerTable(button) {
const block = button.closest('.shop11-api-block');
const keyword = block.querySelector('.customer-search-keyword').value;
const tableBody = block.querySelector('.customer-table-body');
const infoDiv = block.querySelector('.customer-table-info');
if (!keyword) {
alert('検索キーワードを入力してください');
return;
}
tableBody.innerHTML = '| 🔍 検索中... |
';
const endpoint = '/api/Eoc/search?keyword=' + encodeURIComponent(keyword);
const requestData = { keyword };
fetch(endpoint, {
method: 'GET',
headers: { 'Content-Type': 'application/json' }
})
.then(res => res.json())
.then(data => {
const customers = Array.isArray(data) ? data : (data.data || []);
if (customers.length > 0) {
let html = '';
customers.forEach(customer => {
html += '';
html += '| ' + (customer.ecc_id || customer.id || '-') + ' | ';
html += '' + (customer.name1 || customer.name || '-') + ' | ';
html += '' + (customer.tel || customer.phone || '-') + ' | ';
html += '' + (customer.mail1 || customer.email || '-') + ' | ';
html += '' + (customer.zip1 || customer.zip || '-') + ' | ';
html += '' + (customer.address1 || customer.address || '-') + ' | ';
html += '' +
'' +
'' +
' | ';
html += '
';
});
tableBody.innerHTML = html;
infoDiv.innerHTML = '✅ ' + customers.length + '件の顧客が見つかりました';
if (window.saveApiLogToSupabase) {
saveApiLogToSupabase('顧客検索テーブル', endpoint, requestData, { count: customers.length }, 'success');
}
} else {
tableBody.innerHTML = '| 📭 検索結果なし |
';
infoDiv.innerHTML = '検索結果: 0件';
}
})
.catch(err => {
tableBody.innerHTML = '| ❌ エラー: ' + err.message + ' |
';
infoDiv.innerHTML = 'データ読込エラー';
if (window.saveApiLogToSupabase) {
saveApiLogToSupabase('顧客検索テーブル', endpoint, requestData, { error: err.message }, 'error');
}
});
}
// 顧客テーブルクリア
function clearCustomerTable(button) {
const block = button.closest('.shop11-api-block');
block.querySelector('.customer-search-keyword').value = '';
block.querySelector('.customer-table-body').innerHTML = '| 検索キーワードを入力して検索してください |
';
block.querySelector('.customer-table-info').innerHTML = '';
}
// 査定データテーブル読込
function loadSateiTable(button) {
const block = button.closest('.shop11-api-block');
const keyword = block.querySelector('.satei-search-keyword').value;
const tableBody = block.querySelector('.satei-table-body');
const infoDiv = block.querySelector('.satei-table-info');
if (!keyword) {
alert('検索キーワードを入力してください');
return;
}
tableBody.innerHTML = '| 🔍 検索中... |
';
const endpoint = '/api/satei/search?keyword=' + encodeURIComponent(keyword);
const requestData = { keyword };
fetch(endpoint, {
method: 'GET',
headers: { 'Content-Type': 'application/json' }
})
.then(res => res.json())
.then(data => {
const sateiList = Array.isArray(data) ? data : (data.data || []);
if (sateiList.length > 0) {
let html = '';
sateiList.forEach(satei => {
html += '';
html += '| ' + (satei.id || '-') + ' | ';
html += '' + (satei.product_name || satei.title || '-') + ' | ';
html += '' + (satei.customer_name || satei.ecc_name || '-') + ' | ';
html += '' + (satei.price ? '¥' + parseInt(satei.price).toLocaleString() : '-') + ' | ';
html += '' + (satei.satei_date || satei.created_at || '-') + ' | ';
html += '' + getSateiStatusBadge(satei.status) + ' | ';
html += '' +
'' +
'' +
' | ';
html += '
';
});
tableBody.innerHTML = html;
infoDiv.innerHTML = '✅ ' + sateiList.length + '件の査定が見つかりました';
if (window.saveApiLogToSupabase) {
saveApiLogToSupabase('査定検索テーブル', endpoint, requestData, { count: sateiList.length }, 'success');
}
} else {
tableBody.innerHTML = '| 📭 検索結果なし |
';
infoDiv.innerHTML = '検索結果: 0件';
}
})
.catch(err => {
tableBody.innerHTML = '| ❌ エラー: ' + err.message + ' |
';
infoDiv.innerHTML = 'データ読込エラー';
if (window.saveApiLogToSupabase) {
saveApiLogToSupabase('査定検索テーブル', endpoint, requestData, { error: err.message }, 'error');
}
});
}
// 査定テーブルクリア
function clearSateiTable(button) {
const block = button.closest('.shop11-api-block');
block.querySelector('.satei-search-keyword').value = '';
block.querySelector('.satei-table-body').innerHTML = '| 検索キーワードを入力して検索してください |
';
block.querySelector('.satei-table-info').innerHTML = '';
}
// 査定ステータスバッジ
function getSateiStatusBadge(status) {
const statusMap = {
'1': { label: '査定中', color: 'info' },
'2': { label: '査定完了', color: 'success' },
'3': { label: '買取完了', color: 'primary' },
'4': { label: 'キャンセル', color: 'danger' }
};
const statusInfo = statusMap[status] || { label: status || '-', color: 'secondary' };
return '' + statusInfo.label + '';
}
// 商品登録
function registerProduct(button) {
const block = button.closest('.shop11-api-block');
const form = block.querySelector('.product-register-form');
const resultDiv = block.querySelector('.register-result');
const name = block.querySelector('.product-name').value;
const brand = block.querySelector('.product-brand').value;
const category = block.querySelector('.product-category').value;
const price = block.querySelector('.product-price').value;
const description = block.querySelector('.product-description').value;
const status = block.querySelector('.product-status').value;
if (!name || !price) {
alert('商品名と価格は必須です');
return;
}
resultDiv.style.display = 'block';
resultDiv.innerHTML = '💾 登録中...';
const endpoint = '/api/shouhin/register';
const requestData = {
name: name,
brand: brand,
category: category,
price: price,
description: description,
status: status
};
fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.content || ''
},
body: JSON.stringify(requestData)
})
.then(res => res.json())
.then(data => {
resultDiv.innerHTML = '✅ 商品を登録しました!
商品ID: ' + (data.product_id || data.id || '不明') + '
';
form.reset();
if (window.saveApiLogToSupabase) {
saveApiLogToSupabase('商品登録', endpoint, requestData, data, 'success');
}
})
.catch(err => {
resultDiv.innerHTML = '❌ エラー: ' + err.message + '
';
if (window.saveApiLogToSupabase) {
saveApiLogToSupabase('商品登録', endpoint, requestData, { error: err.message }, 'error');
}
});
}