<template>
	<view>
		<!-- 顶部操作条 -->
		<cu-custom :bgColor="'bg-main-color'" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">我的团队</block>
		</cu-custom>
		<!-- 团队成员 -->
		<view>
			<view class="bg-white padding bottom-border" v-for="(member, index) in members">
				<view class="flex justify-between">
					<view class="flex justify-start align-center" style="width: 40%;">
						<view class="cu-avatar round"
							:style="'background-image:url(' + member.workerLogoUrl + ');min-width: 64rpx'"></view>
						<view class="text-lg margin-left-sm">{{member.name}}</view>
					</view>
					<view class="flex justify-end align-center">
						<view v-if="Boolean(member.showEditInput)" :key="member.workerId"
							class="flex justify-end align-center margin-lr-sm">
							<input class="radius-input" type="text" v-model="member.workerName"
								placeholder="姓名备注"></input>
							<view class="text-lg margin-left-sm">
								<text class="cuIcon-roundcheckfill text-main-color" :data-index="index"
									@click="editWorkerName"></text>
								<text class="cuIcon-roundclosefill margin-left-xs" :data-index="index"
									@click="hideEditInput"></text>
							</view>
						</view>
						<view v-else class="flex justify-end margin-lr-sm" :key="member.workerId">
							<text>{{member.workerName ? member.workerName : ''}}</text>
							<text class="text-lg" :data-index="index" @click="showEditInput"><text
									class="cuIcon-edit margin-left-xs"></text></text>
						</view>
						<view class="text-xxl text-main-color" :data-phone="member.phone" @click="makePhoneCall">
							<view class="cuIcon-phone"></view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<load-status-bar class="margin-bottom-with-bar" ref="loadStatusBar" @loadMore="loadTeamPage"></load-status-bar>
		<!-- 底部操作栏 -->
		<view class="cu-bar bg-white tabbar border shop fixed-bottom-bar">
			<view class="left-grid text-center" data-modal="takeRateSet" @click="showModalByRef('setTakeRateModal')">
				<view>设置扣点</view>
				<view v-if="curUserInfo.leaderTeamRate">(扣点：<text
						class="text-red">{{Number(curUserInfo.leaderTeamRate)*100}}%</text>)</view>
			</view>
			<!-- <view class="bg-main-color submit" @click="showModalByRef('inviteMasterModal')">邀请团队</view> -->
			<view class="bg-main-color submit">
				<button class="cu-btn bg-main-color" open-type="share" style="width: 100%; height: 100%;">
					<text class="cuIcon-share"></text>
					<text>邀请师傅</text>
				</button>
			</view>
		</view>

		<set-take-rate ref="setTakeRateModal" :leaderTeamRate="Number(curUserInfo.leaderTeamRate)*100"
			:leaderTeamMoney="curUserInfo.leaderTeamMoney" @confirm="applySetTakeRate"></set-take-rate>
		<invite-master ref="inviteMasterModal" :inviteMasterObj="inviteMasterObj" @confirm="inviteMaster"></invite-master>
		<confirm-modal ref="confirmModal" :content="'当前无法加入师傅团队，请先登录并完成师傅入驻申请。'" @confirm="goToIndex"></confirm-modal>
	</view>
</template>

