<template>
	<view class="min-page bg-f7 u-font-28 color-333">
		<up-sticky>
			<view class="top u-flex">
				<up-select :options="statusList" @select="statusListSelect">
					<template #text>
						<text v-if="query.status">{{returnStatusLabel(query.status)}}</text>
						<text v-else>状态</text>
					</template>
				</up-select>
				<view class="u-flex-1 u-p-l-32">
					<up-search placeholder="店铺名称" v-model="query.shopName" @search="search" @clear="search"
						@custom="search"></up-search>
				</view>
			</view>
		</up-sticky>
		<view class="box">
			<view class="container" v-for="(item,index) in list" :key="index">
				<view class="u-flex u-row-between">
					<view>
						<text class="color-666">到期时间：</text>
						<text class="font-bold">{{item.expireTime}}</text>
					</view>
					<view class="status" :class="returnStatusClass(item.status)">{{item.status?'正式':'试用'}}</view>
				</view>
				<view class="u-flex u-m-t-10">
					<up-avatar :src="item.logo" size="120rpx" shape="square"></up-avatar>
					<view class="u-flex-1 u-p-l-32">
						<view class="font-bold u-line-1">{{item.shopName}}</view>
						<view class="u-m-t-10">{{item.provinces}}-{{item.cities}}-{{item.districts}} {{item.address}}
						</view>
					</view>
				</view>
				<view class="u-m-t-32">
					<view class="">
						<text class="color-666">经营模式：</text>
						<text class="font-bold">{{returnRegisterType(item.registerType) }}</text>
					</view>
					<view class="u-m-t-24">
						<text class="color-666">店铺类型：</text>
						<text class="font-bold">{{returnShopType(item.shopType) }}</text>
						<text class="font-bold"
							v-if="item.shopType!='only'&&item.isHeadShop==0">(主店：{{item.headShopName}})</text>
					</view>
					<view class="u-m-t-24 u-flex">
						<text class="color-666">店铺状态：</text>
						<up-switch disabled v-model="item.status" :active-value="1" :inactive-value="0"
							size="20"></up-switch>
					</view>

					<view class="u-m-t-24 u-flex u-col-center">
						<text class="color-666">创建时间：</text>
						<view class=" font-bold">{{item.createTime}}</view>
					</view>

					<view class="u-flex u-m-t-32  u-row-right gap-20">
						<view style="min-width: 160rpx;">
							<my-button @click="toEdit(item)">编辑</my-button>
						</view>
						<view style="min-width: 160rpx;">
							<my-button @click="showaAtivationCode(item)">激活</my-button>
						</view>
						<view style="min-width: 160rpx;">
							<my-button @click="showMore(item)">
								<text class="u-m-r-10">更多</text>
								<up-icon name="arrow-down" color="#fff"></up-icon>
							</my-button>
						</view>
					</view>
				</view>
			</view>

			<template v-if="query.shopName">
				<up-empty v-if="list.length<=0" text="未搜索到相关信息"></up-empty>
				<up-loadmore :status="isEnd?'nomore':'loading'" v-else></up-loadmore>
			</template>
			<template v-else>
				<up-empty v-if="list.length<=0" text="未搜索到相关信息"></up-empty>
				<up-loadmore v-else :status="isEnd?'nomore':'loading'"></up-loadmore>
			</template>
		</view>

		<view style="height: 140rpx;"></view>
		<view class="bottom">
			<my-button @click="toAdd()">添加店铺</my-button>
		</view>

		<shopSelect v-model="showShopSelect" @confirm="toAdd"></shopSelect>
		<up-popup :show="codeShow" mode="center" @close="codeShow=false" round="16rpx" :close-on-click-overlay="true">
			<view style="border-radius: 16rpx;overflow: hidden;" class="u-p-b-30">
				<up-image width="200" height="200" :src="code"></up-image>
				<view class="u-m-t-0 text-center" v-if="codeType=='wx'">请打开微信扫码</view>
				<view class="u-m-t-0 text-center" v-if="codeType=='aliPay'">请打开支付宝扫码</view>
			</view>
		</up-popup>

		<up-popup :show="activationCode.show" mode="center" @close="activationCode.show=false" round="16rpx"
			:close-on-click-overlay="true">
			<view class="popup-box">
				<view class="font-bold u-font-32 text-center">生成激活码</view>
				<view class="u-p-l-20 u-p-r-20 u-m-r-32">
					<up-form label-width="auto">
						<up-form-item label="激活码" required>
							<up-input v-model="activationCode.form.activateCode"></up-input>
						</up-form-item>
					</up-form>

				</view>
				<view class="u-flex gap-20 u-m-t-42">
					<view class="u-flex-1">
						<my-button type="default" @click="activationCode.show=false">取消</my-button>
					</view>
					<view class="u-flex-1">
						<my-button @click="activationCodeSubmit">确认</my-button>
					</view>
				</view>

			</view>

		</up-popup>

		<up-action-sheet :actions="moreMenus.list" round="16rpx" @select="moreMenusSelect" @close="moreMenus.show=false"
			cancelText="取消" :show="moreMenus.show"></up-action-sheet>
	</view>
</template>

