<template>
	<view class="new_fllow">
		<view class="top_required">
			只展示<span style="color:#f00">必填</span>字段<switch :checked="isRequired" color="#008EFF" @change="changeRequired" />
		</view>
		<view class="fllow_form">
			<view><text>*</text>关联客户</view>
			<view class="form_right" @click="changeCustomer">
				<view class="ri_label" :class="customerObj.name ? 'valueActive' : '' ">{{customerObj.name ? customerObj.name : '点击选择'}}</view>
				<view class="iconfont icon-arrows_right"></view>
			</view>
		</view>
		<view class="fllow_form">
			<view><text>*</text>关联合同</view>
			<view class="form_right" @click="changeContract">
				<view class="ri_label" :class="contractObj.id ? 'valueActive' : '' ">{{contractObj.name ? contractObj.name: '点击选择'}}</view>
				<view class="iconfont icon-arrows_right"></view>
			</view>
		</view>
		<view class="fllow_form">
			<view><text>*</text>合同编号</view>
			<view class="form_input" >
				<view class="ri_label" :class="contractObj.id ? 'valueActive' : '' ">{{contractObj.id ? contractObj.num: '点击选择'}}</view>
			</view>
		</view>
		<view class="fllow_form">
			<view><text>*</text>计划回款金额</view>
			<view class="form_input" >
				<input type="number" :class="money ? 'valueActive' : '' " v-model="money"  placeholder-style="font-size:30rpx;color: #999;" placeholder="请输入计划回款金额" class="">
			</view>
		</view>
		<view class="fllow_form">
			<view><text>*</text>计划回款期数</view>
			<view class="form_input" >
				<input type="number" :class="num ? 'valueActive' : '' " v-model="num"  placeholder-style="font-size:30rpx;color: #999;" placeholder="请输入计划回款期数" class="">
			</view>
		</view>
		<view class="fllow_form">
			<view><text>*</text>计划回款日期</view>
			<picker mode="date" @change="changeTime">
				<view class="form_right">
					<view class="ri_label" :class="return_date ? 'valueActive' : '' ">{{return_date ? return_date : '点击选择'}}</view>
					<view class="iconfont icon-arrows_right"></view>
				</view>
			</picker>
		</view>
		<view class="fllow_form">
			<view><text>*</text>计划回款方式</view>
			<picker :value="index" @change="changeType" :range="type">
				<view class="form_right">
					<view class="ri_label" :class="return_type ? 'valueActive' : '' ">{{return_type ? return_type : '点击选择'}}</view>
					<view class="iconfont icon-arrows_right"></view>
				</view>
			</picker>
		</view>
		<view class="fllow_form">
			<view><text>*</text>提前几天提醒</view>
			<view class="form_input" >
				<input type="number" :class="remind ? 'valueActive' : '' " v-model="remind" placeholder-style="font-size:30rpx;color: #999;" placeholder="请输入提前几天提醒" class="">
			</view>
		</view>
		
		<view class="fllow_area" v-if="!isRequired">
			<view class="area_head">备注</view>
			<textarea v-model="remarks" :class="remarks ? 'valueActive' : '' "  placeholder="请输入备注" placeholder-class="ri_label" />
		</view>
		<view id="btn" class="btn_g bg-blue button-hover round" @click="$noMultipleClicks(saveFollow)">
			保存
		</view>
		
		<!-- 选择客户 -->
		<select-customer ref="customerChild" @getCustomerinfo="sureCustomer"></select-customer>
		<!-- 选择合同 -->
		<select-contract ref="contractChild" @contractInfo="sureContract"></select-contract>
	</view>
</template>

