<template>

	<view>
		<view class="uni-list">		
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					打印份数<text class="require">*</text>
				</view>
				<view class="uni-list-cell-db">
					<input v-model="reportForm.print_num" />
				</view>
			</view>
		</view>
		<view class='line'></view>
		<view class="uni-list">
			<uni-card>
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					收费方式<text class="require">*</text>
				</view>
				<view class="uni-list-cell-db">
					<picker @change="onChangeSelect($event, 'pay_type')" :value="selectPayTypeIndex" range-key="valname"
						:range="payTypeList">
						<view>
							{{payTypeList[selectPayTypeIndex].valname}}
						</view>
					</picker>
				</view>
			</view>
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					公积金<text class="require">*</text>
				</view>
				<view class="uni-list-cell-db">
					<picker @change="onChangeSelect($event, 'provident_fund')" :value="selectProvidentFundIndex"
						range-key="valname" :range="hasProvidentFundList">
						<view>
							{{hasProvidentFundList[selectProvidentFundIndex].valname}}
						</view>
					</picker>
				</view>
			</view>
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					业务类别<text class="require">*</text>
				</view>
				<view class="uni-list-cell-db">
					<picker @change="onChangeSelect($event, 'business_type')" :value="selectBusinessTypeIndex"
						range-key="valname" :range="businessTypeList">
						<view>
							{{businessTypeList[selectBusinessTypeIndex]?businessTypeList[selectBusinessTypeIndex].valname:""}}
						</view>
					</picker>
				</view>
			</view>
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					业务来源<text class="require">*</text>
				</view>
				<view class="uni-list-cell-db">
					<picker @change="onChangeSelect($event, 'business_source')" :value="selectBusinessSourceIndex"
						range-key="valname" :range="businessSourceList">
						<view>
							{{businessSourceList[selectBusinessSourceIndex]?businessSourceList[selectBusinessSourceIndex].valname:''}}
						</view>
					</picker>
				</view>
			</view>
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					借款人
				</view>
				<view class="uni-list-cell-db">
					<input v-model="reportForm.borrow_name" />
				</view>
			</view>
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					借款人身份证号
				</view>
				<view class="uni-list-cell-db">
					<input v-model="reportForm.borrow_num" />
				</view>
			</view>
			</uni-card>
		</view>
		
		<view class="uni-list" v-for="(item, findex) in reportForm.details" :key="findex">
			<uni-card>
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					物业<text class="require">*</text>
				</view>
				<view class="uni-list-cell-db">
					<text class="cu-tag">{{estateList[findex]?estateList[findex].building_name:''}}</text>
				</view>		
			</view>
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					房产证号<text class="require">*</text>
				</view>
				<view class="uni-list-cell-db">
					<input type="number" v-model="reportForm.details[findex].year" maxlength="20" placeholder="输入年份" />
					<input v-model="reportForm.details[findex].property_cert" maxlength="20" placeholder="输入证号" />
				</view>
			</view>


			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					证件类型<text class="require">*</text>
				</view>
				<view class="uni-list-cell-db" v-if="estateList[findex] && estateList[findex].ownership_type === '1'">
					<picker @change="onChangeSelect($event, 'cert_type',findex)" :value="selectIdTypeIndex[findex].index"
						range-key="valname" :range="idTypeList">
						<view>
							{{getidType(findex)}}
						</view>
					</picker>
				</view>
				<view class="uni-list-cell-db" v-else>
					<picker @change="onChangeSelect($event, 'cert_type',findex)"
						:value="getComIndex(findex)" range-key="valname" :range="idCompanyTypeList">
						<view>
							{{getComType(findex)}}
						</view>
					</picker>
				</view>
			</view>
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					证件号<text class="require">*</text>
				</view>
				<view class="uni-list-cell-db">
					<input v-model="reportForm.details[findex].cert_no" />
				</view>
			</view>
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					权利人<text class="require">*</text>
				</view>
				<view class="uni-list-cell-db">
					<input v-model="reportForm.details[findex].obligee" />
				</view>
			</view>
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					委托方<text class="require">*</text>
				</view>
				<view class="uni-list-cell-db">
					<input v-model="reportForm.details[findex].client" />
				</view>
			</view>
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					宗地号<text class="require">*</text>
				</view>
				<view class="uni-list-cell-db">
					<input v-model="reportForm.details[findex].zdbh" />
				</view>
			</view>
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					宗地面积<text class="require">*</text>
				</view>
				<view class="uni-list-cell-db">
					<input type="digit" v-model="reportForm.details[findex].zd_area" />
				</view>
			</view>
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					土地用途<text class="require">*</text>
				</view>
				<view class="uni-list-cell-db">
					<input v-model="reportForm.details[findex].land_use" />
				</view>
			</view>
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					使用权来源<text class="require">*</text>
				</view>
				<view class="uni-list-cell-db" >
					<picker @change="onChangeSelect($event, 'use_source',findex)"
						:value="getUserIndex(findex)" range-key="valname" :range="useSourceList">
						<view>
							{{useSourceList[getUserIndex(findex)]?useSourceList[getUserIndex(findex)].valname:''}}
						</view>
					</picker>
				</view>
			</view>
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					竣工日期<text class="require">*</text>
				</view>
				<view class="uni-list-cell-db">
					<picker mode="date" :value="reportForm.details[findex].completion_date" :start="startDate"
						:end="endDate" @change="onChangeSelect($event, 'completion_date',findex,item)">
						<view class="uni-input">{{reportForm.details[findex].completion_date}}</view>
					</picker>
				</view>
			</view>
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					登记/购买日期<text class="require">*</text>
				</view>
				<view class="uni-list-cell-db">
					<picker mode="date" :value="reportForm.details[findex].buy_date" :start="startDate" :end="endDate"
						@change="onChangeSelect($event, 'buy_date',findex,item)">
						<view class="uni-input">{{reportForm.details[findex].buy_date}}</view>
					</picker>
				</view>
			</view>
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					评估日期<text class="require">*</text>
				</view>
				<view class="uni-list-cell-db">
					<picker mode="date" :value="reportForm.details[findex].evaluate_date" :start="startDate"
						:end="endDate" @change="onChangeSelect($event, 'evaluate_date',findex,item)">
						<view class="uni-input">{{reportForm.details[findex].evaluate_date}}</view>
					</picker>
				</view>
			</view>
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					土地使用年限（年）<text class="require">*</text>
				</view>
				<view class="uni-list-cell-db">
					<input type="number" v-model="reportForm.details[findex].land_use_year" />
				</view>
			</view>
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					土地使用开始时间<text class="require">*</text>
				</view>
				<view class="uni-list-cell-db">
					<picker mode="date" :value="reportForm.details[findex].land_use_begin" :start="startDate"
						:end="endDate" @change="onChangeSelect($event, 'land_use_begin',findex,item)">
						<view class="uni-input">{{reportForm.details[findex].land_use_begin}}</view>
					</picker>
				</view>
			</view>
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					土地使用结束时间<text class="require">*</text>
				</view>
				<view class="uni-list-cell-db">
					<picker mode="date" :value="reportForm.details[findex].land_use_end" :start="startDate"
						:end="endDate" @change="onChangeSelect($event, 'land_use_end',findex,item)">
						<view class="uni-input">{{reportForm.details[findex].land_use_end}}</view>
					</picker>
				</view>
			</view>
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					所在城区<text class="require">*</text>
				</view>
				<view class="uni-list-cell-db" v-if="districtList.length > 0">
					<picker @change="onChangeSelect($event, 'district',findex)" :value="getDicstIndex(findex)"
						range-key="shortname" :range="districtList">
						<view>
							{{districtList[getDicstIndex(findex)]?districtList[ getDicstIndex(findex)].shortname:''}}
						</view>
					</picker>
				</view>
			</view>
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					建筑面积<text class="require">*</text>
				</view>
				<view class="uni-list-cell-db">
					<input type="digit" v-model="reportForm.details[findex].floorage" />
				</view>
			</view>
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					土地位置<text class="require">*</text>
				</view>
				<view class="uni-list-cell-db">
					<input v-model="reportForm.details[findex].land_location" />
				</view>
			</view>
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					房产证照片<text class="require">*</text>
				</view>
				<view class="uni-list-cell-db">
					<text v-if="isUploading" style="color: red;">上传中....</text>
					<uni-file-picker :auto-upload="false" v-model="reportForm.details[findex].house_img_details"
						file-mediatype="image" mode="grid" file-extname="png,jpg,jpeg"  @progress="progress"
						@success="success" @fail="fail" @select="selectImg($event,'orientation_img_ids',findex)"
						@delete="handleDelete" />						
				</view>
			</view>
			</uni-card>
		</view>
		<view class="button-sp-area">
			
			<button class='mini-btn' @click='cancel()' type="primary" size='mini'>取消</button>
			<button v-if="isShowCreateReport()" class='mini-btn' @click='submitReport(2)' :disabled="submitDisable"  type="primary" size='mini'>提交</button>
			<button class='mini-btn' @click='submitReport(1)' :disabled="submitDisable"  type="primary" size='mini'>保存</button>
		</view>
	</view>
