<template>
  <view class="inquiry-container">


  <!---->
	<view >
		<uni-section title="银行信息" type="line">
		 <uni-card>
		 <view class="uni-flex uni-row" style="-webkit-justify-content: space-between;justify-content: space-between;">
				<view  style="width: 150rpx;">询价状态:</view>
				<view class="btnInquiry" style="-webkit-flex: 1;flex: 1;" >{{inquiry.status_str}}</view>
		 </view>
		<view class="uni-flex uni-row" style="-webkit-justify-content: space-between;justify-content: space-between;">
				<view  style="width: 150rpx;">单  号:</view>
				<view  style="-webkit-flex: 1;flex: 1;">{{inquiry.order_no}}</view>
		</view>
		<view class="uni-flex uni-row">
			<view  style="width: 150rpx;">银行名称:</view>
			<view  style="-webkit-flex: 1;flex: 1;">{{inquiry.bank_name}}-{{inquiry.bank_branch_name}}-{{inquiry.bank_sub_name}}</view>
		</view>
		<view class="uni-flex uni-row">
			<view  style="width: 150rpx;">询价时间:</view>
			<view  style="-webkit-flex: 1;flex: 1;">{{inquiry.create_time}}</view>
		</view>
		<view v-if="inquiry!=null&&inquiry.status!=1" class="uni-flex uni-row">
			<view  style="width: 150rpx;">回价时间:</view>
			<view  style="-webkit-flex: 1;flex: 1;">{{inquiry.response_time}}</view>
		</view>
		<view v-if="inquiry!=null&&inquiry.status!=1" class="uni-flex uni-row">
			<view  style="width: 150rpx;">回价员:</view>
			<view  style="-webkit-flex: 1;flex: 1;">{{inquiry.response_username}}</view>
		</view>

		<view class="uni-flex uni-row">
			<view  style="width: 150rpx;">客户经理:</view>
			<view  style="-webkit-flex: 1;flex: 1;">{{inquiry.bank_customer_mgr_name}}</view>
		</view>
		<view class="uni-flex uni-row">
			<view  style="width: 150rpx;">评估目的:</view>
			<view  style="-webkit-flex: 1;flex: 1;">{{inquiry.eva_purpose}}</view>
		</view>
		<view class="uni-flex uni-row">
			<view  style="width: 150rpx;">物业数量:</view>
			<view  style="-webkit-flex: 1;flex: 1;">{{getEstateCount()}}</view>
		</view>
		<view class="uni-flex uni-row">
			<view  style="width: 150rpx;">回价附件:</view>
			<view  style="-webkit-flex: 1;flex: 1;">
				<view v-for="(file, index) in inquiry.return_price_attachments_list" :key="index">
					<view class="uni-flex uni-row">
						<view  style="width: 250rpx;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;">{{file.name}}</view>
					</view>
				</view>

			</view>
			<view  style="" v-if="inquiry.return_price_attachments_list.length>0">
				<uni-button type="primary" plain size="mini" @click="previewFile(inquiry.return_price_attachments_list)">预览</uni-button>
			</view>
		</view>

		</uni-card>
		</uni-section>


		<uni-section title="物业信息" type="line" style="position: relative;">
			<uni-button type="primary" size="mini" style="position: absolute; right: 20px; top: 10px;" @click='showAllDetailsDialog'>
				复制
			</uni-button>
			<view v-for='(item, index) in inquiry.inquiry_details' :key='index'>
				<view class="detail">
					<uni-card>
						<view class="uni-flex uni-row" style="justify-content: flex-end;">
							<view  style="width: 440rpx;"></view>
						</view>
						<view :ref="`copyContainerRef${index}`">
							<view class="uni-flex uni-row">
								<view  style="width: 150rpx;">物业名称{{index+1}}:</view>
								<view  style="-webkit-flex: 1;flex: 1;">{{item.property_full_name}}</view>
							</view>
							<view class="uni-flex uni-row">
								<view  style="width: 150rpx;">面积(㎡):</view>
								<view  style="-webkit-flex: 1;flex: 1;">{{item.size}}</view>
							</view>
							<view class="uni-flex uni-row">
								<view  style="width: 150rpx;">所在地区:</view>
								<view  style="-webkit-flex: 1;flex: 1;">{{inquiry.city}}</view>
							</view>
							<view class="uni-flex uni-row">
								<view  style="width: 150rpx;">房屋用途:</view>
								<view  style="-webkit-flex: 1;flex: 1;">{{item.usage}}</view>
							</view>
							<view class="uni-flex uni-row">
								<view  style="width: 150rpx;">银行:</view>
								<view  style="-webkit-flex: 1;flex: 1;">{{inquiry.bank_name +"-"+ inquiry.bank_branch_name + inquiry.bank_sub_name}}</view>
							</view>
							<view class="uni-flex uni-row">
								<view  style="width: 150rpx;">评估目的:</view>
								<view  style="-webkit-flex: 1;flex: 1;">{{inquiry.eva_purpose}}</view>
							</view>
							<view v-if="item.eva_unit_price!=0" class="uni-flex uni-row">
								<view  style="width: 150rpx;">评估单价:</view>
								<view  style="-webkit-flex: 1;flex: 1;">{{item.eva_unit_price}} 元/㎡</view>
							</view>
							<view v-if="item.eva_total_value!=0" class="uni-flex uni-row">
								<view  style="width: 150rpx;">评估总值:</view>
								<view  style="-webkit-flex: 1;flex: 1;">{{item.eva_total_value}} 元</view>
							</view>
							<view v-if="item.eva_net_value!=0" class="uni-flex uni-row">
								<view  style="width: 150rpx;">评估净值 1:</view>
								<view  style="-webkit-flex: 1;flex: 1;">{{item.eva_net_value}} 元</view>
							</view>
							<view v-if="item.eva_net_value2!=0" class="uni-flex uni-row">
								<view  style="width: 150rpx;">评估净值 2:</view>
								<view  style="-webkit-flex: 1;flex: 1;">{{item.eva_net_value2}} 元</view>
							</view>
							<view v-if="item.total_taxes1!=0&&item.total_taxes1!=null" class="uni-flex uni-row">
								<view  style="width: 150rpx;">税费 1:</view>
								<view  style="-webkit-flex: 1;flex: 1;">{{item.total_taxes1}} 元</view>
							</view>
							<view v-if="item.total_taxes2!=0&&item.total_taxes2" class="uni-flex uni-row">
								<view  style="width: 150rpx;">税费 2:</view>
								<view  style="-webkit-flex: 1;flex: 1;">{{item.total_taxes2}} 元</view>
							</view>
							<view class="uni-flex uni-row">
								<view  style="width: 150rpx;">产证附件:</view>
								<view  style="-webkit-flex: 1;flex: 1;">
									<view v-for="(file, index) in item.attachments_list" :key="index">
										<view class="uni-flex uni-row">
											<view  style="width: 250rpx;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;">{{file.name}}</view>
										</view>
									</view>

								</view>
								<view  style="" v-if="item.attachments_list.length>0">
									<uni-button type="primary" plain size="mini" @click="previewFile(item.attachments_list)">预览</uni-button>
								</view>
							</view>
						</view>
				<!--		<view v-if="item.!=0&&item." class="uni-flex uni-row">
							<view  style="width: 150rpx;">贷款成数:</view>
							<view  style="-webkit-flex: 1;flex: 1;">{{item.}} 元</view>
						</view>
						<view v-if="item.!=0&&item." class="uni-flex uni-row">
							<view  style="width: 150rpx;">贷款金额 2:</view>
							<view  style="-webkit-flex: 1;flex: 1;">{{item.}} 元</view>
						</view>
						<view v-if="item.!=0&&item." class="uni-flex uni-row">
							<view  style="width: 150rpx;">外部备注:</view>
							<view  style="-webkit-flex: 1;flex: 1;">{{item.}} 元</view>
						</view>	-->
					<!--  <button v-if="inquiry!=null&&inquiry.status!=1" @click="showDialog(item)">复制询价条</button>-->
					   <!-- <quick-inquiry-info v-if="inquiryDetail!=null"    :dialog-visible="dialogVisible"  :inquiry-detail="inquiryDetail"     @close-dialog="dialogVisible = false" @copy-to-clipboard="copyToClipboard" ></quick-inquiry-info> -->
