<template>
	<view class="create_customer">
		<view class="top_required">
			只展示<span style="color:#f00">必填</span>字段<switch :checked="isRequired" color="#008EFF" @change="changeRequired" />
		</view>
		<view class="fllow_form">
			<input type="text" class="nameinput" :class="name ? 'valueActive' : '' " v-model="name" placeholder="输入门店名称" />
			<view class="form_right theme">
				<image style="width: 40rpx;height: 40rpx;
				margin: 0 20rpx 0 30rpx;" :src="BASE_IMG_URL+'chachong.png'" mode="scaleToFill"></image>
				<view @click="checkName" style="color:#008EFF;">查重</view>
			</view>
		</view>
		<!-- 基础信息 -->
		<view class="info_head">基础信息</view>
		<view class="fllow_form" v-if="!isRequired">
			<view style="padding-left: 20rpx;">上级公司</view>
			<view class="form_right" @click="changeParent"> 
				<view :class="parentName ? 'valueActive' : '' ">{{parentName ? parentName : '点击选择'}}</view>
				<view class="iconfont icon-arrows_right"></view>
			</view>
		</view>
		<view class="fllow_form">
			<view><text>*</text>地理位置</view>
			<!-- #ifdef MP-ALIPAY || H5 -->
			<view class="form_right" @click="show = true">
				<view :class="address_dingwei ? 'valueActive' : '' ">{{address_dingwei ? address_dingwei : '点击选择'}}</view>
				<view class="iconfont icon-arrows_right"></view>
			</view>
			<!-- #endif -->
			<!-- #ifdef MP-WEIXIN || APP-PLUS-->	
			<view class="form_right" @click="detailAddClick">
				<view class="location" :class="address_dingwei ? 'valueActive' : '' ">
					{{address_dingwei ? address_dingwei : '点击选择'}}
				</view>
				<view class="iconfont icon-arrows_right"></view>
			</view>
			<!-- #endif -->
		</view>
		<view class="fllow_form_text">
			<view style="flex-shrink: 0;"><text>*</text>详细地址</view>
			<view class="form_right">
				<textarea 
					class="detailText"
					:class="address_detail ? 'valueActive' : '' " 
					v-model="address_detail" 
					placeholder="填写详细地址" 
					placeholder-style="font-size:30rpx;color: #999;" 
				/>
			</view>
		</view>
		<form-item v-for="(item,index) in listForm" :key="index" :isNotRequired="isRequired" :objInfo="item" @changeData="changeData"></form-item>
		<view class="" v-if="isAddContact">
			<view class="info_head">联系人信息</view>
			<form-item v-for="(citem,cindex) in contactsForm" :key="cindex" :isNotRequired="isRequired" :objInfo="citem" @changeData="changeContactData"></form-item>
		</view>
		<!-- 审核信息 -->
		<examine-template :flag="false" :remindPeople="remindPeople" title="提醒谁看" @delExamine="delRemind"></examine-template>
		<view id="btn" class="btn_g bg-blue button-hover round" @click="$noMultipleClicks(subSave)">
			保存
		</view>
		<!-- <view class="bot_btn">
			<view class="b_btn" @click="$noMultipleClicks(subSave)">保存</view>
			<view class="b_btn" style="width:440rpx" @click="$noMultipleClicks(saveAdd)">保存并添加联系人</view>
		</view> -->
		<view class="safebottom"></view>	

		<!-- 位置选择 -->
		<u-select 
			v-model="show" 
			mode="mutil-column-auto" 
			value-name="code" 
			label-name="name" 
			child-name="children" 
			:list="addressList"
			 @confirm="confirm"
		></u-select>
		
		<!-- 选择门店 -->
		<select-customer ref="customerChild" @getCustomerinfo="sureSelect"></select-customer>
	</view>
</template>

