<template>
	<view class="p-person-myOrder g_bg_f_5 g_pt_10 g_kuaishou">
		<!-- {
					icon: '',
					label: '发单账号名称',
					result: '',
					value: info.agencyName,
					path: '',
					tip: 'slot-name',
					placeholder: '请填写企业简称',
					type: 'slot',
					pColumn: 12,
					fontSize: '16px',
				},
        		{
					icon: '',
					label: '企业全称',
					result: '',
					value: info.fullName,
					path: '',
					tip: 'slot-name',
					placeholder: '请输入企业全称',
					fontSize: '16px',
					type: 'slot',
					pColumn: 12,
					require: false,
				}, -->
		<g-panel-form-slot
			class="g_mb_10"
			:list="[
				{
					icon: '',
					label: '账号名称',
					result: info.agencyName || '-',
					path: '/root/person/change?type=order&title=修改账号名称&place=请输入账号名称&remark=账号名称&value=' + info.agencyName,
					tip: 'agencyName',
				},
				{
					icon: '',
					label: '发单账号头像',
					result: '',
					value: info.logo,
					path: '',
					tip: 'update-avatar',
					type: 'slot',
					fontSize: '16px',
					pColumn: 12,
				},
				{
					icon: '',
					label: '发单账号简介',
					result: '',
					value: info.desp,
					path: '/root/person/inputAccountInfo',
					tip: 'desp',
					placeholder: '请简单介绍发单号',
					type: '',
					result: info.desp,
					fontSize: '16px',
					fontColor: info.desp == '请简单介绍发单号' ? 'g_c_9' : 'g_c_3',
					pColumn: 18,
				},

				{
					icon: '',
					label: '企业全称',
					result: info.fullName || '-',
					path: '/root/person/change?type=order&title=修改企业全称&place=请输入企业全称&remark=企业全称&value=' + info.fullName,
					tip: 'fullName',
				},
				{
					icon: '',
					label: '企业地址',
					result: '',
					value: info.address,
					path: '',
					tip: 'slot-choose-address',
					placeholder: '请选择企业地址',
					type: 'slot',
					fontSize: '16px',
					pColumn: 20,
					require: false,
					suffix: 'icon-dizhi1',
				},
			]"
			@changeName="changeName"
			@changeMoreWord="changeMoreWord"
			@updateImg="updateImg"
			@chooseAddress="chooseAddress"
			@clickResult="toDesp"
		>
		</g-panel-form-slot>
		<!-- <view class="g_mt_90">
			<g-button type="primary" @clickBtn="commitApply" btnText="修改"></g-button>
		</view> -->
	</view>
</template>

