<script setup>
// 店铺 客服 收藏 支付
import {
	ref,
	reactive,
	getCurrentInstance,
	computed,
	defineEmits,
	onMounted
} from 'vue'
// 
import util from '@/common/js/util.js'
// 
import api from '@/api/index.js'
// 
import {
	useStore
} from 'vuex'

const {
	proxy
} = getCurrentInstance()
// 
const store = useStore()
// 
const props = defineProps({
	// 商品信息
	detail: {
		type: Object,
		default: () => ({})
	},
})
// 
const emit = defineEmits(['update', 'buy'])
// 用户信息
const userinfo = computed(() => store.state.userinfo)

// 商品收藏
function handleCollect() {
	util.isLogin().then(rs => {
		// 
		api.shop.addProductCollect({
			query: {
				productId: props.detail.id,
				type: {
					0: 1,
					1: 0,
				}[props.detail.isCollect]
			}
		}).then(rs => {
			if (rs.code == 200) {
				// 关注状态
				props.detail.isCollect = {
					0: 1,
					1: 0,
				}[props.detail.isCollect]
				// 关注数量
				props.detail.collectNumber = rs.data
				return
			}
			util.alert({
				content: rs.msg,
				showCancel: false,
			})
		})
	}).catch(() => {
		// 登录
		uni.navigateTo({
			url: '/pages/login/loginPhone'
		})
	})
}

/**
 * 跳转
 * @param {Object} url 跳转路径
 */
function link(url) {
	uni.navigateTo({
		url,
	})
}

// 商品购买
function handleBuy() {
	emit('buy')
}


// 客服
function toCustomer() {
	// api.shop.getCustomerService({ merchantId: props.detail.merId }).then(rs => {
	// 	if (rs.code == 200) {
	// 		let param = {};
	// 		param.type = 'C2C'
	// 		param.name = `${props.detail.merName}`
	// 		param.msgId = `${rs.data.serviceId}`
	// 		param.isCustomer = true

	// 		util.toChat(param)

	// 		uni.navigateTo({
	// 			url: util.setUrl('/pages/news/chat/chat', param)
	// 		})
	// 	} else {
	// 		util.alert(rs.msg)
	// 	}
	// })

}
</script>

<template>
	<!-- 底部 -->
	<view class="footerMneu df fdr jcsb aic">
		<view class="menu df fdr jcsb aic">
			<view class="option df fdc aic">
				<image class="wh30" src="/static/store.png" mode="aspectFit" />
				<text class="text mt10" @click="link(`/pages/shop/store/index?storeId=${detail.merId}`)">店铺</text>
			</view>

			<view class="option  df fdc aic" @click="handleCollect">
				<view class="wh30 fmid">
					<uni-icons type="star-filled" size="45rpx" color="#FF9B27" v-if="detail.isCollect == 1" />
					<uni-icons type="star" size="45rpx" color="#666" v-else />
				</view>
				<view class="df fdr aic mt10">
					<text class="text" v-if="detail.isCollect == 1">已</text>
					<text class="text">收藏</text>
				</view>
			</view>

			<view class="option ver" @click="toCustomer" v-if="0">
				<image class="wh30" src="/static/customer-service.png" mode="aspectFit" />
				<text class="text mt10">客服</text>
			</view>
		</view>

		<!-- 下单 -->
		<view class="btn primary f1 ml30" @click="handleBuy">
			<text class="tac cfff f32">立即购买</text>
		</view>
	</view>
</template>

<style scoped lang="scss">
// 底部菜单
.footerMneu {

	// 选项
	.option {
		width: 80rpx;

		.text {
			color: #999;
			font-size: 24rpx;
		}
	}
}
</style>