<script setup>
	import {
		shopTypes,
		registerTypes
	} from '../data.js'


	const code = ref('')
	const codeShow = ref(false)
	const codeType = ref('')


	import {
		reactive,
		ref,
		watch
	} from 'vue';
	import shopSelect from '../components/shop-select.vue'
	import {
		onReachBottom,
		onShow
	} from '@dcloudio/uni-app'
	import * as shopInfoApi from '@/http/api/account/shopInfo.js'

	const activationCode = reactive({
		show: false,
		form: {
			activateCode: '',
			id: '',
		},
	})
	
	async function activationCodeSubmit(){
		if (!activationCode.form.activateCode) {
			return uni.$u.toast('请填写激活码')
		}
		const res = await shopInfoApi.editShop(activationCode.form)
		if (res) {
			uni.$u.toast('激活成功')
			search()
		}
	}

	function showaAtivationCode(item) {
		activationCode.form.id = item.id
		activationCode.show=true
	}

	const statusList = [{
			value: 1,
			name: '开启',
		},
		{
			value: 0,
			name: '关闭',
		},

	]

	const moreMenusList = [{
			name: '添加分店',
		},
		{
			name: '支付配置',
		},
		// {
		// 	name: '续费记录',
		// },
		// {
		// 	name: '重置密码',
		// },
		// {
		// 	name: '删除',
		// 	color: '#FF1C1C'
		// },
	]
	const moreMenus = reactive({
		list: moreMenusList,
		selItem: null,
		show: false
	})

	function moreMenusSelect(e) {
		console.log(e);
		if (e.name == '支付配置') {
			uni.navigateTo({
				url: '/pagesShops/pay-config/pay-config?shopId=' + moreMenus.selItem.id + '&licenceNo=' + moreMenus
					.selItem.licenceNo
			})
		}
		if (e.name == '添加分店') {
			uni.navigateTo({
				url: '/pagesShops/add/add?id=' + moreMenus.selItem.id + '&type=addBranch'
			})
		}
	}

	function showMore(item) {
		if (item.shopType != 'only' && item.isHeadShop) {
			moreMenus.list = moreMenusList
		} else {
			moreMenus.list = moreMenusList.filter(v => v.name != '添加分店')
		}
		moreMenus.selItem = item
		moreMenus.show = true
	}

	function returnRegisterType(registerTyp) {
		return registerTypes[registerTyp] ? registerTypes[registerTyp] : ''
	}


	function returnShopType(shopType) {
		return shopTypes[shopType] ? shopTypes[shopType] : ''
	}

	function statusListSelect(e) {
		query.status = e.value
	}
	const statusLabelJson = {
		'REJECTED': '已拒绝'
	}
	const statusClassJson = {
		'REJECTED': 'error'
	}

	function returnStatusLabel(state) {
		const item = statusList.find(v => v.value == state)
		if (item) {
			return item.name
		}
		return ''
	}

	function returnStatusClass(state) {
		if (state == 1) {
			return 'success'
		}
		return 'warning'
	}

	function showCode(item, type) {
		if (type == 'wx') {
			code.value = item.wechatSignUrl
		}
		if (type == 'aliPay') {
			code.value = item.alipaySignUrl
		}
		codeType.value = type
		codeShow.value = true

	}

	const showShopSelect = ref(false)

	const query = reactive({
		page: 1,
		size: 10,
		shopName: '',
		status: ''
	})
	watch(() => query.status, (newval) => {
		search()
	})
	const isEnd = ref(false)
	const list = ref([])

	function search() {
		isEnd.value = false
		query.page = 1
		getData()
	}

	function toAdd(shop) {
		console.log(shop)
		uni.navigateTo({
			url: '/pagesShops/add/add'
		})
	}


	function toEdit(shop) {
		console.log(shop)
		uni.navigateTo({
			url: '/pagesShops/add/add?id=' + shop.id
		})
	}

	function getData() {
		shopInfoApi.allShopList(query).then(res => {
			isEnd.value = query.page >= res.totalPage * 1
			if (query.page == 1) {
				list.value = res.records
			} else {
				list.value.push(...res.records)
			}
		})
	}

	function queryStatus(item) {
		queryEntry({
			licenceNo: item.licenceNo,
			shopId: item.shopId
		}).then(res => {
			isEnd.value = false
			query.page = 1;
			getData()
		})
	}
	onReachBottom(() => {
		if (!isEnd.value) {
			query.page++
			getData()
		}

	})
	onShow(getData)
</script>

<style lang="scss" scoped>
	.min-page {
		.box {
			padding: 32rpx 28rpx;
		}
	}

	.container {
		padding: 32rpx 28rpx;
		border-radius: 16rpx;
		margin-bottom: 32rpx;
		background-color: #fff;
	}

	.bottom {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		background-color: #fff;
		z-index: 100;
		padding: 32rpx 28rpx;
		padding-bottom: 40rpx;
	}

	.types {}

	.status {
		padding: 8rpx 18rpx;
		border-radius: 8rpx;
		border: 2rpx solid #333;

		&.success {
			border-color: rgba(123, 209, 54, 1);
			color: rgba(123, 209, 54, 1);
			background: rgba(123, 209, 54, 0.12);
		}

		&.warning {
			border-color: rgba(255, 141, 40, 1);
			color: rgba(255, 141, 40, 1);
			background: rgba(255, 141, 40, 0.12);
		}

		&.error {
			border-color: #FF1C1C;
			color: #FF1C1C;
			background: rgba(255, 28, 28, 0.18);
		}

		&.gray {
			color: #bbb;
			background-color: #f7f7f7;
			border-color: #bbb;
		}
	}

	.top {
		padding: 32rpx 28rpx;
		background-color: #fff;
	}

	.popup-box {
		background-color: #fff;
		width: 690rpx;
		background-color: #fff;
		padding: 32rpx 28rpx;
		border-radius: 16rpx;
	}
</style>