<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_right" >
				<view class="ri_label" :class="contractObj.id ? 'valueActive' : '' ">{{contractObj.id ? contractObj.num: '点击选择'}}</view>
			</view>
		</view>
		<view class="fllow_form" v-if="!isRequired">
			<view style="margin-left:15rpx;">回款期数</view>
			<picker :range="numList" :value="numIndex" @change="changeNum" :range-key="'num'">
				<view class="form_right">
					<view class="ri_label" :class="numList[numIndex] ? 'valueActive' : '' ">{{numList[numIndex] ? numList[numIndex].num : '点击选择'}}</view>
					<view class="iconfont icon-arrows_right"></view>
				</view>
			</picker>
		</view>
		<form-item v-for="(item,index) in otherForm" :isNotRequired="isRequired" :key="index" :objInfo="item" @changeData="changeData"></form-item>
		<!-- 固定审批 -->
		<fixed-approval v-if="approvalPro.status == 1" :list="approvalPro.stepList"></fixed-approval>
		<!-- 审核信息 -->
		<examine-template v-if="approvalPro.status == 0" :remindPeople="remindPeople" @delExamine="delRemind"></examine-template>
		<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 { BASE_IMG_URL } from '@/util/api.js'
	import { netSelect, netPaymentNum, netAddPayment, netPrevCustomer, netContractList, netPaymentNumber } from '@/api/kehu.js'
	import { netSetForm, netStaffDefaultReviewer, netApprovalProcess } from '@/api/index.js'
	import formItem from '@/components/form/formitem.vue'
	import { commonF } from '@/common/common'
	import fixedApproval from '@/components/fixedApproval.vue'
	import selectCustomer from '@/components/selectCustomer.vue'
	import selectContract from '@/components/selectContract.vue'
	
	export default {
		mixins:[commonF],
		components:{
			formItem,
			fixedApproval,
			selectCustomer,
			selectContract
		},
		data() {
			return {
				noClick:true,    //防止  重复点击
				type:[],   //回款方式 
				numList:[],   //回款  期数
				numIndex:0,
				info:null,   //合同信息
				customer_id:'',   //客户id
				contract_id:'',  //合同id
				number:'',   //回款  编号 
				money:'',   //回款  金额
				return_time:'',   //  回款  日期
				return_type:'',   //回款  方式
				flow_staff_id:'',   //流程 审批人id
				BASE_IMG_URL:BASE_IMG_URL,
				remindPeople:[], //提醒谁看
				remindType:'contract',
				otherForm:[],
				//客户列表
				customerList:[],
				customerIndex:null,
				customerObj:{},
				//合同列表
				contractObj:{},
				contractList:[],
				contractIndex:null,
				number:'',  //回款编号
				approvalPro:{},   //审批流程
				isRequired:false,  //是否只展示必填字段
			}
		},
		onLoad(options) {
			if(options.kehuid){
				this.customer_id = options.kehuid
			}
			if(options.id){
				this.contract_id = options.id
				//获取 回款  期数
				this.getNum()
			}
			//客户列表
			this.getKehu()
			//获取 回款方式 
			this.getType()
			//获取 审批流程
			this.getProcess()
		},
		//监听  页面 卸载
		onUnload() {
			uni.removeStorageSync('remindlist')
		},
		methods: {	
			//切换是否只展示必填字段
			changeRequired(e) {
				this.isRequired = e.detail.value
			},
			//审批流程
			getProcess() {
				netApprovalProcess({type:'receivables'}).then(res=>{
					this.approvalPro = res.data
				})
			},
			getForm() {
				netSetForm({type:'examine'}).then(res=>{
					res = res.data.data
					res.forEach(ele=>{
						if(ele.id == 'return_time'){
							let date = new Date()
							let year = date.getFullYear()
							let month = date.getMonth()+1
							let day = date.getDate()
							let time = year+'-'+month+'-'+day
							ele.value = time
						}
						if(ele.id == 'money'){
							ele.value = this.contractObj.id?this.contractObj.receivables.be_money:''
						}
						if(ele.id == 'return_type'){
							ele.value = ele.config.content?ele.config.content[0].label:''
						}
						if(ele.id == 'number'){
							ele.value = this.number
						}
					})
					this.otherForm = res
				})
			},
			changeData(name,value) {
				let arr = this.otherForm
				arr.forEach((ele,index) => {
					if(ele.id == name){
						ele.value = value
						this.$set(this.otherForm,index,ele)
					}
				})
			},
			getType() {
				netSelect().then(res=>{
					this.type = res.data['回款方式']
				})
			},
			//客户列表
			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()
				this.customerIndex = e.detail.value
				this.index = null
				//获取合同列表
				this.getContract()
			},
			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
							}
						})
					}
					//获取回款编号
					this.getNumber()
				})
			},
			getNumber() {
				netPaymentNumber().then(res=>{
					this.number = res.data.number
					//获取 form
					this.otherForm = []
					this.getForm()
				})
			},
			changeContract(e) {
				this.$refs.contractChild.init(this.customerObj.id)
			},
			sureContract(obj) {
				this.contractObj = obj
				this.getNum()
			},
			//获取  回款  期数
			getNum() {
				let params = {
					contract_id:this.contractObj.id?this.contractObj.id:this.contract_id
				}
				netPaymentNum(params).then(res=>{
					this.numList = res.data
				})
			},
			//改变  回款  期数
			changeNum(e) {
				this.numIndex = e.detail.value
			},
			//改变 回款  方式
			changeType(e) {
				this.return_type = this.type[e.detail.value]
			},
			//计划 回款  日期
			changeTime(e) {
				this.return_time = e.detail.value
			},
			saveFollow() {
				let {
					info,
					remindPeople
				} = this
				if(!this.customerObj.id){
					uni.showToast({
						title:'请选择关联客户',
						icon:'none'
					})
					return
				}
				if(!this.contractObj.id){
					uni.showToast({
						title:'请选择关联合同',
						icon:'none'
					})
					return
				}
				let params = this.checkCommonForm(this.otherForm)
				if(!params){
					return
				}
				if((!remindPeople || remindPeople.length == 0) && this.approvalPro.status == 0) {
					uni.showToast({
						title:'请选择审批人',
						icon:'none'
					})
					return
				}
				let arr = []
				remindPeople.forEach(ele=>{
					arr.push(ele.id)
				})
				params = Object.assign({},params,{
					customer_id: this.customerObj.id?this.customerObj.id:'',
					contract_id: this.contractObj?this.contractObj.id:'',
					plan_id:this.numList[this.numIndex]?this.numList[this.numIndex].id:'',   //回款  期数id
					flow_staff_ids:this.approvalPro.status == 0 ? arr.join(',') : ''
				})
				netAddPayment(params).then(res=>{
					uni.showToast({
						title: res.msg,
						icon:'none'
					})
					setTimeout(()=>{
						uni.navigateBack({
							delta:1
						})
					},2000)
				})
			}
		}
	}
</script>

<style lang="scss">
	.info_head{
		height: 80rpx;
		line-height: 80rpx;
		font-size: 30rpx;
		color: #999;
		padding-left: 69rpx;
	}
.valueActive{
	color:#333;
}
.new_fllow {
	font-size: 28rpx;
	.fllow_form {
		display: flex;
		justify-content: space-between;
		align-items: center;
		background-color: #fff;
		border-bottom: 1rpx solid #EAEAEA;
		font-size: 32rpx;
		color: #999;
		padding: 30rpx 20rpx;
		.form_right {
			display: flex;
			align-items: center;
			color: #666;
			font-size: 32rpx;
			.ri_label{
				color:#999;
			}
			image {
				width: 40rpx;
				height: 40rpx;
				margin: 28rpx 5rpx 0 0;
			}
		}
		text {
			color: #f00;
		}
		.form_input {
			input {
				text-align: right;
				color: #333;
			}
		}
		.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 30rpx;
		margin-bottom: 30rpx;
		.area_head {
			height: 100rpx;
			line-height: 100rpx;
			text {
				color: #f00;
			}
		}
		textarea {
			width: 100%;
			height: 300rpx;
			background: #f8f8f8;
		}
	}
	.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>