<script>
export default {
	onReady() {
		this.G.setNavStyle();
	},
	onShareAppMessage() {
		return this.G.shareFun();
	},
	data() {
		return {
			info: {
				userName: "",
				imgs: {},
				newImg: [],
				addressDetail: {},
				desp: "请简单介绍发单号",
			},
			loginInfo: {},
		};
	},
	onLoad(options) {
		let that = this;
		that.getTobeSupplierInfo();

		console.log("获取老乡信息：", options);
		uni.$on("changeDesp", function (data) {
			console.log(data.info);
			console.log("监听到事件来自 update ，携带参数 msg 为：" + data.info);
			that.info.desp = data.info;
			let info = {
				fieldName: "desp",
				fieldValue: that.info.desp,
			};
			setTimeout(() => {
				that.editField(info);
			}, 100);
			that.$forceUpdate();
		});
		uni.$on("changeAccountName", function (data) {
			console.log(data.info);
			console.log("监听到事件来自 update ，携带参数 msg 为：" + data.info);
			that.info.agencyName = data.info;
			let info = {
				name: that.info.agencyName,
			};
			setTimeout(() => {
				that.editName(info);
			}, 100);
			that.$forceUpdate();
		});
		uni.$on("changeStoreName", function (data) {
			console.log(data.info);
			console.log("监听到事件来自 update ，携带参数 msg 为：" + data.info);
			that.info.fullName = data.info;
			let info = {
				fieldName: "fullName",
				fieldValue: that.info.fullName,
			};
			setTimeout(() => {
				that.editField(info);
			}, 100);
			that.$forceUpdate();
		});
		// 河南省郑州市管城回族区商都路街道升龙广场2号楼B座
	},
	onShow() {
		let that = this;
		that.loginInfo = uni.getStorageSync("apply-userinfo");

		// that.info.desp = uni.getStorageSync("agencyDesp") || "请简单介绍发单号";
		// this.$forceUpdate();
	},
	onUnload() {
		uni.removeStorageSync("agencyDesp");
	},
	methods: {
		getTobeSupplierInfo() {
			let that = this;
			console.log("getTobeSupplierInfo");
			that.G.Get(that.api.user_getTobeSupplierInfo, "", (res) => {
				console.log(res.agency);
				let agency = res.agency;
				that.info.recordId = agency.id;
				that.info.fullName = agency.fullName;
				that.info.address = agency.address;
				that.info.lng = agency.lng;
				that.info.lat = agency.lat;
				that.info.logo = agency.logo;
				that.info.agencyName = agency.agencyName;
				that.info.desp = agency.desp || "请简单介绍发单号";
				that.$forceUpdate();
			});
		},
		changeName(e, item) {
			console.log("e", e);
			console.log("item", item);
			if (item.label == "发单账号名称") {
				this.info.agencyName = e;
				let info = {
					name: this.info.agencyName,
				};
				if (info.name.length > 10) {
					uni.showToast({
						title: "账号名称最多10个字",
						icon: "none",
					});
					return;
				}
				this.editName(info);
			} else if (item.label == "申请人名称") {
				this.info.userName = e;
			} else if (item.label == "企业全称") {
				this.info.fullName = e;
				let info = {
					fieldName: "fullName",
					fieldValue: this.info.fullName,
				};
				this.editField(info);
			}
		},
		toDesp(e) {
			uni.navigateTo({
				url: e.path + "?val=" + this.info.desp,
			});
		},
		changeTel(e) {
			console.log("e", e);
			this.info.tel = e;
		},
		changeMoreWord(e) {
			if (e.label == "简介") {
				this.info.desp = e.value;
				let info = {
					fieldName: "desp",
					fieldValue: this.info.desp,
				};
				this.editField(info);
			} else if (e.label == "备注") {
				// this.info.desp = e.value;
			}
		},
		chooseAddress(e) {
			console.log("e", e);
			// this.info.addressDetail = e;
			// this.info.district = e.district;

			let info = {};
			info.address = e.address;
			// info.address = '河南省郑州市管城回族区商都路街道升龙广场2号楼B座';
			info.lng = e.lng;
			info.lat = e.lat;
			this.editAddress(info);
			// this.$forceUpdate();
		},
		updateImg(e) {
			this.info.logo = e;
			let info = {
				fieldName: "logo",
				fieldValue: this.info.logo,
			};
			this.editField(info);
			// this.$forceUpdate();
		},
		editNickname() {
			let obj = {};
			uni.navigateTo({
				url: `/root/person/change?title=设置用户名&value=${this.loginInfo.userName}`,
			});
		},
		editAddress(info) {
			let that = this;
			// that.G.handleConfirm(
			// 	"是否确认修改该信息",
			// 	() => {
			that.G.Put(that.api.user_changeTobeSupplierAddress, info, (res) => {
				that.showSuccess();
			});
			// 	},
			// 	() => {}
			// );
		},
		editName(info) {
			let that = this;
			console.log(info);
			// that.G.handleConfirm(
			// 	"是否确认修改该信息",
			// 	() => {
			that.G.Put(that.api.user_changeTobeSupplierName, info, (res) => {
				console.log("res", res);
				that.showSuccess();
			});
			// 	},
			// 	() => {}
			// );
		},
		editField(info) {
			let that = this;
			console.log(info);
			// that.G.handleConfirm(
			// 	"是否确认修改该信息",
			// 	() => {
			console.log(info);
			that.G.Put(that.api.user_changeTobeSupplierField, info, (res) => {
				that.showSuccess();
			});
			// 	},
			// 	() => {}
			// );
		},
		showSuccess() {
			uni.showToast({
				title: "修改成功",
				icon: "none",
			});
			this.getTobeSupplierInfo();
		},
		commitApply() {
			console.log("this.info", this.info);
			let that = this;
			// uni.navigateTo({
			// 	url: "/root/person/creatSuccess",
			// });
			// return;
			console.log(that.info);
			if (!that.info.agencyName) {
				wx.showToast({
					title: "请输入发单号名称",
					icon: "none",
				});
				return;
			}
			if (!that.info.logo) {
				wx.showToast({
					title: "请设置发单号的头像",
					icon: "none",
				});
				return;
			}
			let info = {
				recordId: that.info.recordId,
				fullName: that.info.fullName || "",
				address: that.info.address || "",
				lng: that.info.lng || "",
				lat: that.info.lat || "",
				logo: that.info.logo,
				agencyName: that.info.agencyName,
				desp: that.info.desp || "",
			};
			console.log("info", info);
			// uni.navigateTo({
			// 	url: '/root/person/creatSuccess'
			// })
			// return
			that.G.Post(that.api.user_changeTobeSupplierInfo, info, (res) => {
				uni.navigateBack({
					delta: 1,
				});
			});

			// uni.showToast({
			// 	title: '模拟提交'
			// })
		},
	},
};
</script>

<style>
.p-person-myOrder {
	min-height: 100%;
}
</style>
