<template>
	<view>
		<u-form :model="form" ref="uForm">
			<u-card :border="false" padding="30" box-shadow="0px 1px 10px rgba(0,0,0,0.2)" border-radius="20" :show-foot="false">
				<view slot="head">
					<view class="u-flex">
						<view style="width: 8rpx;height: 34rpx;background: #0292FD;margin-right:20rpx;"></view>
						选择合作品牌
					</view>
				</view>
				<view slot="body">
					<u-form-item prop="cooperation">
						<view style="margin-right: 10rpx;">
							<u-icon name="order" size="36"></u-icon>
						</view>
						<u-input placeholder="点击选择合作品牌" :disabled="true" @click="brandShow = true;" v-model="form.cooperation"></u-input>
						<view class="" slot="right">
							<u-icon name="arrow-right" color="#666666" size="36"></u-icon>
						</view>
					</u-form-item>
				</view>
			</u-card>
			<u-popup mode="bottom" v-model="brandShow">
				<view class="u-flex u-row-between u-padding-left-40 u-padding-right-40" style="height: 90rpx;" @click="show = false;">
					<view style="padding: 16rpx;font-size:30rpx" @click="brandShow=false">
						取消
					</view>
					<view style="padding: 16rpx;color:#0095FF;font-size:30rpx" @click="getCooperation">
						确认
					</view>
				</view>
				<u-line color="#e4e7ed"></u-line>
				<view class="content">
					<scroll-view scroll-y="true" style="height: 300rpx;">
						<view class="u-flex u-row-center">
							<u-checkbox-group :wrap="true" @change="checkboxGroupChange" style="display: flex;justify-content: center">
								<u-checkbox v-model="item.checked" v-for="(item, index) in brandList" :key="index" :name="item.brand" shape="circle">
									<view class="u-flex u-row-left" style="width: 150rpx;">
										{{item.brand}}
									</view>
								</u-checkbox>
							</u-checkbox-group>
						</view>
					</scroll-view>
				</view>
			</u-popup>
			<u-card v-if="querybrandList.length" :border="false" padding="30" box-shadow="0px 1px 10px rgba(0,0,0,0.2)" border-radius="20" :show-foot="false">
				<view slot="head">
					<view class="u-flex">
						<view style="width: 8rpx;height: 34rpx;background: #0292FD;margin-right:20rpx;"></view>
						已选合作品牌
					</view>
				</view>
				<view slot="body" style="display:flex;width:100%;flex-flow: wrap;justify-content: space-around;align-items: center;">
					<view v-for="(item,index) in querybrandList" :key="index" style="width: 250rpx;">
						<u-image width="250rpx" :src="item.logUrl" mode="widthFix"></u-image>
					</view>
				</view>
			</u-card>
			<u-card :border="false" padding="30" box-shadow="0px 1px 10px rgba(0,0,0,0.2)" border-radius="20" :show-foot="false"
			 v-for="(item, index) in checkedAgentlist" :key="index">
				<view slot="head">
					<view class="u-flex">
						<view style="width: 8rpx;height: 34rpx;background: #0292FD;margin-right:20rpx;"></view>
						{{item.brand}}合作品牌签约任务
					</view>
				</view>
				<view slot="body">
					<u-form-item>
						<view style="margin-right: 10rpx;">
							<u-icon name="account" size="36"></u-icon>
						</view>
						<view class="">
							{{item.name}}
						</view>
					</u-form-item>
					<u-form-item prop="taskNum">
						<view style="margin-right: 10rpx;">
							<u-icon name="order" size="36"></u-icon>
						</view>
						<u-radio-group v-model="item.taskNum" :disabled="item.disabledGroup" @change="changeRadiogroup(item)">
							<u-radio v-for="(i, index) in tasknameList" :key="index" :name="i.signLv">
								{{i.signLv}}
							</u-radio>
						</u-radio-group>
					</u-form-item>
				</view>

			</u-card>
			<u-form-item :border-bottom="false">
				<view style="margin:0 30rpx;">
					<u-checkbox-group>
						<u-checkbox v-model="protocol">同意《用户协议》及《隐私政策》</u-checkbox>
					</u-checkbox-group>
				</view>
			</u-form-item>
		</u-form>
		<view style="margin:0 30rpx;">
			<u-button @click="nextStep" type="primary" @tap="$u.debounce(submit, 2000,immediate = true)" :loading="loading"
			 :ripple="true" ripple-bg-color="#99d4ff">确认注册</u-button>
		</view>
	</view>
</template>

