<template>
	<view class="addCustomer-content">
		<view class="content-form">
			<u-form :model="form" ref="uForm"  label-width="180" :error-type="['toast']">
				<u-form-item label="客户名称" required ><u-input v-model.trim="form.customerName" placeholder="请输入客户名称(最多30个字符)" input-align="right" :maxlength="60"/></u-form-item>
				<u-form-item label="客户地址" required >
					<u-input v-model="areaInfo.label" :disabled="true" input-align="right" placeholder="请选择客户地址" @click="openAddress"/>
					<u-icon name="arrow-right" color="#C0C4CC" slot="right"  @click="openAddress"></u-icon>
				</u-form-item>
				<u-form-item label="详细地址">
					<textarea :auto-height="true" input-align="right" placeholder="请输入详细地址(最多60个字符)" :maxlength="60" v-model="form.customerAddr" class="receiveAddress" placeholder-style="color:#C0C4CC;font-size:28upx;"/>
				</u-form-item>
				<u-form-item label="联系人">
					<u-input v-model="form.contactName" input-align="right" placeholder="请输入联系人(最多30个字符)" :maxlength="11"/>
				</u-form-item>
				<u-form-item label="联系电话">
					<u-input v-model.trim="form.contactTel" placeholder="请输入联系电话(最多15个字符)" input-align="right" :maxlength="15"/>
				</u-form-item>
				<u-form-item label="联系手机"><u-input v-model.trim="form.contactMobile" placeholder="请输入联系手机(最多11位字符)" input-align="right" :maxlength="11"/>
				</u-form-item>
				<u-form-item label="价格类型" required >
					<v-select ref="priceType" :disabled="true" placeholder="请选择价格类型" @itemChange="chooseType" code="PRICE_TYPE" style="width: 100%"></v-select>
				</u-form-item>
				<u-form-item label="收款方式" required prop="settleStyleSn">
					<u-input v-model="settleStyleName" input-align="right" :disabled="true" placeholder="请选择收款方式" @click="showSettle=true"/>
					<u-icon name="arrow-right" color="#C0C4CC" slot="right"></u-icon>
				</u-form-item>
				<u-form-item label="客户传真">
					<u-input v-model="form.fax" input-align="right" placeholder="请输入客户传真(最多30个字符)" :maxlength="30"/>
				</u-form-item>
				<u-form-item label="客户类型">
					<u-input v-model="custTypeName" input-align="right" :disabled="true" placeholder="请选择客户类型"  @click="showCust=true"/>
					<u-icon name="arrow-right" color="#C0C4CC" slot="right"  @click="showCust=true"></u-icon>
				</u-form-item>
				<u-form-item label="配送方式" >
					<v-select ref="dispatchType" :disabled="true" placeholder="请选择配送方式" @itemChange="choosedispatchType" code="DISPATCH_TYPE" style="width: 100%"></v-select>
				</u-form-item>
			</u-form>
		</view>
		<view class="form-footer-btn">
			<u-button class="handle-btn" size="medium" shape="circle" hover-class="none" @click="customerSubmit" :custom-style="customBtnStyle">保存</u-button>
		</view>
		<!-- 省市区 -->
		<Address ref="applyAddress" :isJrMini="false" @onConfirm="areaConfirm"></Address>
		<!-- 选择收款方式 -->
		<u-picker v-model="showSettle" title="收款方式"  @confirm="settleStyleChange" :range="settleTypeList" range-key="name" mode="selector" :default-selector="JSON.parse(JSON.stringify(defaultSettleIndex))"></u-picker>
		<!-- 客户类型 -->
		<u-picker v-model="showCust" title="客户类型" @confirm="custTypeChange" :range="custTypeList" range-key="name" mode="selector" :default-selector="JSON.parse(JSON.stringify(defaultCustIndex))"></u-picker>
	</view>
</template>

