<template>
	<div class="order-wrapper flex">
		<div class="l-wrap">
			<div v-if="tokenList.length == 0" class="dr-wrap hand">
				<i class="el-icon-document dr-icon"></i>
				<div class="dr-tit">点击批量导入账号文件</div>
				<input type="file" ref="fileInput" @change="readTxtFile('fileInput')" class="dr-int" />
			</div>
			<div v-else style="padding: 10px;">
				<div>账号列表</div>
				<el-scrollbar style="height:680px;" wrap-style="overflow-x:hidden; padding-top:10px;">
					<!-- <div class="flex" style="flex-direction: column;"> -->
					<div v-for="(item,index) in tokenList" class="left-t hand"
						:class="{'used': item.bl,'checked':item.id == token.id}" @click="chooseToken(item)">
						<span class="token-in">{{index+1}}、</span>{{item.token}}
					</div>
					<!-- </div> -->
				</el-scrollbar>
			</div>



		</div>
		<template v-if="token.id">
			<div class="center-wrap">
				当前token已下单数{{token.count}}
				<div class="tit">代付信息</div>
				<div>宝贝类型</div>
				<div>
					<el-checkbox v-model="xdForm.thby">退货包邮</el-checkbox>
					<el-checkbox v-model="xdForm.sxhmd">刷新黑名单</el-checkbox>
				</div>
				<div>
					<el-checkbox v-model="xdForm.wly">7天无理由退货</el-checkbox>
					<el-checkbox v-model="xdForm.pbsj">屏蔽同省商家</el-checkbox>
				</div>

				<div>浏览宝贝</div>
				<div>
					<el-input v-model="xdForm.lls" placeholder="" class="jige chang" type="number"></el-input> 秒 ~
					<el-input v-model="xdForm.lle" placeholder="" class="jige chang" type="number"></el-input> 秒
				</div>
				<div>浏览店铺</div>
				<div>
					浏览 <el-input v-model="xdForm.lds" placeholder="" class="jige chang" type="number"></el-input> 秒
					~ <el-input v-model="xdForm.lde" placeholder="" class="jige chang" type="number"></el-input> 秒
				</div>
				<div>
					货比 <el-input v-model="xdForm.hbmin" placeholder="" class="jige chang" type="number"></el-input> ~
					<el-input v-model="xdForm.hbmax" placeholder="" class="jige chang" type="number"></el-input> 家
				</div>
				<div>
					浏览 <el-input v-model="xdForm.hbs" placeholder="" class="jige chang" type="number"></el-input> ~
					<el-input v-model="xdForm.hbe" placeholder="" class="jige chang" type="number"></el-input>秒
				</div>
				<div>
					销量 <el-input v-model="xdForm.xl" placeholder="" class="jige chang" type="number"></el-input>
					单以下不下单
				</div>
				<div>
					价格区间 <el-input v-model="xdForm.minPrice" placeholder="" class="jige chang" type="number"
						min="10"></el-input>
					~ <el-input v-model="xdForm.maxPrice" placeholder="" class="jige chang" type="number"></el-input>
				</div>
				<div class="tit">下单方案</div>
				<div>
					下单方式 <el-select v-model="xdForm.xtype" placeholder="请选择">
						<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
						</el-option>
					</el-select>
				</div>
				<div>
					收藏选择 <el-radio v-model="xdForm.scxz" label="店铺">店铺</el-radio>
					<el-radio v-model="xdForm.scxz" label="商品">商品</el-radio>
				</div>
				<div>
					sku选择 <el-radio v-model="xdForm.sku" label="最低sku">最低sku</el-radio>
					<el-radio v-model="xdForm.sku" label="随机sku">随机sku</el-radio>
				</div>
				<div>
					<el-checkbox v-model="xdForm.sjxg">随机修改名称</el-checkbox>
				</div>
			</div>
			<div class="center-wrap" style="width: 600px;">
				<div class="tit">控制台</div>
				<div class="dialog-footer flex" style="width: 80%;justify-content: flex-start;align-items: center;">
					<div style="flex-shrink: 0;">下单数量</div>
					<el-select v-model="xdnum" placeholder="请选择" style="margin:0 10px;width: 100px;">
						<el-option v-for="item in optionsnum" :key="item.value" :label="item.label" :value="item.value">
						</el-option>
					</el-select>
					<!-- <el-button @click="shdzVisible = true" >地址信息</el-button> -->
					<el-button @click="openZd" v-if="!auto_order">自动下单</el-button>
					<el-button :type="(loading|| auto_order) ? 'info': 'primary'" @click="createOrder">下
						单</el-button>
				</div>
				<div class="tit">运行日志</div>
				<el-scrollbar style="height:520px" wrap-style="overflow-x:hidden;">
					<div class="flex tip-list">
						<div class="tip" v-for="item in tipList">
							{{item.tit}}
						</div>
					</div>
				</el-scrollbar>
			</div>
		</template>
		<template v-else>
			<div class="empty-token">
				<div class="empty-left">
					<el-steps direction="vertical" :active="actionStep">
						<el-step title="导入账号" description="1先导入账号"></el-step>
						<el-step title="选择账号" description="选择账号"></el-step>
						<el-step title="交易" description="选择商品开始交易"></el-step>
					</el-steps>
				</div>

				<el-empty :image-size="200" class="empty"
					:description="actionStep == 1?'请先导入数据再操作！':'请先选择账号再操作！'"></el-empty>
			</div>
		</template>
		<div>
			<div class="redr">
				<el-button>重新导入</el-button>
				<input type="file" ref="fileInputt" @change="readTxtFile('fileInputt')" class="dr-int" />
			</div>
		</div>
		<div style="display: none;">
			<div class="wrap">
				<!-- <div class="left-wrap">
					<div class="left-tit">token</div>
					<el-scrollbar style="height:100%" wrap-style="overflow-x:hidden;">
						<div class="flex" style="flex-direction: column;">
							<div v-for="item in tokenList" class="left-t hand"
								:class="{'used': item.bl,'checked':item.id == token.id}" @click="chooseToken(item)">
								{{item.token}}
							</div>
						</div>
					</el-scrollbar>
				</div> -->

				<div class="right-wrap">
					<div class="right-top">
						<el-form :model="form">
							<!-- <el-form-item label="当前token" :label-width="formLabelWidth">
								<el-input v-model="token.token" type="text" disabled></el-input>
							</el-form-item> -->
							<el-form-item label="最大下单数量" :label-width="formLabelWidth">
								<el-input v-model="baseInfo.token_day_count" type="number" disabled></el-input>
							</el-form-item>
							<el-form-item label="收货人" :label-width="formLabelWidth">
								<div class="flex"><el-input v-model="xdForm.consignee_name" type="text"
										:disabled="loading || auto_order"></el-input><el-button
										style="margin-left: 10px;" @click="sjAddress">随机</el-button></div>
							</el-form-item>
							<el-form-item label="联系方式" :label-width="formLabelWidth">
								<div class="flex"><el-input v-model="xdForm.phone" type="number"
										:disabled="loading || auto_order"></el-input><el-button
										style="margin-left: 10px;" @click="sjPhone">随机</el-button></div>
							</el-form-item>
							<el-form-item label="省市区" :label-width="formLabelWidth">
								<el-cascader size="large" :options="regionData" :props="{value: 'label'}"
									v-model="area">
								</el-cascader>
							</el-form-item>
							<el-form-item label="收货地址" :label-width="formLabelWidth">
								<el-input v-model="xdForm.address" type="text"></el-input>
							</el-form-item>
							<el-form-item label="邮编" :label-width="formLabelWidth">
								<el-input v-model="xdForm.zip" type="text"></el-input>
							</el-form-item>
						</el-form>
						<div class="dialog-footer flex" style="width: 80%;justify-content: flex-end;">

							<el-button @click="openZd" v-if="!auto_order">自动下单</el-button>


							<el-button :type="(loading|| auto_order) ? 'info': 'primary'" @click="createOrder">下
								单</el-button>
						</div>
					</div>
					<div class="right-btm">
						<el-scrollbar style="height:100%" wrap-style="overflow-x:hidden;">
							<div class="flex tip-list">
								<div class="tip" v-for="item in tipList">
									{{item.tit}}
								</div>
							</div>
						</el-scrollbar>
					</div>
				</div>
			</div>

		</div>
		<!-- 收货地址对话框 -->
		<el-dialog title="绑定收货地址" :visible.sync="shdzVisible" class="shdz" @close="closeShdz">
			<el-form :model="form">
				<el-form-item label="收货人" :label-width="formLabelWidth">
					<div class="flex">
						<el-input v-model="showname" type="text" :disabled="loading || auto_order" v-if="!xgname"
							disabled></el-input>
						<el-input v-model="xdForm.consignee_name" type="text" :disabled="loading || auto_order"
							v-if="xgname == true"></el-input>
						<el-button style="margin-left: 10px;" @click="xgname = true">修改</el-button>
						<el-button style="margin-left: 10px;" @click="sjName">随机</el-button>
					</div>
				</el-form-item>
				<el-form-item label="联系方式" :label-width="formLabelWidth">
					<div class="flex">
						<el-input v-model="xdForm.phone" type="number" :disabled="loading || auto_order" v-if="xgaddress"></el-input>
						<el-input v-model="showAddress" type="text" :disabled="loading || auto_order" v-else disabled></el-input>
						<el-button style="margin-left: 10px;" @click="xgaddress = true">随机</el-button>
						<el-button style="margin-left: 10px;" @click="sjPhone">随机</el-button>
					</div>
				</el-form-item>
				<el-form-item label="省市区" :label-width="formLabelWidth">
					<el-cascader size="large" :options="regionData" :props="{value: 'label'}" v-model="area">
					</el-cascader>
				</el-form-item>
				<el-form-item label="收货地址" :label-width="formLabelWidth">
					<el-input v-model="xdForm.address" type="text"></el-input>
				</el-form-item>
				<el-form-item label="邮编" :label-width="formLabelWidth">
					<el-input v-model="xdForm.zip" type="text"></el-input>
				</el-form-item>
			</el-form>
			<div style="text-align: center;color: red;padding-bottom: 10px;">
				修改信息会导致订单不予赔付。
			</div>
			<div class="dialog-footer flex" style="justify-content: flex-end;">
				<el-button @click="shdzVisible = false">取消</el-button>
				<el-button type="primary" @click="closeShdz()">创建</el-button>
			</div>
		</el-dialog>
		<!-- 安全验证对话框 -->
		<el-dialog title="拼多多安全验证" :visible.sync="dialogTableVisible" class="rjjy" @close="closeJy">
			<div>
				<div class="jy-wrap">
					<el-image style="width: 262px; height:175px" class="jy-img" :src="jyimg"
						@click="jyClick"></el-image>
					<div class="jy-hd" v-for="(item,index) in hdList"
						:style="{top: item.y ,left: item.x,width: hdw+ 'px',height: hdw + 'px'}">{{index+1}}</div>
					<div class="suaxin hand" @click="refresh">
						<img src="../../assets/img/sx.png" alt="" class="">
					</div>
				</div>
			</div>
		</el-dialog>
		<el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
			<span style="display: block; padding: 0 0 20px 20px;">开通自动下单需支付{{baseInfo.auto_order}}元</span>

			<el-form v-if="userInfo.is_binding == 1">
				<el-form-item label="动态口令" :label-width="formLabelWidth">
					<el-input v-model="dtkl" type="number"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="dialogVisible = false">取 消</el-button>
				<el-button type="primary" @click="ktAuto">确 定</el-button>
			</span>
		</el-dialog>
		<el-dialog title="提示" :visible.sync="showStop" width="30%">
			<span>您确定停止自动下单？</span>
			<span slot="footer" class="dialog-footer">
				<el-button @click="showStop = false">取 消</el-button>
				<el-button type="primary" @click="stopAuto">确 定</el-button>
			</span>
		</el-dialog>
		<div class="mask" v-if="auto_order || loading">
			<!-- el-icon-loading -->
			<template v-if="auto_order">
				<i class="el-icon-loading mask-loading"></i>
				<el-button @click="showStop = true" style="margin-top: 20px;" type="primary">停止自动下单</el-button>
			</template>

		</div>
	</div>
