<template>
	<page-meta :page-style="'overflow:'+(adminListShow?'hidden':'visible')"></page-meta>
	<view class="containers">
		<view class="steps">
			<uni-steps :options="steps" :active="stepActive" active-color="#044ed7" />
		</view>
		<uni-forms ref="formdata" :model="form" :rules="rules" labelWidth="100px">
			<view class="formtext">拜访信息</view>
			<uni-forms-item label="到访日期" required name="date">
				<uni-datetime-picker type="date" :start="toadyStart" return-type="string" v-model="form.date" />
			</uni-forms-item>
			<uni-forms-item label="到访时段" required name="visit_time_id">
				<uni-data-checkbox v-model="form.visit_time_id" :localdata="visitTime"
					:map="{text:'visitTimeRange',value:'id'}" />
			</uni-forms-item>
			<uni-forms-item label="前往区域" required name="visit_area_id">
				<uni-data-checkbox v-model="form.visit_area_id" :localdata="visitArea"
					:map="{text:'name',value:'id'}" />
			</uni-forms-item>
			<uni-forms-item label="到访事由" v-if="form.type==1">
				<uni-data-select v-model="form.reason" :localdata="reasonList">
				</uni-data-select>
			</uni-forms-item>
			<uni-forms-item label="施工开始时段" v-if="form.type==2">
				<uni-datetime-picker type="date" :start="toadyStart" return-type="string" v-model="form.work_start_time" />
			</uni-forms-item>
			<uni-forms-item label="施工结束时段" v-if="form.type==2">
				<uni-datetime-picker type="date" :start="toadyStart" return-type="string" v-model="form.work_end_time" />
			</uni-forms-item>
			<uni-forms-item label="车牌号" v-if="form.type==3">
				<uni-easyinput @tap="plateShow=true" disabled v-model.trim="form.plate" placeholder="请输入车牌号" />
				<plate-input v-if="plateShow" :plate="form.plate" @export="setPlate" @close="plateShow = false" />
			</uni-forms-item>
			<uni-forms-item label="备注">
				<uni-easyinput type="textarea" v-model="form.remark" placeholder="请输入自我备注" />
			</uni-forms-item>
			<view class="formtext">拜访人信息</view>
			<uni-forms-item label="姓名" required name="name">
				<uni-easyinput v-model="form.name" placeholder="请输入姓名" />
			</uni-forms-item>
			<uni-forms-item label="联系电话" required name="mobile">
				<uni-easyinput v-model="form.mobile" placeholder="请输入联系电话" />
			</uni-forms-item>
			<uni-forms-item label="证件类型" required name="credent">
				<uni-data-select v-model="form.credent" :localdata="credentList">
				</uni-data-select>
			</uni-forms-item>
			<uni-forms-item label="证件号码" required name="idcard">
				<uni-easyinput type="idcard" v-model="form.idcard" placeholder="请输入证件号码" />
			</uni-forms-item>
			<uni-forms-item label="单位名称" required name="company_name">
				<uni-easyinput v-model="form.company_name" placeholder="请输入单位名称" />
			</uni-forms-item>
			<uni-forms-item label="到访车辆" v-if="form.type!=3">
				<uni-easyinput disabled @tap="plateShow=true" v-model.trim="form.cars"
					placeholder="请输入到访车辆" />
				<plate-input v-if="plateShow" :plate="form.cars" @export="setCars" @close="plateShow = false" />
			</uni-forms-item>
			<template v-if="form.type!=3">
				<view class="formtext" style="display: flex;justify-content: space-between;">
					<text>随访人员</text>
					<button style="background-color: #044ed7;" type="primary" size="mini"
						@click="$refs.showRight.open(),follw_people_obj={},follow_people_index=-1">新增随访人员</button>
				</view>
				<!-- 随访人员表格 -->
				<template>
					<uni-table ref="table" border emptyText="暂无更多数据">
						<uni-tr>
							<uni-th width="160" align="center">操作</uni-th>
							<uni-th width="120" align="center">姓名</uni-th>
							<uni-th width="120" align="left">联系电话</uni-th>
							<uni-th width="120" align="center">证件类型</uni-th>
							<uni-th width="120" align="left">证件号码</uni-th>
						</uni-tr>
						<uni-tr style="vertical-align: middle;" v-for="(item, index) in form.follw_people" :key="index">
							<uni-td>
								<view class="uni-group">
									<button style="background-color: #044ed7;margin-right:8px"
										@click="editFollwPeople(index)" class="uni-button" size="mini"
										type="primary">修改</button>
									<button class="uni-button" size="mini" type="warn"
										@click="form.follw_people.splice(index,1)">删除</button>
								</view>
							</uni-td>
							<uni-td>{{ item.name }}</uni-td>
							<uni-td>
								<view class="name">{{ item.mobile }}</view>
							</uni-td>
							<uni-td align="center">{{ item.credent==1?'身份证':"护照" }}</uni-td>
							<uni-td align="center">{{ item.idcard }}</uni-td>
						</uni-tr>
					</uni-table>
				</template>
			</template>

			<uni-forms-item label="长访客申请">
				<uni-data-checkbox v-model="form.long_time" :localdata="longList" :map="{text:'value',value:'id'}" />
			</uni-forms-item>
			<template v-if="form.long_time==1">
				<uni-forms-item label="开始日期">
					<uni-datetime-picker type="date" :start="toadyStart" return-type="string" v-model="form.start_date" />
				</uni-forms-item>
				<uni-forms-item label="结束日期">
					<uni-datetime-picker type="date" :start="toadyStart" return-type="string" v-model="form.end_date" />
				</uni-forms-item>
			</template>
			<view class="formtext">被访人信息</view>
			<uni-forms-item label="被访人" required name="acceptName">
				<!-- <uni-data-select @focus="adminListShow=true" v-model="form.accept_admin_id" :localdata="adminList">
				</uni-data-select> -->
				<uni-easyinput @clear="clearAccept('accept')" disabled v-model="form.acceptName" @tap="adminListShowOpen('accept')" placeholder="请选择联系人员" />
				<qianziyu-select :show="adminListShow" type="radio" :popupTitle="'联系人员'" name="cworkStationName"
					:dataLists="adminList" placeholder="输入被访人具体姓名搜索" @search="selectSearch"
					@submit="submitAccept" @cancel="cancelAccept"/>
			</uni-forms-item>
			<uni-forms-item label="收货人" v-if="form.type==3" required name="goodsName">
				<!-- <uni-data-select v-model="form.accept_goods_admin_id" :localdata="adminList">
				</uni-data-select> -->
				<uni-easyinput @clear="clearAccept('goods')" disabled v-model="form.goodsName" @tap="adminListShowOpen('goods')" placeholder="请选择联系人员" />
			</uni-forms-item>
		</uni-forms>
		<button type="primary" style="background-color: #044ed7;" @click="saveFormgoStudy">{{goStudy}}</button>


		<!-- 随访人员弹出 -->
		<uni-drawer ref="showRight" mode="right" :mask-click="true">
			<uni-forms ref="peopleform" :model="follw_people_obj" :rules="peoplerules" labelWidth="80px">
				<uni-forms-item label="姓名" required name='name'>
					<uni-easyinput v-model="follw_people_obj.name" placeholder="请输入姓名" />
				</uni-forms-item>
				<uni-forms-item label="联系电话" required name='mobile'>
					<uni-easyinput v-model="follw_people_obj.mobile" placeholder="请输入联系电话" />
				</uni-forms-item>
				<uni-forms-item label="证件类型" required name='credent'>
					<uni-data-select @change="chooseCredent" v-model="follw_people_obj.credent"
						:localdata="credentList">
					</uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="证件号码" required name='idcard'>
					<uni-easyinput type="idcard" v-model="follw_people_obj.idcard" placeholder="请输入证件号码" />
				</uni-forms-item>
			</uni-forms>
			<view class="uni-group" style="text-align: right;">
				<button @click="closeDrawer" size="mini">取消</button>
				<button @click="pushFollowPeople" size="mini" type="primary"
					style="background-color: #044ed7;margin-left:8px">确认</button>
			</view>
		</uni-drawer>
	</view>