<script>
	import {
		request
	} from '../../common/request/request'
	require("promise.prototype.finally").shim()
	export default {
		data() {
			return {
				brandShow: false,
				loading: false,
				form: {
					cooperation: '',
				},
				formData: '',
				dataUrl: '',
				agentList: [], //经销商信息
				checkedAgentlist: [],
				taskList: [],
				tasknameList: [],
				brandAgentLvQueryList: [],
				rules: {
					cooperation: [{
						required: true,
						message: '请选择品牌',
						trigger: 'blur,change'
					}]
				},
				protocol: false,
				querybrandList:[],
				brandList:[]
			}
		},
		created() {
			uni.$on('formData', (data) => {
				this.formData = data.formData
				this.dataUrl = data.dataUrl
			})
		},
		mounted() {
			uni.showLoading({
				title: '加载中...'
			});
			this.getAgentArea()
			this.getTasknum()
		},
		methods: {
			getAgentArea() {
				console.log(this.formData)
				request({
					url: '/sailun/appStoreBasicInfo/getAgentArea',
					method: 'post',
					data:{
						"area": this.formData.district,
						"city": this.formData.city,
						"province": this.formData.province,
						// "area": "市北区",
						// "city": "青岛市",
						// "province": "山东省",
						"spart": "10",
						"vkorg": "8100",
						"vtweg": "10"
					}
				}).then(res => {
					if (res.data.code == 0) {
						this.agentList = res.data.Data.data
						this.agentList = this.agentList.reduce((res, item, index, array) => {
							for (let i = 0; i < array[index].regionBrandList.length; ++i) {
								res.push({
									brand: array[index].regionBrandList[i],
									brandCode: array[index].regionBrandList[i],
									name: item.name,
									kunnr: item.kunnr,
									agent_id: item.kunnr,
									taskNum: "",
									disabledGroup:false,
								})
							}
							return res;
						}, []);
						this.brandList=[...new Set(this.agentList.reduce((res, item, index, array) => {
							res.push(item.brand)
							return res;
						}, []))].reduce((res, item, index, array) => {
							res.push({brand:item})
							return res;
						}, []);
					}
					if (res.data.code == 500) {
						this.$u.toast(res.data.msg);
					}
					return this.agentList
				}).catch(err => {
					console.log(err)
				}).finally(() => {
					uni.hideLoading()
				})
			},
			getQuerybrand(){
				request({
					url: '/sailun/appStoreBasicInfo/queryBrand',
					method: 'get',
					data: {},
				}).then(res => {
					if (res.data.code == 0) {
					this.querybrandList=res.data.data
					this.querybrandList = this.querybrandList.filter(item => this.cooperations.indexOf(item.brandCode) > -1)
					}
					if (res.data.code == 500) {
						this.$u.toast(res.data.msg);
					}
				}).catch(err => {
					console.log(err)
				}).finally(() => {
				
				})
			},
			getTasknum() {
				request({
					url: '/sailun/appStoreBasicInfo/enum',
					method: 'get',
					data: {},
				}).then(res => {
					if (res.data.code == 0) {
						this.tasknameList = res.data.data
						this.tasknameList.push({
							brand: null,
							brandCode: null,
							mainId:null,
							numTask:null,
							signLv: "无"
						})
					}
					if (res.data.code == 500) {
						this.$u.toast(res.data.msg);
					}
				}).catch(err => {
					console.log(err)
				}).finally(() => {

				})
			},
			checkboxGroupChange(e) {
				this.cooperations = e
			},
			changeRadiogroup(item){
				if(item.taskNum=="无"){
					this.checkedAgentlist.filter(e=>(e.brand===item.brand&&e.agent_id!=item.agent_id)).forEach(e=>e.disabledGroup=false)
				}else{
					this.checkedAgentlist.filter(e=>(e.brand===item.brand&&e.agent_id!=item.agent_id)).forEach(e=>e.disabledGroup=true)
				}
			},
			getCooperation() {
				if (this.cooperations != null) {
					this.form.cooperation = this.cooperations.toString();
					this.checkedAgentlist = this.agentList.filter(item => this.cooperations.indexOf(item.brand) > -1)
					this.getQuerybrand()
					this.brandShow = false
				} else {
					console.log("至少选择一项")
				}

			},
			getTaskconfirm(e) {
				this.form.tasklist = e[0].label;
			},
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						if(!this.protocol)return this.$u.toast('请勾选协议');
						this.setData()
					} else {
						console.log('验证失败');
					}
				});
			},
			setData(){
				this.brandAgentLvQueryList = this.checkedAgentlist.reduce((res, item) => {
					res.push({
						agent_id: item.agent_id,
						agent_name:item.name,
						brand: item.brand,
						brandCode: item.brandCode,
						kunnr: item.kunnr,
						lvCount: parseInt(item.taskNum.substring(2)).toString(),
						showLv: item.taskNum,
						spart: "10",
						vkorg: "8100",
						vtweg: "10"
					})
					return res;
				}, []);
				let data = {
					"brandAgentLvQueryList":this.brandAgentLvQueryList,
					"city":this.formData.city,
					"contactName":this.formData.name,
					"district":this.formData.district,
					"jd":this.formData.longitude,
					"licenseUrl":this.dataUrl.licenseUrl,
					"mobileCode":this.formData.code,
					"province":this.formData.province,
					"storeImageUrl":this.dataUrl.storeUrl,
					"storeName":this.formData.store,
					"storePhone":this.formData.phone,
					"version":1.00,
					"wd":this.formData.latitude,
				}
				console.log(data)
				request({
					url: '/sailun/appStoreBasicInfo/storeReg',
					method: 'post',
					data: data
				}).then(res => {
					console.log(res)
					if (res.data.code == 0) {
						this.loading = true
						this.$u.toast(res.data.msg);
						setTimeout(() => {
							this.$u.route({
								url: 'pages/login/index',
							})
						}, 2000)
					}
					if (res.data.code == 500) {
						this.$u.toast(res.data.msg);
					}
				}).catch(err => {
					console.log(err)
				}).finally(() => {
					this.loading = false
				})
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		padding: 24rpx;
		text-align: center;

		.u-checkbox[data-v-54acf820] {
			display: flex;
			justify-content: center;
		}
	}
</style>
