<template>
	<view>
		<view class="content-box">
			<label class="content-label">
				<view class="label-name">
					<text>客户名称</text>
				</view >
				<view class="label-data">
					<!-- <input type="text" v-model="msgform.customerName"  class="label-input"/> -->
					<!-- <uni-data-select
					  v-model="value"
					  :localdata="range"
					  @change="change"
					></uni-data-select> -->
					<picker @change="PickerChangeName" :value="indexName" :range="pickerName" class="common-picker">
						<view class="picker">
							{{indexName>-1?pickerName[indexName]:' 请选择 '}}
						</view>
					</picker>
				</view>
			</label>
			<label class="content-label">
				<view class="label-name">
					<text>拜访时间</text>
				</view >
				<view class="label-data">
					<picker mode="date" :value="date" start="2015-09-01" end="2024-09-01" @change="DateChange" class="common-picker">
						<view class="picker">
							{{date}}
						</view>
					</picker>
					 <!-- <button @click="open">打开日历</button> -->
					<!-- <input type="text" @click="open" v-model="msgform.visitTime"  class="label-input"/> -->
				</view>
			</label>
			<label class="content-label">
				<view class="label-name">
					<text>拜访方式</text>
				</view >
				<view class="label-data">
					<picker @change="PickerChangeMethod" :value="indexMethod" :range="pickerMethod" class="common-picker">
						<view class="picker">
							{{indexMethod>-1?pickerMethod[indexMethod]:' 请选择 '}}
						</view>
					</picker>
				</view>
				
			</label>
			<label class="content-label">
				<view class="label-name">
					<text>回访记录</text>
				</view >
				<view class="label-data">
					<input type="text" v-model="msgform.record"  class="label-input"/>
				</view>
			</label>
			<label class="content-label">
				<view class="label-name">
					<text>是否内部人员</text>
				</view >
				<view class="label-data">
					<!-- <input type="text" v-model="msgform.isIn"  class="label-input"/> -->
					<picker @change="PickerChangeIn" :value="indexIn" :range="pickerIn" class="common-picker">
						<view class="picker">
							{{indexIn>-1?pickerIn[indexIn]:' 请选择 '}}
						</view>
					</picker>
				</view>
			</label>
		</view>
		<button  @click="addF()"  style="width: 80%; background-color:cornflowerblue;color: white">保存</button>
	</view>
</template>

<script>
	import{ addVisit,updateVisit,getVisit } from "../../api/marketmini/index"
	import { listCustomerInfo } from "../../api/marketmini/index"
 	export default {
		data() {
			return {
				msgform:{
					customerId: "",
					customerName: "",
					isIn: "",
					record: "",
					visitId: "",
					visitMethod: "",
					visitTime: ""
				},
				flag:'msg',
				id:'',
				value: 0,
				range: [],
				valueMethod: 0,
				rangeMethod: [
					{ value: 1, text: "自驾" },
					{ value: 2, text: "交通工具" },
					{ value: 3, text: "步行" },
					{ value: 4, text: "其他" }
				],
				valueIn: 0,
				rangeIn: [
					{ value: 1, text: "是" },
					{ value: 2, text: "否" }
				],
				queryParams: {
					pageNum: 1,
					pageSize: 10
				},
				msgdata : [],
				date: '请选择',
				pickerName: [],
				nameId:[],
				indexName: -1,
				pickerIn: ['是', '否'],
				indexIn: -1,
				pickerMethod: ['自驾', '交通工具','步行','其他'],
				indexMethod: -1,
			}
		},
		methods: {
			DateChange(e) {
				this.date = e.detail.value
				// console.log(this.date)
				this.msgform.visitTime = this.date
			},
			PickerChangeIn(e) {
				this.indexIn = e.detail.value
				// console.log(this.pickerIn[this.indexIn])
				this.msgform.isIn = this.indexIn + 1
				console.log(this.msgform.isIn)
			},
			PickerChangeMethod(e) {
				this.indexMethod = e.detail.value
				// console.log(this.pickerMethod[this.indexMethod])
				this.msgform.visitMethod = Number(this.indexMethod)+1
				console.log(this.msgform.visitMethod)
			},
			PickerChangeName(e) {
				this.indexName = e.detail.value
				this.msgform.customerId = this.nameId[this.indexName]
				this.msgform.customerName = this.pickerName[this.indexName]
			},
			open(){
				this.$refs.calendar.open();
			},
			confirm(e) {
				// console.log(e);
				this.msgform.visitTime = e.fulldate
			},
			change(e) {
			  // console.log("e:", e);
			  this.msgform.customerId = e
			  // console.log(this.value)
			},
			changeMethod(e) {
			  // console.log("e:", e);
			  this.msgform.visitMethod = e
			  // console.log(this.value)
			},
			changeIn(e) {
			  // console.log("e:", e);
			  this.msgform.isIn = e
			  // console.log(this.value)
			},
			addF(){
				const form = this.msgform
				console.log(form)
				if(this.id !== ""){
					updateVisit(form).then(()=>{
						uni.navigateBack({
							delta: 1
						});
					})
				}else{
					addVisit(form).then(data=>{
						// uni.navigateTo({
						// 	url:"/pages/index/index"
						// })
						uni.switchTab({
							url:"/pages/index/index"
						})
					})
				}
				
			}
		},
		onLoad:function(option){
			if(option.id!=null){
				// console.log(option.id)
				this.id = option.id
				getVisit(option.id).then(data=>{
					// this.msgform = data.data
					// console.log(this.msgform)
					this.msgform.customerId = data.data.customerId,
					// this.msgform.customerName= data.data.,
					this.msgform.isIn= data.data.isIn,
					this.msgform.record= data.data.record,
					this.msgform.visitId= data.data.visitId,
					this.msgform.visitMethod= data.data.visitMethod,
					this.msgform.visitTime= data.data.visitTime
					this.indexIn = data.data.isIn - 1
					this.date = data.data.visitTime
					this.indexMethod = data.data.visitMethod - 1
					// console.log(this.msgform.customerId)
					listCustomerInfo(this.queryParams).then(data=>{
						this.msgdata = data.rows
						// console.log(this.msgdata)
						this.pickerName =  this.msgdata.map(item =>{
							return item.customerName
						})
						this.nameId = this.msgdata.map(item =>{
							return item.customerId
						})
						const oindex = this.nameId.findIndex((value,index)=>{return value==this.msgform.customerId})
						// console.log(this.pickerName,this.nameId,oindex)
						this.indexName = oindex
						this.msgform.customerName = this.pickerName[oindex]
					})
					
				}) 
			}else{
				listCustomerInfo(this.queryParams).then(data=>{
					this.msgdata = data.rows
					// console.log(this.msgdata)
					this.pickerName =  this.msgdata.map(item =>{
						return item.customerName
					})
					this.nameId = this.msgdata.map(item =>{
						return item.customerId
					})
				})
			}
		}
	}
</script>

<style>
@import url("@/common/scss/core.scss");
</style>