<script>
	import { netSelect, netContractList, netAddPlan, netPrevCustomer } from '@/api/kehu.js'
	import selectCustomer from '@/components/selectCustomer.vue'
	import selectContract from '@/components/selectContract.vue'
	
	export default {
		components:{
			selectCustomer,
			selectContract
		},
		data() {
			return {
				noClick:true,    //防止  重复点击
				type:[],   //回款方式 
				index:null,
				info:null,   //合同信息
				customer_id:'',   //客户id
				contract_id:'',  //合同id
				num:'',
				money:'',   //计划 回款  金额
				return_date:'',   //计划  回款  日期
				return_type:'',   //计划 回款  方式
				remind:'',   //提前几天  提醒
				remarks:'',   //备注
				//客户列表
				customerList:[],
				customerObj:{},
				//合同列表
				contractList:[],
				contractIndex:null,
				contractObj:{},
				isRequired:false,  //是否只展示必填字段
			}
		},
		onLoad(options) {
			if(options.kehuid){
				this.customer_id = options.kehuid
				this.contract_id = options.id
			}
			this.getKehu()
		},
		onShow() {
			//获取 回款方式 
			this.getType()
		},
		//监听  页面 卸载
		onUnload() {
			
		},
		methods: {	
			//切换是否只展示必填字段
			changeRequired(e) {
				this.isRequired = e.detail.value
			},
			//客户列表
			getKehu() {
				netPrevCustomer().then(res=>{
					this.customerList = res.data
					if(this.customer_id){
						this.customerList.forEach((ele,index)=>{
							if(ele.id == this.customer_id){
								this.customerObj = ele
							}
						})
						//获取合同列表
						this.getContract()
					}
				})
			},
			//选择客户
			changeCustomer(e) {
				this.$refs.customerChild.init()
			},
			sureCustomer(obj) {
				this.customerObj = obj
				this.contractObj = {}
			},
			getContract() {
				let params = {
					customer_id:this.customerObj.id?this.customerObj.id:this.customer_id
				}
				netContractList(params).then(res=>{
					this.contractList = res.data
					if(this.contract_id){
						this.contractList.forEach((item,index)=>{
							if(item.id == this.contract_id){
								this.contractObj = item
							}
						})
					}
				})
			},
			// 选择合同
			changeContract() {
				this.$refs.contractChild.init(this.customerObj.id)
			},
			sureContract(obj) {
				this.contractObj = obj
			},
			getType() {
				netSelect().then(res=>{
					this.type = res.data['回款方式']
				})
			},
			//改变 回款  方式
			changeType(e) {
				this.return_type = this.type[e.detail.value]
			},
			//计划 回款  日期
			changeTime(e) {
				this.return_date = e.detail.value
			},
			saveFollow() {
				let {
					money,   //计划 回款  金额
					return_date,   //计划  回款  日期
					return_type,   //计划 回款  方式
					remind,   //提前几天  提醒
					remarks,   //备注
					info,
					num,   //计划 回款  期数
				} = this
				if(!this.customerObj.id){
					uni.showToast({
						title:'请选择关联客户',
						icon:'none'
					})
					return
				}
				if(!this.contractObj.id){
					uni.showToast({
						title:'请选择关联合同',
						icon:'none'
					})
					return
				}
				if(!money) {
					uni.showToast({
						title:'请输入计划回款金额',
						icon:'none'
					})
					return
				}
				if(!num) {
					uni.showToast({
						title:'请输入计划回款期数',
						icon:'none'
					})
					return
				}
				if(!return_date) {
					uni.showToast({
						title:'请选择计划回款日期',
						icon:'none'
					})
					return
				}
				if(!return_type) {
					uni.showToast({
						title:'请选择计划回款方式',
						icon:'none'
					})
					return
				}
				if(!remind) {
					uni.showToast({
						title:'请填写提前几天提醒',
						icon:'none'
					})
					return
				}
				let params = {
					customer_id: this.customerObj.id?this.customerObj.id:'',
					contract_id: this.contractObj.id?this.contractObj.id:'',
					money,   //计划 回款  金额
					return_date,   //计划  回款  日期
					return_type,   //计划 回款  方式
					remind,   //提前几天  提醒
					remarks,
					num
				}
				netAddPlan(params).then(res=>{
					uni.showToast({
						title:res.msg,
						icon:'none'
					})
					setTimeout(()=>{
						uni.navigateBack({
							delta:1
						})
					},2000)
				})
			}
		}
	}
</script>

<style lang="scss">
.valueActive{
	color:#333;
}
.new_fllow {
	font-size: 28rpx;
	.fllow_form {
		display: flex;
		justify-content: space-between;
		height: 100rpx;
		line-height: 100rpx;
		background-color: #fff;
		border-bottom: 1rpx solid #EAEAEA;
		// padding: 0 24rpx;
		padding-left: 30rpx;
		color: #999;
		font-size: 32rpx;
		.form_right {
			display: flex;
			color: #333;
			font-size: 32rpx;
			padding-right: 30rpx;
			.ri_label{
				color:#999;
			}
			image {
				width: 40rpx;
				height: 40rpx;
				margin: 28rpx 5rpx 0 0;
			}
		}
		text {
			color: #f00;
			margin-left: 10rpx;
		}
		.form_input {
			margin-right: 30rpx;
			display: flex;
			align-items: center;
			input {
				// height: 100rpx;
				// line-height: 100rpx;
				text-align: right;
				margin-right: 10rpx;
			}
		}
		.fllow_button {
			margin-top: 15rpx;
		}
	}
	.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;
			}
			.selfimg{
				width:160rpx;
				height:160rpx;
			}
		}
	}
	.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 30rpx;
				text {
					color: #f00;
				}
			}
			textarea {
				width: 80%;
				height: 300rpx;
				padding: 30rpx;
				background: #f8f8f8;
				margin: 0 auto;
			}
		}
	.follow_task {
		height: 80rpx;
		line-height: 80rpx;
	}
	#btn{
		width: 500rpx;
		height: 88rpx;
		background: $uni-text-color;
		border-radius: 29rpx;
		font-size: 34rpx;
		color: #fff;
		line-height: 88rpx;
	}
}
</style>
