<template>
	<view class="" style="height: calc(100vh - 47px)" v-if="userInfo.agencyStatus != 1" hover-class="none" hover-stop-propagation="false">
		<rh-unlogininfo></rh-unlogininfo>
	</view>
	<div class="g_pb_32" style="min-height: calc(100vh - 47px)" v-else :data-admin="isAdmin">
		<view class="" v-if="false">
			<g-panel-form-item
				:list="[
					{
						icon: '',
						label: '当前团队',
						result: userInfo.agencyName,
						path: '',
						tip: 'userAgreement',
					},
				]"
				@clickItem="teamSet"
			/>
		</view>
		<view class="">
			<g-panel-form-item :isDisabled="isAdmin ? 0 : 1" class="g_mb_10" :list="companyConfig" @clickItem="handleItemClick"> </g-panel-form-item>
		</view>
		<view class="g_mt_10">
			<g-panel-form-item
				class="g_mb_10"
				:isDisabled="isAdmin ? 0 : 1"
				:list="[
					{
						icon: '',
						label: '联系人',
						result: info.contactUserName || '请输入联系人姓名',
						value: info.contactUserName,
						fontColor: !info.contactUserName ? 'g_c_9' : 'g_c_3',
						path: '/root/agency/change?label=联系人&fieldName=contactUserName&value=' + info.contactUserName,
						tip: 'user-name',
						placeholder: '请输入联系人姓名',
						fontSize: '16px',
						type: 'text',
						pColumn: 19,
						pRow: 12,
						require: false,
						params: {
							label: '联系人',
							fieldName: 'contactUserName',
						},
					},
					{
						icon: '',
						label: '联系电话',
						result: info.contactTel || '请输入联系电话',
						value: info.contactTel,
						fontColor: !info.contactTel ? 'g_c_9' : 'g_c_3',
						path: '/root/agency/change?label=联系电话&fieldName=contactTel&value=' + info.contactTel,
						tip: 'user-name',
						placeholder: '请输入联系电话',
						fontSize: '16px',
						type: 'text',
						pColumn: 19,
						pRow: 12,
						require: false,
						params: {
							label: '联系电话',
							fieldName: 'contactTel',
						},
					},
					{
						icon: '', 
						label: '联系人二维码',
						fontColor: !info.contactWechatQrcode ? 'g_c_9' : 'g_c_3',
						value: info.contactWechatQrcode,
						path: '/root/agency/avatar?label=联系人二维码&fieldName=contactWechatQrcode&value=' + info.contactWechatQrcode,
						tip: 'avatar',
						radius: 2,
						type: 'text',
						fontSize: '16px',
						pColumn: 22,
						pRow: 12,
						params: {
							fieldName: 'contactWechatQrcode',
						},
					},
				]"
				@clickItem="handleItemClick"
			>
			</g-panel-form-item>
		</view>
		<!-- <view class="g_mt_24"  >
			<rh-button btnText="保存" type="primary" @clickBtn="save"></rh-button>
		</view> -->

		<u-popup v-model="teamSetShow" mode="bottom" uZindex="9999" height="880" border-radius="12" :mask-close-able="true" width="694rpx">
			<view class="g_text_c g_fw_600 g_fs_18 g_border_e_b" style="line-height: 56px">切换团队</view>
			<scroll-view class="g_pb_32 g_fs_16" scroll-y="true" style="height: calc(100% - 92px)">
				<view class="g_flex_row_between g_p_16 g_border_e_b" hover-class="thover" hover-stop-propagation="true" v-for="(item, index) in teamList" :key="index" @click="toogleTeam(item)">
					<view class="g_flex_row_between">
						<view class="f17">{{ item.agencyName }}</view>
					</view>
					<view class style="color: #1890ff"> {{ item.checked ? "当前" : "" }}</view>
				</view>
			</scroll-view>
		</u-popup>
	</div>
</template>

<script>

