<template>
	<view class="evan-form-show">
		<view class="bottom-border">
			<view class="left-border"><text class="mgl10">基本信息</text></view>
		</view>
		<uni-forms :value="formData" ref="form" validate-trigger="bind" err-show-type="toast">
			<uni-forms-item required name="cusType" label="客户类型">
				<uni-data-checkbox v-model="cusType" :localdata="cusTypeList"></uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item name="code" required label="客户编号">
				<uni-easyinput disabled type="text" :inputBorder="true" v-model="code" placeholder="请输入客户编号"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="code" required label="客户经理">
				<picker disabled @change="managerChange" :value="managerVal" :range="employeeMsg" :range-key="'name'">
					<view class="picker-view flex-between disabled-bg">
						<text>{{managerVal||'请选择'}}</text>
						<text class="cuIcon-unfold lg text-gray"></text>
					</view>
				</picker>
			</uni-forms-item>	
		</uni-forms>	
		
		<!-- 个人客户 -->
		<uni-forms v-show="cusType == 0" :value="personageForm" ref="personageForm" validate-trigger="bind" err-show-type="toast">
			<uni-forms-item name="name" required label="客户姓名">
				<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="personageForm.name" placeholder="请输入客户姓名"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="idCard" required label="身份证号">
				<uni-easyinput :disabled="formDisabled" type="idcard" :inputBorder="true" maxlength="18" v-model="personageForm.idCard" placeholder="请输入身份证号"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="age" required label="年龄">
				<uni-easyinput :disabled="formDisabled" type="number" maxlength="3" :inputBorder="true" v-model="personageForm.age" placeholder="请输入年龄"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="phone" required label="联系方式">
				<uni-easyinput :disabled="formDisabled" type="number" maxlength="11" :inputBorder="true" v-model="personageForm.phone" placeholder="请输入手机号"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="gender" required label="性别">
				<uni-data-checkbox v-model="personageForm.gender" :localdata="genderList"></uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item name="maritalStatus" required label="婚姻状况">
				<picker :disabled="formDisabled" @change="maritalChange" :value="maritalStatusVal" :range="maritalList" :range-key="'name'">
					<view class="picker-view flex-between" :class="formDisabled ? 'disabled-bg' : ''">
						<text>{{maritalStatusVal!=='' ? maritalList[maritalStatusVal].name : '请选择'}}</text>
						<text class="cuIcon-unfold lg text-gray"></text>
					</view>
				</picker>
			</uni-forms-item>
			<uni-forms-item name="education" required label="学历">
				<picker :disabled="formDisabled" @change="educationChange" :value="educationVal" :range="educationList" :range-key="'name'">
					<view class="picker-view flex-between" :class="formDisabled ? 'disabled-bg' : ''">
						<text>{{educationVal!=='' ? educationList[educationVal].name : '请选择'}}</text>
						<text class="cuIcon-unfold lg text-gray"></text>
					</view>
				</picker>
			</uni-forms-item>
			<uni-forms-item name="employer" required label="工作单位">
				<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="personageForm.employer" placeholder="请输入单位名称"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="position" required label="职务">
				<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="personageForm.position" placeholder="请输入职务名称"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="workingYears" required label="从业年限(年)">
				<uni-easyinput :disabled="formDisabled" type="number" maxlength="3" :inputBorder="true" v-model="personageForm.workingYears" placeholder="请输入从业年限"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="socialSecurityNum" required label="社保编号">
				<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="personageForm.socialSecurityNum" placeholder="请输入社保编号"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="livingSituation" required label="居住情况">
				<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="personageForm.livingSituation" placeholder="请输入居住情况"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="addr" required label="联系地址">
				<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="personageForm.addr" placeholder="请输入联系地址"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="residenceAddr" required label="户籍地址">
				<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="personageForm.residenceAddr" placeholder="请输入户籍地址"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="businessSource" required label="业务来源">
				<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="personageForm.businessSource" placeholder="请输入业务来源"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="emergencyLinkman" required label="紧急联系人">
				<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="personageForm.emergencyLinkman" placeholder="请输入紧急联系人"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="emergencyLinkmanRelationship" required label="紧急联系人关系">
				<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="personageForm.emergencyLinkmanRelationship" placeholder="请输入紧急联系人关系"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="emergencyLinkmanPhone" required label="紧急联系人电话">
				<uni-easyinput :disabled="formDisabled" type="number" :maxlength="11" :inputBorder="true" v-model="personageForm.emergencyLinkmanPhone" placeholder="请输入紧急联系人电话"></uni-easyinput>
			</uni-forms-item>
		</uni-forms>		
		
		<!-- 企业客户 -->
		<uni-forms v-show="cusType == 1" :value="enterpriseForm" ref="enterpriseForm" validate-trigger="bind" err-show-type="toast">
			<uni-forms-item name="name" required label="客户姓名">
				<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="enterpriseForm.name" placeholder="请输入客户姓名"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="socialUnifiedCode" required label="社会统一代码">
				<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="enterpriseForm.socialUnifiedCode" placeholder="请输入社会统一代码"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="phone" required label="联系电话">
				<uni-easyinput :disabled="formDisabled" :maxlength="11" type="text" :inputBorder="true" v-model="enterpriseForm.phone" placeholder="请输入联系电话"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="registerTime" required label="注册时间">
				<picker :disabled="formDisabled" mode="date" :value="enterpriseForm.registerTime" @change="bindDateChange">
					<view class="picker-view flex-between" :class="formDisabled ? 'disabled-bg' : ''">
						<text>{{enterpriseForm.registerTime!=='' ? enterpriseForm.registerTime : '请选择'}}</text>
						<text class="cuIcon-unfold lg text-gray"></text>
					</view>
				</picker>
			</uni-forms-item>
			<uni-forms-item name="registeredCapital" required label="注册资金(万元)">
				<uni-easyinput :disabled="formDisabled" type="digit" :inputBorder="true" v-model="enterpriseForm.registeredCapital" placeholder="请输入注册资金"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="empNum" required label="员工人数">
				<uni-easyinput :disabled="formDisabled" type="number" :inputBorder="true" v-model="enterpriseForm.empNum" placeholder="请输入员工人数"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="registerAddr" required label="注册地址">
				<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="enterpriseForm.registerAddr" placeholder="请输入注册地址"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="linkman" required label="联系人">
				<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="enterpriseForm.linkman" placeholder="请输入联系人"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="linkPhone" required label="联系人电话">
				<uni-easyinput :disabled="formDisabled" type="number" :maxlength="11" :inputBorder="true" v-model="enterpriseForm.linkPhone" placeholder="请输入联系人电话"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="businessAddr" required label="经营地址">
				<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="enterpriseForm.businessAddr" placeholder="请输入经营地址"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="businessSource" required label="业务来源">
				<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="enterpriseForm.businessSource" placeholder="请输入业务来源"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="businessScope" required label="经营范围">
				<uni-easyinput :disabled="formDisabled" type="textarea" :inputBorder="true" v-model="enterpriseForm.businessScope" placeholder="请输入经营范围"></uni-easyinput>
			</uni-forms-item>
			<!-- 法人信息 -->
			<view class="bottom-border mab15">
				<view class="left-border"><text class="mgl10">法人信息</text></view>
			</view>
			<uni-forms-item name="legalName" required label="姓名">
				<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="enterpriseForm.legalName" placeholder="请输入法人姓名"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="legalGender" required label="性别">
				<uni-data-checkbox v-model="enterpriseForm.legalGender" :localdata="genderList"></uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item name="legalIdNumber" required label="身份证号码">
				<uni-easyinput :disabled="formDisabled" type="idcard" :inputBorder="true" v-model="enterpriseForm.legalIdNumber" placeholder="请输入身份证号码"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="legalHukouAddr" required label="户口所在地">
				<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="enterpriseForm.legalHukouAddr" placeholder="请输入户口所在地"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="legalPhone" required label="联系电话">
				<uni-easyinput :disabled="formDisabled" :maxlength="11" type="text" :inputBorder="true" v-model="enterpriseForm.legalPhone" placeholder="请输入联系电话"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="legalHomeAddr" required label="家庭地址">
				<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="enterpriseForm.legalHomeAddr" placeholder="请输入家庭地址"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="isExistRelated" required label="是否存在关联人">
				<uni-data-checkbox v-model="enterpriseForm.isExistRelated" :localdata="isExistList"></uni-data-checkbox>
			</uni-forms-item>
		</uni-forms>
		<!-- 展示关联人,传递handle默认false不禁用，传递companyId调用获取关联人接口 -->
		<!-- <linkMan v-if="enterpriseForm.isExistRelated==1" :handle="formDisabled" :companyId="companyId"></linkMan> -->
		
		<!-- 旧的关联人 -->
		<view v-if="cusType == 1 && enterpriseForm.isExistRelated == 1">				
			<block v-for="(item,index) in list" :key="index">
				<uni-forms :value="item" ref="item" validate-trigger="bind" err-show-type="toast">
				<view class="bottom-border mab15">
					<view class="left-border"><text class="mgl10">关联人{{index+1}}</text></view>
				</view>
				<uni-forms-item required name="type" label="关联人类型">
					<uni-data-checkbox v-model="item.type" :localdata="cusTypeList1"></uni-data-checkbox>
				</uni-forms-item>
				<view v-if="item.type == 0">
					<uni-forms-item name="relatedName" required label="关联人名称">
						<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="item.relatedName" placeholder="请输入关联人名称"></uni-easyinput>
					</uni-forms-item>
					<uni-forms-item name="phone" required label="联系电话">
						<uni-easyinput :disabled="formDisabled" :maxlength="11" type="text" :inputBorder="true" v-model="item.phone" placeholder="请输入联系电话"></uni-easyinput>
					</uni-forms-item>
					<uni-forms-item name="maritalStatus" required label="婚姻状况">
						<picker :disabled="formDisabled" @change="relatedMaritalChange($event,item)" :value="item.maritalStatusVal" :range="maritalList" :range-key="'name'">
							<view class="picker-view flex-between" :class="formDisabled ? 'disabled-bg' : ''">
								<text>{{item.maritalStatusVal!==''?maritalList[item.maritalStatusVal].name : '请选择'}}</text>
								<text class="cuIcon-unfold lg text-gray"></text>
							</view>
						</picker>
					</uni-forms-item>
					<uni-forms-item name="idCard" required label="身份证号码">
						<uni-easyinput :disabled="formDisabled" type="idcard" :inputBorder="true" v-model="item.idCard" placeholder="请输入身份证号码"></uni-easyinput>
					</uni-forms-item>
					<uni-forms-item name="age" required label="年龄">
						<uni-easyinput :disabled="formDisabled" type="number" :inputBorder="true" v-model="item.age" placeholder="请输入年龄"></uni-easyinput>
					</uni-forms-item>
					<uni-forms-item name="gender" required label="性别">
						<uni-data-checkbox v-model="item.gender" :localdata="genderList"></uni-data-checkbox>
					</uni-forms-item>
					<uni-forms-item name="address" required label="联系地址">
						<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="item.address" placeholder="请输入联系地址"></uni-easyinput>
					</uni-forms-item>
					<uni-forms-item name="relatedEducation(index)" required label="学历">
						<picker :disabled="formDisabled" @change="linkEducationChange($event,index)" :value="item.educationVal" :range="educationList" :range-key="'name'">
							<view class="picker-view flex-between" :class="formDisabled ? 'disabled-bg' : ''">
								<text>{{item.educationVal!=='' ? educationList[item.educationVal].name : '请选择'}}</text>
								<text class="cuIcon-unfold lg text-gray"></text>
							</view>
						</picker>
					</uni-forms-item>
					<uni-forms-item name="employer" required label="工作单位">
						<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="item.employer" placeholder="请输入工作单位"></uni-easyinput>
					</uni-forms-item>
					<uni-forms-item name="position" required label="职务">
						<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="item.position" placeholder="请输入职务"></uni-easyinput>
					</uni-forms-item>
					<uni-forms-item name="relationship" required label="关联关系">
						<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="item.relationship" placeholder="请输入关联关系"></uni-easyinput>
					</uni-forms-item>
				</view>
				<view v-if="item.type == 1">
					<uni-forms-item name="relatedName" required label="关联人名称">
						<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="item.relatedName" placeholder="请输入关联人名称"></uni-easyinput>
					</uni-forms-item>
					<uni-forms-item name="address" required label="联系住址">
						<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="item.address" placeholder="请输入联系住址"></uni-easyinput>
					</uni-forms-item>
					<uni-forms-item name="socialUnifiedCode" required label="社会统一代码">
						<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="item.socialUnifiedCode" placeholder="请输入社会统一代码"></uni-easyinput>
					</uni-forms-item>
					<uni-forms-item name="registeredCapital" required label="注册资金(万元)">
						<uni-easyinput :disabled="formDisabled" type="digit" :inputBorder="true" v-model="item.registeredCapital" placeholder="请输入注册资金"></uni-easyinput>
					</uni-forms-item>
					<uni-forms-item name="registeredTime" required label="注册时间">
						<picker :disabled="formDisabled" mode="date" :value="item.registeredTime" @change="relatedtimeChange($event,item)">
							<view class="picker-view flex-between" :class="formDisabled ? 'disabled-bg' : ''">
								<text>{{item.registeredTime!=='' ? item.registeredTime : '请选择'}}</text>
								<text class="cuIcon-unfold lg text-gray"></text>
							</view>
						</picker>
					</uni-forms-item>
					<uni-forms-item name="industry" required label="所属行业">
						<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="item.industry" placeholder="请输入所属行业"></uni-easyinput>
					</uni-forms-item>
					<uni-forms-item name="legalPersonName" required label="法人姓名">
						<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="item.legalPersonName" placeholder="请输入法人姓名"></uni-easyinput>
					</uni-forms-item>
					<uni-forms-item name="phone" required label="联系电话">
						<uni-easyinput :disabled="formDisabled" type="text" :maxlength="11" :inputBorder="true" v-model="item.phone" placeholder="请输入联系电话"></uni-easyinput>
					</uni-forms-item>
					<uni-forms-item name="relationship" required label="关联关系">
						<uni-easyinput :disabled="formDisabled" type="text" :inputBorder="true" v-model="item.relationship" placeholder="请输入关联关系"></uni-easyinput>
					</uni-forms-item>
					<uni-forms-item name="shareholdersSituation" required label="股东情况">
						<uni-easyinput :disabled="formDisabled" type="textarea" :inputBorder="true" v-model="item.shareholdersSituation" placeholder="请输入关联关系"></uni-easyinput>
					</uni-forms-item>				
				</view>
				<view class="foot-btn btn-rig pad-bt" v-if="!formDisabled">
					<button class="mini-btn round def-btn mar-lr" type="primary" size="mini" @tap="saveContact(item)">保存</button>
					<button class="mini-btn round" type="warn" size="mini" @tap="deleteContact(index,item)">删除</button>
				</view>
				</uni-forms>
			</block>
		</view>
		<button v-if="cusType == 1 && enterpriseForm.isExistRelated == 1&&handle!=='查看'" class="cu-btn block bg-blue margin-tb-sm lg round" @tap="addContact">新增关联人</button>
		
		<view v-if="!formDisabled">
			<button v-if="cusType == 0" class="cu-btn block bg-blue margin-tb-sm lg round" @tap="submitForm('personageForm')">保存</button>
			<button v-if="cusType == 1" class="cu-btn block bg-blue margin-tb-sm lg round" @tap="submitForm('enterpriseForm')">保存</button>
		</view>
	</view>