<script>
	import setTakeRate from '@/pages/area-proxy/modal/set-take-rate.vue';
	import inviteMaster from '@/pages/area-proxy/modal/invite-master.vue';
	import loadStatusBar from '@/components/custom-bar/load-status-bar.vue';

	export default {
		components: {
			inviteMaster,
			setTakeRate,
			loadStatusBar
		},
		data() {
			return {
				members: [],
				pageNum: 0,
				pageSize: 0,
				curUserInfo: {},
				inviteMasterObj: {}
			}
		},
		onLoad(options) {
			if (options && options.inviteMasterObj) {
				// 点击邀请链接进入的页面
				this.beInvited2Team(JSON.parse(decodeURIComponent(options.inviteMasterObj)));
			} else {
				this.loadData();
			}
		},
		onShareAppMessage() {
			return {
				title: '邀请你成为我的团队成员',
				path: '/pages/area-proxy/my-team?inviteMasterObj=' + encodeURIComponent(JSON.stringify(this.curUserInfo)),
				// imageUrl: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big5000.jpg'
			}
		},
		methods: {
			async beInvited2Team(inviteMasterObj) {
				this.loadBasicData();
				this.inviteMasterObj = inviteMasterObj;
				let curUserInfo = await this.login();
				if (curUserInfo == null || curUserInfo.status !== 0) {
					// 提示登录和入驻成为师傅，返回首页
					this.showModalByRef('confirmModal');
				} else {
					this.showModalByRef('inviteMasterModal');
				}
			},
			async login() {
				// 更新缓存中的userInfo
				await this.$request.storageExistUser();
				// 返回缓存中的userInfo
				return this.$request.getCurUserInfo();
			},
			goToIndex() {
				uni.reLaunch({
					url: '/pages/index/index'
				})
			},
			loadBasicData() {
				this.pageSize = this.$globalData.initPageSize;
				this.curUserInfo = this.$request.getCurUserInfo();
			},
			async loadData() {
				// this.myTeamInfo = await this.$api.data('myTeamInfo');
				// this.members = this.myTeamInfo.members;
				this.loadBasicData();
				this.loadTeamPage();
			},
			async reloadData() {
				this.pageNum = 0;
				this.members = [];
				this.loadData();
			},
			async loadTeamPage(params = {}) {
				params.pageNum = this.pageNum;
				params.pageSize = this.pageSize;
				params.leaderId = this.curUserInfo.workerId;
				this.$refs.loadStatusBar.showLoading();
				try {
					let res = await this.$request.getTeamPage(params);
					let rowsLength = res.rows.length;
					if (rowsLength > 0) {
						this.members = this.members.concat(res.rows);
						this.pageNum++;
						if (rowsLength === this.pageSize) {
							this.$refs.loadStatusBar.showLoadMore();
						}
					}
					this.$refs.loadStatusBar.showLoadOver();
				} catch (e) {
					console.error(e)
					this.$refs.loadStatusBar.showLoadErr();
				}
			},
			showEditInput(e) {
				let curIndex = e.currentTarget.dataset.index;
				this.$set(this.members, curIndex, Object.assign(this.members[curIndex], {
					showEditInput: true
				}))
			},
			hideEditInput(e) {
				let curIndex = e.currentTarget.dataset.index;
				this.$set(this.members, curIndex, Object.assign(this.members[curIndex], {
					showEditInput: false
				}))
			},
			async editWorkerName(e) {
				let curIndex = e.currentTarget.dataset.index;
				let memberInfo = this.members[curIndex];
				let res = await this.$request.updateWorkerTeam({
					workerTeamId: memberInfo.workerTeamId,
					workerName: memberInfo.workerName
				});
				if (res.code === 0) {
					uni.showToast({
						title: '修改成功',
						icon: 'success',
						mask: true
					});
					this.hideEditInput(e);
				}
			},
			makePhoneCall(e) {
				let phoneNum = e.currentTarget.dataset.phone;
				uni.makePhoneCall({
					phoneNumber: phoneNum
				})
			},
			showModalByRef(refName) {
				this.$refs[refName].showModal();
			},
			async applySetTakeRate(params) {
				let res = await this.$request.updateWorker(params);
				if (res.code === 0) {
					uni.showToast({
						title: '保存成功',
						icon: 'success',
						mask: true
					});
					this.reloadData();
				}
			},
			async inviteMaster(params) {
				let res = await this.$request.addTeam(params);
				if (res.code === 0) {
					uni.showToast({
						title: '加入成功',
						icon: 'success',
						mask: true
					})
					this.reloadData();
				} else if (res.code === 301) {
					uni.showToast({
						title: res.msg,
						icon: 'none',
						mask: true
					})
				}
			}
		},
	}
</script>

<style scoped>
	.left-grid {
		width: 50%;
	}

	.bottom-border {
		border-bottom: 1rpx solid rgba(0, 0, 0, 0.1);
	}
</style>
