<template>
	<view style="padding-top: 1rpx;">
		<u--form labelPosition="left" :labelStyle="{color: '#fd4b09'}" :model="form" ref="form" 
		labelWidth="180rpx"
			style="margin-top: 10rpx;padding-bottom: 130rpx;">
			<view style="width: calc(96%);margin: 0 auto;background-color: #fff;border-radius: 20rpx;">
				<u-cell-group :border="false">
					<u-cell :border="false" center title="基础信息" arrow-direction="down">
						<view slot="icon" style="width: 10rpx;height: 35rpx;background-color: #fd4b09;"></view>
					</u-cell>
				</u-cell-group>
				<view style="padding: 0 10rpx;">
					<u-form-item leftIcon="account" :leftIconStyle="{color: '#FD4B09',fontSize:'40rpx'}" 
					label="客户名称" prop="customerName" borderBottom @click="selectCustomer">
						<u-input v-model="form.customerName" inputAlign="right" border="none"
						 placeholder="请选择客户名称" disabled
							disabledColor="#ffffff" />
					</u-form-item>
					<u-form-item leftIcon="account" :leftIconStyle="{color: '#FD4B09',fontSize:'40rpx'}" 
					 label="联系人" prop="contacts" borderBottom @click="filterAll(['contacts','contacts'])">
						<u-input v-model="form.contacts" inputAlign="right" border="none" placeholder="请选择"
							disabled disabledColor="#ffffff">
						</u-input>
					</u-form-item>
					<u-form-item leftIcon="phone" :leftIconStyle="{color: '#FD4B09',fontSize:'40rpx'}" 
					label="电话" prop="phone" borderBottom>
						<u-input v-model="form.phone" inputAlign="right" border="none" placeholder="请选择"
							:disabled="form.generateTask != '未生成'" disabledColor="#ffffff">
						</u-input>
					</u-form-item>
					<u-form-item leftIcon="map" :leftIconStyle="{color: '#FD4B09',fontSize:'40rpx'}" 
					label="送货地址" prop="recAddress" borderBottom>
						<view style="width: 100%;text-align:right;padding-right: 10rpx;" 
						v-if="form.generateTask != '未生成'" >{{form.recAddress}}</view>
						<u--textarea v-else v-model="form.recAddress" autoHeight
						inputAlign="right" border="none" placeholder="请选择"
							:disabled="form.generateTask != '未生成'" disabledColor="#ffffff" >
							<!-- <template slot="suffix">
								<u-icon name="map" color="#fd4b09" @tap="getMap"></u-icon>
							</template> -->
						</u--textarea>
					</u-form-item>
					<u-form-item leftIcon="account" :leftIconStyle="{color: '#FD4B09',fontSize:'40rpx'}"
					label="业务员" prop="salerName" borderBottom
					 @click="filterAll(['salerName','salerId'])">
						<u-input v-model="form.salerName" border="none" 
						inputAlign="right" placeholder="请选择业务员" disabled
							disabledColor="#ffffff" />
					</u-form-item>
					<u-form-item leftIcon="calendar" :leftIconStyle="{color: '#FD4B09',fontSize:'40rpx'}"
					label="日期" prop="businesDate" borderBottom
						@click="filterAll(['businesDate','businesDate'])">
						<u-input v-model="form.businesDate" inputAlign="right" 
						border="none" placeholder="请选择日期" disabled
							disabledColor="#ffffff" />
					</u-form-item>
					<u-form-item leftIcon="home" :leftIconStyle="{color: '#FD4B09',fontSize:'40rpx'}"
					label="发货仓库" prop="storageName" borderBottom
					 @click="filterAll(['storageName','storageId'])">
						<u-input v-model="form.storageName" border="none" 
						inputAlign="right" placeholder="请选择发货仓库" disabled
							disabledColor="#ffffff" />
					</u-form-item>
					<u-form-item leftIcon="car" :leftIconStyle="{color: '#FD4B09',fontSize:'40rpx'}"
					label="配送方式" prop="cname" borderBottom
						@click="filterAll(['shipType','shipType'])">
						<u-input v-model="form.shipType" inputAlign="right" border="none" 
						placeholder="请选择配送方式" disabled
							disabledColor="#ffffff" />
					</u-form-item>
					<u-form-item leftIcon="home" :leftIconStyle="{color: '#FD4B09',fontSize:'40rpx'}"
					label="物流公司" prop="cname" borderBottom
						@click="filterAll(['logisticsCorpName','logisticsCorpId'])">
						<u-input v-model="form.logisticsCorpName" inputAlign="right" border="none" 
						placeholder="请选择物流公司" disabled
							disabledColor="#ffffff" />
					</u-form-item>
					<u-form-item leftIcon="file-text" :leftIconStyle="{color: '#FD4B09',fontSize:'40rpx'}"
					label="物流单号" prop="cname" borderBottom >
						<u-input v-model="form.expressNo" inputAlign="right" border="none" 
						placeholder="请输入物流单号" :disabled="form.generateTask != '未生成'"
							disabledColor="#ffffff" />
					</u-form-item>
					
				</view>
			</view>
			
			<view style="width: 95%;margin: 10rpx auto;">
				<view style="display: flex;align-items: center;justify-content: space-between;background: #fff;border-radius: 12rpx; padding: 20rpx;">
					<view style="font-size: 30rpx;color: #fd4b09;">产品列表{{form.orderItemsList.length}}</view>
					<view v-if="form.generateTask == '未生成'">
						<u-button type="primary" text="添加产品" shape="circle" size="small" color="#FD4B09"
						 @click="chanpintiao()"></u-button>
					</view>
				</view>
				<view style="position: relative;background: #fff;padding: 20rpx;margin-top: 20rpx;border-box;font-size: 32rpx;" 
				v-for="(item,index) in form.orderItemsList" :key="index" @click="tanchuangxgai(item,index)">
					<view v-if="form.generateTask == '未生成'" class="orderItemsDelent" 
					@click="orderItemsDelent(item.id,index)">删除</view>	
					<view>
						<u-row>
							<u-col span="3">
								<u--image :showLoading="true"
								:src="item.url?item.url:'../../../static/images/404.png'"
									width="80px"
									height="80px" radius="10">
								</u--image>
							</u-col>
							<u-col span="8">
								<!-- style="font-size: 36rpx;width: 420rpx;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;" -->
								<view
									style="font-size: 36rpx;">
									{{item.goodsName}}
								</view>
								<view style="color: #707070;">批次号:{{item.dot}}</view>
								<view style="display: flex;justify-content: space-between;">
									<view style="color: #707070;">单价:￥{{Number(item.price).toFixed(2)}}</view>
									<view style="color: #707070;">数量:{{item.goodsNum?parseInt(item.goodsNum):item.goodsNum}}</view>
								</view>
								<view style="display: flex;justify-content: space-between;">
									<view style="color: #707070;">金额:￥{{item.subTotalMoney}}</view>
									<view v-if="form.confirmStatus == 0" style="color: #707070; color: red;"
										@click="deleteProduct(index)">
										<u-button type="error" size="mini" text="删除"></u-button>
									</view>
								</view>
							</u-col>
						</u-row>
					</view>
				</view>
			</view>
			
		
		</u--form>
		<view style="width: 100%;position: fixed;
		bottom: 0;background-color: #fff;border-top: 1rpx solid #ccc;">
			<view style="width: 100%;display: flex;font-size: 24rpx;">
				<view @click="orderRevokeGenerateShipTaskfun" v-if="form.generateTask == '已生成'"
					style="width: 100%; padding: 10rpx 0; 
					background-color: #d2983c;display: grid;justify-items: center;">
					<u-icon name="edit-pen" color="#fff"></u-icon>
					<view style="color: #fff;">撤销任务</view>
				</view>
				<view @click="orderGenerateShipTaskfun" v-else
					style="width: 100%; padding: 10rpx 0; background-color: #5ac725;display: grid;justify-items: center;">
					<u-icon name="edit-pen" color="#fff"></u-icon>
					<view style="color: #fff;">生成任务</view>
				</view>
				
				
				<view @click="conserve"
					style="width: 100%; background-color: #FD4B09;display: grid;justify-items: center;">
					<u-icon name="edit-pen" color="#fff"></u-icon>
					<view style="color: #fff;">保存</view>
				</view>
			</view>
		</view>
		
		
		<!-- 弹窗数据 -->
		<u-popup :show="popupShow" @close="popupShow = false" 
		:closeable="true" mode="center" customStyle="width: 90%;margin: 0 auto;">
			<view style="padding: 40rpx 25rpx;box-sizing: border-box;">
				<view style="text-align: center;margin: 10rpx 0;">
					<view style="font-size: 36rpx;word-break:break-all;">
						{{popupForm.cname}}
					</view>
				</view>
				<view style="width: 96%;margin: 0 auto;clear: both;">
					<u-cell-group>
						<u-cell title="单价">
							<view slot="value">
								<u--input type='number'  placeholder="请输入单价" border="none"
									inputAlign="right" v-model="popupForm.price" clearable disabledColor="#ffffff">
								</u--input>
							</view>
						</u-cell>
						<u-cell title="数量">
							<view slot="value">
								<u-number-box v-model="popupForm.goodsNum" :min="0">
								</u-number-box>
							</view>
						</u-cell>
						<u-cell title="批次号(DOT)" @click="dotClickfun">
							<view slot="value">
								<u--input border="none" inputAlign="right" v-model="popupForm.dot"
									disabled placeholder="请选择批次号" disabledColor="#ffffff"></u--input>
							</view>
						</u-cell>
						
						<u-cell title="库存">
							<view slot="value">
								{{popupForm.inventory?popupForm.inventory:'--'}}
							</view>
						</u-cell>
						<u-cell title="规格型号">
							<view slot="value">
								{{popupForm.specificationAndModel?popupForm.specificationAndModel:'--'}}
							</view>
						</u-cell>
					</u-cell-group>
				</view>
				
				<view style="margin-top: 40rpx;text-align: center;
				font-size: 32rpx;">
					<view style="display: flex; color: #fff;justify-content: space-around;">
						<view style="color: #FD4B09; border: 1rpx solid #FD4B09;
						width: 200rpx; height: 80rpx; 
						line-height: 80rpx; border-radius: 12rpx;"
							@click="popupShow = false">取消</view>
						<view style="background-color: #FF6F3B; width: 200rpx; height: 80rpx; line-height: 80rpx; border-radius: 12rpx;"
							@click="popupConserve()">保存</view>
					</view>
				</view>
			</view>
		</u-popup>
		
		<!-- 批次号弹窗 -->
		<u-picker :show="dotPickerShow" :columns="dotPickerColumns" keyName="dot"
		 @confirm="dotPickerConfirm" @cancel="dotPickerShow = false"></u-picker>
		
		<u-datetime-picker :show="timeOpen" v-model="datetime" mode="date" @cancel="timeOpen = false"
			@confirm="confirmTiem"></u-datetime-picker>
		<u-picker :show="showOpen" :columns="columns" :defaultIndex="defaultIndex" :keyName="keyName" @cancel="showOpen = false"
			@confirm="showPicker"></u-picker>
			
			<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {
		queryBrand
	} from '@/api/views/stock/index.js'
	import {
		gainUser,
		storageDescListAll,
		corpsDescDetail,
		orderItemsRemove,
		stockDescDotList,
		corpsAddrCorpIdByAddr,
		orderGenerateShipTask,
		orderRevokeGenerateShipTask
	} from '@/api/views/sale/index.js'
	import {
		typeSave,
		submitApp,
		confirm,
		revoke,
		removeItem,
		removeId,
		copyOrder,
		collectPayment,
		saveAtta,
	} from '@/api/views/sale/salesOrderDetails.js'
	import http from '@/http/api.js'
	import {
		clientId,
		clientSecret
	} from '@/common/setting'
	import {
		dateFormat
	} from '@/common/dateFormat'
	// #ifdef APP-PLUS
	const HanyinPlugin = uni.requireNativePlugin('Hanyin-Plugin'); //汉印
	import printConnect from "@/common/print.js"; //引入打印机模板文件
	let print;
	// #endif
	import {
		GET_REQUEST_DATA
	} from "@/store/gettersType.js";
	import {
		mapGetters,
		mapActions
	} from 'vuex';
	import {
		DX
	} from '@/common/dateFormat.js';
	export default {
		data() {
			return {
				form: {
					orderItemsList: [],
				},
				datetime: Number(new Date()),
				timeOpen: false,
				screen: [],
				showOpen: false,
				choice: false,
				columns: [],
				connected:false,
				amountNumber: {
					deliveryAll: 0,
					cost: 0,
					grossProfit: 0,
					delivery: 0,
					saleAll: 0,
					amount: 0
				},
				// 会员卡余额
				balanceAmount:0,
				// 深拷贝的优惠金额数据
				JsonthisUsedProfit:0,
				keyName:'',
				// 业务员数据
				salesmanData:[],
				// 物流配送方式请求到的数据
				selectData:[],
				// 物流公司请求的数据
				logisticsCorpData:[],
				// 仓库数据
				warehouseData:[],
				// 联系人数据
				contactsData:[],
				popupShow:false,
				popupForm:{},
				popupIndex:0,
				dotPickerShow:false,
				dotPickerColumns:[],
				defaultIndex:[0]
			}
		},
		onLoad(data) {
			this.queryBrandfun()
			this.HYqueryBrandfun()
			// 获取详情信息
			this.refresh(data.id)
		},
		computed: {
			...mapGetters([GET_REQUEST_DATA])
		},
		onShow() {
			this.choice = false
			let this_ = this
			// #ifdef APP-PLUS
			HanyinPlugin.IsOpened(res => {
				this_.connected = res.state
				if(this_.GET_REQUEST_DATA.length == 0){
					this_.connected = false
					print = new printConnect({},1); //打印机连接
				}
			});
			// #endif
		},
		onNavigationBarButtonTap(e) {
			this.choice = !this.choice
		},
		methods: {
			// 批次号打开弹窗按钮 获取批次号数据
			dotClickfun(){
				this.dotPickerShow = true
				console.log(this.popupForm,284);
				stockDescDotList({
					storageId: this.form.storageId,
					goodsId: this.popupForm.goodsId
				}).then(res=>{
					this.dotPickerColumns = [res.data]
				})
			},
			// 批次号弹窗的确认
			dotPickerConfirm(e){
				this.popupForm.dot = e.value[0].dot
				this.dotPickerShow = false
			},
			
			
			//修改弹窗点击保存事件
			popupConserve() {
				if (this.popupForm.whether == 1) {
					if (!this.popupForm.dot) {
						return uni.showToast({
							icon: 'none',
							title: '批次号必填',
							position: "bottom"
						})
					}
				}
				if (!this.popupForm.goodsNum) {
					return uni.showToast({
						icon: 'none',
						title: '数量不能为0',
						position: "bottom"
					})
				}
				this.form.orderItemsList[this.popupIndex] = this.popupForm
				uni.hideLoading();
				this.popupShow = false
			},
			
			// 获取仓库数据
			storageDescListAllfun(){
				storageDescListAll().then(res => {
					this.warehouseData = res.data
					this.salesmanData.map(item=>{
						if(item.id == this.form.storageId) {
							this.$set(this.form,'storageName',item.cname)
						}
					})
				})
			},
			// 获取联系人数据 赋值第一项
			corpsAddrCorpIdByAddrfun(type){
				corpsAddrCorpIdByAddr({
					pid:this.form.customerId
				}).then(res=>{
					this.contactsData = res.data
					if (type == 'KH') {
						this.$set(this.form,'contacts',this.contactsData[0].contacts)
						this.$set(this.form,'recAddress',this.contactsData[0].address)
						this.$set(this.form,'phone',this.contactsData[0].tel)
					}
					
				})
			},
			// 获取业务员信息
			gainUserfun(){
				gainUser({
					roleName:'业务员'
				}).then(res => {
					this.salesmanData = res.data
					this.salesmanData.map(item=>{
						if(item.id == this.form.salerId) {
							this.$set(this.form,'salerName',item.name)
						}
					})
				})
			},
			// 获取物流配送方式数据
			queryBrandfun(){
				queryBrand({code:'deliveryMethod'}).then(res=>{
					this.selectData = res.data
				})
			},
			// 获取货运公司数据
			HYqueryBrandfun(){
				queryBrand({code:'tyre_express_company'}).then(res=>{
					this.logisticsCorpData = res.data
				})
			},
			
			//跳转连接设备
			connectingDevices() {
				if(this.connected){
					this.senBleLabel()
				}else{
					uni.$u.route('/pages/views/bluetooth/index', {
						data: JSON.stringify(this.form)
					});
				}
			},
			senBleLabel() {
				// if (!print) return;
				console.log(this.$Mock.printTemplate.pagConfig)
				this.$Mock.printTemplate.pagConfig = 0
				let data = []
				let i = 0
				for (let item of this.form.orderItemsList) {
					data.push({
						"top": Number(i * 40 + 90),
						"left": "0",
						"textAlign": "start",
						"width": "270",
						"fontSize": 9,
						"text": item.cname,
						"type": "text",
						"fontWeight": "400",
						"height": "20"
					})
					data.push({
						"top": Number(i * 40 + 110),
						"left": "140",
						"textAlign": "start",
						"width": "40",
						"fontSize": 8,
						"text": Number(item.storageInQuantity),
						"type": "text",
						"fontWeight": "400",
						"height": "20"
					})
					data.push({
						"top": Number(i * 40 + 110),
						"left": "160",
						"textAlign": "start",
						"width": "50",
						"fontSize": 8,
						"text": item.price,
						"type": "text",
						"fontWeight": "400",
						"height": "20"
					})
					data.push({
						"top": Number(i * 40 + 110),
						"left": "210",
						"textAlign": "start",
						"width": "60",
						"fontSize": 8,
						"text": item.amount,
						"type": "text",
						"fontWeight": "400",
						"height": "20"
					})
					i++
				}
				data.push({
					"top": Number(i * 40 + 90),
					"left": "20",
					"textAlign": "start",
					"width": "270",
					"fontSize": 9,
					"text": "合计:",
					"type": "text",
					"fontWeight": "400",
					"height": "20"
				})
				data.push({
					"top": Number(i * 40 + 90),
					"left": "140",
					"textAlign": "start",
					"width": "40",
					"fontSize": 8,
					"text": this.amountNumber.saleAll,
					"type": "text",
					"fontWeight": "400",
					"height": "20"
				})
				data.push({
					"top": Number(i * 40 + 90),
					"left": "210",
					"textAlign": "start",
					"width": "60",
					"fontSize": 8,
					"text": this.form.debitAmount,
					"type": "text",
					"fontWeight": "400",
					"height": "20"
				})
			
				data.push({
					"top": Number(i * 40 + 110),
					"left": "20",
					"textAlign": "start",
					"width": "270",
					"fontSize": 9,
					"text": "产品合计:",
					"type": "text",
					"fontWeight": "400",
					"height": "20"
				})
				data.push({
					"top": Number(i * 40 + 110),
					"left": "70",
					"textAlign": "start",
					"width": "70",
					"fontSize": 8,
					"text": this.amountNumber.amount,
					"type": "text",
					"fontWeight": "400",
					"height": "20"
				})
				data.push({
					"top": Number(i * 40 + 110),
					"left": "140",
					"textAlign": "start",
					"width": "60",
					"fontSize": 8,
					"text": "合计金额:",
					"type": "text",
					"fontWeight": "400",
					"height": "20"
				})
				data.push({
					"top": Number(i * 40 + 110),
					"left": "190",
					"textAlign": "start",
					"width": "80",
					"fontSize": 8,
					"text": DX(this.amountNumber.amount),
					"type": "text",
					"fontWeight": "400",
					"height": "40"
				})
			
			
				data.push({
					"top": Number(i * 40 + 130),
					"left": "20",
					"textAlign": "start",
					"width": "270",
					"fontSize": 9,
					"text": "订货电话:" + this.form.clientAttn,
					"type": "text",
					"fontWeight": "400",
					"height": "20"
				})
				data.push({
					"top": Number(i * 40 + 150),
					"left": "20",
					"textAlign": "start",
					"width": "270",
					"fontSize": 9,
					"text": "制单人:" + this.form.client,
					"type": "text",
					"fontWeight": "400",
					"height": "20"
				})
				data.push({
					"top": Number(i * 40 + 150),
					"left": "140",
					"textAlign": "start",
					"width": "80",
					"fontSize": 8,
					"text": "收货人(签字):",
					"type": "text",
					"fontWeight": "400",
					"height": "20"
				})
				print.startPrint({
					SALES_SLIP: this.form.sysNo,
					CUSTOMER_NAME: this.form.customerName,
					SALES_DATE: this.form.businesDate.slice(0, 10)
				}, {
					pageHeight: (this.form.orderItemsList.length * 40) / 3
				}, data);
			},
			generatePictures() {
				uni.$u.route('/pages/views/bluetooth/salesSlipTable', {
					data: JSON.stringify(this.form)
				});
			},
			//遮罩层禁止点击空方法
			moveHandle() {},
			newCompany() {
				uni.$u.route('/pages/views/product/createCompany', {
					code: "account",
					title: "收款账户"
				});
			},
			
			
			getMap() {
				let this_ = this
				uni.chooseLocation({
					success: function(res) {
						this_.$set(this_.form, "recAddress", res.address)
					}
				});
			},
			copyDocument() {
				let form = {
					...this.form,
				}
				if (form.orderFilesList.length > 0) {
					form.orderFilesList.forEach(item => delete item.status)
				}
				let this_ = this
				uni.showModal({
					title: '提示',
					content: "是否确认复制新增",
					success: function(rest) {
						if (rest.confirm == true) {
							uni.showLoading({
								title: '加载中',
								mask: true
							});
							copyOrder(form).then(res => {
								uni.hideLoading();
								uni.showToast({
									icon: "none",
									title: '复制新增成功',
									mask: true
								});
								this_.form = res.data
								this_.amountNumber = {
									deliveryAll: 0, //送货总数量
									cost: 0, //成本
									grossProfit: 0, //毛利
									delivery: 0, //送货
									saleAll: 0, //销售数量
									amount: 0
								}
								for (let item of this_.form.orderItemsList) {
									this_.amountNumber.saleAll += Number(item.storageInQuantity)
									this_.amountNumber.cost += Number(item.storageInQuantity) * Number(
										item.purchasePrice)
									this_.amountNumber.delivery += Number(item.actualQuantity)
									this_.amountNumber.deliveryAll += Number(item.actualQuantity)
									this_.amountNumber.amount += Number(item.amount)
									this_.amountNumber.grossProfit += Number(item.amount) - (item
										.purchasePrice ? Number(item.storageInQuantity) * Number(
											item.purchasePrice) : 0)
								}
								this.form.orderAmount = this.amountNumber.amount
							})
						}
					}
				})
			},
			deleteDoc() {
				if (this.form.id) {
					let this_ = this
					if (this_.form.confirmStatus == 1) {
						return uni.showToast({
							icon: "none",
							title: '禁止删除',
							mask: true
						});
					}
					uni.showModal({
						title: '提示',
						content: "是否确认删除",
						success: function(rest) {
							if (rest.confirm == true) {
								removeId({
									id: this_.form.id
								}).then(res => {
									uni.showToast({
										icon: "none",
										title: '删除成功',
										mask: true
									});
									setTimeout(function() {
										uni.navigateBack()
									}, 1000);
								})
							}
						}
					})
				} else {
					uni.showToast({
						icon: "none",
						title: '未保存',
						mask: true
					});
				}
			},
			deleteProduct(index) {
				let this_ = this
				if (this_.form.orderItemsList[index].id) {
					uni.showModal({
						title: '提示',
						content: "是否确认删除",
						success: function(rest) {
							if (rest.confirm == true) {
								uni.showLoading({
									title: '加载中',
									mask: true
								});
								removeItem({
									itemId: this_.form.orderItemsList[index].id
								}).then(res => {
									uni.hideLoading();
									uni.showToast({
										icon: "none",
										title: '删除成功',
										mask: true
									});
									this_.form.orderItemsList.splice(index, 1)
								}).catch(err => {
									uni.hideLoading();
								})
							}
						}
					})
				} else {
					this_.form.orderItemsList.splice(index, 1)
				}
			},
			//时间确认选择
			confirmTiem(e) {
				this.form[this.screen[1]] = dateFormat(new Date(e.value), "YYYY-MM-DD HH:mm:ss")
				this.timeOpen = false
			},
			// 下拉选择
			showPicker(e) {
				console.log(this.screen[1]);
				if (this.screen[1] == "account") {
					this.formTwo.account = e.value[0].dictValue
					this.showOpen = false
				} else if(this.screen[1] == "salerId") {
					this.form.salerName = e.value[0].name
					this.form.salerId = e.value[0].id
					this.showOpen = false
				} else if(this.screen[1] == "storageId") {
					this.form.storageName = e.value[0].cname
					this.form.storageId = e.value[0].id
					this.showOpen = false
				}else if(this.screen[1] == "contacts") {
					this.form.contacts = e.value[0].contacts
					this.form.recAddress = e.value[0].address
					this.form.phone = e.value[0].tel
					this.showOpen = false
				}else if(this.screen[1] == "shipType") {
					this.form.shipType = e.value[0].dictValue
					this.showOpen = false
				}else if(this.screen[1] == "logisticsCorpId") {
					this.form.logisticsCorpId = e.value[0].dictKey
					this.form.logisticsCorpName = e.value[0].dictValue
					this.showOpen = false
				}else {}
				
				// this.form[this.screen[1]] = e.value[0].dictValue
				// this.showOpen = false
			},
			//集合筛选条件
			filterAll(screen) {
				// if (this.form.status != '录入') return 
				if(this.form.generateTask != '未生成') return
				this.screen = screen
				switch (screen[1]) {
					case "businesDate":
						this.timeOpen = true
						break;
					case "account":
						this.$u.api.getWorkDicts('account').then(res => {
							this.columns = [res.data]
							this.defaultIndex = [0]
							this.keyName = 'dictValue'
							this.showOpen = true
						})
						break;
					case "settlmentAmount":
						uni.$u.route('/pages/views/salesSlip/collectionList', this.form);
						break;
					// 仓库
					case "storageId":
						this.columns = [this.warehouseData]
						this.defaultIndex = [0]
						this.keyName = 'cname'
						this.showOpen = true
						break;
					// 业务员
					case "salerId":
						this.columns = [this.salesmanData]
						this.defaultIndex = [0]
						this.keyName = 'name'
						this.showOpen = true
						break;
					// 联系人
					case "contacts":
						this.columns = [this.contactsData]
						this.defaultIndex = [0]
						this.keyName = 'contacts'
						this.showOpen = true
						break;
					// 配送方式
					case "shipType":
						this.columns = [this.selectData]
						this.defaultIndex = [0]
						this.keyName = 'dictValue'
						this.showOpen = true
						break;
					// 物流公司
					case "logisticsCorpId":
						this.columns = [this.logisticsCorpData]
						this.defaultIndex = [0]
						this.keyName = 'dictValue'
						this.showOpen = true
						break;
				}
			},
			// 弹窗修改
			tanchuangxgai(row,index){
				if(this.form.generateTask != '未生成') return
				this.popupForm = row
				this.popupIndex = index
				this.popupShow = true
			},
			// 产品删除
			orderItemsDelent(id,index){
				uni.showModal({
					title: '提示',
					content: '是否确认删除',
					success: (rest)=> {
						console.log(rest);
						if (rest.confirm == true) {
							if (id) {
								orderItemsRemove({ids:id}).then(res=>{
									this.form.orderItemsList.splice(index, 1);
									this.$refs.uToast.show({
										type: 'success',
										message: "操作成功",
									})
								})
							}else {
								this.form.orderItemsList.splice(index, 1);
								this.$refs.uToast.show({
									type: 'success',
									message: "操作成功",
								})
							}
						}
					}
				})
			},
			// 获取详情数据
			refresh(id) {
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				typeSave({
					id: id
				}).then(res => {
					this.form = res.data
					this.gainUserfun()
					this.storageDescListAllfun()
					this.corpsAddrCorpIdByAddrfun()
					
					this.form.memberAmout = 0
					
					this.amountNumber = {
						deliveryAll: 0, //送货总数量
						cost: 0, //成本
						grossProfit: 0, //毛利
						delivery: 0, //送货
						saleAll: 0, //销售数量
						amount: 0
					}
					for (let item of this.form.orderItemsList) {
						this.amountNumber.saleAll += Number(item.storageInQuantity)
						this.amountNumber.cost += Number(item.storageInQuantity) * Number(item.purchasePrice)
						this.amountNumber.delivery += Number(item.actualQuantity)
						this.amountNumber.deliveryAll += Number(item.actualQuantity)
						this.amountNumber.amount += Number(item.amount)
						this.amountNumber.grossProfit += Number(item.amount) - (item.purchasePrice ? Number(item
							.storageInQuantity) * Number(item.purchasePrice) : 0)
					}
					this.form.orderAmount = this.amountNumber.amount
					setTimeout(function() {
						uni.hideLoading();
					}, 500);
				})
			},
			//确认收款
			confirmReceipt() {
				let this_ = this
				let form = {
					...this_.form,
				}
				if (form.orderFilesList.length > 0) {
					form.orderFilesList.forEach(item => delete item.status)
				}
				// if (!form.currentAmount) {
				// 	return uni.showToast({
				// 		icon: "none",
				// 		title: '本次收款金额不能为空',
				// 		mask: true,
				// 		duration: 2000
				// 	});
				// }
				// 判断本次收款和会员卡收款不能为空
				if (!this.form.currentAmount || this.form.currentAmount == 0) {
					if (!this.form.memberAmout || this.form.memberAmout == 0) {
						return uni.showToast({
							icon: "none",
							title: '本次收款金额或会员卡金额不能为空或零',
							mask: true
						});
					}
				}
				if (!form.account) {
					return uni.showToast({
						icon: "none",
						title: '未选择收款账户',
						mask: true,
						duration: 2000
					});
				}
				form.currentAmount = form.currentAmount?form.currentAmount:0
				uni.showModal({
					title: '收款:',
					content: '现金收款:' + form.currentAmount +'元\n会员卡收款:' + form.memberAmout + '元',
					success: function(rest) {
						if (rest.confirm == true) {
							uni.showLoading({
								title: '加载中',
								mask: true
							});
							collectPayment(form).then(res => {
								uni.showToast({
									icon: "none",
									title: '收款成功',
									mask: true,
									duration: 2000
								});
								setTimeout(function() {
									uni.hideLoading();
									this_.refresh(res.data.id)
								}, 1000);
							})
						}
					}
				})
			},
			//保存附件
			saveAttachment() {
				let form = {
					pid: this.form.id,
				}
				if (form.orderFilesList.length > 0) {
					form.orderFilesList.forEach(item => delete item.status)
				}
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				saveAtta(form).then(res => {
					uni.showToast({
						icon: "none",
						title: '保存成功',
						mask: true
					});
					let _this = this
					setTimeout(function() {
						uni.hideLoading();
						_this.refresh(res.data.id)
					}, 1000);
				})
			},
			// 生成任务
			orderGenerateShipTaskfun(){
				uni.showModal({
					title: '提示',
					content: '是否确认生成任务',
					success: (rest)=> {
						if (rest.confirm == true) {
							uni.showLoading({
								title: '加载中',
								mask: true
							});
							submitApp({
								bsType: "XS",
								...this.form
							}).then(() => {
								orderGenerateShipTask({
									bsType: "XS",
									...this.form
								}).then(res=>{
									uni.showToast({
										icon: "none",
										title: '生成任务成功',
										mask: true
									});
									uni.hideLoading();
									this.refresh(res.data.id)
								})
							})
							
						}
					}
				})
			},
			// 撤销任务
			orderRevokeGenerateShipTaskfun(){
				uni.showModal({
					title: '提示',
					content: '是否确认生成任务',
					success: (rest)=> {
						if (rest.confirm == true) {
							uni.showLoading({
								title: '加载中',
								mask: true
							});
							submitApp({
								bsType: "XS",
								...this.form
							}).then(() => {
								orderRevokeGenerateShipTask({
									bsType: "XS",
									...this.form
								}).then(res=>{
									uni.showToast({
										icon: "none",
										title: '撤销任务成功',
										mask: true
									});
									uni.hideLoading();
									this.refresh(res.data.id)
								})
							})
							
						}
					}
				})
			},
			//保存
			conserve() {
				// if (!this.form.shipType) {
				// 	return this.$refs.uNotify.show({
				// 		top: this.revenueTop,
				// 		type: 'warning',
				// 		message: '请选择配送方式！'
				// 	})
				// }
				// if (!this.form.logisticsCorpId) {
				// 	return this.$refs.uNotify.show({
				// 		top: this.revenueTop,
				// 		type: 'warning',
				// 		message: '请选择物流公司！'
				// 	})
				// }
				// if (!this.form.expressNo) {
				// 	return this.$refs.uNotify.show({
				// 		top: this.revenueTop,
				// 		type: 'warning',
				// 		message: '请输入物流单号！'
				// 	})
				// }
				let form = {
					...this.form,
				}
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				submitApp(form).then(res => {
					uni.showToast({
						icon: "none",
						title: '保存成功',
						mask: true
					});
					let _this = this
					setTimeout(function() {
						uni.hideLoading();
						_this.refresh(res.data.id)
					}, 1000);
				})
			},
			//客户选择返回触发
			otherFun(customer) {
				this.form.customerName = customer.cname
				this.form.customerId = customer.id
				this.form.salerId = customer.salesmanId
				this.form.salerName = ''
				this.salesmanData.map(item=>{
					if (item.id == this.form.salerId) {
						this.form.salerName = item.name
					}
				})
				this.form.storageName = ''
				this.form.storageId = customer.deliveryWarehouseId
				this.warehouseData.map(item=>{
					if (item.id == this.form.storageId) {
						this.form.storageName = item.cname
					}
				})
				this.corpsAddrCorpIdByAddrfun('KH')
				// corpsDescDetail({id:customer.id}).then(res=>{
				// 	this.form.contacts = res.data.corpsAddrList[0].contacts
				// 	this.form.recAddress = res.data.corpsAddrList[0].belongtoarea + res.data.corpsAddrList[0].detailedAddress
				// 	this.form.phone =  res.data.corpsAddrList[0].tel
				// })
				
			},
			selectProduct() {
				if (this.form.confirmStatus == 0) {
					this.$u.route('/pages/views/salesSlip/selectProduct', this.form);
				}
			},
			// 产品跳转
			chanpintiao(){
				if (!this.form.customerId) return this.$refs.uNotify.show({
					top: this.revenueTop,
					type: 'warning',
					message: '请选择客户！'
				})
				if (this.form.storageId) {
					this.$u.route('/pages/views/salesSlip/selectProduct', {
						form: JSON.stringify(this.form),
						data: JSON.stringify(this.form.orderItemsList),
						corpId:this.form.corpId
					});
				} else {
					this.$refs.uNotify.show({
						top: this.revenueTop,
						type: 'warning',
						message: '请选择发货仓库！'
					})
				}
			},
			//商品选择返回
			getProduct(dataList) {
				for (let item of dataList) {
					this.form.orderItemsList.push({
					    goodsId:item.id,
					    price:item.price,
					    goodsName:item.cname,
					    goodsNum:item.goodsNum,
					    brandName:item.brandName,
					    brandId:item.brandId,
					    goodsNo:item.code,
					    propertyName: item.specificationAndModel,
					    inventory:item.inventory,
					    pattern:item.brandItem,
					    goodsDescription:item.goodsDescription,
					    dot:item.dot,
					    whether:item.whether,
					    units:item.unit,
					    // 小计
					    subTotalMoney:item.goodsNum * item.price,
					    // 备注
					    remarks:item.remarks,
					    // 批次号的状态
					    dotedittype:false,
					    // 价格数量
					    goodsNumtype:false,
					    // 价格
					    pricetype:false,
					})
				}
				console.log(this.form.orderItemsList,963);
			},
			
			selectCustomer() {
				// if (this.form.status != '录入') return 
				if (this.form.generateTask != '未生成') return
				this.$u.route('/pages/views/salesSlip/selectCustomer', {
					corpType: 'KH'
				});
			},
		}
	}
</script>
<style lang="scss" scoped>
	::v-deep .u-cell__body {
		padding: 20rpx 10rpx;
	}

	.choice {
		position: fixed;
		right: 10rpx;
		border-radius: 10rpx;
		padding: 0 10rpx 10rpx 10rpx;
		background-color: #fff;
		// #ifdef H5
			top: calc(var(--status-bar-height) + 44px + 20rpx);
		// #endif
		// #ifdef APP-PLUS
			top: 30rpx,
		// #endif
	}

	.mask {
		width: 100%;
		height: 100%;
		position: fixed;
		top: 0;
		bottom: 0;
		background-color: rgba(1, 1, 1, 0.3);
	}
	.orderItemsDelent {
		position: absolute;
		top: 15rpx;
		right: 15rpx;
		color: #fd4b09;
	}
</style>
