<template>
	<view class="container">
		<!-- <view class="searchBox" :style="{ height: localHeight + 'px', paddingTop: top + 'px' }">
			<view class="searchBox_add">
				<u-icon bold color="#000" size="40" name="arrow-left" @click="back"></u-icon>
			</view>
		</view> -->

		<view class="community">
			<view class="community_left">
				<image mode="aspectFill" :src="selectedShop.album_images" alt="" />
			</view>
			<view class="community_right" @click="changeShow">
				<view class="community_right_text">
					<view class="community_right_text1">{{ selectedShop.merchant_name }}</view>
					<view class="community_right_text2">{{ selectedShop.address }}</view>
				</view>
				<view class="community_right_more">
					<u-icon bold color="#999999" size="30" name="arrow-right"></u-icon>
				</view>
			</view>
		</view>

		<view class="content">
			<view class="walletBox">
				<view class="walletBox_top">钱包</view>
				<view class="walletBox_content">
					<view class="walletBox_item" @click="goWallet(1, walletInfo.guarantee.toFixed(2))">
						<view>{{ walletInfo.guarantee ? walletInfo.guarantee.toFixed(2) : "0.00" }}元</view>
						<view class="walletBox_item_text">保证金</view>
					</view>
					<view class="walletBox_item" @click="goWallet(2, walletInfo.receivable.toFixed(2))">
						<view>{{ walletInfo.receivable ? walletInfo.receivable.toFixed(2) : "0.00" }}元</view>
						<view class="walletBox_item_text">应收</view>
					</view>
					<!-- <view class="walletBox_item" @click="goWallet(3, walletInfo.handling_fee.toFixed(2))">
						<view>{{ walletInfo.handling_fee ? walletInfo.handling_fee.toFixed(2) : "0.00" }}元</view>
						<view class="walletBox_item_text">手续费</view>
					</view> -->
				</view>
			</view>
			<view class="iconStyle content2">
				<u-grid col="4" :border="false">
					<u-grid-item v-for="(item, index) in baseList" :key="index" @click="click(item)">
						<view class="grid-item" v-if="item.isShow">
							<image class="nav_icon" :src="item.image" mode=""></image>
						</view>
						<text class="grid-text" v-if="item.isShow">{{ item.name }}</text>
					</u-grid-item>
				</u-grid>
			</view>
		</view>


		<view class="boxshadow" v-if="show" @click="changeShow">
			<view class="boxshadowCon">
				<view class="boxshadowCon_Tit">
					请选择店铺
					<br />
					<view class="boxshadowCon_Text">从以下列表中选择您的管理权限的店铺</view>
					<view class="cancel">确定</view>
				</view>
				<view class="lines"></view>
				<view class="communityList">
					<view class="boxshadowCon_Text2">您有{{ shopList.length }}个店铺的管理权限</view>
					<view style="width: 100%;height: 200rpx;">
						<view class="communityItem" v-for="(item, index) in shopList" :key="index">
							<view class="community_left">
								<image mode="aspectFill" :src="item.album_images" alt="" />
							</view>
							<view class="community_right" @click="changeShow2(item)">
								<view class="community_right_text">
									<view class="community_right_text1">{{ item.merchant_name }}</view>
									<view class="community_right_text2">{{ item.address }}</view>
								</view>
							</view>
							<view class="communityItem_radio" @click="changeShow2(item)">
								<radio :checked="selectedShop.merchant_name === item.merchant_name"></radio>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>

	</view>

</template>

<script>
import {
	request,
	picUrl,
	uniqueByField,
	menuButtonInfo,
	NavgateTo
} from '../../../utils';
import {
	apiArr
} from '../../../api/community';
import { apiArr as walletApi } from '../../../api/wallet';
import { apiArr as apiArr2 } from '../../../api/contract';


export default {
	data() {
		return {
			top: "",
			localHeight: "",
			active: 1,
			show: false,
			show2: false,
			shopList: [],
			selectedShop: {},
			baseList: [
				// {
				// 	image: "https://static.hshuishang.com/property-img-file/page_user_Group_1568.png",
				// 	name: "订单",
				// 	url: "/packages/storeManagement/order/index",
				// },
				{
					image: "https://static.hshuishang.com/gp_cancelAfterVerification.png",
					name: "到店核销",
					url: "/packages/storeManagement/orderVerification/index",
					isShow: true,
				},
				{
					image: "https://static.hshuishang.com/kefu0.png",
					name: "客服",
					url: "/packages/customerService/chattingRecords/index",
					isShow: true,
				},
				{
					image: "https://static.hshuishang.com/contract.png",
					name: "电子签约",
					url: "/packages/customerService/sign/index",
					index: 1,
					isShow: false,
				},
				{
					image: "https://static.hshuishang.com/contract.png",
					name: "商户进件",
					url: "/packages/customerService/addMer/index",
					index: 2,
					isShow: false,
				},
				{
					image: "https://static.hshuishang.com/contract.png",
					name: "分账开通",
					url: "/packages/customerService/applyLedgerMer/index",
					index: 4,
					isShow: false,
				},
				{
					image: "https://static.hshuishang.com/contract.png",
					name: "创建接收方",
					url: "/packages/customerService/establishAcceptor/index",
					isShow: false,
				},
				{
					image: "https://static.hshuishang.com/contract.png",
					name: "申请绑定",
					url: "/packages/customerService/applyBind/index",
					index: 5,
					isShow: false,
				}
			],
			walletInfo: {},
		}
	},
	onLoad(options) {
		const meun = menuButtonInfo();
		this.top = meun.top;
		// this.top = meun.height + meun.top;
		this.localHeight = meun.height;
		this.getShopList().then(() => {
			uni.setStorageSync('mchId', this.selectedShop.id);
			this.getStep();
		});
	},

	methods: {
		// 获取进件流程步骤
		getStep() {
			const params = {
				mch_id: this.selectedShop.id,
			}
			request(apiArr2.progresss, 'POST', params, { silent: false }).then(res => {
				if(res.data.link){
					this.baseList.forEach(item => {
						if(item.index == res.data.link){
							item.isShow = true;
						}
					})
				}
			})
		},
		// 获取钱包信息
		getWalletInfo() {
			const params = {
				merchant_id: this.selectedShop.id,
			}
			request(walletApi.walletList, 'POST', params, { silent: false }).then(res => {
				this.walletInfo = res;
			})
		},
		// 获取商家列表
		getShopList() {
			return request(apiArr.getShopList, 'POST', {}, { silent: false }).then(res => {
				res.rows.map(item => {
					item.album_images = picUrl + item.album_images;
				})
				this.shopList = res.rows;
				if (this.shopList.length > 0) {
					this.selectedShop = this.shopList[0];
					// 确保selectedShop有值后再调用getWalletInfo()
					this.getWalletInfo();
				}
			})
		},
		// 跳转钱包页面
		goWallet(type, amount) {
			NavgateTo('/packages/storeManagement/wallet/index?type=' + type + '&merchant_id=' + this.selectedShop.id + '&amount=' + amount);
		},
		changeTab(index) {
			this.active = index;
		},
		back() {
			uni.navigateBack({
				delta: 1
			});
		},
		changeShow() {
			this.show = !this.show
		},
		changeShow2(item) {
			this.selectedShop = item;
			this.show = false;
		},
		click(item) {
			// 根据item中的url进行跳转
			if (item.url) {
				NavgateTo(item.url + '?merchant_id=' + this.selectedShop.id)
			} else {
				console.warn('未配置跳转URL');
				// 可添加默认跳转或提示
			}
		}
	},


}
</script>

<style>
@import url("./index.css");
</style>