<!--					   <copy-inquiry-info v-if="inquiryDetail!=null" :dialog-visible="dialogVisible" :inquiry="inquiryDetail" @close-dialog="dialogVisible = false" @copy-to-clipboard="copyToClipboard" :detailIndex="detailIndex"></copy-inquiry-info>-->
					   <copy-inquiry-info v-if="inquiry.inquiry_details.length > 0" :dialog-visible="dialogVisible" :inquiry="inquiry" :list="inquiry.inquiry_details" @close-dialog="dialogVisible = false" @copy-to-clipboard="copyToClipboard"></copy-inquiry-info>
					</uni-card>
				</view>
			</view>
		</uni-section>
	</view>
</view>
</template>

<script>
import { getUserID } from '@/utils/auth'
import { returnedPriceDetails,inquiryDetail } from '../../api/inquiry/inquiry'
// import QuickInquiryInfo from "./quickInquiryInfo.vue";
import CopyInquiryInfo from "./copyInquiryInfo.vue";

  export default {
	components: {
	    CopyInquiryInfo,
	},
    data() {
      return {
		keyword:'',
		currentItem:{},
        inquiry: [],
		//inquiry_detail:this.$route.query.inquir_detail_item,
		total:0,
		evaluatePurposeList:[],
		dialogVisible: false,
		inquiryDetail: null,
		list:[],
		detailIndex:0
      }
    },
  mounted() {
		this.dialogVisible//	dialogVisible, //是否
  	},


	onLoad(options)
	{
		// 外部传入
		let inquir_detail_item =  JSON.parse(decodeURIComponent(options.inquir_detail_item));
		this.inquiry = inquir_detail_item;

	},

    methods: {

		// 获取物业数量
		getEstateCount()
		{
			if( undefined != this.inquiry.inquiry_details)
			{
				return this.inquiry.inquiry_details.length
			}
			else{
				return 0
			}
		},
      showAllDetailsDialog(){
        this.dialogVisible = true;
      },

		  showDialog(index) {
			//   let detail = item;
			//   let params = { inquiry_id: this.inquiry.id, property_cert_info_id: detail.id };
			//   // 打开询价条窗口前向服务器请求数据
			//   inquiryDetail(params).then(response => {
			// 	if (response && response.data) {
			// 	  this.inquiryDetail = response.data;
			// 	  console.log("showDialog(item) ",this.inquiryDetail)
			// 	  this.dialogVisible = true;
			// 	}
			//   });
				this.inquiryDetail = this.inquiry;
				this.detailIndex = index;
				console.log("showDialog(item) ",this.inquiryDetail)

				this.dialogVisible = true;
			},

			openQuickInquiryInfo(item) {
				let detail = item;
				let params = { inquiry_id: this.inquiry.id, property_cert_info_id: detail.id };

				inquiryDetail(params).then(response => {
				if (response && response.data) {
				  this.$refs.quickInquiryInfo.open();
				  }
				})


			},

		  copyToClipboard() {
		      // 将多行输入框的内容复制到手机剪贴板上
		      uni.setClipboardData({
		        data: this.inquiryDetail,
		        success() {
		          uni.showToast({
		            title: '已复制',
		            duration: 2000
		          });
		        }
		      });
		},

		search() {

			this.getWaitReturnPriceList(this.keyword);
		},
		newInquiry() {
			this.$tab.navigateTo('/pages/inquiry/addInquiry')
		},

		// 请求详情
		onRequestInquiryDetal() {
		        uni.navigateTo({
		          url: '/pages/inquiry/detail?inquiry_id=${this.currentItem.order_no}'
		        })
		      },

		//询价待回价
		getWaitReturnPriceList(keyword) {
		  let params = {
			"page": 1,
			"limit": 100,
			"bank_id": "",
			// "status": "1",
			"type": "",
			// "start_time": "2023-02-10",
			// "end_time": "2023-05-11",
			"eva_purpose": "",
			"search_text": keyword,
			"user_id": getUserID(),
			"response_username": "",
			"start_time_length": "",
			"end_time_length": ""
		  }
		  waitReturnPrice(params).then(res => {
			window.console.log(res)
		    this.list = res.data.data
			this.total = res.data.count
		  }).catch(() => {
			console.log('waitReturnPrice Error')
		  })
		},
		handleClick(item) {
			this.currentItem = item
			this.$refs.surveyType.open()
			//this.$tab.navigateTo(`/pages/survey/detail?order_no==${item.order_no}&id=${item.id}&survey_id=${item.survey_id}&survey_type=${item.survey_type}`)
		},
		navSurvey(surveyType) {
			let item = this.currentItem
			if(surveyType == 'house') {
				this.$tab.navigateTo(`/pages/survey/houseDetail?order_no==${item.order_no}&id=${item.id}&survey_id=${item.survey_id}&survey_type=1`)
			}else if (surveyType == 'shop') {
				this.$tab.navigateTo(`/pages/survey/shopDetail?order_no==${item.order_no}&id=${item.id}&survey_id=${item.survey_id}&survey_type=2`)
			}
		},
		onCopyPress(index) {
			const ref = `copyContainerRef${index}`;
			const element = this.$refs[ref];

			if (!element) {
				uni.showToast({ title: '复制失败：未找到内容', icon: 'none' });
				return;
			}

			const targetElement = Array.isArray(element) ? element[0] : element;
			let textContent = '';

			// 关键修改：使用 innerText 代替 textContent 保留换行符
			if (targetElement instanceof HTMLElement) {
				textContent = targetElement.textContent.trim();
			} else if (targetElement && targetElement.$el) {
				// 如果是 Vue 组件实例，获取其根元素的文本
				textContent = targetElement.$el.textContent.trim();
			}
			console.log("textContent",textContent);
			// 去除首尾空白并确保换行符统一为 \n
			textContent = textContent.trim().replace(/。/g, '\n');

			if (!textContent) {
				uni.showToast({ title: '复制失败：无内容可复制', icon: 'none' });
				return;
			}

			uni.setClipboardData({
				data: textContent,
				success() {
					uni.showToast({ title: '复制成功', duration: 2000 });
				},
				fail(err) {
					uni.showToast({ title: `复制失败：${err.errMsg}`, icon: 'none' });
				}
			});
		},
		previewFile(file) {
			if(file.length == 0) {
				uni.showToast({ title: '无图片可预览', icon: 'none' });
				return;
			}
			const urls = file.filter(item => item.ext === 'jpg' || item.ext === 'png' || item.ext === 'jpeg')
			.map(item => item.url);

			uni.previewImage({
				urls: urls,
				longPressActions: {
					itemList: ['保存图片'],
					success: function(data) {
						console.log('选中了第' + (data.tapIndex + 1) + '个按钮, 下标为：' + data.index)
					},
					fail: function(err) {
						console.log(err.errMsg)
					}
				}
			})
		},
    }
  }
</script>

<style lang="scss" scoped>
  page {
    background-color: #f8f8f8;
  }

  .inquiry-container {
    margin-bottom: 100rpx;
    padding: 20rpx;
  }
  .toolbar {
	 margin-bottom: 50rpx;
	 padding: 10rpx;
  }
  .btn {
  	  color: #ff6a00;
	  border:1px;
	  border-color: blue;
  }
  .btnInquiry {
	  color: #ff6a00;
  }

  .line {
    width: 100%;
    height: 5rpx;
    background-color: #E6E6E6;
  }
  .line2 {
    width: 100%;
    height: 20rpx;
    background-color: #F5F5F5;
  }
  .text-title {
    color: #303133;
    font-size: 32rpx;
    font-weight: bold;
    margin-left: 10rpx;

    .iconfont {
      font-size: 16px;
      margin-right: 10rpx;
    }
  }

  .text-item {
    font-size: 28rpx;
    padding: 24rpx;
  }
  .flex-item {
  	width: 33.3%;
  	height: 50rpx;
  	text-align: center;
  	//line-height: 200rpx;
  }
</style>