<script>
	import {
		BASE_IMG_URL
	} from '@/util/api.js'
	import {
		dateTimePicker,
		getMonthDay
	} from '@/util/dateTimePicker.js'
	import {
		netSetForm,
		netStaffDefaultReviewer,
		uploadUrl
	} from '@/api/index.js'
	import formItem from '@/components/form/formitem.vue'
	import {
		checkPhone,
		checkCNPhone
	} from '@/util/util.js'
	import {
		pageJumps
	} from "./createCust_page_jumps.js";
	import {
		netAddkehu,
		netSelect,
		netPrevCustomer,
		netCheckName
	} from '@/api/kehu.js'
	import { netCluesDetail } from '@/api/clues.js'
	import { commonF } from '@/common/common'
	const citysJSON = require('@/util/address.js');
	import selectCustomer from '@/components/selectCustomer.vue'
	export default {
		mixins: [pageJumps,commonF],
		components: {
			formItem,
			selectCustomer
		},
		data() {
			return {
				BASE_IMG_URL: BASE_IMG_URL,
				noClick: true, //防止  重复点击
				name: '', //门店名称
				subname: '', //助记名称
				industry: '', //门店行业
				level: '', //门店星级
				source: '', //门店来源
				follow: '', //跟进状态
				address_dingwei: '', //地址定位
				address_detail: '', //详细地址
				lng: '', //经度
				lat: '', //纬度
				parent_id: '', //上级公司
				parentList: [],
				plist: [],
				parentIndex: null,
				parentName: '',
				BASE_IMG_URL: BASE_IMG_URL,
				location: '', //所在地区
				show: false,
				//联系人
				remindPeople: [], //提醒谁看
				remindType:'customer',
				sourceIndex: null,
				listForm: [], //门店附加信息
				isAddContact:false,   //是否添加联系人
				contactsForm:[],   //联系人信息
				list: [],
				isRequired:false,  //是否只展示必填字段
				//地址
				addressList:[],
				parentDisabled:false,
				leads_id:'',  //线索ID
			}
		},
		onLoad(options) {
			this.addressList = citysJSON.citys
			if(options.id) {
				this.parent_id = options.id
				uni.setNavigationBarTitle({
					title:'新建子公司'
				})
			}
			//转客
			if(options.leads_id) {
				this.leads_id = options.leads_id
			}
			this.listForm = []
			this.init()
			// #ifdef MP-WEIXIN
			this.initTime()
			// #endif
		},
		onShow() {
			
		},
		methods: {
			//切换是否只展示必填字段
			changeRequired(e) {
				this.isRequired = e.detail.value
			},
			init() {
				if(this.leads_id){
					return Promise.all([this.getForm(),this.getPrevCoustomer(),this.getclubInfo()])
				}else{
					return Promise.all([this.getForm(),this.getPrevCoustomer()])
				}
			},
			initTime() {
				// 获取完整的年月日 时分秒，以及默认显示的数组
				let obj = dateTimePicker()
				// 精确到分的处理，将数组的秒去掉
				let lastArray = obj.dateTimeArray.pop();
				let lastTime = obj.dateTime.pop();
				let lastDate = obj.dateArray.pop()
			
				this.dateTimeArray = obj.dateTimeArray
				this.dateTime = obj.dateTime
				this.dateArr = obj.dateArray
			},
			//获取线索信息
			getclubInfo() {
				netCluesDetail({id:this.leads_id}).then(res=>{
					this.name = res.data.name
					this.address_detail = res.data.address_detail
			
					//处理默认 门店附加信息
					this.listForm.forEach(item=>{
						item.value = res.data[item.id]
					})
					res.data.name='';
				})
			},
			// 选择成员
			moreClick() {
				uni.navigateTo({
					url: '/pagesA/crm/selectMember/selectMember'
				})
			},
			getForm() {
				netSetForm({
					type: 'customer'
				}).then(res => {
					res = res.data
					let customerForm = res.data
					this.isAddContact = res.isAddContact
					let arr = []
					customerForm.forEach((item, index) => {
						if (item.id != 'name') {
							item.value = ''
							arr.push(item)
						}else{
							item.value = item.value?item.value:''
						}
					})
					this.listForm = arr
					if(res.isAddContact){
						this.getContactForm()
					}
				})
			},
			getContactForm() {
				netSetForm({type:'contacts'}).then(res=>{
					let arr = res.data.data
					arr.forEach(ele=>{
						ele.value = ele.value?ele.value:''
					})
					this.contactsForm = arr
				})
			},
			changeData(name, value, obj) {
				let arr = this.listForm
				arr.forEach((ele, index) => {
					if (ele.id == name) {
						ele.value = value
						this.$set(this.listForm, index, ele)
					}
				})
			},
			changeContactData(name,value,obj) {
				let arr = this.contactsForm
				arr.forEach((ele, index) => {
					if (ele.id == name) {
						ele.value = value
						this.$set(this.contactsForm, index, ele)
					}
				})
			},
			getPrevCoustomer() {
				netPrevCustomer().then(res => {
					this.parentList = res.data
					this.handleData(res.data)
					if (this.parent_id) {
						//设置默认 上级公司
						this.handleParent()
						this.parentDisabled = true
					} else {
						this.parentDisabled = false
					}
				})
			},
			handleData(data) {
				let arr = []
				data.forEach(ele => {
					arr.push(ele.name)
				})
				this.plist = arr
			},
			handleParent() {
				let id = this.parent_id
				this.parentList.forEach((ele, index) => {
					if (ele.id == id) {
						this.parentIndex = index
						this.parentName = ele.name
					}
				})
			},
			//姓名查重
			checkName() {
				let name = this.name
				if (!name) {
					uni.showToast({
						title: '请填写门店姓名',
						icon: 'none',
						duration: 2000
					})
					return
				}
				netCheckName({
					name
				}).then(res => {
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 2000
					})

				})
			},
			//改变上级
			changeParent() {
				this.$refs.customerChild.init()
			},
			sureSelect(obj) {
				this.parentName = obj.name
				this.parent_id = obj.id
			},
			// 详细地址
			detailAddClick() {
				uni.chooseLocation({
					success: res => {
						this.lat = res.latitude //纬度
						this.lng = res.longitude //经度
						this.address_detail = res.name //位置名称
						this.address_dingwei = res.address //详细地址
					},
					fail: err => {
						if (err.errMsg == 'chooseLocation:fail auth deny') {
							uni.showToast({
								title: '请允许使用位置信息',
								icon: 'none'
							})
						}
					}
				})
			},
			confirm(e) {
				this.address_dingwei = e[0].label+e[1].label+e[2].label
			},
			//保存并添加联系人
			saveAdd() {
				this.subSave(2)
			},
			//完成
			subSave(type) {
				let {
					name, //门店名称
					location, //所在地区
					address_dingwei,
					address_detail, //详细地址
					lng, //经度
					lat, //纬度
					parent_id, //上级公司
				} = this
				if (!name) {
					uni.showToast({
						title: '请填写门店名称',
						icon: 'none',
						duration: 2000
					})
					return
				}
				if (!address_dingwei) {
					uni.showToast({
						title: '请选择所在位置',
						icon: 'none',
						duration: 2000
					})
					return
				}
				if(!address_detail){
					uni.showToast({
						title: '请填写详细地址',
						icon: 'none',
						duration: 2000
					})
					return
				}
				let params = {}
				// 附加信息
				let customer = this.checkCommonForm(this.listForm)
				if(!customer){
					return
				}
				customer = Object.assign({}, customer, {
					name,
					address_detail,
					lng,
					lat,
					address: address_dingwei,
					parent_id
				})
				//联系人信息
				let  contacts = {}
				if(this.isAddContact){
					contacts = this.checkCommonForm(this.contactsForm)
					if(!contacts){
						return
					}
				}
				let remindsid = '' //提醒谁看
				let remarr = []
				this.remindPeople.forEach(ele => {
					remarr.push(ele.id)
				})
				remindsid = remarr.join(',')
				let record = {
					reminds_id: remindsid,
				}
				params.record = record
				params.customer = customer
				params.contacts = contacts
				if(this.leads_id){
					params.leads_id = this.leads_id
				}
				console.log(params,'提交')
				netAddkehu(params).then(res => {
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 2000
					})
					setTimeout(() => {
						if(type && type == 2){
							uni.redirectTo({
								url:'/pagesA/crm/linkman/newLinkman/newLinkman?id='+res.data.id
							})
						}else{
							uni.redirectTo({
								url: '/pagesA/crm/customerManagement/customerManagement'
							})
						}
					}, 2000)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.bot_btn{
		width:750rpx;
		display: flex;
		justify-content: space-around;
		align-items: center;
		margin-bottom:45rpx;
		.b_btn{
			width:200rpx;
			height: 88rpx;
			background: $uni-text-color;
			border-radius: 44rpx;
			font-size: 32rpx;
			color: #fff;
			line-height: 88rpx;
			text-align: center;
		}
	}

	.linktab {
		padding: 0rpx 24rpx 20rpx 0;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.link_right {
			display: flex;
			justify-content: flex-end;
			align-items: center;

			.linkbtn {
				margin-right: 15rpx;
				width: 120rpx;
				height: 60rpx;
				border-radius: 45rpx;
				font-size: 24rpx;
				color: #666;
				text-align: center;
				line-height: 58rpx;
				border: 1rpx solid #DDDDDD;
			}

			.linkbtnactive {
				background: $uni-text-color;
				border: 1rpx solid $uni-text-color;
				color: #fff;
			}
		}
	}
	.imglist {
		padding: 24rpx 34rpx;
		background: #fff;
		border-bottom: 1rpx solid #EAEAEA;
		display: flex;
		justify-content: flex-start;
		align-items: center;
		flex-wrap: wrap;

		.imgbox {
			width: 160rpx;
			height: 160rpx;
			margin-right: 14rpx;
			position: relative;
			margin-bottom: 24rpx;
			border: 1rpx solid #EAEAEA;

			.delimg {
				width: 32rpx;
				height: 32rpx;
				border-radius: 50%;
				position: absolute;
				right: -16rpx;
				top: -16rpx;
				background: rgba(0, 0, 0, 0.5);
				z-index: 2;
				color:#fff;
				font-size:26rpx;
				display: flex;
				justify-content: center;
				align-items: center;
			}

			.selfimg {
				width: 160rpx;
				height: 160rpx;
			}
		}
	}

	.fllow_file_li {
		padding: 24rpx 34rpx;
		background: #fff;
		border-bottom: 1rpx solid #EAEAEA;

		.fil_li {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 14rpx;

			.fileimg {
				width: 20rpx;
				height: 20rpx;
			}

			.filename {
				font-size: 24rpx;
				color: #999999;
			}

			.closeimg {
				width: 32rpx;
				height: 32rpx;
			}
		}
	}

	.create_customer {
		font-size: 28rpx;
		.info_head {
			height: 80rpx;
			line-height: 80rpx;
			font-size: 30rpx;
			color: #999;
			padding-left: 30rpx;
		}

		.mar_top {
			margin-top: 30rpx;
		}
		.fllow_form_text{
			background-color: #fff;
			border-bottom: 1rpx solid #EAEAEA;
			padding: 30rpx 20rpx;
			color: #999;
			font-size: 32rpx;
			.theme {
				color: #5ca9fe;
			}
			text {
				color: #f00;
				margin-left: 10rpx;
			}
			.detailText{
				width:85%;
				background:#f8f8f8;
				border-radius: 10rpx;
				height:120rpx;
				padding:10rpx;
				margin:20rpx auto 0;
			}
		}
		.fllow_form {
			display: flex;
			justify-content: space-between;
			align-items: center;
			background-color: #fff;
			border-bottom: 1rpx solid #EAEAEA;
			padding: 30rpx 20rpx;
			color: #999;
			font-size: 32rpx;
			.nameinput {
				width: 450rpx;
				margin-left: 20rpx;
			}
			.form_right {
				display: flex;
				align-items: center;
				justify-content: flex-end;
				color: #999;
				text-align: right;
				.location {
					width: 450rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}
			}
			.theme {
				color: #5ca9fe;
			}
			text {
				color: #f00;
				margin-left: 10rpx;
			}

			.form_input {
				input {
					width: 300rpx;
					text-align: right;
					height: 100rpx;
					line-height: 100rpx;
					text-align: right;
					margin-right: 10rpx;
				}
			}
		}

		.fllow_area {
			background-color: #fff;
			// padding: 0 70rpx;
			margin-bottom: 30rpx;
			padding-bottom: 30rpx;
			color: #999;

			.area_head {
				height: 100rpx;
				line-height: 100rpx;
				font-size: 32rpx;
				padding: 0 55rpx;

				text {
					color: #f00;
				}
			}

			textarea {
				width: 80%;
				height: 300rpx;
				padding: 30rpx;
				background: #f8f8f8;
				margin: 0 auto;
			}
		}
	}
</style>
