var prod_file = function(option){
var sets = {
//類型
type : '1', //1->列表,2->詳細頁,3->活動詳細頁
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
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網址
//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桌機網址
};
$.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);
if(SID){
$.ajax({
url:sets.specUrl,
type:"POST",
cache:false,
async:false,
data:{Type:sets.type,SID:SID},
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);
if(SID){
$.ajax({
url:sets.specUrl,
type:"POST",
cache:false,
async:false,
data:{Type:sets.type,SID:SID},
dataType: 'json',
error:function(d){
alert(d.responseText);
},
success:function(d){
_this.setFile(_item,$this,d);
}
});
}
});
//加入購物車按鈕
$("."+sets.contBox).on("click","."+sets.addButton,function(){
var _item = $(this).parents("."+sets.itemBox),
SID = $(this).attr('sid') || 0,
num = _item.find("."+sets.amountBox).val() || 0;
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){
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_Total>.num").html("("+d.Total+")");
$("#Shop_Cart_Total .num").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',
conetents:d.FBQ
});
}
//GA4
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){
$(".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');
});
},3000);
}
})
}
}
});
}
});
//貨到通知按鈕
$("."+sets.contBox).on("click","."+sets.informBtn,function(){
$("#"+sets.informID).val($(this).attr('SID'));
});
$("#"+sets.informSend).click(function(){
$.ajax({
url:sets.informUrl,
type:"POST",
cache:false,
async:false,
data:{SID:$("#"+sets.informID).val(),email:$("#"+sets.informEmail).val()},
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('.button1').attr('SID'),
type = 'add';
if($this.attr("class").match('click')){
type = 'del';
}
$.ajax({
url:sets.wishUrl,
type:"POST",
cache:false,
async:false,
data:{Type:type,SID:SID},
dataType: 'json',
error:function(d){
alert(d.responseText);
},
success:function(d){
if(d.Msg=='OK'){
if(type == 'add'){
if(typeof fbq == 'function')
{
fbq('track', 'AddToWishlist', {
conetent_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
});
}
$this.addClass("click");
}else{
$this.removeClass("click");
}
}else if(d.Msg == 'NO_MEMBER'){
alert('請先登入會員');
}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,
data:{SID:SID},
dataType: 'json',
error:function(d){
alert(d.responseText);
},
success:function(d){
if(d.Msg=='OK'){
if(typeof fbq == 'function')
{
fbq('track', 'AddToWishlist', {
conetent_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{
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.Pre_Flag==1){
_item.find("."+sets.stockBox).html('預購品');
}else{
if(d.Stock){
_item.find("."+sets.stockBox).html('庫存 : '+d.Stock);
}else{
_item.find("."+sets.stockBox).html('');
}
}
if(sets.type == 2){
if(d.Serial_No){
var new_url = '/product-detail/'+d.Serial_No;
window.history.replaceState({id:d.ID},d.Name,new_url);
}
}
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)_item.find("."+sets.amountBox).html(d.Amount);
}
for($i=1;$i<=3;$i++){
if(d.Button[$i])_item.find("."+sets.addBox+$i).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{
_this.item_num = 1;
var _Pic = (Pic_Path+','+Pic).split(',');
$_Pic1 = '',
$_Pic2 = '';
for($i=0;$i<_Pic.length;$i++){
if(_Pic[$i]){
$_Pic1 += '