<template>
	<view style="padding-top: 1rpx;">
		<!-- :style="{marginTop:revenueTop+120+'rpx'}" -->
		<view style="width: 96%;margin: 20rpx auto;background-color: #fff;border-radius: 20rpx;">
			<u-cell-group :border="false">
				<u-cell icon="account" title="客户" :value="form.corpName" :isLink="true" @click="selectCustomer">
				</u-cell>
				<u-cell icon="calendar" title="日期" :value="form.bizDate?form.bizDate.slice(0,10):''" :isLink="true"
					@click="show = true"></u-cell>
				<u-cell icon="man-add" title="联系人" :isLink="false">
					<view slot="right-icon" style="width:400rpx;">
						<u--input placeholder="请输入联系人" border="none" inputAlign="right" v-model="form.contacts">
						</u--input>
					</view>
				</u-cell>
				<u-cell icon="phone" title="联系电话" :isLink="false">
					<view slot="right-icon" style="width:400rpx;">
						<u--input placeholder="请输入联系电话" border="none" inputAlign="right" v-model="form.tel"></u--input>
					</view>
				</u-cell>
				<u-cell icon="edit-pen" title="备注" :isLink="false">
					<view slot="right-icon" style="width:400rpx;">
						<u--textarea :showConfirmBar="false" height="40" v-model="form.remarks" confirmType="return"
							placeholder="请输入内容" maxlength="-1" autoHeight border="none"></u--textarea>
					</view>
				</u-cell>
			</u-cell-group>
		</view>
		<view style="width: 96%;;margin: 0 auto;">
			<u-button type="primary" class="custom-style" color="linear-gradient(0deg, #FD5003 0%, #FBA680 100%)" shape="circle" @click="saveOrder" text="确认保存">
			</u-button>
		</view>
		<u-datetime-picker :show="show" v-model="timeValue" mode="date" @confirm="confirm" @cancel="show = false">
		</u-datetime-picker>
		<u-picker :show="showWarehouse" :columns="columns" keyName="cname" @confirm="confirmWarehouse"
			@cancel="showWarehouse = false"></u-picker>
		<u-notify ref="uNotify"></u-notify>
	</view>
</template>

<script>
	import {
		customerList
	} from '@/api/views/sale/index.js'
	import {
		typeSave,
		getUserInfo
	} from '@/api/views/visit/index.js'
	import {
		dateFormat
	} from '@/common/dateFormat.js'
	export default {
		data() {
			return {
				show: false,
				showWarehouse: false,
				timeValue: Number(new Date()),
				columns: [],
				dataList: [],
				amount: 0.00,
				form: {
					corpId: '',
					corpName: '',
					customer: '',
					bizDate: '',
					storageName: '',
					remarks: '',
					total: 0
				},
				revenueTop: 0
			}
		},
		onLoad() {
			this.form.bizDate = new Date((new Date()).getTime() + 8 * 60 * 60 * 1000).toJSON().split('T').join(' ').substr(
				0, 19)
			uni.getSystemInfo({
				success: (e) => {
					this.revenueTop = e.statusBarHeight
				}
			})
		},
		created() {
			let this_ = this
			uni.getLocation({
				type: 'gcj02',
				geocode: true,
				success: function(res) {
					console.log(res)
					let positionName= {
						country: res.address.country||'',
						province: res.address.province||'',
						city: res.address.city||'',
						district: res.address.district||'',
						street: res.address.street||'',
						streetNum: res.address.streetNum||'',
						poiName: res.address.poiName||'',
					}
					this_.form.position = [res.latitude, res.longitude]
					this_.form.positionName = positionName.country + positionName.province + positionName.city + positionName.district + positionName.street + positionName.streetNum + positionName.poiName					
				},
				fail: function(err) {
					console.log('获取定位失败', err);
					uni.showToast({
						title: '获取地址失败，将导致部分功能不可用',
						icon: 'none'
					});
				}
			});
			getUserInfo().then(res => {
				this.$set(this.form, 'fromUser', res.data.id);
				this.$set(this.form, 'toUser', res.data.id);
			})
			customerList({
				size: 1,
				current: 1
			}).then(request => {
				customerList({
					size: request.data.total,
					current: 1
				}).then(res => {
					this.columns = [res.data.records]
				})
			})
		},
		onNavigationBarButtonTap(e) {
			uni.$u.route('/pages/views/visit/historical0rders');
		},
		methods: {
			deleteProduct(index) {
				this.amount = Number(this.amount) - Number(this.dataList[index].amount)
				this.amount.toFixed(2)
				this.dataList.splice(index, 1)
			},
			//客户选择返回触发
			otherFun(customer) {
				this.form.contacts = customer.attn
				this.form.tel = customer.tel
				this.form.corpName = customer.cname
				this.form.corpId = customer.id
				this.form.belongToCorpId = customer.belongtocompany
			},
			selectCustomer() {
				this.$u.route('/pages/views/salesSlip/selectCustomer', {
					corpType: 'KH'
				});
			},
			confirm(e) {
				this.show = false
				this.form.bizDate = new Date((new Date(e.value)).getTime() + 8 * 60 * 60 * 1000).toJSON().split('T')
					.join(' ').substr(0, 19)
			},
			confirmWarehouse(e) {
				this.form.storageName = e.value[0].cname
				this.form.storageId = e.value[0].id
				this.showWarehouse = false
			},
			//保存订单
			saveOrder() {
				let this_ = this
				uni.showModal({
					content: '是否确认保存',
					success: function(res) {
						if (res.confirm) {
							if (!this_.form.corpId) return this_.$refs.uNotify.show({
								top: this_.revenueTop,
								type: 'warning',
								message: '请选择客户！'
							})
							if (!this_.form.bizDate) return this_.$refs.uNotify.show({
								top: this_.revenueTop,
								type: 'warning',
								message: '请选择日期！'
							})
							uni.showLoading({
								title: '操作中',
								mask: true
							});
							let form = {
								...this_.form,
								itemList: []
							}
							typeSave(form).then(res => {
								uni.hideLoading();
								this_.$refs.uNotify.success('操作成功')
								uni.navigateBack()
							})
						}
					}
				});
			}
		}
	}
</script>

<style scoped>
	.submitBar {
		width: 100%;
		position: fixed;
		bottom: 20rpx;
		font-size: 32rpx;
		color: #000;
		text-align: center;
		border-top: 2rpx solid #f2f2f6;
	}
	.custom-style {
		color: #3ab2f7;
		margin-top: 40rpx;
		box-shadow: 2px 5px 10px 1px rgba(43, 117, 250, 0.3);
	}
</style>
