<template>
	<view class="flex-col padding">
		<view class="response flex-col align-center">



			<view class="response flex-row justify-between bg-white margin-top padding">
				<view class="flex-row align-center">
					<text class="text-red margin-right-xs">*</text>
					<text class="text-black">经销商名称</text>
				</view>
				<view class="w250">
					<u--input inputAlign="right" placeholder="请输入经销商名称" border="none"
						v-model.trim="postData.dealersName"></u--input>
				</view>
			</view>
			<view class="response flex-row justify-between bg-white margin-top padding">
				<view class="flex-row align-center">
					<text class="text-red margin-right-xs">*</text>
					<text class="text-black">经销商地址</text>
				</view>
				<view class="w250">
					<u--input inputAlign="right" placeholder="请输入经销商地址" border="none"
						v-model.trim="postData.dealersAddress"></u--input>
				</view>
			</view>

			<view class="response flex-col justify-between bg-white margin-top padding">
				<view class="flex-row align-center">
					<text class="text-red margin-right-xs"></text>
					<text class="text-black">上传营业执照（如果您不想进行认证，可不上传）</text>
				</view>
				<view class="w700 flex-row align-center margin-top">
					<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" accept='image'
						:previewFullImage="fileList1.length>0" :maxCount="1" width="120" height="120"
						maxSize="10485760"></u-upload>
				</view>
			</view>


			<view class="response margin-top">
				<u-button text="提交" :customStyle="{color:'#8a6e4b'}" @click="addWx" throttleTime="3000"
					:disabled="limitNum <= 0 && role != 'dealers'"
					color="linear-gradient(to right, rgb(244, 230, 186), rgb(252, 236, 210))"></u-button>
			</view>
		</view>
	</view>

</template>
<script>
	import {
		getToken
	} from "@/utils/auth.js"
	export default {
		data() {
			return {
				baseUrl: getApp().globalData.config.baseUrl,
				fileList1: [],

				postData: {
					dealersName: null,
					dealersPic: null,
					dealersAddress: null
				},
				user: {},
			};
		},
		onLoad() {
			this.getConfig();
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this.postData.dealersPic = null
				this[`fileList${event.name}`].splice(event.index, 1)
				console.log(this.postData);
			},



			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
				// console.log(this.fileList1);
			},

			// 上传图片
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.baseUrl + '/common/uploadImg',
						headers: {
							Authorization: "Bearer " + getToken(),
						},
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							try {
								let img = JSON.parse(res.data);
								console.log(111, img);

								this.postData.dealersPic = img.fileName
							} catch (e) {
								//TODO handle the exception
							}
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},

			//提交数据
			async addWx() {
				if (uni.$u.test.isEmpty(this.postData.dealersName)) {
					return this.$modal.msgError('请输入名称')
				}
				if (uni.$u.test.isEmpty(this.postData.dealersAddress)) {
					return this.$modal.msgError('请输入地址')
				}
				console.log(this.postData);

				const res = await this.$request({
					method: 'PUT',
					url: '/system/user/profile/updateDealersInfo',
					data: this.postData
				})

				this.$modal.msgSuccess('提交成功')
				setTimeout(() => {
					this.$tab.navigateBack()
				}, 2500)
			},
			// 获取配置:用户发布次数
			async getConfig() {
				const user = await this.$request({
					url: '/getAppInfo',
				})
				if (user.code == 200 && user.hasOwnProperty('user')) {
					this.user = user.user
					console.log(this.user);

					const {
						dealersName,
						dealersPic,
						dealersAddress
					} = user.user;
					this.postData.dealersName = dealersName;

					this.postData.dealersAddress = dealersAddress;
					if (dealersPic) {
						this.fileList1.push({
							url: this.baseUrl + dealersPic
						})
						this.postData.dealersPic = this.baseUrl + dealersPic;
					}

				}
			}


		},
	};
</script>
<style>
</style>