</template>

<script>
	import utils from '@/components/evan-form/utils.js'
	import uniPagination from '@/components/uni-pagination/uni-pagination.vue'
	export default {
		components: {uniPagination},
		data() {
			return {
				customerId: '',
				cusTypeList: [//客户类型列表
					// {
					// 	text: '个人',
					// 	value: 0,
					// 	disabled: false
					// },
					{
						text: '企业',
						value: 1,
						disabled: false
					},
				],
				cusTypeList1: [//客户类型列表
				{
					text: '个人',
					value: '0',
					disabled: false
				},
				{
					text: '企业',
					value: '1',
					disabled: false
				}],
				employeeMsg: [],//客户经理列表
				genderList: [{
					text: '男',
					value: 1
				},
				{
					text: '女',
					value: 0
				}],//性别列表
				maritalList: [{
					name: '未婚',
					value: 0
				},
				{
					name: '已婚',
					value: 1
				},
				{
					name: '离异',
					value: 2
				},
				{
					name: '再婚',
					value: 3
				}],//婚姻状况列表
				educationList: [{
					name: '本科',
					value: 0
				},
				{
					name: '大专',
					value: 1
				},
				{
					name: '高职',
					value: 2
				},
				{
					name: '中专',
					value: 3
				},
				{
					name: '其他',
					value: 4
				}],//学历列表
				isExistList: [{
					text: '否',
					value: 0
				},
				{
					text: '是',
					value: 1
				}],//是否存在关联人列表
				cusType: 1,//客户类型
				code: '',//客户编号
				managerVal: '',//客户经理列表下标
				manager: '',//客户经理
				maritalStatusVal: '',//婚姻状况列表下标
				educationVal: '', //学历列表下标
				personageForm: {
					name: '', //客户姓名
					idCard: '', //身份证号
					age: '',//年龄
					phone: '', //联系方式
					gender: 1,//性别
					maritalStatus: '',//婚姻状况
					education: '', //学历
					employer: '', //工作单位
					position: '', //职务
					workingYears: '', //从业年限
					socialSecurityNum: '',//社保编号
					livingSituation: '', //居住情况
					addr: '', //联系地址
					residenceAddr: '', //户籍地址
					businessSource: '', //业务来源
					emergencyLinkman: '', //紧急联系人
					emergencyLinkmanRelationship: '', //紧急联系人关系
					emergencyLinkmanPhone: '', //紧急联系人电话				
				},//个人form
				personageRules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '请输入客户姓名',
						}]
					},
					idCard: {
						rules: [{
							required: true,
							errorMessage: '请输入身份证号',
						},{
							validateFunction: function(rule, value, data, callback) {
								let pattern = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
								if (!pattern.test(value)) {
									callback('请输入正确的身份证号')
								}
								return true
							}
						}]
					},
					age: {
						rules: [{
							required: true,
							errorMessage: '请输入年龄',
						}]
					},
					phone: {
						rules: [{
							required: true,
							errorMessage: '请输入手机号码',
						},{
							validateFunction: function(rule, value, data, callback) {
								let pattern = /^1[3456789]\d{9}$/
								if (!pattern.test(value)) {
									callback('请输入正确的手机号码')
								}
								return true
							}
						}]						
					},
					maritalStatus: {
						rules: [{
							required: true,
							errorMessage: '请选择婚姻状况',
						},]
					},
					education: {
						rules: [{
							required: true,
							errorMessage: '请选择学历',
						},]
					},
					employer: {
						rules: [{
							required: true,
							errorMessage: '请输入工作单位',
						},]
					},
					position: {
						rules: [{
							required: true,
							errorMessage: '请输入职务',
						},]
					},
					workingYears: {
						rules: [{
							required: true,
							errorMessage: '请输入从业年限',
						},]
					},
					socialSecurityNum: {
						rules: [{
							required: true,
							errorMessage: '请输入社保编号',
						},]
					},
					livingSituation: {
						rules: [{
							required: true,
							errorMessage: '请输入居住情况',
						},]
					},
					addr: {
						rules: [{
							required: true,
							errorMessage: '请输入联系地址',
						},]
					},
					residenceAddr: {
						rules: [{
							required: true,
							errorMessage: '请输入户籍地址',
						},]
					},
					businessSource: {
						rules: [{
							required: true,
							errorMessage: '请输入业务来源',
						},]
					},
					emergencyLinkman: {
						rules: [{
							required: true,
							errorMessage: '请输入紧急联系人',
						},]
					},
					emergencyLinkmanRelationship: {
						rules: [{
							required: true,
							errorMessage: '请输入紧急联系人关系',
						},]
					},
					emergencyLinkmanPhone: {
						rules: [{
							required: true,
							errorMessage: '请输入紧急联系人电话',
						},{
							validateFunction: function(rule, value, data, callback) {
								let pattern = /^1[3456789]\d{9}$/
								if (!pattern.test(value)) {
									callback('请输入正确的手机号码')
								}
								return true
							}
						}]
					},
				},
				enterpriseForm: {
					name: '', //客户姓名
					socialUnifiedCode: '', //社会统一代码
					phone: '', //联系电话
					registerTime: '',//注册时间
					registeredCapital: '',//注册资金
					empNum: '', //员工人数
					registerAddr: '', //注册地址
					linkman: '', //联系人
					linkPhone: '', //联系人电话
					businessAddr: '', //经营地址
					businessSource: '', //业务来源
					businessScope: '', //经营范围
					
					legalName: '', //法人姓名
					legalGender: 1, //性别
					legalIdNumber: '', //身份证号码
					legalHukouAddr: '', //户口所在地
					legalPhone: '', //联系电话	
					legalHomeAddr: '', //家庭地址	
					isExistRelated: 0, //是否存在联系人
				},//企业form
				enterpriseRules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '请输入客户姓名',
						}]
					},
					socialUnifiedCode: {
						rules: [{
							required: true,
							errorMessage: '请输入社会统一代码',
						}]
					},
					phone: {
						rules: [{
							required: true,
							errorMessage: '请输入手机号码',
						},{
							validateFunction: function(rule, value, data, callback) {
								let pattern = /^1[3456789]\d{9}$/
								if (!pattern.test(value)) {
									callback('请输入正确的手机号码')
								}
								return true
							}
						}]						
					},
					registerTime: {
						rules: [{
							required: true,
							errorMessage: '请选择注册时间',
						},]
					},
					registeredCapital: {
						rules: [{
							required: true,
							errorMessage: '请输入注册资金',
						},{
							validateFunction: function(rule, value, data, callback) {
								let pattern = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
								if (!pattern.test(value)) {
									callback('请输入正确金额格式,可保留两位小数')
								}
								return true
							}
						}]
					},
					empNum: {
						rules: [{
							required: true,
							errorMessage: '请输入员工人数',
						}]
					},
					registerAddr: {
						rules: [{
							required: true,
							errorMessage: '请输入注册地址',
						}]
					},
					linkman: {
						rules: [{
							required: true,
							errorMessage: '请输入联系人姓名',
						}]
					},
					linkPhone: {
						rules: [{
							required: true,
							errorMessage: '请输入联系人手机号码',
						},{
							validateFunction: function(rule, value, data, callback) {
								let pattern = /^1[3456789]\d{9}$/
								if (!pattern.test(value)) {
									callback('请输入正确的手机号码')
								}
								return true
							}
						}]	
					},
					businessAddr: {
						rules: [{
							required: true,
							errorMessage: '请输入经营地址',
						}]
					},
					businessSource: {
						rules: [{
							required: true,
							errorMessage: '请输入业务来源',
						}]
					},
					businessScope: {
						rules: [{
							required: true,
							errorMessage: '请输入经营范围',
						}]
					},
					legalName: {
						rules: [{
							required: true,
							errorMessage: '请输入法人姓名',
						}]
					},
					legalIdNumber: {
						rules: [{
							required: true,
							errorMessage: '请输入法人身份证号',
						},{
							validateFunction: function(rule, value, data, callback) {
								let pattern = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
								if (!pattern.test(value)) {
									callback('请输入正确的身份证号')
								}
								return true
							}
						}]
					},
					legalHukouAddr: {
						rules: [{
							required: true,
							errorMessage: '请输入法人户口所在地',
						}]
					},
					legalPhone: {
						rules: [{
							required: true,
							errorMessage: '请输入联系人手机号码',
						},{
							validateFunction: function(rule, value, data, callback) {
								let pattern = /^1[3456789]\d{9}$/
								if (!pattern.test(value)) {
									callback('请输入正确的手机号码')
								}
								return true
							}
						}]						
					},
					legalHomeAddr: {
						rules: [{
							required: true,
							errorMessage: '请输入家庭地址',
						}]
					}
				},
				hideRequiredAsterisk: false,
				list: [{
					// 个人关联人
					type: 0,
					relatedName: '',//关联人名称
					phone: '',//联系电话
					maritalStatusVal: '',
					maritalStatus: '',//婚姻状况
					idCard: '',//身份证号码
					age: '',//年龄
					gender: 1,//性别
					address: '',//联系地址
					educationVal: '',
					education: '',//学历
					employer: '',//工作单位
					position: '',//职务
					relationship: '',//关联关系
					//企业关联人
					socialUnifiedCode: '',//社会统一代码
					registeredCapital: '',//注册资金
					registeredTime: '',//注册时间
					industry: '',//所属行业
					legalPersonName: '',//法人姓名
					shareholdersSituation: ''//股东情况
				}],//关联人列表
				page: 1,
				pageSize: 999,
				companyId: '',
				formDisabled: false,
				managerArr:{},// 所有经理的键值对
				
				handle:'新增',
				
			}
		},
		onReady() {
			if(this.$refs.personageForm) this.$refs.personageForm.setRules(this.personageRules)
			if(this.$refs.enterpriseForm) this.$refs.enterpriseForm.setRules(this.enterpriseRules)
		},
		onLoad(option) {
			this.getManager()
			 if(option.disabled == '查看'){
				 this.handle = '查看'
				 this.formDisabled = true
				 this.genderList.map(e =>{
				 	e.disabled = true
				 })
				 this.isExistList.map(e =>{
				 	e.disabled = true
				 })
				this.cusTypeList.map(e =>{
					e.disabled = true
				})
				this.cusTypeList1.map(e =>{
					e.disabled = true
				})
			 }else{
				if(option.id) this.handle = '编辑'
			 }
			 if(option.companyId){// 查看编辑
				 this.companyId = option.companyId
			}
			if(option.id){// 也可能是编辑
				this.customerId = option.id
				this.getMsg()
			 }
			this.getAllEmployeeMsg()
			// this.getLinkList()
		},
		methods: {
			getManager(){
				this.$http.get('/api-hrms/hrms/employee/listEmployeeName').then(res=>{
					this.employeeMsg = res.data
					this.managerArr = res.data
				})
			},
			// 获取详情信息
			getMsg(){
				this.$http.get('/api-crms/crms/workbench/info',{
					id: this.customerId
				}).then(res => {
					this.cusType = res.data.type
					this.employeeMsg.map(e=>{
						if(e.id==res.data.manager){
							this.managerVal = e.name
						}
					})
					this.managerVal
					if(res.data.type === 0){
						//   个人表单赋值
						this.personageForm = res.data
						this.code = res.data.code
						if(res.data.manager){
							this.manager = res.data.manager
							// this.employeeMsg.map((e,index) =>{
							// 	if(e.id == res.data.manager){
							// 		this.managerVal = index
							// 	}
							// })
						}
						this.maritalList.map((e,index) =>{
							if(e.value == res.data.maritalStatus){
								this.maritalStatusVal = index
							}
						})
						this.educationList.map((e,index) =>{
							if(e.value == res.data.education){
								this.educationVal = index
							}
						})
					}else{
					  //   企业表单赋值
					  this.enterpriseForm = res.data
					  this.code = res.data.code
					  this.manager = res.data.manager
					  if(this.enterpriseForm.isExistRelated==1)this.getLinkList()
					}
				}).catch(function (error) {});
			},
			// 获取客户经理列表
			getAllEmployeeMsg(){
				this.$http.get('/api-hrms/hrms/employee/listEmployeeName').then(res => {
					this.employeeMsg = res.data
				}).catch(function (error) {});
			},
			// 获取关联人列表
			getLinkList(){
				let params = {
					page: this.page,
					size: this.pageSize,
					customerId: this.companyId
				}
				this.$http.get('/api-crms/crms-company-personal/companyPersonalList',params).then(res => {
					res.data.list.map(e=>{
						if(e.type=='0') {
							e.maritalStatusVal = e.maritalStatus
							e.educationVal = e.education
						}
						
					})
					
					this.list = res.data.list
				}).catch(function (error) {});
			},
			//客户类型选择
			cusChange(e) {
				this.cusType = e.detail.value
			},
			//客户经理选择
			managerChange(e){
				this.managerVal = e.detail.value
				this.manager = this.employeeMsg[e.detail.value].id
			},
			//性别选择
			genderChange(e) {
				this.personageForm.gender = e.detail.value
			},
			//婚姻状况选择
			maritalChange(e) {
				this.maritalStatusVal = e.detail.value
				this.personageForm.maritalStatus = this.maritalList[e.detail.value].value
			},
			//学历选择
			educationChange(e) {
				this.educationVal = e.detail.value
				this.personageForm.education = this.educationList[e.detail.value].value
			},
			// 关联人学历
			linkEducationChange(e,index) {
				this.list[index].educationVal = e.detail.value
				this.list[index].education = this.educationList[e.detail.value].value
			},
			//关联人性别选择
			legalGenderChange(e) {
				this.personageForm.legalGender = e.detail.value
			},
			//是否有关联人
			isExistChange(e) {
				this.personageForm.isExistRelated = e.detail.value
			},
			//关联人婚姻状况选择
			relatedMaritalChange(e,item) {
				item.maritalStatusVal = e.detail.value
				item.maritalStatus = this.maritalList[e.detail.value].value
			},
			//注册时间选择
			bindDateChange(e) {
				this.enterpriseForm.registerTime = e.detail.value
			},
			//企业关联人注册时间选择
			relatedtimeChange(e,item) {
				item.registeredTime = e.detail.value
			},
			//提交表单
			submitForm(form) {
				let that = this
				// 莫名的bug，如果啥都不填，反而能通过验证
				if(!this.enterpriseForm.name){
					return uni.showToast({
						title:'请输入客户姓名！',
						icon:'none'
					})
				}
				this.$refs[form].submit()
					.then((res) => {
						if(this.customerId){
							if(this.cusType == 0){
								//编辑个人
								this.personageForm.type = this.cusType
								this.personageForm.manager = this.manager
								this.personageForm.code = this.code
								
								this.personageForm.id = +this.customerId
								this.$http.post('/api-crms/crms/workbench/updatePersonal',this.personageForm).then(res => {
									uni.showToast({title: '编辑成功'})
									this.back()
								}).catch(function (error) {});
							}else{
								//编辑企业
								let result = this.list.map(e => e.id)
								this.enterpriseForm.type = this.cusType
								this.enterpriseForm.manager = this.manager
								this.enterpriseForm.code = this.code
								this.enterpriseForm.relatedId = result
								this.enterpriseForm.id = +this.customerId
								
								this.$http.post('/api-crms/crms/workbench/updateCompanyNew',this.enterpriseForm).then(res => {
									uni.showToast({title: '编辑成功'})
									this.back()
								}).catch(function (error) {});
							}
						}else{
							if(this.cusType == 0){
								//新增个人
								let obj = { type: this.cusType, manager: this.manager, code: this.code }
								let newObj = { ...this.personageForm, ...obj }
								this.$http.post('/api-crms/crms/workbench/savePersonal',newObj).then(res => {
									uni.showToast({title: '添加成功'})
									this.back()
								}).catch(function (error) {});
							}else{
								//新增企业
								let result = this.list.map(e => e.id)
								let obj = { type: this.cusType, manager: this.manager, code: this.code, relatedId: result }
								let newObj = { ...this.enterpriseForm, ...obj }
								this.$http.post('/api-crms/crms/workbench/insertCompany',newObj).then(res => {
									uni.showToast({title: '添加成功'})
									this.back()
								}).catch(function (error) {});
							}
						}
					}).catch((errors) => {})
			},
			//添加关联人
			addContact(){
				let CONTACT_INFO = {
					type: 0,
					relatedName: '',
					phone: '',
					maritalStatusVal: '',
					maritalStatus: '',
					idCard: '',
					age: '',
					gender: '',
					address: '',
					educationVal: '',
					education: '',
					employer: '',
					position: '',
					relationship: '',
					
					socialUnifiedCode: '',
					registeredCapital: '',
					registeredTime: '',
					industry: '',
					legalPersonName: '',
					shareholdersSituation: ''
				}
				this.list.push({...CONTACT_INFO})
			},
			//保存关联人
			saveContact(item){
				// 进行判断，关联人必要的信息是否有为空值的
				// 保存需要区分是编辑关联人还是添加新的关联人
				// let data = JSON.parse(JSON.stringify(item))
				// ,arr = Object.values(data)
				let obj
				// 共用验证
				if (!item.relatedName)return uni.showToast({title:'请输入关联人名称',icon:"none"})
				if (!item.relationship)return uni.showToast({title:'请输入关联关系',icon:"none"})
				if (!item.phone)return uni.showToast({title:'请输入联系电话',icon:"none"})
				if (!item.address)return uni.showToast({title:'请输入联系地址',icon:"none"})
				obj = {
						type:item.type,
						relatedName:item.relatedName,
						relationship:item.relationship,
						phone:item.phone,
						address:item.address,
						// relatedCompanyId:this.companyId
					}
				// 验证和赋值
				if(item.type==0){ // 个人
					if (!item.maritalStatusVal)return uni.showToast({title:'请输入婚姻状况',icon:"none"})
					if (!item.idCard)return uni.showToast({title:'请输入身份证号码',icon:"none"})
					if (!item.age)return uni.showToast({title:'请输入年龄',icon:"none"})
					if (item.gender!=0&&item.gender!=1)return uni.showToast({title:'请选择性别',icon:"none"})
					if (!item.educationVal)return uni.showToast({title:'请选择学历',icon:"none"})
					if (!item.employer)return uni.showToast({title:'请输入工作单位',icon:"none"})
					if (!item.position)return uni.showToast({title:'请输入职务',icon:"none"})
					obj.maritalStatus = item.maritalStatus
					obj.idCard = item.idCard 
					obj.age = item.age 
					obj.gender = item.gender
					obj.education = item.education
					obj.employer = item.employer
					obj.position = item.position
				}else{//企业
					if (!item.socialUnifiedCode)return uni.showToast({title:'请输入社会统一代码',icon:"none"})
					if (!item.registeredCapital)return uni.showToast({title:'请输入注册资金',icon:"none"})
					if (!item.registeredTime)return uni.showToast({title:'请选择注册时间',icon:"none"})
					if (!item.industry)return uni.showToast({title:'请输入所属行业',icon:"none"})
					if (!item.legalPersonName)return uni.showToast({title:'请输入法人姓名',icon:"none"})
					if (!item.shareholdersSituation)return uni.showToast({title:'请输入股东情况',icon:"none"})
					obj.socialUnifiedCode = item.socialUnifiedCode  
					obj.registeredCapital = item.registeredCapital  
					obj.registeredTime = item.registeredTime  
					obj.industry = item.industry  
					obj.legalPersonName = item.legalPersonName  
					obj.shareholdersSituation = item.shareholdersSituation  
				}
				if(item.id){ // 老数据才有id--修改操作
					// if(data.type === '1'){ // 企业关联人
					// 	arr.splice(9,4).splice(3,6)
					// }else{ // 个人关联人拆list做验证
					// 	arr.splice(13,7)
					// }		
					// if(arr.indexOf("")!==-1) return uni.showToast({title: '请把资料填齐！',icon:'none'})
					// 修改接口
					obj.id = item.id
					this.$http.post('/api-crms/crms-company-personal/updateCompanyPersonal',obj).then(res=>{
						uni.showToast({title: '修改成功'})
					})
				} else{ // 下面为新增操作
					// 做验证的，截取必要的value
					// if(data.type === '1'){ // 企业关联人
					// 	arr.splice(9,4).splice(3,6)
					// }else{ // 个人关联人拆list做验证
					// 	arr.splice(13,7)
					// }					
					// if(arr.indexOf("") !== -1) return uni.showToast({title: '请把资料填齐！',icon:'none'})
					this.$http.post('/api-crms/crms-company-personal/insertCompanyPersonal',obj).then(res => {
						uni.showToast({title: '添加成功'})
						item.id = res.data[0].id // 添加成功,重新请求就有了
						// this.list.splice(list[this.list.length-1],1)// 删除添加的框，并请求一次接口
						// this.getLinkmanList()//调关联人接口
					}).catch(function (error) {});					
				}
			},
			//删除关联人
			deleteContact(index,item){
				let _this = this
				uni.showModal({
				    title: '提示',
				    content: '是否删除该关联人',
				    success: function (res) {
				        if (res.confirm) {
							if(item.id){
								let arr = []
								arr.push(item.id)
								_this.$http.post('/api-crms/crms-company-personal/deleteCompanyPersonal',arr).then(res => {
									_this.getLinkList()
								}).catch(function (error) {});
							}
				            _this.list.splice(index,1)
							uni.showToast({title: '删除成功'})
				        } else if (res.cancel) {}
				    }
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.evan-form-show {
		padding: 0 30rpx;
		background-color: #fff;

		.form-input {
			font-size: 28rpx;
			color: #333;
			text-align: right;
			width: 100%;
			box-sizing: border-box;
			height: 60rpx;
			&.textarea{
				height: 240rpx;
				padding: 24rpx 0;
				text-align: left;
			}
		}

		.form-input-placeholder {
			font-size: 28rpx;
			color: #999;
		}

		&__button {
			width: 100%;
			height: 88rpx;
			border-radius: 8rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			padding: 0;
			font-size: 36rpx;
			color: #fff;
			margin-top: 20rpx;
			background-color: #2D87D5;

			&::before,
			&::after {
				border: none;
			}
		}

		.customize-form-item {
			&__label {
				font-size: 28rpx;
				color: #333;
				margin-bottom: 16rpx;
			}

			&__radio {
				display: flex;
				align-items: center;
				margin-bottom: 16rpx;

				&__text {
					font-size: 28rpx;
					color: #333;
				}
			}
		}
	}
</style>
