var prod_file = function(option){
var sets = {
//類型
type : '1', //1->列表,2->詳細頁,3->活動詳細頁,4.一頁式購物
action : 'poducts', //頁面類型
//商品內容
contBox : 'content', //總容器的class
itemBox : 'productBox', //商品容器的class
nameBox : 'nameBox', //商品名稱的class
prodnoBox : 'prodnoBox', //商品編號的class
loveBox : 'love', //願望清單的class
price2Box : 'font-delete', //定價的class
price1Box : 'font-big', //結帳價的class
rbonusBox : 'price-rbonus', //小福幣的class
bonusBox : 'get_bonus', //購物金的class
colorBox : 'colorbox', //顏色的class
colorSelect: 'color-select', //顏色下拉的class
colorName : 'Spec_Name1', //顏色名稱的class
sizeBox : 'sizebox', //尺寸-按鈕的class
sizeSelect: 'size-select', //尺寸-下拉的class
sizeName : 'Spec_Name2', //尺寸名稱的class
sp3Box : 'sp3box', //規格三-按鈕的class
sp3Select : 'sp3-select', //規格三-下拉的class
sp3Name : 'Spec_Name3', //規格三名稱的class
amountBox : 'amountBox', //數量的class
stockBox : 'stockBox', //庫存的class
picpathBox: 'productImg', //主圖片的class
picBox : 'owl-carousel', //圖片容器的class
specClass : 'box-item', //規格 -按鈕的class
specID2 : 'SID', //規格 -要傳送的標籤屬性
specUrl : '/products/ajax/common/ajax_get_prod_item.php', //規格 -ajax網址
//加入購物車
addButton : 'add_to_cart', //加入購物車按鈕的class
toButton : 'to_cart', //加入購物車按鈕的class
addUrl : '/ajax/ajax_add_to_cart.php', //加入購物車網址
addBox : 'Add_Button', //加入購物車按鈕的容器
ordButton : 'button1', //選購按鈕(行銷)
//貨到通知
informBtn : 'informButton', //貨到通知按鈕的class
informSend: 'informSend', //送出按鈕的ID
informID : 'informID', //存ID欄位的ID
informEmail: 'informEmail', //Email欄位的ID
informClose: 'informClose', //關閉按鈕的ID
informUrl : '/products/ajax/common/ajax_add_prod_inform.php', //ajax網址
//小福幣(電子書)
bookcaseBtn : 'add_bookcase', //電子書按鈕class
bookcaseUrl : '/activities/ajax/act15/ajax_buy_ebook.php', //電子書小福幣ajax網址,
//wishList
loveButton: 'love', //列表加入願望清單
wishButton: 'wishButton', //加入願望清單按鈕的class
wishUrl : '/products/ajax/common/ajax_add_wish_list.php', //ajax網址
//設定圖片
mobileUrl : '/products/ajax/detail/productView_moblie.php', //ajax手機網址
pcUrl : '/products/ajax/detail/productView_pc.php', //ajax桌機網址
otherData : ''
};
$.extend(sets, option);
var _this = this;
if(sets.type == 1 || $(window).width() <= 768){
_this.item_num = 1;
}else{
_this.item_num = 5;
}
//初始
this.init = function(){
//點選規格
$("."+sets.contBox).on('click',"."+sets.specClass,function(){
var _item = $(this).parents("."+sets.itemBox),
SID = $(this).attr(sets.specID2),
$this = $(this),
Page_Shop_Type = $(this).parents('.productData').attr("type"),
Page_Shop_Class_ID = $(this).parents('.productData').attr("pclass");
var data = 'Type='+sets.type+'&SID='+SID+'&Page_Shop_Type='+Page_Shop_Type+'&Page_Shop_Class_ID='+Page_Shop_Class_ID+sets.otherData;
if(SID){
$.ajax({
url:sets.specUrl,
type:"POST",
cache:false,
async:false,
data:data,
dataType: 'json',
error:function(d){
alert(d.responseText);
},
success:function(d){
_this.setFile(_item,$this,d);
}
});
}
});
//尺寸下拉選單
$("."+sets.contBox).on('change',"."+sets.colorSelect+",."+sets.sizeSelect+",."+sets.sp3Select,function(){
var _item = $(this).parents("."+sets.itemBox),
SID = $(this).val(),
$this = $(this),
Page_Shop_Type = $(this).parents('.productData').attr("type"),
Page_Shop_Class_ID = $(this).parents('.productData').attr("pclass");
var data = 'Type='+sets.type+'&SID='+SID+'&Page_Shop_Type='+Page_Shop_Type+'&Page_Shop_Class_ID='+Page_Shop_Class_ID+sets.otherData;
if(SID){
$.ajax({
url:sets.specUrl,
type:"POST",
cache:false,
async:false,
data:data,
dataType: 'json',
error:function(d){
alert(d.responseText);
},
success:function(d){
_this.setFile(_item,$this,d);
}
});
}
});
//數量輸入
$("."+sets.contBox).on('keyup',"."+sets.amountBox,function(){
var now_num = $(this).val();
var max_num = $(this).attr('max');
now_num = parseInt(now_num);
$(this).val(now_num);
if(now_num>max_num){
alert('庫存不足');
$(this).val(max_num);
}
if(now_num<1){
$(this).val(1);
}
if(isNaN(now_num)){
$(this).val(1);
}
});
//數量
$("."+sets.contBox).off("click",".fa.fa-plus")
$("."+sets.contBox).on("click",".fa.fa-plus",function(){
var amount = $(this).prev("."+sets.amountBox).val(),
max = $(this).prev("."+sets.amountBox).attr("max");
if(amount == '無庫存'){
return false;
}
var amount = parseInt(amount);
if(amount+1 > max){
$(this).prev("."+sets.amountBox).val(max);
}else{
$(this).prev("."+sets.amountBox).val(amount+1);
}
});
$("."+sets.contBox).off("click",".fa.fa-minus")
$("."+sets.contBox).on("click",".fa.fa-minus",function(){
var amount = $(this).next("."+sets.amountBox).val();
if(amount == '無庫存'){
return false;
}
var amount = parseInt(amount);
if(amount-1 <= 1){
$(this).next("."+sets.amountBox).val(1);
}else{
$(this).next("."+sets.amountBox).val(amount-1);
}
});
//關閉跳窗
$("."+sets.contBox).on('click','.close',function(){
$("."+sets.itemBox).removeClass('is-show');
});
//直接購買按鈕
$("."+sets.contBox).on("click","."+sets.toButton,function(){
var _item = $("."+sets.addButton).parents("."+sets.itemBox),
SID = $("."+sets.addButton).attr('sid') || 0,
num = _item.find("."+sets.amountBox).val() || 1;
if(SID == 0){
alert('請選擇尺寸或規格');
//errorbox();
}else if(num ==0){
alert('請選擇數量');
//errorbox();
}else{
$.ajax({
url:sets.addUrl,
type:"POST",
cache:false,
async:false,
data:{SID:SID, num:num, webcacheno:$('#webcacheno').val()},
dataType: 'json',
error:function(d){
alert(d.responseText);
},
success:function(d){
alert(d.Alert);
if(d.Alert == '加入購物車成功'){
if(d.GA && typeof ga == 'function')
{
for(var key in d.GA)
{
ga('ec:addProduct',d.GA[key]);
}
ga('ec:setAction','add');
ga('send','event','購物車','加入購物車');
}
if(d.FBQ && d.Total_Money && typeof fbq == 'function')
{
fbq('track', 'AddToCart', {
value:d.Total_Money,
currency:'TWD',
content_ids:d.IDs,
content_type: 'product',
contents:d.FBQ
});
}
if(d.GA4 && typeof gtag == 'function')
{
for(var key in d.GA4)
{
gtag("event","add_to_cart",{
"items":d.GA4
});
}
}
$.ajax({
url:"/ajax/ajax_get_cart.php",
type:"POST",
async:false,
cache:false,
data:{Type:'Prod',ID:SID},
success:function(d){
window.location="/shopcart/";
}
})
}
}
});
}
});
//加入購物車按鈕
//$("."+sets.contBox).off("click","."+sets.addButton)
$("."+sets.contBox).on("click","."+sets.addButton,function(){
console.log(sets.itemBox)
var _item = $(this).parents("."+sets.itemBox),
SID = $(this).attr('sid') || 0,
addtype = $(this).parent(".PopupBtn1").attr('addtype') || 0,
num = _item.find("."+sets.amountBox).val() || 1;
if(SID == 0){
alert('請選擇尺寸或規格');
//errorbox();
}else if(num ==0){
alert('請選擇數量');
//errorbox();
}else{
var data = 'SID='+SID+'&num='+num+'&webcacheno='+$('#webcacheno').val()+sets.otherData+'&addtype='+addtype;
$.ajax({
url:sets.addUrl,
type:"POST",
cache:false,
async:false,
data:data,
dataType: 'json',
error:function(d){
alert(d.responseText);
},
success:function(d){
$("."+sets.itemBox).removeClass('is-show');
if(d.Pre_Flag==1){
if(d.Stock<=5)_item.find("."+sets.stockBox).html('預購品');
}else{
if(d.Stock<=5)_item.find("."+sets.stockBox).html('庫存 : '+d.Stock);
}
if(d.Amount)_item.find("."+sets.amountBox).html(d.Amount);
$("#Shop_Cart_Total2,#Shop_Cart_Total3,.cart-num-total").html(d.Total);
alert(d.Alert);
_this.setCartOther();
if(d.Alert == '加入購物車成功'){
if(d.GA && typeof ga == 'function')
{
for(var key in d.GA)
{
ga('ec:addProduct',d.GA[key]);
}
ga('ec:setAction','add');
ga('send','event','購物車','加入購物車');
}
if(d.FBQ && d.Total_Money && typeof fbq == 'function')
{
fbq('track', 'AddToCart', {
value:d.Total_Money,
currency:'TWD',
content_ids:d.IDs,
content_type: 'product',
contents:d.FBQ
});
}
if(d.GA4 && typeof gtag == 'function')
{
gtag("event","add_to_cart",{
"items":d.GA4
});
}
if(sets.type != '4')
{
$.ajax({
url:"/ajax/ajax_get_cart.php",
type:"POST",
async:false,
cache:false,
data:{Type:'Prod',ID:SID},
success:function(d){
$(".shopping-cartbox").css('display','none');
$("#Header_Shopcart_Prod").html(d);
$("#Header_Shopcart_Prod").parent(".one-cartbox").animate({'opacity':1,'maxHeight':'999px'}).css('z-index','100');
setTimeout(function(){
$("#Header_Shopcart_Prod").parent(".one-cartbox").animate({'maxHeight':'0px','opacity':0},function(){
$(this).removeAttr('style');
$(".shopping-cartbox").removeAttr('style');
});
},5000);
}
})
}
}
}
});
}
});
//小福幣購買
$("."+sets.contBox).on("click","."+sets.bookcaseBtn,function(){
var _item = $(this).parents("."+sets.itemBox),
SID = $(this).attr('sid') || 0;
$.ajax({
url:sets.bookcaseUrl,
type:"POST",
cache:false,
async:false,
data:{'sid':SID},
dataType: 'html',
error:function(d){
alert(d.responseText);
},
success:function(d){
console.log(d);
switch(d){
case "NO_MEMBER_ID":
alert('請先登入');
window.location='/member-login/';
break;
case "NO_ENOUGH_MONEY":
alert('小福幣不足,無法兌換');
break;
case "ERROR1":
case "ERROR2":
alert('兌換失敗,資料庫異常');
break;
case "ALREADY_BUY":
alert('已擁有此本電子書,請至「我的書櫃」觀看');
break;
case "SUCCESS":
alert('兌換成功,請至「我的書櫃」觀看');
break;
}
}
});
});
//貨到通知按鈕
$("."+sets.contBox).on("click","."+sets.informBtn,function(){
$("#"+sets.informID).val($(this).attr('SID'));
});
$("#"+sets.informSend).click(function(){
var email = $("#"+sets.informEmail).val();
if(!email)
{
alert('請輸入E-mail');
return false;
}
var emailRule = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]+$/;
if(email.search(emailRule) == -1)
{
alert('E-mail格式錯誤');
return false;
}
$.ajax({
url:sets.informUrl,
type:"POST",
cache:false,
async:false,
data:{SID:$("#"+sets.informID).val(),email:email},
error:function(d){
alert(d.responseText);
},
success:function(d){
if(d=='OK'){
alert('您已成功新增一筆貨到通知,當商品有貨時我們會儘快通知您!');
}else{
alert('資料庫忙線中');
}
$("#"+sets.informClose).trigger("click");
}
});
});
//列表增加wishList
$("."+sets.contBox).on("click","."+sets.loveButton,function(){
var $this = $(this),
_item = $(this).parents("."+sets.itemBox),
SID = _item.find('.'+sets.addButton).attr('sid'),
type = 'add';
if(!SID){
var SID = _item.find('.'+sets.informBtn).attr('sid')
}
if($this.attr("class").match('click')){
type = 'del';
}
$.ajax({
url:sets.wishUrl,
type:"POST",
cache:false,
async:false,
dataType: 'json',
data:{Type:type,SID:SID},
error:function(d){
alert(d.responseText);
},
success:function(d){
if(d.Msg=='OK'){
if(type == 'add'){
if(d.GA4 && typeof gtag == 'function')
{
gtag("event","add_to_wishlist",{
"items":d.GA4
});
}
if(typeof fbq == 'function')
{
fbq('track', 'AddToWishlist', {
content_ids:"['"+d.Prod_No+"']",
value:d.Price1,
currency:'TWD',
content_type:'product'
});
}
$this.addClass("click");
alert('加入追蹤清單成功');
}else{
alert('此商品已加入追蹤清單');
}
}else if(d.Msg == 'NO_MEMBER'){
alert('請先登入會員');
}else if(d.Msg == 'ADULT'){
alert('本商品為限制級商品,限18歲以上會員瀏覽');
}else if(d.Msg == 'ADULT2'){
alert('本商品為限制級商品,限18歲以上會員瀏覽,請先登入會員');
}else{
alert('資料庫忙線中');
}
}
});
});
//wishList
$("."+sets.contBox).on("click","."+sets.wishButton,function(){
var _item = $(this).parents("."+sets.itemBox),
SID = _item.find('.button2').attr('SID');
$.ajax({
url:sets.wishUrl,
type:"POST",
cache:false,
async:false,
dataType: 'json',
data:{SID:SID},
error:function(d){
alert(d.responseText);
},
success:function(d){
if(d.Msg=='OK'){
if(typeof fbq == 'function')
{
fbq('track', 'AddToWishlist', {
content_ids:"['"+d.Prod_No+"']",
value:d.Price1,
currency:'TWD',
content_type:'product'
});
}
if(d.GA4 && typeof gtag == 'function')
{
gtag("event","add_to_wishlist",{
"items":d.GA4
});
}
alert('新增成功!');
}else if(d.Msg == 'NO_MEMBER'){
alert('請先登入會員');
}else if(d.Msg == 'ADULT'){
alert('未滿18歲,無法加入希望清單');
}else{
alert('資料庫忙線中');
}
}
});
});
//圖片輪播
if($("."+sets.picBox+" .picitem ").length > 5){
$("."+sets.picBox).owlCarousel({
loop : false,
items : _this.item_num,
nav : true,
});
}
};
//設定內容
this.setFile= function(_item,$this,d){
_item.find("."+sets.loveBox).removeClass("click");
if(d.ID)_item.find("."+sets.ordButton).attr("sid",d.ID);
if(d.Name)_item.find("."+sets.nameBox).html(d.Name);
if(d.Prod_No)_item.find("."+sets.prodnoBox).html(d.Prod_No);
if(d.Love)_item.find("."+sets.loveBox).addClass(d.Love);
_item.find("."+sets.price1Box).html(d.Price1);
_item.find("."+sets.price2Box).html(d.Price2);
if(d.RBonus)_item.find("."+sets.rbonusBox).html(d.RBonus);
if(d.Get_Bonus)_item.find("."+sets.bonusBox).html(d.Get_Bonus);
if(d.Color)_item.find("."+sets.colorBox).html(d.Color);
if(d.Color_Name)_item.find("."+sets.colorName).html(d.Color_Name);
if(d.Stock){
_item.find("."+sets.stockBox).html('庫存 : '+d.Stock);
}else{
_item.find("."+sets.stockBox).html('');
}
if(sets.type == 3){
if(d.Size_Sel)$this.next("."+sets.sizeSelect).html(d.Size_Sel);
if(d.SP3_Sel)$this.nextAll("."+sets.sp3Select).eq(0).html(d.SP3_Sel);
if(d.Amount)$this.nextAll("."+sets.amountBox).eq(0).html(d.Amount);
$this.nextAll("."+sets.amountBox).eq(0).trigger('change');
}else{
if(d.Size_Button)_item.find("."+sets.sizeBox).html(d.Size_Button);
if(d.Size_Sel)_item.find("."+sets.sizeSelect).html(d.Size_Sel);
if(d.Size_Name)_item.find("."+sets.sizeName).html(d.Size_Name);
if(d.SP3_Button)_item.find("."+sets.sp3Box).html(d.SP3_Button);
if(d.SP3_Sel)_item.find("."+sets.sp3Select).html(d.SP3_Sel);
if(d.SP3_Name)_item.find("."+sets.sp3Name).html(d.SP3_Name);
if(d.Amount > '0'){
if(d.Amount > 20){
d.Amount = '20';
}
_item.find("."+sets.amountBox).val('1');
_item.find("."+sets.amountBox).attr('max',d.Amount);
_item.find("."+sets.amountBox).attr('readonly',false);
}else{
_item.find("."+sets.amountBox).val('無庫存');
_item.find("."+sets.amountBox).attr('readonly',true);
}
}
for($i=1;$i<=3;$i++){
if(d.Button[$i])_item.find("."+sets.addBox+$i).not('.button1').html(d.Button[$i]);
}
_this.setImg(_item,d.Pic_Path,d.Pic,d.Video);
_this.setFileOther(_item,d);
}
//變更內容自訂Function
this.setFileOther = function(){
}
//加入購物車後自訂Function
this.setCartOther = function(){
}
//設定圖片、影片
this.setImg = function(_item,Pic_Path,Pic,Video){
//手機
if($(window).width() <=768){
//列表
if(sets.type == 1){
_item.find("."+sets.picpathBox).css("background-image","url('/upload_files/fonlego-rwd/prodpic/"+Pic_Path+"')");
_item.find("."+sets.picpathBox).html('');
}
//詳細頁
else{
if(sets.type != '4')
{
_this.item_num = 1;
var _Pic = (Pic_Path+','+Pic).split(',');
$_Pic1 = '',
$_Pic2 = '';
for($i=0;$i<_Pic.length;$i++){
if(_Pic[$i]){
$_Pic1 += '