</template>

<script>
	import {
		regionData
	} from 'element-china-area-data'
	import {
		mapState,
		mapMutations
	} from 'vuex';

	import {
		createCode,
		getUserList,
		getIndex,
		getSpace,
		spaceCheck
	} from '@/request/agent.js'
	import {
		getRechangeList,
		rechange,
		getTokenList,
		createOrder,
		getUserInfo,
		payAuto,

	} from '@/request/user.js'
	import {
		showTime,
		sj,
		sjz
	} from '@/assets/js/tools.js'
	import city from '@/assets/js/cityData.js'
	import {
		imgs
	} from '@/assets/js/admun.js'
	import {
		set
	} from 'core-js/library/core/dict';
	export default {

		name: 'Order',
		data() {
			return {
				xgaddress: false,
				xgname: false,
				dqxdsy: 0,
				xdnum: 2,
				jyToken: [],
				errToken: [],
				unique_code: '',
				hdcount: 0,
				hdw: 20,
				hdList: [],
				jyimg: '',
				dtkl: '',
				orderInfo: '',
				area: ['甘肃省', '白银市', '白银区'],
				regionData,
				citys: city,
				options: [{
					value: '拼团',
					label: '拼团'
				}, {
					value: '开团',
					label: '开团'
				}],
				optionsnum: [{
					value: 1,
					label: 1
				}, {
					value: 2,
					label: 2
				}],
				showStop: false,
				dialogVisible: false,
				xdForm: {
					// 有用数据
					phone: '',
					address: '',
					minPrice: '10',
					maxPrice: '100',
					consignee_name: '',
					area: '',
					zip: '000000',
					// 虚假数据
					lls: '5',
					lle: '10',
					lds: '5',
					lde: '10',
					hbmin: 3,
					hbmax: 5,
					hbs: '5',
					hbe: '10',
					xl: '300',
					xtype: '拼团',
					scxz: '店铺',
					// 无效数据
					thby: true,
					sxhmd: true,
					wly: true,
					pbsj: true,
					sku: '最低sku',
					sjxg: true
				},
				imgs: imgs,
				msg: '888',
				loading: false,
				formLabelWidth: '100px',
				form: {
					recharge_num: '',
					image: ''
				},
				dialogTableVisible: false,
				xiadanDia: false, //下单弹窗
				shdzVisible: false, //收货地址弹窗
				queryInfo: {
					keyword: '',
					page: 1,
					limit: 10
				},
				list: [],
				total: 0,
				dataloading: false,
				headers: {},
				tokenList: [],
				token: {},
				ybToken: {},
				minPriceList: [],
				maxPriceList: [],
				tipList: [],
				auto_order: false, //是否启动自动下单
				pdarr: [{
						id: 1,
						tit: '账号正常开始任务'
					},
					{
						id: 2,
						tit: '模拟搜素翻页'
					},
					{
						id: 3,
						tit: '浏览宝贝'
					},
					{
						id: 4,
						tit: '浏览店铺'
					},
					{
						id: 5,
						tit: '选中商品'
					},
					{
						id: 6,
						tit: '收藏'
					},
					{
						id: 7,
						tit: '随机修改昵称'
					},
					{
						id: 8,
						tit: '正在选宝 sku，'
					},
					{
						id: 9,
						tit: '正在通信付款'
					},
					{
						id: 10,
						tit: '通信正常，正常付款'

					},
					{
						id: 11,
						tit: '付款成功商品名：'
					},
					{
						id: 12,
						tit: '完成订单'
					}
				]
			}
		},
		computed: {
			...mapState(['baseInfo', 'userInfo', 'addOrderToken']),
			showname() {
				return this.xdForm.consignee_name.substring(0, 1) + '**'
			},
			showAddress() {
				let str = this.xdForm.phone + ''
				let arr =str.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2");
				
				return arr
			},
			actionStep() {
				if (this.tokenList.length == 0) {
					return 1
				} else {
					return 2
				}
			}
		},
		created() {
			console.log(this.regionData, 'ddd');
			console.log();
			this.tokenList = this.addOrderToken
			// if(this.tokenList.length > 0) {
			// 	this.sjAddress()
			// 	this.sjPhone()
			// 	this.sjName()
			// 	this.chooseNext()
			// }
			this.getUserInfo()
			this.getIndex()
			this.headers['token'] = window.sessionStorage.getItem('token')
			console.log(this.imgs, 'imgs');
		},
		methods: {
			...mapMutations(['setBaseInfo', 'setUserInfo', 'jUserMoney', 'setAddOrderToken']),
			closeShdz() {
				let that = this
				// this.shdzVisible = true
				if (!that.shdzVisible) {
					that.shdzVisible = true
				}
				if (that.xdForm.consignee_name == '') {
					return that.$msg.error('请输入收货人')
				}
				if (that.xdForm.phone == '') {
					return that.$msg.error('请输入手机号码')
				}
				if (!/(^1[2|3|4|5|6|7|8|9][0-9]{9}$)/.test(that.xdForm.phone)) {
					return that.$msg.error('请输入正确的手机号码')
				}
				if (that.area == '') {
					return that.$msg.error('请选择省市区')
				}
				that.xdForm.area = that.area.join(',')
				if (that.xdForm.address == '') {
					return that.$msg.error('请输入收货地址')
				}
				if (that.xdForm.zip == '') {
					return that.$msg.error('请输入邮编')
				}
				this.xgaddress = false
				this.xgname = false
				this.shdzVisible = false
			},
			//加载token文件
			readTxtFile(refs) {
				let that = this
				const input = this.$refs[refs]
				const file = input.files[0]

				const reader = new FileReader()
				reader.onload = (e) => {
					let srt = e.target.result.replace(/\n\r/g, ',').replace(/\r\n/g, ',')
					let arr = srt.split(',').filter(item => item !== '')
					console.log(arr, 'arr');
					if (arr.length > 0) {
						let tokenList = srt.split(',')
						that.jyToken = []
						getTokenList({
							token: arr.join(',')
						}).then(res => {
							that.tokenList = res.data
							that.setAddOrderToken(that.tokenList)
							that.tokenList.forEach(item => {
								that.jyToken.push(item.token)
							})

							that.errToken = arr.filter(element => !that.jyToken.includes(element));

						})
					} else {
						that.$msg.error('请导入正确的账号文件')
					}

				}
				reader.readAsText(file)
			},
			jyClick(event) {
				let that = this
				if (that.hdList.length < that.hdcount) {
					let item = {
						x: event.offsetX - that.hdw / 2 + 'px',
						jyx: event.offsetX,
						y: event.offsetY - that.hdw / 2 + 'px',
						jyy: event.offsetY,
					}
					that.hdList.push(item)
					if (that.hdList.length == that.hdcount) {
						that.rjjy()
					}
				}
			},
			closeJy() {
				this.hdList = []
				this.hdcount = 0
				this.jyimg = ''
			},
			rjjy() {
				let that = this
				let str = ''
				that.hdList.forEach((item, index) => {
					console.log(item);
					str = str + item.jyx + ',' + item.jyy + ((index < that.hdcount - 1) ? '|' : '')
				})
				// console.log(str);
				spaceCheck({
					position: str,
					unique_code: that.unique_code
				}).then(res => {
					that.$msg.success('校验成功')
					that.token = this.ybToken
					that.dialogTableVisible = false
					that.sjAddress()
					that.sjPhone()
					that.sjName()
					if (that.auto_order) {

					} else {

						that.shdzVisible = true

					}
				}).catch(err => {
					console.log(err, 'daole');
					this.closeJy()
					getSpace().then(res => {
						console.log(res);
						that.hdcount = res.data.count
						that.jyimg = res.data.url1
						that.unique_code = res.data.unique_code
						that.dialogTableVisible = true
					})
				})
			},
			refresh() {
				let that = this
				this.hdList = []
				this.hdcount = 0
				getSpace().then(res => {
					console.log(res);
					that.hdcount = res.data.count
					that.jyimg = res.data.url1
					that.unique_code = res.data.unique_code
					that.dialogTableVisible = true
				})
			},
			handleChange(e) {
				console.log(e);
			},
			stopAuto() {
				this.auto_order = false
				this.showStop = false
			},
			getUserInfo() {
				getUserInfo().then(res => {
					this.setUserInfo(res.data)
				})
			},
			pd(arr, i) {
				let that = this
				let len = arr.length - 1
				console.log(i, '进入');
				if (i <= len) {
					switch (i) {
						// 账号正常
						case 0:
							that.tipList = []
							that.tipList.unshift({
								tit: '第' + that.token.count + '次下单【' + that.token.token + '-' + Date.now() + '】' +
									arr[i].tit
							})
							that.pd(arr, ++i)
							break;
							// 翻页 
						case 1:
							setTimeout(() => {
								that.tipList.unshift({
									tit: '第' + that.token.count + '次下单【' + that.token.token + '-' + Date
										.now() + '】' + arr[i].tit
								})
								that.pd(arr, ++i)
							}, 1000)
							break;
							//浏览宝贝
						case 2:
							setTimeout(() => {
								that.tipList.unshift({
									tit: '第' + that.token.count + '次下单【' + that.token.token + '-' + Date
										.now() + '】' + arr[i].tit
								})
								that.pd(arr, ++i)
							}, 1000)
							break;
							// 浏览店铺
						case 3:
							let len = sjz(that.xdForm.hbmin, that.xdForm.hbmax)

							function lldp(len, j = 0) {
								if (len == j) {
									that.pd(arr, ++i)
								} else {
									setTimeout(() => {
										that.tipList.unshift({
											tit: '第' + that.token.count + '次下单【' + that.token.token + '-' +
												Date
												.now() + '】' + '浏览' + (j + 1) + '家店铺'
										})
										lldp(len, ++j)
									}, sjz(that.xdForm.lls, that.xdForm.lle) * 1000)

								}
							}
							lldp(len, 0)
							// for(let j = 0;j<= len;j++) {
							// 	setTimeout(() => {
							// 		that.tipList.unshift({
							// 			tit: '第' + that.token.count + '次下单【' + that.token.token + '-' + Date
							// 				.now() + '】' + '浏览'+(j+1) +'家店铺'
							// 		})

							// 	}, sjz(that.xdForm.lls, that.xdForm.lle) * 1000)
							// }


							break;
							//选择商品
						case 4:
							console.log(4, i)
							setTimeout(() => {
								that.tipList.unshift({
									tit: '第' + that.token.count + '次下单【' + that.token.token + '-' + Date
										.now() + '】' + arr[i].tit
								})
								that.pd(arr, ++i)
							}, sjz(that.xdForm.lds, that.xdForm.lde) * 1000)
							break;
							//收藏店铺/商品
						case 5:
							setTimeout(() => {
								that.tipList.unshift({
									tit: '第' + that.token.count + '次下单【' + that.token.token + '-' + Date
										.now() + '】' + arr[i].tit + that.xdForm.scxz
								})
								that.pd(arr, ++i)
							}, sjz(that.xdForm.lds, that.xdForm.lde) * 1000)
							break;
							//随机修改昵
						case 6:
							setTimeout(() => {
								if (this.xdForm.sjxg) {
									that.tipList.unshift({
										tit: '第' + that.token.count + '次下单【' + that.token.token + '-' +
											Date
											.now() + '】' + arr[i].tit
									})
								}
								that.pd(arr, ++i)
							}, 1000)
							break;
							//sku
						case 7:
							setTimeout(() => {
								if (this.xdForm.sjxg) {
									that.tipList.unshift({
										tit: '第' + that.token.count + '次下单【' + that.token.token + '-' +
											Date
											.now() + '】' + arr[i].tit + that.xdForm.xtype
									})
								}
								that.pd(arr, ++i)
							}, 1000)
							break;
						case 8:
							setTimeout(() => {
								if (this.xdForm.sjxg) {
									that.tipList.unshift({
										tit: '第' + that.token.count + '次下单【' + that.token.token + '-' +
											Date
											.now() + '】' + arr[i].tit
									})
								}
								that.pd(arr, ++i)
							}, 2000)
							break;
						case 9:
							setTimeout(() => {
								if (this.xdForm.sjxg) {
									that.tipList.unshift({
										tit: '第' + that.token.count + '次下单【' + that.token.token + '-' +
											Date
											.now() + '】' + arr[i].tit
									})
								}
								that.pd(arr, ++i)
							}, 2000)
							break;
						case 10:
							setTimeout(() => {

								that.tipList.unshift({
									tit: '第' + that.token.count + '次下单【' + that.token.token + '-' +
										Date
										.now() + '】' + arr[i].tit + that.orderInfo.goods_name + '  商品价格：' +
										that.orderInfo.pay_price * 1 +
										' 手续费：' + that.orderInfo.commission
								})
								console.log(that.orderInfo.pay_price * 1 + that.orderInfo.commission * 1);
								that.jUserMoney(-(that.orderInfo.pay_price * 1 + that.orderInfo.commission * 1))
								that.pd(arr, ++i)
							}, 2000)
							break;
						case 11:
							setTimeout(() => {
								if (this.xdForm.sjxg) {
									that.tipList.unshift({
										tit: '第' + that.token.count + '次下单【' + that.token.token + '-' +
											Date
											.now() + '】' + arr[i].tit
									})
								}
								that.pd(arr, ++i)
							}, 2000)
							break;
						default:
							that.pd(arr, ++i)
							break;
					}

					// if (i == 0) {
					// 	that.tipList.unshift(arr[i])
					// 	that.pd(arr, ++i)
					// } else {
					// 	setTimeout(() => {
					// 		that.tipList.unshift(arr[i])
					// 		if (arr[i].title.indexOf('支付金额：') != -1) {
					// 			// let price = zfInfo.title.replace(/支付金额：/,'')*1
					// 			that.jUserMoney(-1 * arr[i].title.replace(/支付金额：/, ''))
					// 		}
					// 		that.pd(arr, ++i)
					// 	}, arr[i - 1].time * 1000)
					// }
				} else {
					// that.xdForm.phone = ''
					// that.xdForm.consignee_name = ''
					that.loading = false
					if (that.auto_order) {
						that.chooseNext()
					} else {
						if (that.dqxdsy > 0) {
							that.createOrder()
						} else {

						}
						// that.xdForm.minPrice = ''
						// that.xdForm.maxPrice = ''
					}
				}
			},
			ktAuto() {
				let that = this
				if (that.userInfo.money * 1 < that.baseInfo.auto_order * 1) {
					return that.$msg.error('您当前余额不足')
				}
				payAuto({
					code: that.dtkl
				}).then(res => {
					that.getUserInfo()
					that.dialogVisible = false
					that.auto_order = true
					that.chooseNext()
					//自动下单启动
				})
			},
			openZd() {
				let that = this
				if (that.loading) {
					return that.$msg.error('当前处于下单状态，请等待下单完成')
				}
				if (that.userInfo.auto_order == 0) {
					that.dialogVisible = true
				} else {
					//自动下单启动
					if (that.xdForm.lls == '') {
						return that.$msg.error('请输入浏览最小时间')
					}
					if (that.xdForm.lls < 5) {
						return that.$msg.error('浏览最小时间需大于5秒')
					}
					if (that.xdForm.lle == '') {
						return that.$msg.error('请输入浏览最大时间')
					}
					if (that.xdForm.lle*1 <= that.xdForm.lls*1) {
						return that.$msg.error('浏览最大时间应大于最小时间')
					}

					if (that.xdForm.lds == '') {
						return that.$msg.error('请输入浏览店铺最小时间')
					}
					if (that.xdForm.lds < 5) {
						return that.$msg.error('浏览店铺最小时间需大于5秒')
					}
					if (that.xdForm.lde == '') {
						return that.$msg.error('请输入浏览店铺最大时间')
					}
					if (that.xdForm.lde*1 <= that.xdForm.lds*1) {
						return that.$msg.error('浏览店铺最大时间应大于最小时间')
					}

					if (that.xdForm.hbmin == '') {
						return that.$msg.error('请输入浏览店铺最小数量')
					}
					if (that.xdForm.hbmax == '') {
						return that.$msg.error('请输入浏览店铺最大数量')
					}
					if (that.xdForm.hbmax*1 <= that.xdForm.hbmin*1) {
						return that.$msg.error('请输入浏览店铺最大数量应大于最小数量')
					}
					// if (that.xdForm.hb == '') {
					// 	return that.$msg.error('请输入比货店铺数量')
					// }
					if (that.xdForm.hbs == '') {
						return that.$msg.error('请输入商品比较最小时间')
					}
					if (that.xdForm.hbs < 5) {
						return that.$msg.error('商品比较时间最小需大于5秒')
					}
					if (that.xdForm.hbe == '') {
						return that.$msg.error('请输入商品比较最大时间')
					}

					if (that.xdForm.xl == '') {
						return that.$msg.error('请输入销量')
					}
					if (that.xdForm.minPrice == '') {
						return that.$msg.error('请选择最低价格')
					}

					if (that.xdForm.minPrice * 1 < 10) {
						return that.$msg.error('最低价格不能低于10')
					}
					if (that.xdForm.maxPrice == '') {
						return that.$msg.error('请选择最高价格')
					}


					// if (that.xdForm.minPrice * 1 > that.xdForm.maxPrice * 1) {
					// 	return that.$msg.error('最高价格需大于最低价格')
					// }

					if ((that.xdForm.maxPrice * 1 - that.xdForm.minPrice * 1) < 5) {
						return that.$msg.error('最高价格需大于最低价格5元起')
					}
					if (that.xdForm.xtype == '') {
						return that.$msg.error('请选择下单类型')
					}
					if (that.xdForm.consignee_name == '') {
						return that.$msg.error('请输入收货人')
					}
					if (that.xdForm.phone == '') {
						return that.$msg.error('请输入手机号码')
					}
					if (!/(^1[2|3|4|5|6|7|8|9][0-9]{9}$)/.test(that.xdForm.phone)) {
						return that.$msg.error('请输入正确的手机号码')
					}
					if (that.area == '') {
						return that.$msg.error('请选择省市区')
					}
					that.xdForm.area = that.area.join(',')
					console.log(that.xdForm.area, 'that.xdForm.area');
					if (that.xdForm.address == '') {
						return that.$msg.error('请输入收货地址')
					}
					if (that.xdForm.zip == '') {
						return that.$msg.error('请输入邮编')
					}
					that.auto_order = true
					that.chooseNext()


				}
			},
			//选择下一个可用token
			chooseNext() {
				let that = this
				// if()
				let token = that.tokenList.find(item => item.bl == false)
				if(token && token.id) {
					that.token = token
				}else {
					that.auto_order = false
				}
				
				if (that.auto_order) {
					if (that.token) {
						if (that.xdForm.lls == '') {
							return that.$msg.error('请输入浏览最小时间')
						}
						if (that.xdForm.lls < 5) {
							return that.$msg.error('浏览最小时间需大于5秒')
						}
						if (that.xdForm.lle == '') {
							return that.$msg.error('请输入浏览最大时间')
						}
						if (that.xdForm.lle*1 <= that.xdForm.lls*1) {
							return that.$msg.error('浏览最大时间应大于最小时间')
						}

						if (that.xdForm.lds == '') {
							return that.$msg.error('请输入浏览店铺最小时间')
						}
						if (that.xdForm.lds < 5) {
							return that.$msg.error('浏览店铺最小时间需大于5秒')
						}
						if (that.xdForm.lde == '') {
							return that.$msg.error('请输入浏览店铺最大时间')
						}
						if (that.xdForm.lde*1 <= that.xdForm.lds*1) {
							return that.$msg.error('浏览店铺最大时间应大于最小时间')
						}

						if (that.xdForm.hbmin == '') {
							return that.$msg.error('请输入浏览店铺最小数量')
						}
						if (that.xdForm.hbmax == '') {
							return that.$msg.error('请输入浏览店铺最大数量')
						}
						if (that.xdForm.hbmax*1 <= that.xdForm.hbmin*1) {
							return that.$msg.error('请输入浏览店铺最大数量应大于最小数量')
						}
						// if (that.xdForm.hb == '') {
						// 	return that.$msg.error('请输入比货店铺数量')
						// }
						if (that.xdForm.hbs == '') {
							return that.$msg.error('请输入商品比较最小时间')
						}
						if (that.xdForm.hbs < 5) {
							return that.$msg.error('商品比较时间最小需大于5秒')
						}
						if (that.xdForm.hbe == '') {
							return that.$msg.error('请输入商品比较最大时间')
						}

						if (that.xdForm.xl == '') {
							return that.$msg.error('请输入销量')
						}
						if (that.xdForm.minPrice == '') {
							return that.$msg.error('请选择最低价格')
						}

						if (that.xdForm.minPrice * 1 < 10) {
							return that.$msg.error('最低价格不能低于10')
						}
						if (that.xdForm.maxPrice == '') {
							return that.$msg.error('请选择最高价格')
						}


						// if (that.xdForm.minPrice * 1 > that.xdForm.maxPrice * 1) {
						// 	return that.$msg.error('最高价格需大于最低价格')
						// }

						if ((that.xdForm.maxPrice * 1 - that.xdForm.minPrice * 1) < 5) {
							return that.$msg.error('最高价格需大于最低价格5元起')
						}
						if (that.xdForm.xtype == '') {
							return that.$msg.error('请选择下单类型')
						}
						if (that.xdForm.consignee_name == '') {
							return that.$msg.error('请输入收货人')
						}
						if (that.xdForm.phone == '') {
							return that.$msg.error('请输入手机号码')
						}
						if (!/(^1[2|3|4|5|6|7|8|9][0-9]{9}$)/.test(that.xdForm.phone)) {
							return that.$msg.error('请输入正确的手机号码')
						}
						if (that.area == '') {
							return that.$msg.error('请选择省市区')
						}
						that.xdForm.area = that.area.join(',')
						console.log(that.xdForm.area, 'that.xdForm.area');
						if (that.xdForm.address == '') {
							return that.$msg.error('请输入收货地址')
						}
						if (that.xdForm.zip == '') {
							return that.$msg.error('请输入邮编')
						}
						that.sjAddress()
						that.sjPhone()
						that.sjName()
						// that.$msg.success('开启自动下单，请勿进行操作')
						// 启动
						that.createOrder()
					} else {
						//停止
						that.auto_order = false
					}

				}
			},
			createOrder() {
				let that = this
				console.log(that.xdForm.area);
				if (that.dqxdsy == 0) {
					that.dqxdsy = that.xdnum
				}
				if (that.loading) {
					return this.$msg.error('正在下单中，请等待下单完成')
				}
				if (that.token.bl) {
					if (that.auto_order) {

					} else {
						that.dqxdsy = 0
						return this.$msg.error('该TOKEN，当天使用次数已满!')
					}
				}
				if (that.xdForm.lls == '') {
					return that.$msg.error('请输入浏览最小时间')
				}
				if (that.xdForm.lls < 5) {
					return that.$msg.error('浏览最小时间需大于5秒')
				}
				if (that.xdForm.lle == '') {
					return that.$msg.error('请输入浏览最大时间')
				}
				if (that.xdForm.lle*1 <= that.xdForm.lls*1) {
					return that.$msg.error('浏览最大时间应大于最小时间')
				}

				if (that.xdForm.lds == '') {
					return that.$msg.error('请输入浏览店铺最小时间')
				}
				if (that.xdForm.lds < 5) {
					return that.$msg.error('浏览店铺最小时间需大于5秒')
				}
				if (that.xdForm.lde == '') {
					return that.$msg.error('请输入浏览店铺最大时间')
				}
				if (that.xdForm.lde*1 <= that.xdForm.lds*1) {
					return that.$msg.error('浏览店铺最大时间应大于最小时间')
				}

				if (that.xdForm.hbmin == '') {
					return that.$msg.error('请输入浏览店铺最小数量')
				}
				if (that.xdForm.hbmax == '') {
					return that.$msg.error('请输入浏览店铺最大数量')
				}
				if (that.xdForm.hbmax*1 <= that.xdForm.hbmin*1) {
					return that.$msg.error('请输入浏览店铺最大数量应大于最小数量')
				}
				// if (that.xdForm.hb == '') {
				// 	return that.$msg.error('请输入比货店铺数量')
				// }
				if (that.xdForm.hbs == '') {
					return that.$msg.error('请输入商品比较最小时间')
				}
				if (that.xdForm.hbs < 5) {
					return that.$msg.error('商品比较时间最小需大于5秒')
				}
				if (that.xdForm.hbe == '') {
					return that.$msg.error('请输入商品比较最大时间')
				}

				if (that.xdForm.xl == '') {
					return that.$msg.error('请输入销量')
				}
				if (that.xdForm.minPrice == '') {
					return that.$msg.error('请选择最低价格')
				}

				if (that.xdForm.minPrice * 1 < 10) {
					return that.$msg.error('最低价格不能低于10')
				}
				if (that.xdForm.maxPrice == '') {
					return that.$msg.error('请选择最高价格')
				}


				// if (that.xdForm.minPrice * 1 > that.xdForm.maxPrice * 1) {
				// 	return that.$msg.error('最高价格需大于最低价格')
				// }

				if ((that.xdForm.maxPrice * 1 - that.xdForm.minPrice * 1) < 5) {
					return that.$msg.error('最高价格需大于最低价格5元起')
				}
				if (that.xdForm.xtype == '') {
					return that.$msg.error('请选择下单类型')
				}
				if (that.xdForm.consignee_name == '') {
					return that.$msg.error('请输入收货人')
				}
				if (that.xdForm.phone == '') {
					return that.$msg.error('请输入手机号码')
				}
				if (!/(^1[2|3|4|5|6|7|8|9][0-9]{9}$)/.test(that.xdForm.phone)) {
					return that.$msg.error('请输入正确的手机号码')
				}
				if (that.area == '') {
					return that.$msg.error('请选择省市区')
				}
				that.xdForm.area = that.area.join(',')
				console.log(that.xdForm.area, 'that.xdForm.area');
				if (that.xdForm.address == '') {
					return that.$msg.error('请输入收货地址')
				}
				if (that.xdForm.zip == '') {
					return that.$msg.error('请输入邮编')
				}
				that.loading = true
				createOrder({
					min_price: that.xdForm.minPrice,
					max_price: that.xdForm.maxPrice,
					address: that.xdForm.address,
					phone: that.xdForm.phone,
					token: that.token.token,
					consignee_name: that.xdForm.consignee_name,
					area: that.xdForm.area,
					zip: that.xdForm.zip,
				}).then(res => {
					--that.dqxdsy
					//更新token
					that.token.count++
					if (that.token.count == 2) {
						that.token.bl = true
					}

					// 加载tip
					that.orderInfo = res.data
					that.pd(that.pdarr, 0)

				}).catch(err => {
					console.log(err);
					that.loading = false
					that.auto_order = false
					that.dqxdsy = 0

				})
			},
			getIndex() {
				getIndex().then(res => {
					this.setBaseInfo(res.data)
					let arr = [],
						arr2 = []
					res.data.price_min.forEach(item => {
						let a = {
							value: item * 1,
							label: item
						}
						arr.push(a)
					})
					res.data.price_max.forEach(item => {
						let a = {
							value: item * 1,
							label: item
						}
						arr2.push(a)
					})
					this.minPriceList = arr
					this.maxPriceList = arr2
				})
			},
			showTime,
			sjPhone() {
				if (this.loading) {
					return
				}
				this.xdForm.phone = sj(this.baseInfo.sys_phone)
			},
			sjName() {
				if (this.loading) {
					return
				}
				this.xdForm.consignee_name = sj(this.baseInfo.sys_name)
			},
			sjAddress() {
				if (this.loading) {
					return
				}
				this.xdForm.address = sj(this.baseInfo.system_address)
			},
			closeXd() {
				this.xiadanDia = false
				this.xdForm.phone = ''
				this.xdForm.address = ''
			},
			onSuccess() {
				console.log('success');
				this.token = this.ybToken
				this.tipList = []
				this.dialogTableVisible = false
			},
			onFail() {
				console.log('fail');
			},
			onRefresh() {
				console.log('refresh');
			},
			getTokenList() {
				getTokenList().then(res => {
					this.tokenList = res.data.map(item => {
						item.count = item.count * 1
						return item
					})
					// 获取第一次选择的token
					this.chooseNext()
					console.log(this.token, 'this.token');
				})
			},
			chooseToken(item) {
				// console.log(item,'ddddd');
				if (this.loading) {
					return this.$msg.error('正在下单中，请等待下单完成')
				}
				if (item.id == this.token.id) {
					return
				}
				if (item.bl) {
					return this.$msg.error('该TOKEN，当天使用次数已满!')
				}

				this.ybToken = item
				getSpace().then(res => {
					console.log(res);
					this.hdcount = res.data.count
					this.jyimg = res.data.url1
					this.unique_code = res.data.unique_code
					this.dialogTableVisible = true
				})
				// this.dialogTableVisible = true
			},
			handleAvatarSuccess(res, file) {
				this.form.image = res.data.fullurl;
			},
			beforeAvatarUpload(file) {
				// const isJPG = file.type === 'image/jpeg/png';
				// const isLt2M = file.size / 1024 / 1024 < 2;

				// if (!isJPG) {
				// 	this.$message.error('上传头像图片只能是 JPG 格式!');
				// }
				// if (!isLt2M) {
				// 	this.$message.error('上传头像图片大小不能超过 2MB!');
				// }
				// return isJPG && isLt2M;
				return true
			},
			handleAvatarError() {

			},
			getList(type) {
				let that = this
				if (type == 'reload') {
					that.queryInfo.page = 1
					that.dataloading = false
				}
				if (that.dataloading) {
					return
				}

				that.dataloading = true
				getRechangeList(that.queryInfo).then(res => {
					that.total = res.data.count
					that.list = res.data.data
					that.dataloading = false
				}).catch(err => {
					that.dataloading = false
				})
			},
			handleSizeChange(newPageSize) {
				this.queryInfo.limit = newPageSize
				this.getList()
			},
			handleCurrentChange(newPageNum) {
				this.queryInfo.page = newPageNum
				this.getList()
			},
		},

	}