import gPanelFormItem from "@/components/panel/formItem.vue";
export default {
	components: { 
		gPanelFormItem,
	},
	data() {
		return {
			isRelease: uni.getAccountInfoSync().miniProgram.envVersion === 'release',
			isAdmin: uni.getStorageSync("apply-userinfo").admin,
			userInfo: uni.getStorageSync("apply-userinfo"),
			teamSetShow: false,
			teamList: [],
			companyConfig: [],
			info: {
				userName: "",
				desp: "",
			},
			shopImg: [],
		};
	},
	onLoad() {},
	onShow() {},
	created() {
		var that = this;
		console.log("createdcreatedcreatedcreatedcreatedcreatedcreatedcreatedcreatedcreatedcreatedcreated");
		uni.$on("changeCompanyDesp", function (data) {
			console.log("data", data);
			console.log("changeCompanyDespchangeCompanyDespchangeCompanyDespchangeCompanyDesp");
			that.getAgencyInfo();
		});
		that.resetInfo();
		that.getTeamList();
		if (that.userInfo.agencyStatus == 1) {
			that.getAgencyInfo();
		}
	},

	methods: {
		save() {
			let that = this;
			let subObj = {
				recordId: that.info.id,
				fullName: that.info.fullName,
				logo: that.info.logo,
				agencyName: that.info.agencyName,
				desp: that.info.desp,
				imgs: that.shopImg.map((item) => item.url).join(","),
				aboutUs: that.info.aboutUs,
				contactUserName: that.info.contactUserName,
				contactTel: that.info.contactTel,
				contactAvatar: that.info.contactAvatar,
				contactWechatQrcode: that.info.contactWechatQrcode,
				address: that.info.address,
				lat: that.info.lat,
				lng: that.info.lng,
				district: that.info.district,
				codeUSC: that.info.codeUSC,
				registerTel: that.info.registerTel,
				storeStatus: that.info.storeStatus,
				bank: that.info.bank,
				registerAddress: that.info.registerAddress,
				bankCardNo: that.info.bankCardNo,
				slogan: that.info.slogan,
			};
			this.G.Post("/yishoudan/agency/updageAgencyInfo", subObj, (res) => {
				console.log("resresresresresres", res);
				that.getAgencyInfo();
			});
		},
		getAgencyInfo() {
			let that = this;
			this.G.Get(this.api.login_agencyInfo, {}, (res) => {
				console.log("resresres", res);
				that.info = res.agency;
				if (res.agency.imgs) {
					let imgList = res.agency.imgs.split(",");
					that.shopImg = imgList.map((item) => {
						return {
							url: item,
						};
					});
				}
				that.resetInfo();
			});
		},
		resetInfo() {
			let that = this;
			
			if(that.isRelease){
				that.companyConfig = [
				{
					icon: "",
					label: "企业头像",
					value: that.info.logo,
					path: "/root/agency/avatar?label=企业头像&fieldName=logo&value=" + that.info.logo,
					tip: "avatar",
					type: "text",
					fontSize: "16px",
					radius: 2,
					pColumn: 19,
					pRow: 12,
					require: false,
				},
				{
					icon: "",
					label: "企业全称",
					result: that.info.agencyName || "请输入企业全称",
					value: that.info.agencyName,
					fontColor: !that.info.agencyName ? "g_c_9" : "g_c_3",
					path: "/root/agency/change?label=企业全称&fieldName=agencyName&value=" + that.info.agencyName,
					tip: "user-name",
					placeholder: "请输入企业全称",
					fontSize: "16px",
					type: "text",
					pColumn: 19,
					pRow: 12,
					require: false,
					params: {
						label: "企业全称",
						fieldName: "agencyName",
					},
				},
				{
					icon: "",
					label: "企业简称",
					result: that.info.fullName || "请输入企业简称",
					value: that.info.fullName,
					fontColor: !that.info.fullName ? "g_c_9" : "g_c_3",
					path: "/root/agency/change?label=企业简称&fieldName=fullName&value=" + that.info.fullName,
					tip: "user-name",
					placeholder: "请输入企业简称",
					fontSize: "16px",
					type: "text",
					pColumn: 19,
					pRow: 12,
					require: false,
					params: {
						label: "企业简称",
						fieldName: "fullName",
					},
				},
				{
					icon: "",
					label: "宣传口号",
					result: that.info.slogan || "请输入宣传口号",
					value: that.info.slogan,
					fontColor: !that.info.slogan ? "g_c_9" : "g_c_3",
					path: "/root/agency/change?label=宣传口号&fieldName=slogan&value=" + that.info.slogan,
					tip: "user-name",
					placeholder: "请输入宣传口号",
					fontSize: "16px",
					type: "text",
					pColumn: 19,
					pRow: 12,
					require: false,
					params: {
						label: "宣传口号",
						fieldName: "slogan",
					},
				},
				{
					icon: "",
					label: "企业地址",
					result: that.info.address || "请选择企业地址",
					fontColor: !that.info.address ? "g_c_9" : "g_c_3",
					path: "/root/agency/change?label=企业地址&fieldName=address&value=" + JSON.stringify({ address: that.info.address, lat: that.info.lat, lng: that.info.lng }),
					tip: "dizhi",
					placeholder: "请选择企业地址",
					type: "slot",
					fontSize: "16px",
					pColumn: 19,
					pRow: 12,
					require: false,
					suffix: "icon-dizhi1",
				},

				{
					icon: "",
					label: "企业简介",
					result: that.info.desp || "请输入企业简介",
					fontColor: !that.info.desp ? "g_c_9" : "g_c_3",
					// value: that.info.desp || '请输入企业简介',
					path: "/root/agency/change?label=企业简介&fieldName=desp&value=" + that.info.desp,
					tip: "user-more-word",
					placeholder: "请输入企业简介",
					type: "text",
					fontSize: "16px",
					pColumn: 19,
					pRow: 12,
					params: {
						label: "企业简介",
						fieldName: "desp",
					},
				},
			];
			}else{
				that.companyConfig = [
				{
					icon: "",
					label: "企业全称",
					result: that.info.agencyName || "请输入企业全称",
					value: that.info.agencyName,
					fontColor: !that.info.agencyName ? "g_c_9" : "g_c_3",
					path: "/root/agency/change?label=企业全称&fieldName=agencyName&value=" + that.info.agencyName,
					tip: "user-name",
					placeholder: "请输入企业全称",
					fontSize: "16px",
					type: "text",
					pColumn: 19,
					pRow: 12,
					require: false,
					params: {
						label: "企业全称",
						fieldName: "agencyName",
					},
				},
				{
					icon: "",
					label: "企业简称",
					result: that.info.fullName || "请输入企业简称",
					value: that.info.fullName,
					fontColor: !that.info.fullName ? "g_c_9" : "g_c_3",
					path: "/root/agency/change?label=企业简称&fieldName=fullName&value=" + that.info.fullName,
					tip: "user-name",
					placeholder: "请输入企业简称",
					fontSize: "16px",
					type: "text",
					pColumn: 19,
					pRow: 12,
					require: false,
					params: {
						label: "企业简称",
						fieldName: "fullName",
					},
				},
				{
					icon: "",
					label: "宣传口号",
					result: that.info.slogan || "请输入宣传口号",
					value: that.info.slogan,
					fontColor: !that.info.slogan ? "g_c_9" : "g_c_3",
					path: "/root/agency/change?label=宣传口号&fieldName=slogan&value=" + that.info.slogan,
					tip: "user-name",
					placeholder: "请输入宣传口号",
					fontSize: "16px",
					type: "text",
					pColumn: 19,
					pRow: 12,
					require: false,
					params: {
						label: "宣传口号",
						fieldName: "slogan",
					},
				},
				{
					icon: "",
					label: "企业地址",
					result: that.info.address || "请选择企业地址",
					fontColor: !that.info.address ? "g_c_9" : "g_c_3",
					path: "/root/agency/change?label=企业地址&fieldName=address&value=" + JSON.stringify({ address: that.info.address, lat: that.info.lat, lng: that.info.lng }),
					tip: "dizhi",
					placeholder: "请选择企业地址",
					type: "slot",
					fontSize: "16px",
					pColumn: 19,
					pRow: 12,
					require: false,
					suffix: "icon-dizhi1",
				},

				{
					icon: "",
					label: "企业简介",
					result: that.info.desp || "请输入企业简介",
					fontColor: !that.info.desp ? "g_c_9" : "g_c_3",
					// value: that.info.desp || '请输入企业简介',
					path: "/root/agency/change?label=企业简介&fieldName=desp&value=" + that.info.desp,
					tip: "user-more-word",
					placeholder: "请输入企业简介",
					type: "text",
					fontSize: "16px",
					pColumn: 19,
					pRow: 12,
					params: {
						label: "企业简介",
						fieldName: "desp",
					},
				},
			];
			}
		},

		handleItemClick(e) {
			let that = this;
			console.log("e", e);
			uni.navigateTo({
				url: e.item.path,
			});
			// console.log("点击了", item);
			// if (item.path) {
			//     let url = item.path;
			//     if (item.params) {
			//         let paramsStr = "";
			//         for (let key in item.params) {
			//             if (paramsStr) {
			//                 paramsStr += "&";
			//             }
			//             paramsStr += `${key}=${encodeURIComponent(item.params[key])}`;
			//         }
			//         url += `?${paramsStr}`;
			//     }
			//     uni.navigateTo({
			//         url: url,
			//     });
			// }
		},

		teamSet() {
			this.teamSetShow = true;
		},
		getTeamList() {
			let that = this;
			this.G.Get(this.api.user_getTeamList, {}, (res) => {
				console.log(res);
				res.forEach((item) => {
					if (item.id == that.userInfo.agencyId) {
						item.checked = true;
					}
				});
				that.teamList = res;
			});
		},
		toogleTeam($item) {
			let that = this;
			if (that.userInfo.agencyId == $item.id) {
				return false;
			}
			wx.showLoading({
				title: "切换中...",
			});
			that.G.Get(that.api.user_toggleTeam + `/${$item.id}`, {}, (res) => {
				console.log(res);
				that.G.checkToken().then(() => {
					uni.showToast({
						title: "切换成功",
						icon: "success",
						duration: 2000,
					});
					that.teamSetShow = false;
					that.userInfo = uni.getStorageSync("apply-userinfo");
					uni.$emit("isGlogin", {
						uid: that.userInfo.user.id,
					});
					that.getAgencyInfo();
					that.getTeamList();
					that.isAdmin = uni.getStorageSync("apply-userinfo").admin;
				});
			});
		},
	},
};
</script>

<style lang="scss">
page {
	background: #ededed;
}
</style>