</template>

<script>
	import {
		reqPropertyInfo,
		getDistrict,
		getDictionaryByType,
        createReport,
		getReportDetail
	} from '@/api/inquiry/inquiry'
    import { uploadImg } from "@/api/common"
	import {
		toast
	} from '@/utils/common'
import { fail } from 'assert'
	export default {

		data() {

			return {
				estateList: [],
				bank_name:"",
				inquiryStatus:1,
				isUploading:false, //是否上传中状态
				reportForm: {
					type: 2, // 1保存 2提交
					inquiry_id: this.$route.query.inquiry_id,
					print_num: '0',
					pay_type: 1,
					has_provident_fund: 0,
					borrow_name: '',
					borrow_num: '',
					business_type: '',
					business_source: '',
					details: [{
						id: '',
						hadDetailFlag: false, // 是否已经赋值
						property_cert_info_id: '',
						year: '',
						property_cert: '',
						cert_type: '',
						obligee: '',
						cert_no: '',
						client: '',
						zdbh: '',
						zd_area: '',
						land_use: '',
						use_source: '',
						completion_date: '1970-01-01',
						buy_date: '',
						evaluate_date: this.currentDate(), 
						land_use_year: '',
						land_use_begin: '',
						land_use_end: '',
						floorage: '',
						area: '',
						area_id: '',
						land_location: '',
						house_img_details: [],
						house_img: []
					}]
				},

				selectPayTypeIndex: 0,
				selectProvidentFundIndex: 0,
				selectBusinessTypeIndex: 0,
				selectBusinessSourceIndex: 0,
				selectDistrictIndex: [{
					index: 0
				}],
				selectUseSourceIndex:[{index:0}],
				selectIdTypeIndex: [{
					index: 0
				}],
				selectCompanyTypeIndex: [{
					index: 0
				}],


				districtList: [],
				surveyorList: [],
				payTypeList: [{
						code: 1,
						valname: '月结'
					},
					{
						code: 2,
						valname: '个人'
					}
				],
				hasProvidentFundList: [
					{
						code: 0,
						valname: '否'
					},
					{
						code: 1,
						valname: '是'
					},
					
				],
				businessTypeList: [],
				useSourceList:[],
				
				idTypeList: [],
				idCompanyTypeList: [],
				businessSourceList: [],
				filePathsList: [],
				submitDisable : false,
				saveDisable : false
			}
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		async created() {
			this.bank_name = this.$route.query.bank_name;
			//console.log("bank_name", this.bank_name)
            await this.initDictionaryByType();
			await this.initDistrict(this.$route.query.city_id);

			await this.initEstateInfo(this.$route.query.inquiry_id);
						  
			await this.onGetReportDetail( this.$route.query.inquiry_id );
			
			
			this.inquiryStatus = this.$route.query.inquiryStatus;
		
		},

		methods: {
			
			isShowCreateReport()
			{
				if( this.inquiryStatus != 5 )
				{
					return false;
				}
				else
				{
					return true
				}
			},
			
			getDicstIndex(findex)
			{
				let oi = this.selectDistrictIndex[findex]				
				if( !oi )
				{					
					return 0;
				}
				return oi.index				
			},
			
			// 获取证件类型
			getidType( findex )
			{				
				let oi = this.selectIdTypeIndex[findex]				
				if( !oi )
				{
					
					return '';
				}
				let idx = oi.index
				return this.idTypeList[idx]?this.idTypeList[idx].valname:''
			},
			
			getUserIndex(findex)
			{
				let oi = this.selectUseSourceIndex[findex]				
				if( !oi )
				{					
					return 0;
				}
				return oi.index
				
			},
			
			getComIndex(findex)
			{
				let oi = this.selectCompanyTypeIndex[findex]
				
				if( !oi )
				{					
					return 0;
				}
				return oi.index
			},
			
			getComType(findex)
			{
				let idx = this.getComIndex(findex)
				return this.idCompanyTypeList[idx]?this.idCompanyTypeList[idx].valname:''
			},
			
			currentDate() {
			return this.getDate({format: true})
			},			
			saveReport(index){
				let details = this.reportForm.details;// [index]
				
				let check = this.reportRuleCurDetails(details)
				if(!check) {
					return false
				}
				this.reportForm.type = 1			
				let  reportSaveForm = JSON.parse(JSON.stringify(this.reportForm))
				reportSaveForm.details = this.reportForm.details
				this.saveDisable = true;
				createReport(reportSaveForm).then(res => {
					this.saveDisable = false;
					if (res.code === 1) {
						toast(res.msg)
						
					} else {
						this.onShowReportToast(res.data)
						//toast(res.msg)
					}
				})
			},
			
			getClientFullName( c )
			{
				if( c == '中国银行')
				{
					return "中国银行股份有限公司深圳市分行";
				}
				if( c == '中国建设银行' )
				{
					return "中国建设银行股份有限公司深圳市分行";
				}
				if( c== '中国光大银行')
				{
					return "中国光大银行股份有限公司深圳分行";
				}
				if( c== '珠海华润银行')
				{
					return "珠海华润银行股份有限公司深圳分行";
				}
				if( c== '中信银行')
				{
					return "中信银行股份有限公司深圳分行";
				}
                if( c== '华夏银行')
                {
                    return "华夏银行股份有限公司深圳分行";
                }
                if( c== '广州银行')
                {
                    return "广州银行股份有限公司深圳分行";
                }
                if( c== '徽商银行股份有限公司')
                {
                    return "徽商银行股份有限公司深圳分行";
                }
                if( c== '中国民生银行')
                {
                    return "中国民生银行股份有限公司深圳分行";
                }
				if( c== '广东南粤银行')
				{
				  return "广东南粤银行股份有限公司深圳分行";
				}
				if( c== '广东华兴银行')
				{
				  return "广东华兴银行股份有限公司深圳分行";
				}
				if( c== '融兴村镇银行')
				{
				  return "深圳宝安融兴村镇银行有限责任公司";
				}	
				return c;
			},
			// 通过值找到对应索引
			findIndexByValue( list , v , col="valname")
			{
			  for (let i = 0; i < list.length; i++) {
				const obj = list[i];				
				if (obj[col] === v ) {
				  return i;
				}
			  }
			  return -1;
			},
			
			onGetReportDetail( inquiry_id )
			{
				getReportDetail({ inquiry_id:inquiry_id }).
					then( res=>{
						if( res.data != null)
						{							
							this.selectIdTypeIndex =[]					
							this.selectCompanyTypeIndex =[]						
							this.selectDistrictIndex = []
							this.selectUseSourceIndex = []
							
							this.reportForm = res.data
							
							for(let i = 0; i< this.reportForm.details.length;i++) {
								this.selectUseSourceIndex.push({index: this.findIndexByValue( this.useSourceList, this.reportForm.details[i].use_source )});
								this.selectIdTypeIndex.push({index: this.findIndexByValue( this.idTypeList, this.reportForm.details[i].cert_type , "code" )});
							
								this.selectCompanyTypeIndex.push({index: this.findIndexByValue( this.idCompanyTypeList, this.reportForm.details[i].company_certtype )});
								this.selectDistrictIndex.push({index: this.findIndexByValue( this.districtList, this.reportForm.details[i].area_id, "id")});
							}  
							
							this.reportForm.inquiry_id = this.$route.query.inquiry_id							
						}						 
				})
			},
			
			cancel() {
				this.$tab.navigateTo('/pages/inquiry/index')
			},
			checkReportRule() {

				if (this.reportForm.pay_type === '' || this.reportForm.pay_type === undefined) {
					toast('收费方式项未填写，请补充完整')
					return false
				}

				if (this.reportForm.has_provident_fund === '' || this.reportForm.has_provident_fund === undefined) {
					toast('公积金项未填写，请补充完整')
					return false
				}

			 if (this.reportForm.business_type === '' || this.reportForm.business_type === undefined) {
			  toast('业务类别项未填写，请补充完整')
					return false
				}

				if (this.reportForm.business_source === '' || this.reportForm.business_source === undefined) {
					toast('业务来源项未填写，请补充完整')
					return false
				}

				for (let i = 0; i < this.reportForm.details.length; i++) {
					let details = this.reportForm.details[i]
					let check = this.reportRuleCurDetails(details)
					if(!check) {
						return false
					}
				}
                return true
			},
			
			
			onShowReportToast( data ){
				let str = "";
				for( let k in data )
				{
					str += "[物业 "+(k+1)+']';					
					let info = data[k];									
					
					for( let key in info )
					{
						str += " "+info[key]
						break;
					}
					if("" != str.trim() )
						break;
				}
				
				if("" != str.trim() )
				str += "字段未填写！"
				
				uni.showToast({
					title:str,
					icon:'error'
				})
			},
			
			reportRuleCurDetails(details) {
				window.console.log('call reportRuleCurDetails')
				window.console.log(details)
				if (details.property_cert === '' || details.property_cert === undefined) {
					toast('房产证号项未填写，请补充完整')
					return false
				}

				if (details.cert_type === '' || details.cert_type === undefined) {
					toast('证件类型项未填写，请补充完整')
					return false
				}

                if (details.cert_no === '' || details.cert_no === undefined) {
                	toast('证件号项未填写，请补充完整')
                    return false
                }
			    
				if (details.obligee === '' || details.obligee === undefined) {
					toast('权利人项未填写，请补充完整')
					return false
				}

				if (details.client === '' || details.client === undefined) {
					toast('委托方项未填写，请补充完整')
					return false
				}

				if (details.zdbh === '' || details.zdbh === undefined) {
					toast('宗地号项未填写，请补充完整')
					return false
				}

				if (details.zd_area === '' || details.zd_area === undefined) {
					toast('宗地面积项未填写，请补充完整')
					return false
				}

				// if (details.zd_area !== '' && !validateIndDec(details.zd_area, 9, 2)) {
				// 	toast('宗地面积项限制整数位9位，小数位2位')
				// 	return false
				// }

				if (details.land_use === '' || details.land_use === undefined) {
					toast('土地用途项未填写，请补充完整')
					return false
				}

				if (details.use_source === '' || details.use_source === undefined) {
					toast('使用权来源项未填写，请补充完整')
					return false
				}

				if (details.completion_date === '' || details.completion_date === undefined) {
					toast('竣工日期项未填写，请补充完整')
					return false
				}

				if (details.buy_date === '' || details.buy_date === undefined) {
					toast('登记/购买日期项未填写，请补充完整')
					return false
				}

				if (details.evaluate_date === '' || details.evaluate_date === undefined) {
					toast('评估日期项未填写，请补充完整')
					return false
				}

				if (details.land_use_year === '' || details.land_use_year === undefined) {
					toast('土地使用年限项未填写，请补充完整')
					return false
				}

				// if (details.land_use_year !== '' && !validateIndDec(details.land_use_year, 5, 0)) {
				// 	toast('土地使用年限限制整数位5位')
				// 	return false
				// }

			 if (details.land_use_begin === '' || details.land_use_begin === undefined) {
					toast('土地使用开始时间项未填写，请补充完整')
					return false
				}

				if (details.land_use_end === '' || details.land_use_end === undefined) {
					toast('土地使用结束时间项未填写，请补充完整')
					return false
				}

				if (details.area_id === '' || details.area_id === undefined) {
					toast('所在城区项未填写，请补充完整')
					return false
				}

				if (details.land_location === '' || details.land_location === undefined) {
					toast('土地位置项未填写，请补充完整')
					return false
				}

				if (details.house_img.length === 0) {
					toast('房产证照片项未填写，请补充完整')
					return false
				}
				return true
			},
			

			
			submitReport( type ) {
				console.log( this.reportForm )
				if (!this.checkReportRule()) {
					return
				}
				this.reportForm.type = type
				// 防止多次提交：加锁
				this.submitDisable = true
				createReport(this.reportForm).then(res => {
					// 防止多次提交：解锁
					this.submitDisable = false
					if (res.code === 1) {
						toast(res.msg)
						setTimeout(()=>{
							this.$tab.navigateTo('/pages/inquiry/index')
						},1000)
					} else {
					//	toast(res.msg)
						this.onShowReportToast(res.data)
					}
				})
			},
			// 选择文件，获取上传状态
			selectImg(e, name, index) {

				let tempFilePaths = e.tempFilePaths

				if (!tempFilePaths.length) return
				let imgLen = tempFilePaths.length
				for(let i = 0; i < imgLen; i++) {
					
				
					const path = tempFilePaths.pop()

					this.filePathsList.push({
						url: path,
						name: ""
					})
					
					// 置上传状态
					this.isUploading = true;
					
					uploadImg({
						filePath: path
					}).then(response => {
						const data = response.data
						const id = data.id
						console.log('load success ',index,this.reportForm.details)
						if( "" == this.reportForm.details[index].house_img)
							this.reportForm.details[index].house_img = []
							
						this.reportForm.details[index].house_img.push(id)
												
						uni.showToast({
							title: "第" + ++i + "张上传成功",
							icon: 'success'
						})						
						
						// 解锁上传状态
						this.isUploading = false;					

					}).catch((err) => {
						uni.showToast({
							title: err,
							icon: 'error'
						})
						window.console.log(err)
						// 解锁上传状态
						this.isUploading = false;
					});
			    }
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 100;
				} else if (type === 'end') {
					year = year + 100;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			initDictionaryByType() {
				getDictionaryByType({
					'type': ['USE_SOURCE','ORDER_STATUS', 'CERTTYPE', 'COMPANY_CERTTYPE', 'BUSINESS_TYPE', 'BUSINESS_SOURCE']
				}).then(res => {
					//console.log('getDictionaryByType')
					if (res.code === 1) {
						const data = res.data
						console.log('getdictionary',data)
						// this.optionList.status = data.order_status.map(item => {
						//   return {
						//     label: item.valname,
						//     value: item.code
						//   }
						// })
                        this.useSourceList = data.use_source
						this.idTypeList = data.certtype
						this.idCompanyTypeList = data.company_certtype
						this.businessSourceList = data.business_source
						this.businessTypeList = data.business_type
                        this.reportForm.business_source = this.businessSourceList[0].code
						this.reportForm.business_type = this.businessTypeList[0].code
						// init data
						//this.initData()
					} else {
						toast(res.msg)
					}
				})
			},
			
			

			async initEstateInfo(inquiry_id) {
				let params = {
					inquiry_id: inquiry_id
				}
				
				this.selectIdTypeIndex = [{index:0}];
				//console.log('	this.selectCompanyTypeIndex[0].index',	this.selectCompanyTypeIndex[0].index)
				this.selectCompanyTypeIndex = [{index:0}];
				this.selectDistrictIndex = [{index:0}];
				this.selectUseSourceIndex = [{index:0}];	
				
				await reqPropertyInfo(params).then(res => {
					//console.log('reqPropertyInfo')
					try{
					if (res.code === 1) {
						this.estateList = res.data
					//	console.log( 'getetateinfo res.data', this.estateList )
										
						
						this.estateList.forEach((item, index) => {
							console.log('index',index)
                            if(index != 0) {
								this.selectIdTypeIndex.push({
									index: 0
								})
								this.selectCompanyTypeIndex.push({
									index: 0
								})
								this.selectDistrictIndex.push({
									index: 0
								})
								this.selectUseSourceIndex.push({index:0})
								//this.select
								this.reportForm.details.push({
									id: '',
									hadDetailFlag: false, // 是否已经赋值
									property_cert_info_id: item.id,
									year: '',
									property_cert: '',
									cert_type: '',
									obligee: '',
									cert_no: '',
									client: this.getClientFullName(this.bank_name),
									zdbh: '',
									zd_area: '',
									land_use: '',
									use_source: '',
									completion_date: '1970-01-01',
									buy_date: '',
									evaluate_date: this.currentDate(),
									land_use_year: '',
									land_use_begin: '',
									land_use_end: '',
									area: this.districtList[0].shortname,
									floorage: '',
									area_id: this.districtList[0].id,
									business_source: '',
									land_location: '',
									house_img_details: [],
									house_img: []
								})
                                if(item.ownership_type == 1) {
                                    this.reportForm.details[index].cert_type = this.idTypeList[0].code
                                }else {
                                	this.reportForm.details[index].cert_type = this.idCompanyTypeList[0].code
                                } 
								this.reportForm.details[index].use_source = this.useSourceList[0].valname
								// 赋值
								for (let ekey in item) {
									let eParams = item[ekey]
									for (let pKey in this.reportForm.details[index]) {
											
										if( pKey == 'client')
										{
											this.reportForm.details[index][pKey] = this.getClientFullName( this.bank_name )
										}										
										else if (ekey === pKey && eParams !== null && pKey !== 'id') {	
											this.reportForm.details[index][pKey] = eParams
										}
										else
										{
											
										}
									}
								}
							} else {
								this.reportForm.details[0].property_cert_info_id = item.id
                                if(item.ownership_type == 1) {
								    this.reportForm.details[0].cert_type = this.idTypeList[0].code
								}else {
									this.reportForm.details[0].cert_type = this.idCompanyTypeList[0].code
								}
								this.reportForm.details[0].use_source = this.useSourceList[0].valname
								this.reportForm.details[0].area_id = this.districtList[0].id
								window.console.log(this.reportForm.details[0].area_id)
								this.reportForm.details[0].area = this.districtList[0].shortname
								// 赋值
								for (let ekey in item) {
									let eParams = item[ekey]
									for (let pKey in this.reportForm.details[index]) {
										
										if( pKey == 'client')
										{
											this.reportForm.details[index][pKey] = this.getClientFullName( this.bank_name )
										}
										else if (ekey === pKey && eParams !== null && pKey !== 'id') {
											//	console.log('inital estate:',index)
												this.reportForm.details[index][pKey] = eParams									
										}
										else{
											
										}
									}
								}
							}

							// 图片字段名不一致
							this.reportForm.details[index].floorage = item.size
							this.reportForm.details[index].obligee = item.owner_name
							//this.reportForm.details[index].house_img = item.attachments
							console.log( 'get estate info:', this.reportForm,'selectIdTypeIndex ',this.selectIdTypeIndex )
							//this.reportForm.details[index].house_img_details = item.defaultAttachments

						})
					} else {

						toast(res.msg)
					}
					
					}catch(e)
					{
						console.log( "reqPropertyInfo catch err", e )
					}
				})
			},
			async initDistrict(city_id) {
				let params = {
					id: city_id
				}
				await getDistrict(params).then(res => {
					console.log('getDistrict')
					if (res.code === 1) {
						this.districtList = res.data

					} else {
						toast(res.msg)
					}
				})
			},
			
			onChangeSelect(event, name, index, item) {
				if (!event) {
					return
				}

				switch (name) {

					case 'pay_type':
						this.selectPayTypeIndex = event.detail.value
						this.reportForm.pay_type = this.payTypeList[this.selectPayTypeIndex].code

					case 'provident_fund':
						this.selectProvidentFundIndex = event.detail.value
						this.reportForm.has_provident_fund = this.hasProvidentFundList[this.selectProvidentFundIndex].code
						break
					case 'business_type':
						this.selectBusinessTypeIndex = event.detail.value
						this.reportForm.business_type = this.businessTypeList[this.selectBusinessTypeIndex].code
						break
					case 'business_source':
						this.selectBusinessSourceIndex = event.detail.value
						this.reportForm.business_source = this.businessSourceList[this.selectBusinessSourceIndex].code
						break
					case 'use_source':
					    this.selectUseSourceIndex[index].index = event.detail.value
					    this.reportForm.details[index].use_source = this.useSourceList[this.selectUseSourceIndex[index].index].valname
						break
					case 'cert_type':
						if (this.estateList[index].ownership_type === '1') {
						 this.selectIdTypeIndex[index].index = event.detail.value
							this.reportForm.details[index].cert_type = this.idTypeList[this.selectIdTypeIndex[index].index]
								.code

						} else {
							console.log('this.selectCompanyTypeIndex[index].index', this.selectCompanyTypeIndex )
							this.selectCompanyTypeIndex[index].index = event.detail.value
							console.log('this.selectCompanyTypeIndex[index].index', this.selectCompanyTypeIndex,index )
							this.reportForm.details[index].cert_type = this.idCompanyTypeList[this.selectCompanyTypeIndex[
								index].index].code
						}
						break
					case 'completion_date':
						item.completion_date = event.detail.value

						break
					case 'buy_date':
						item.buy_date = event.detail.value

						break
					case 'evaluate_date':
						item.evaluate_date = event.detail.value

						break
					case 'land_use_begin':
						item.land_use_begin = event.detail.value

						break
					case 'land_use_end':
						item.land_use_end = event.detail.value

						break
					case 'district':

						this.selectDistrictIndex[index].index = event.detail.value
						this.reportForm.details[index].area_id = this.districtList[this.selectDistrictIndex[index].index].id
						this.reportForm.details[index].area = this.districtList[this.selectDistrictIndex[index].index].shortname
						break

				}
			}
		}
	}
</script>

<style>
	/* 将 mask 缩小到一行 */
	.uni-popup-mask {
		height: 1px;
	}

	/* 将 mask 演示为浅蓝色 */
	.uni-popup-mask::before {
		background-color: #e6f1ff;
	}

	.uni-list-cell {
		line-height: 90rpx;
		display: flex;
		align-items: center;
	}


	.btn {
		color: #ff6a00;
		border: 1px;
		border-color: blue;
	}

	.require {
		color: red;
	}

	.line {
		width: 100%;
		height: 5rpx;
		background-color: #E6E6E6;
	}

	.line2 {
		width: 100%;
		height: 20rpx;
		background-color: #F5F5F5;
	}

	.button-sp-area {
		margin: 0 auto;
		width: 80%;
		text-align: center;
	}

	.mini-btn {
		margin-right: 20rpx;
	}
</style>