</template>

<script>
	import plateInput from '@/uni_modules/uni-plate-input/uni-plate-input.vue'
	import qianziyuSelect from '@/components/qianziyu-select/qianziyu-select.vue'
	export default {
		components: {
			plateInput,
			qianziyuSelect
		},
		data() {
			return {
				steps: [{
					title: '填报'
				}, {
					title: '学习'
				}, {
					title: '完成'
				}],
				stepActive: 0,
				toadyStart: '',
				plateShow: false,
				adminListShow: false,
				adminKeyword: '',
				acceptType: "",
				acceptName: "",
				goodsName: '',
				form: {
					type: 1,
					date: "",
					visit_time_id: "",
					visit_area_id: "",
					reason: "",
					plate: "",
					remark: "",
					name: "",
					mobile: "",
					credent: 1,
					idcard: "",
					company_name: "",
					cars: "",
					follw_people: [],
					long_time: 0,
					start_date: "",
					end_date: "",
					accpet_department_id: "",
					accept_admin_id: "",
					accept_goods_admin_id: "",
					acceptName: "",
					goodsName: '',
				},
				follw_people_obj: {
					name: "",
					mobile: "",
					credent: 1,
					idcard: "",
				},
				follow_people_index: -1,
				rules: {
					date: {
						rules: [{
							required: true,
							errorMessage: '请选择到访日期'
						}]
					},
					visit_time_id: {
						rules: [{
							required: true,
							errorMessage: '请选择拜访时段'
						}]
					},
					visit_area_id: {
						rules: [{
							required: true,
							errorMessage: '请选择前往区域'
						}]
					},
					name: {
						rules: [{
							required: true,
							errorMessage: '姓名不能为空'
						}]
					},
					mobile: {
						rules: [{
							required: true,
							errorMessage: '联系电话不能为空'
						}, {
							pattern: '^[1][3-9][\\d]{9}',
							errorMessage: '联系号码格式错误'
						}]
					},
					credent: {
						rules: [{
							required: true,
							errorMessage: '请选择身份证类型'
						}]
					},
					idcard: {
						rules: [{
							required: true,
							errorMessage: '身份证号码不能为空'
						}, {
							pattern: '^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]',
							errorMessage: '身份证格式错误'
						}]
					},
					company_name: {
						rules: [{
							required: true,
							errorMessage: '单位名称不能为空'
						}]
					},
					acceptName:{
						rules: [{
							required: true,
							errorMessage: '请选择被访人'
						}]
					},
					goodsName:{
						rules: [{
							required: true,
							errorMessage: '请选择收货人'
						}]
					}
				},
				peoplerules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '随访人姓名不能为空'
						}]
					},
					mobile: {
						rules: [{
							required: true,
							errorMessage: '联系电话不能为空'
						}, {
							pattern: '^[1][3-9][\\d]{9}',
							errorMessage: '联系号码格式错误'
						}]
					},
					credent: {
						rules: [{
							required: true,
							errorMessage: '请选择身份证类型'
						}]
					},
					idcard: {
						rules: [{
							required: true,
							errorMessage: '身份证号码不能为空'
						}, {
							pattern: '^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]',
							errorMessage: '身份证格式错误'
						}]
					}
				},
				// 单选数据源
				visitTime: [],
				visitArea: [],
				reasonList: [],
				credentList: [{
						value: 1,
						text: "身份证"
					},
					{
						value: 2,
						text: "护照"
					}
				],
				longList: [{
					id: 0,
					value: '否'
				}, {
					id: 1,
					value: '是'
				}],
				adminList: [],
				isCall: '', // 是否待邀约
				goStudy: '去学习'

			}
		},
		onLoad(options) {
			this.toadyStart = new Date()
			this.form.type = options.type
			// uni.redirectTo({
			// 	url: '/pages/visit/study?type='+this.form.type
			// })
			// return
			this.isCall = options.iscall ? options.iscall : ''
			this.goStudy = this.isCall == 'call' ? '提交' : '去学习'
			this.form.audit_status = this.isCall == 'call' ? 1 : 0
			this.getVisitArea()
			this.getVisitTime()
			this.getReason()
			// this.getAdminList()
		},
		onReady() {},
		methods: {
			adminListShowOpen(type) {
				this.acceptType = type
				this.adminKeyword = ""
				// this.getAdminList()
				
				this.adminListShow = true
			},
			cancelAccept() {
				console.log('123')
				this.adminKeyword = ""
				this.adminList = []
				this.adminListShow = false
			},
			selectSearch(e) {
				this.adminKeyword = e
				this.getAdminList()
			},
			clearAccept(type){
				if (type == 'accept') {
					this.form.accept_admin_id = ''
					this.form.acceptName = ''
				} else {
					this.form.accept_goods_admin_id = ''
					this.form.goodsName = ''
				}
			},
			submitAccept(e) {
				if (this.acceptType == 'accept') {
					this.form.accept_admin_id = e.id
					this.form.acceptName = e.name
				} else {
					this.form.accept_goods_admin_id = e.id
					this.form.goodsName = e.name
				}
				this.adminListShow = false
			},
			setCars(e) {
				this.form.cars = e
				this.plateShow = false
				console.log(e)
			},
			setPlate(e) {
				this.form.plate = e
				this.plateShow = false
			},
			// 获取单选数据
			async getVisitArea() {
				let that = this
				// util.request()
				this.util.request({
					api: '/api/mobile/visit/visit-area',
					data: {},
					utilSuccess: function(res) {
						console.log(res)
						that.visitArea = res
					},
					utilFail: function(res) {}
				})
			},
			async getVisitTime() {
				let that = this
				// util.request()
				this.util.request({
					api: '/api/mobile/visit/visit-time',
					data: {},
					utilSuccess: function(res) {

						for (var m of res) {
							m.visitTimeRange = m.start_time + "-" + m.end_time
						}
						console.log(res)
						that.visitTime = res
					},
					utilFail: function(res) {}
				})
			},
			async getReason() {
				let that = this
				// util.request()
				this.util.request({
					api: '/api/admin/parameter/show',
					data: {
						number: 'reasonList'
					},
					utilSuccess: function(res) {
						let arr = []
						console.log(res)
						for (var m of res.detail) {
							arr.push({
								text: m.value,
								value: m.value
							})
						}
						that.reasonList = arr
					},
					utilFail: function(res) {}
				})
			},
			async getAdminList() {
				let that = this
				// util.request()
				this.util.request({
					api: '/api/admin/other/admin-user-list',
					data: {
						page_size: 99,
						page: 1,
						keyword: this.adminKeyword
					},
					utilSuccess: function(res) {
						that.adminList = res.data
					},
					utilFail: function(res) {}
				})
			},
			//
			chooseCredent(e) {
				console.log(e)
			},
			// 随访人员弹出
			editFollwPeople(index) {
				this.follow_people_index = index
				this.follw_people_obj = this.form.follw_people[index]
				this.$refs.showRight.open()
			},
			closeDrawer() {
				this.follw_people_obj = {}
				this.follow_people_index = -1
				this.$refs.showRight.close()
			},
			pushFollowPeople() {
				console.log('this.follw_people_obj', this.follw_people_obj)
				this.$refs['peopleform'].validate().then(res => {
					if (this.follow_people_index > -1) {
						this.form.follw_people[this.follow_people_index] = this.follw_people_obj
					} else {
						this.form.follw_people.push(this.follw_people_obj)
					}
					this.follow_people_index = -1
					this.$refs.showRight.close()
				}).catch(err => {
					console.log('err', err);
				})



			},
			// 去学习
			saveFormgoStudy() {
				console.log("form", this.form)
				this.$refs['formdata'].validate().then(res => {
					if (this.isCall === 'call') {
						this.submitForm()
						return
					}
					uni.setStorageSync('formdata', this.form)
					uni.redirectTo({
						url: '/pages/visit/study?type=' + this.form.type
					})
				}).catch(err => {
					console.log('err', err);
				})
			},
			submitForm() {
				let that = this
				this.util.request({
					api: '/api/admin/visit/save',
					method: "POST",
					requestType: 'bd',
					data: that.form,
					utilSuccess: function(res) {
						uni.showToast({
							title: res.msg,
							duration: 2000,
							icon: 'none'
						})
						// uni.removeStorageSync('formData')
						uni.redirectTo({
							url: '/pages/visit/successform?iscall=call'
						})
					},
					utilFail: function(res) {
						uni.showToast({
							title: res.errmsg,
							duration: 2000,
							icon: 'none'
						})
					}
				})
			}
		}
	}
</script>

<style scoped>
	.containers {
		background-color: #fff;
		min-height: 100vh;
		padding: 20rpx;
		padding-bottom: 60rpx;
	}

	.steps {
		margin-bottom: 40rpx
	}

	.formtext {
		margin-bottom: 20rpx
	}

	/deep/ .uni-table-td {
		vertical-align: middle;
	}

	/deep/ .uni-drawer__content {
		padding: 40rpx
	}
</style>