<template>
	<view class="content">
		<u-form :model="form" ref="form" label-width="140">
			<u-form-item v-if="fromType == 1" label="线索标签" prop="signName" required right-icon="arrow-right" @click.native="signShow = true">
				<text>{{form.signName}}</text>
			</u-form-item>
			<u-form-item label="家长姓名" prop="parentsName" required>
				<u-input v-model="form.parentsName" placeholder="请输入家长姓名" />
			</u-form-item>
			<u-form-item label="学员姓名" prop="studentName" required>
				<u-input v-model="form.studentName" placeholder="请输入学员姓名" />
			</u-form-item>
			<u-form-item label="学员性别" prop="sex" required right-icon="arrow-right" @click.native="sexShow = true">
				<text>{{form.sex}}</text>
			</u-form-item>
			<u-form-item label="学员年龄" prop="age" required>
				<u-input v-model="form.age" type="number" placeholder="请输入学员年龄" />
			</u-form-item>
			<u-form-item label="手机号码" prop="phone" required >
				<u-input v-model="form.phone" type="number" placeholder="请输入手机号码" />
			</u-form-item>
			<u-form-item label="获客来源" prop="source" required right-icon="arrow-right" @click.native="sourceShow = true">
				<text>{{form.source}}</text>
			</u-form-item>
			<u-form-item label="购卡意向" prop="intendRemark" required right-icon="arrow-right" @click.native="intentionShow = true">
				<text>{{form.intendRemark}}</text>
			</u-form-item>
			<u-form-item label="分配">
				<u-checkbox-group>
					<u-checkbox @change="handleCheckChange" 
										shape="circle"
										v-model="form.saleIsMeFlag" 
										v-for="(item, index) in checkList" :key="index" 
										:name="item.name">
						{{ item.name }}
					</u-checkbox>
				</u-checkbox-group>
			</u-form-item>
			<u-form-item label="备注">
				<u-input v-model="form.remark"placeholder="请输入备注"/>
			</u-form-item>
		</u-form>
		<!-- 标签 -->
		<u-action-sheet :list="signList" v-model="signShow" @click="handleSetSexClick"></u-action-sheet>
		<!-- 性别  -->
		<u-action-sheet :list="sexList" v-model="sexShow" @click="setSex"></u-action-sheet>
		<!-- 获客来源 -->
		<u-action-sheet :list="sourceList" v-model="sourceShow" @click="handleSetSourceClick"></u-action-sheet>
		<!-- 购卡意向 -->
		<u-action-sheet :list="intentionList" v-model="intentionShow" @click="handleSetIntentionClick"></u-action-sheet>
		<view class="handle-fix-box">
			<u-button type="warning" shape="circle" :ripple="true" :custom-style="customStyle" @click="submitForm()">提交</u-button>
		</view>
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	import {
		mapGetters
	} from 'vuex'
	const NET = require('@/utils/request')
	const API = require('@/config/api')
	export default {
		computed: {
			...mapGetters([
				'customStyle',
			])
		},
		data() {
			return {
				// type 1 线索  2 商机
				fromType: null,
				form: {
					// 线索标签
					signName: '未联系',
					clueState: 1,
					studentName: '',
					parentsName: '',
					sex: '',
					age: '',
					phone: '',
					source: '',
					intendRemark: '',
					saleIsMeFlag: true,
					remark: ''
				},
				signShow: false,
				signList: [
					{ text: '无效', type: 0 },
					{ text: '未联系', type: 1  },
					{ text: '有意向', type: 2  },
					{ text: '已预约', type: 3  },
					{ text: '商机', type: 4  },
				],
				sourceShow: false,
				sourceList: [
					{ text: '问卷' },
					{ text: '电话' },
					{ text: '转介' },
					{ text: '活动' }
				],
				intentionShow: false,
				intentionList: [
					{ text: '很强' },
					{ text: '较强' },
					{ text: '一般' },
					{ text: '较弱' },
					{ text: '很弱' }
				],
				checkList: [
					{
						name: '是否分配给当前用户',
						checked: false,
						disabled: false
					}
				],
				rules: {
					studentName: [{
						required: true,
						message: '请输入学员姓名',
						trigger: 'change'
					}],
					parentsName: [{
						required: true,
						message: '请输入家长姓名',
						trigger: 'change'
					}],
					sex: [{
						required: true,
						message: '请选择学员性别',
						trigger: 'change'
					}],
					age: [{
						required: true,
						message: '请输入学员年龄',
						trigger: 'change'
					}],
					source: [{
						required: true,
						message: '请选择获客来源',
						trigger: 'change'
					}],
					intendRemark: [{
						required: true,
						message: '请选择购买意向',
						trigger: 'change'
					}],
					phone: [{
							required: true,
							message: '请输入手机号码',
							trigger: 'change'
						},
						{
							pattern: /^1[0-9]{10}$/g,
							transform(value) {
								return String(value);
							},
							message: '请输入正确格式的手机号码'
						},
					],
				},
				sexShow: false,
				sexList: [
					{ text: '男' },
					{ text: '女' }
				]
			}
		},
		onLoad(options) {
			// type 1 线索  2 商机
			this.fromType = options.type
			if(options.type == 2) {
				this.form.signName = '商机'
				this.form.clueState = 4
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules);
		},
		methods: {
			// 单选
			handleCheckChange(e) {
				this.form.saleIsMeFlag = e.value
			},
			//  设置线索标签
			handleSetSexClick(index) {
				this.form.signName = this.signList[index].text
				this.form.clueState = this.signList[index].type
			},
			//  获客来源
			handleSetSourceClick(index) {
				this.form.source = this.sourceList[index].text
			},
			//  购卡意向 
			handleSetIntentionClick(index) {
				this.form.intendRemark = this.intentionList[index].text
			},
			//  设置性别
			setSex(index) {
				this.form.sex = this.sexList[index].text
			},
			//  提交表单
			submitForm() {
				console.log(this.form);
				this.$refs.form.validate(valid => {
					if (valid) {
						NET.request(API.submitIntentionForm, {
							...this.form
						}, 'POST').then(res => {
							this.$refs.uTips.show({
								title: '提交成功',
								type: 'success',
							})
							setTimeout(() => {
								uni.navigateBack()
							}, 1000)
						}).catch(error => {
							this.$refs.uTips.show({
								title: error.message,
								type: 'warning',
							})
						})
					}
				});
			},
		},
	}
</script>

<style>
	page {
		width: 100%;
		height: 100%;
		position: relative;
	}
</style>
<style lang="scss" scoped>
	@import "@/static/css/themes.scss";

	.content {
		width: 100%;
		float: left;
		padding: 0 15px 60px 15px;
		box-sizing: border-box;
	}
</style>
