<script>
$(function () {
if ($("#custom_chamfer").length) {
if ($("#custom_chamfer option:nth-child(2)").length) {
$("#custom_chamfer option:nth-child(2)").prop('selected', true).trigger('change');
}
}
$('#custom_width, #custom_height').on('input', function () {
let frameWidth = $('.frame_block_wrap').width(); // 親要素の幅
let inputWidth = parseInt($('#custom_width').val()) || 0;
let inputHeight = parseInt($('#custom_height').val()) || 0;
let borderWidth = {{Product.minlength}};
let maxHeight = 350; // 最大高さを500pxとする
if (inputWidth > 0 && inputHeight > 0 && borderWidth >= 0) {
let gcd = function (a, b) {
return b == 0 ? a : gcd(b, a % b);
};
let greatestCommonDivisor = gcd(inputWidth, inputHeight);
let aspectWidth = inputWidth / greatestCommonDivisor;
let aspectHeight = inputHeight / greatestCommonDivisor;
let heightBasedOnWidth = frameWidth * (aspectHeight / aspectWidth);
// 高さが最大高さを超える場合は縮小する
if (heightBasedOnWidth > maxHeight) {
heightBasedOnWidth = maxHeight; // 高さを最大高さに設定
frameWidth = maxHeight * (aspectWidth / aspectHeight); // 幅もアスペクト比に基づいて再計算
}
let borderRatioWidth = borderWidth / greatestCommonDivisor;
let borderBasedOnWidth = frameWidth * (borderRatioWidth / aspectWidth);
$('#frame_block').css({
width: frameWidth + 'px', // 幅を100%に設定
height: heightBasedOnWidth + 'px', // 高さをアスペクト比に基づいて設定
"border-width": borderBasedOnWidth
});
}
});
});
</script>
<script>
$(function () {
const PRODUCT_PRICE = $("#add_price");
function numberFormat(num = 0) {
return num.toString().replace(/(\d+?)(?=(?:\d{3})+$)/g, x => {
return x + ','
})
}
$('#form1').on('change keyup', 'input, select', function () {
// ここでフォームのデータを取得
var formData = $('#form1').serialize(); // フォームのデータをURLエンコードされた文字列で取得
// AJAXリクエストを送信
$.ajax({
url: '/api/custom_price/product/{{Product.id}}', // サーバーエンドポイントURL
type: 'GET', // メソッドタイプ
dataType: 'json', // 期待するレスポンスの形式
data: formData, // フォームデータ
success: function (response) {
//let formattedPrice = numberFormat(response.price);
// PRODUCT_PRICE.html(`ご購入価格:<span class="txt_pink">${formattedPrice}円<span class="unit">(税込)</span></span>`);
PRODUCT_PRICE.html(`
<div class="price_s">
本体価格:<span class="txt_pink txt_pink_s">${response.price}円<span class="unit unit_s">(税込)</span></span><br>
面取り加工価格:<span class="txt_pink txt_pink_s">${response.chamfer_price}円<span class="unit unit_s">(税込)</span></span><br>
取付金具価格:<span class="txt_pink txt_pink_s">${response.mounting_price}円<span class="unit unit_s">(税込)</span></span>
</div>
合計価格:<span class="txt_pink">${response.main_price}円<span class="unit">(税込)</span></span>
`);
$("#error-message").remove();
},
error: function (xhr, status, error) {
// エラー処理
let response = JSON.parse(xhr.responseText);
$("#error-message").remove();
if (response?.error) {
// エラーメッセージを挿入
PRODUCT_PRICE.html("");
PRODUCT_PRICE.after(`<div id="error-message" style="color: red; font-size:13px;">${response.error}</div>`);
}
}
});
});
});
</script>
<style>
*{
box-sizing: border-box;
}
.frame_block_ttl{
color: #804040;
text-align: center;
margin-top: 40px;
}
.frame_block_wrap {
width: 90%;
margin: 0 auto;
margin-top: 20px;
overflow: hidden;
margin-bottom: 15px;
text-align: center;
}
#frame_block {
height: 200px;
border: 5px solid #804040;
margin: 0 auto;
}
.price_s{
font-size: 14px;
}
.item_detail .item_detail_info .goods_info span.txt_pink_s{
font-size: 17px;
}
.item_detail .item_detail_info .goods_info .unit.unit_s{
font-size: 14px;
}
</style>