<template>
	<view class="page">
		<navBar title="新增兑奖记录" :back="true" color="black" background="white" />

		<view class="content">
			<view class="item">
				<view class="label">
					*姓名
				</view>
				<u--input clearable placeholder="请输入姓名" border="surround" v-model="name" @change="change"></u--input>
			</view>
			<view class="item">
				<view class="label">
					*电话
				</view>
				<u--input clearable placeholder="请输入电话" border="surround" v-model="phone" @change="change"></u--input>
			</view>


			<view class="item">
				<view class="label">
					*来源
				</view>
				<uni-data-select placeholder="请选择来源" v-model="source" :localdata="range"></uni-data-select>
			</view>

			<view class="item" style="margin-bottom: 24rpx;height: auto;">
				<view class="label">
					兑奖信息
				</view>
				<u--textarea v-model="award_content" placeholder="请输入兑奖信息"></u--textarea>
			</view>
			<button class="orderBtn" :plain="true" @click="postRecord">
				新增
			</button>
		</view>
	</view>
</template>

<script>
	import {
		getUserPhone,
		addAward
	} from '@/api/test/index.js'
	export default {
		components: {

		},
		data() {
			return {
				phone: '',
				name: '',

				type: 3,
				source: -1,
				status: '产业电工',
				award_content: '',
				user_id: null,

				range: [{
						value: 0,
						text: "兑奖"
					},
					{
						value: 1,
						text: "到店"
					},
					{
						value: 2,
						text: "朋友"
					},
					{
						value: 3,
						text: "其他"
					},
				],
			}
		},
		async onLoad(options) {

		},
		methods: {
			async postRecord() {
				let parms = {}
				let phoneReg = /^[1][3,4,5,7,8,9][0-9]{9}$/

				if (!this.name) {
					return this.$toast('请输入姓名')
				}
				if (!this.phone) {
					return this.$toast('请输入电话')
				}
				if (!phoneReg.test(this.phone)) {
					return this.$toast('手机号格式不正确')
				}
				if (this.source == -1) {
					return this.$toast('请选择来源')
				}
				uni.showLoading({
					title: '提交中'
				})
				let res = await getUserPhone({
					phone: this.phone,
				})
				if (res.code == 0) {
					console.log('getUserPhone返回值', res.data.id);
					parms = {
						type: this.type,
						name: this.name,
						phone: this.phone,
						// status: this.d_ly == 0 ? '兑奖' : this.d_ly == 1 ? '到店' : this.d_ly == 2 ? '朋友' : '其他',
						source: this.source + 1,
						status: '产业电工',
						award_content: this.award_content,

						user_id: res.data.id
					}
					console.log('addAward--parmas', parms);

					let res1 = await addAward(parms)
					if (res1.code == 0) {
						uni.hideLoading()

						console.log('addAward返回值', res1);

						this.$toast('新增成功')

						setTimeout(() => {
							uni.navigateBack()
						}, 1500)
					} else {
						uni.hideLoading()
						uni.showToast({
							title: res1.message,
							icon: 'none'
						})
					}

				} else {
					uni.hideLoading()

					uni.showToast({
						// title:res1.msg,
						title: "电话号码不正确，没有查找到相应的人员",
						icon: 'none',
						duration: 3000
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import "./index.scss";
</style>