<script>
	import {settleStyleQueryAll} from '@/api/sales.js'
	import {customerDetail,custType,custSave,employeeAllList} from '@/api/customer.js'
	import Address from '@/components/address.vue'
	import vSelect  from  '@/components/select/v-select.vue'
	import { isvalidPhone } from '@/libs/validate.js'
	export default{
		components:{Address,vSelect},
		data(){
			return{
				customerData:null, // 客户信息
				customBtnStyle: {  //  自定义按钮样式
					margin:0
				},
				// 客户form
				form:{
					id: null,
					customerName: '', // 客户名称
					contactTel: '', //  联系电话
					contactMobile: '', //  联系手机
					contactName: '', //  联系人
					provinceSn: undefined, //  省
					provinceName: '',
					citySn: undefined, // 市
					cityName: '',
					countySn: undefined, // 区
					countyName: '',
					customerAddr: '', //  详细地址
					fax: '', //  客户传真
					dispatchType: undefined, //  配送方式
					satelliteFlag: 0, //  是否卫星仓客户
					customerTypeSn: undefined, //  客户类型
					priceType: '', //  价格类型
					settleStyleSn: undefined //  收款方式
				},
				showSettle:false,//结算方式初始值
				settleTypeList:[],//收款方式列表
				settleStyleName:'',//收款方式名称
				custTypeList:[] ,// 客户类型数据
				showCust:false, //客户类型下拉弹窗初始值
				custTypeName:'', // 客户类型名称
				areaInfo: {},  //  省市区
				areaIdArr: [],  //  省市区id数组
				defaultSettleIndex:[0] ,//选中收款方式的下标
				defaultCustIndex:[0],//选中客户类型的下标
			}
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		onLoad(option) {
			this.getSettleStyle()
			this.getCustomerList()
		},
		methods:{
			// 选择价格类型
			chooseType(val){
				console.log(val,'vai')
				this.form.priceType=val
			},
			// 选择配送方式
			choosedispatchType(val){
				this.form.dispatchType=val
			},
			//  选择省区市
			areaConfirm(e) {
				console.log('已选择的省市区', e)
				this.areaInfo = e
				this.areaIdArr = [ e.provinceCode, e.cityCode, e.areaCode ]
				this.form.provinceSn=e.provinceCode
				this.form.citySn=e.cityCode
				this.form.countySn=e.areaCode
				this.form.provinceName=e.labelArr[0]
				this.form.cityName=e.labelArr[1]
				this.form.countyName=e.labelArr[2]
			},
			openAddress(){
				//  省市区  回显     参数为省市区id数组
				// console.log(this.areaIdArr,'this.areaIdArr')
				this.$refs.applyAddress.open(this.areaIdArr)
			},
			brandClean(){
				this.settleStyleName=''
				this.form.settleStyleSn=undefined
			},
			// 获取客户类型列表
			getCustomerList(){
				custType({}).then(res=>{
					if(res.status==200){
						this.custTypeList=res.data
					}else{
						this.custTypeList=[]
					}
				})
			},
			// 获取结算方式列表
			getSettleStyle(){
				settleStyleQueryAll({}).then(res=>{
					if(res.status==200){
						this.settleTypeList=res.data
					}else{
						this.settleTypeList=null
					}
				})
			},
			// 收款方式change
			settleStyleChange(e){
				console.log(e,'data')
				this.defaultSettleIndex=[e]
				this.settleStyleName=this.settleTypeList[e].name
				this.form.settleStyleSn=this.settleTypeList[e].settleStyleSn
			},
			// 客户类型change
			custTypeChange(e){
				this.defaultCustIndex=[e]
				this.custTypeName=this.custTypeList[e].name
				this.form.customerTypeSn=this.custTypeList[e].customerTypeSn
			},
			// 保存 (新增客户)
			customerSubmit(){
				console.log('保存===========')
				this.$refs.uForm.validate(valid => {
					if (valid) {
						console.log('验证通过');
						if (this.form.customerName ==='') {
							uni.showToast({
								title: '请输入客户名称',
								icon: 'none'
							})
							return false
						}
						if(this.form.contactMobile){
							if (!isvalidPhone(this.form.contactMobile)) {
								uni.showToast({
									title: '请输入正确的联系手机',
									icon: 'none'
								})
								return false
							}
						}
						//  省市区
						if(this.areaIdArr.length == 0){
							uni.showToast({icon: 'none',title: '请选择省市区'})
							return
						}
						if (this.form.priceType ==undefined) {
							uni.showToast({
								title: '请选择价格类型',
								icon: 'none'
							})
							return false
						}
						if (this.form.settleStyleSn ==undefined) {
							uni.showToast({
								title: '请选择收款方式',
								icon: 'none'
							})
							return false
						}
						const params=Object.assign({},this.form,this.areaIdArr)
						console.log(params,'params')
						if(this.form.id) {
							params.id = this.form.id
						}
						custSave(params).then(res=>{
							if(res.status == 200) {
								uni.showToast({
									title: res.message,
									icon: 'none'
								})
								setTimeout(()=>{
									uni.redirectTo({
										url:'/pages/sales/addSales?pageType=add&nowId='+res.data.id
									})
								},300)
							} 
						})
					} else {
						console.log('验证失败');
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.addCustomer-content{
		width: 100%;
		height: 100vh;
		background-color: #fff;
		padding:0 40upx;
		display: flex;
		flex-direction: column;
		.content-customerName{
			// padding: 10upx;
			// border-radius: 10upx;
			// border: 1upx solid #e5e5e5;
		}
		.content-form{
			flex: 1;
			// box-shadow:0 8upx 24upx rgba(0,0,0,.1);
			// border: 1upx solid #e5e5e5;
			.receiveAddress{
				width: 100%;
				line-height: 1.5em;
				box-sizing: border-box;
				font-style: normal;
				text-align: end;
			}
		}
		.content-form:not(:last-child){
			border-bottom: none;
		}
		.form-footer-btn{
			background-color: #fff;
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			padding: 16upx 20upx 12upx;
			z-index: 999;
			.handle-btn{
				width: 100%;
				background-color: rgb(51, 95, 182);
				color: #fff;
			}
		}
	}
</style>