</script>

<style lang="scss" scoped>
	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
		border: 1px dashed #d9d9d9;
		border-radius: 10px;
	}

	.left-t {
		// display: flex;
		// justify-content: center;
		font-weight: bold;
		// height: 50px;
		width: 180px; //定宽 
		overflow: hidden; //超出的文本隐藏
		text-overflow: ellipsis; //溢出用省略号显示
		white-space: nowrap; // 默认不换行；
	}

	.token-in {
		display: inline-block;
		padding: 5px 5px 5px 0;
		color: #000;
		// background-color: #314659;
		margin: 0 10px;
		border-radius: 50%;
	}

	.used {
		color: #a6a9ad;
	}

	.checked {
		color: #67c23a !important;
	}

	.center-wrap {
		font-size: 14px;
		// width: 33%;
		width: 400px;
		height: 720px;
		// flex-shrink: 0;
		// margin-left: 40px;
		background-color: #fff;
		// margin: 0 25px;
		padding: 20px;

		.tit {
			font-size: 16px;
			font-weight: bold;
			border-bottom: 2px solid #eaedf2;
			margin: 10px 0;
			padding: 10px 0;
		}

		div {
			padding: 5px 0;

			/deep/ .el-input__inner {
				height: 30px;
			}
		}

		.jige {
			width: 50px;
		}

		.chang {
			width: 100px;
		}
	}

	.wrap {
		overflow: hidden;
		display: flex;
		width: 100%;
		height: 700px;
		min-height: 100%;

		.left-wrap {
			width: 20%;
			background-color: #fff;
			color: rgb(15, 131, 254);

			.left-tit {
				text-align: center;
				padding: 20px;
				font-size: 18px;
				font-weight: bold;
			}
		}



		.right-wrap {
			flex-grow: 1;
			display: flex;
			flex-direction: column;
			padding-left: 40px;

			// width: 100%;
			// background-color: gray;
			.right-top {
				width: 100%;
				flex-shrink: 0;
				// height: 30%;
				// background-color: red;

			}

			.right-btm {
				// flex-grow: 1;
				height: 288px;
				padding-top: 20px;


			}
		}
	}

	.tip-list {
		flex-direction: column;
		justify-content: flex-start;

		.tip {
			width: 80%;
			// height: 40px;
			border-radius: 10px;
			// background-color: #67c23a;
			// text-align: center;
			margin: 5px 0;
			// line-height: 40px;
			color: #000;
			padding-bottom: 10px;
		}
	}

	.shdz {
		/deep/ .el-dialog {
			width: 520px;
		}
	}

	.rjjy {

		// width: 600px;
		/deep/ .el-dialog {
			width: 400px;
		}

		.jy-wrap {
			width: 262px;
			height: 175px;
			margin: auto;
			position: relative;

			.jy-img {
				position: relative;
			}

			.jy-hd {
				position: absolute;
				background-color: red;
				border-radius: 50%;
				display: flex;
				justify-content: center;
				align-items: center;
				color: #fff;
				font-size: 14px;
			}

			.suaxin {
				background-color: rgba(0, 0, 0, 0.6);
				display: flex;
				justify-content: center;
				align-items: center;
				width: 30px;
				height: 30px;
				position: absolute;
				right: 10px;
				bottom: 35px;
				border-radius: 50%;

				img {
					width: 18px;
					height: 18px;
				}
			}
		}
	}

	.mask {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		.mask-loading {
			font-size: 100px;
			color: #409eff;
		}
	}

	.order-wrapper {
		justify-content: space-between;
	}

	.l-wrap {
		width: 200px;
		height: 720px;
		background-color: #fff;
		position: relative;

		.dr-wrap {
			width: 140px;
			height: 125px;
			position: absolute;
			top: 0;
			left: 0;
			right: 0;
			bottom: 0;
			margin: auto;
			border: 1px #999 dashed;
			border-radius: 5px;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			font-size: 12px;

			.dr-icon {
				font-size: 32px;
				color: #007aff;
			}

			.dr-tit {
				margin-top: 20px;
			}

			// .dr-int {
			// 	width: 140px;
			// 	height: 125px;
			// }
		}
	}

	.dr-int {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		outline: medium none;
		cursor: pointer;
		-moz-opacity: 0;
		opacity: 0;
		filter: alpha(opacity=0);
	}

	.redr {
		position: relative;
	}

	.empty-token {
		width: 1040px;
		position: relative;

		.empty-left {
			height: 200px;
			position: absolute;
			top: 0;
			bottom: 0;
			margin: auto;
		}

		.empty {
			width: 100%;
			height: 100%;
		}
	}
</style>