<template>

  <view class="inquiry-container">

  <view class="uni-flex uni-row uni-mb-4">
	  <view class="flex-item "><button size="mini" type="primary" @click="newInquiry()">发起询价</button></view>
	  <!-- <view class="flex-item "> <button size="mini" type="primary" @click="openFilter()">展示全部</button> </view> -->
	  <view class="flex-item" style="margin-top: 10rpx;  margin-left: 80rpx; text-align: center; color: cornflowerblue;  width: 350rpx;height: 50rpx;"  >  “过滤” 能提升速度  </view>
	  <view class="flex-item "><button style="background-color:blue" size="mini" type="primary" @click="openFilter()">过滤</button></view>
  </view>
  <uni-card>
		  <view class="uni-flex uni-row uni-mb-4">
			<view class="uni-list-cell-left" style="margin-top: 20rpx;">
				<text style="color: blue;" >询价开始日期</text>
			</view>
			<view class="uni-list-cell-db">
				<picker mode="date" :value="inquiryRequest.start_time" :start="startDate" :end="endDate" @change="onChangeSelect($event, 'InquirDate',0,inquiryRequest)">
					<view class="uni-input" style="color: blue; border-radius: 10rpx; border: 1rpx solid #999999 ;margin-left: 10rpx; margin-top: 0rpx; width: 280rpx;height: 80rpx;text-align: center;"  >{{inquiryRequest.start_time}}</view>
				</picker>
			</view>
		  </view>
			<view class="uni-flex uni-row uni-mb-4">
						<view class="uni-list-cell-left" style="margin-top: 20rpx;">
							<text >询价结束日期</text>
						</view>
						<view class="uni-list-cell-db">
							<picker mode="date" :value="inquiryRequest.end_time" :start="startDate" :end="endDate" @change="onChangeSelect($event, 'InquiryEndDate',0,inquiryRequest)">
								<view class="uni-input" style=" border-radius: 10rpx; border: 1rpx solid #999999 ;margin-left: 10rpx; margin-top: 0rpx; width: 280rpx;height: 80rpx;text-align: center;"  >{{inquiryRequest.end_time}}</view>
							</picker>
						</view>
			</view>

			<view class="uni-flex uni-row uni-mb-4">
				<view class="flex-item" style="border-radius: 10rpx; margin-left: 23rpx; border: 1rpx solid #dddddd ; text-align: center;  width: 480rpx;height: 70rpx;"  >
					<input class="uni-input" style="margin-left: 5rpx;width:460rpx;height: 65rpx" v-model="keyword"  placeholder="搜索 物业名 / 银行" /></view>
			</view>
	</uni-card>

	<view class="uni-flex uni-row uni-mb-4">
		<view class="flex-item" style="margin-left: 530rpx;margin-top: 0rpx;">
			<button size="mini" type="primary" @click="search()">搜索</button></view>
	</view>

	<view class="uni-flex uni-row uni-mb-4">
	<view class="flex-item" style=" margin-left: 40rpx; text-align: left;width: 450rpx;height: 30rpx;font-weight: 700;"  >询价量：{{ total==0?"加载中...":pageInfo.limit>total?total:pageInfo.limit}} / {{total==0?"":total}} </view>
	</view>

	<view  v-for="(item, findex) in list" :key="findex">
		 <uni-card>
		<view class="detail" @click="goToDetail(item)">
		<view class="uni-flex uni-row" style="-webkit-justify-content: space-between;justify-content: space-between;">
				<view >{{item.order_no}}</view>
				<view class="btnInquiry" >{{item.status_str}}</view>
		</view>
		<view class="line" ></view>

			<view class="uni-flex uni-row">
				<view  style="width: 150rpx;">物业名称:</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.is_multi_str}}</view>
			</view>
			<view class="uni-flex uni-row">
				<view  style="width: 150rpx;">所属银行:</view>
				<view  style="-webkit-flex: 1;flex: 1;">{{item.bank_name +'-'+ item.bank_branch_name + "-"+ item.bank_sub_name}}</view>
			</view>
			<view class="uni-flex uni-row">
				<view  style="width: 150rpx;">客户经理:</view>
				<view  style="-webkit-flex: 1;flex: 1;">{{item.bank_customer_mgr_name}}</view>
			</view>
			<view class="uni-flex uni-row">
				<view  style="width: 150rpx;">评估目的:</view>
				<view  style="-webkit-flex: 1;flex: 1;">{{item.eva_purpose}}</view>
			</view>
			<view class="uni-flex uni-row">
				<view  style="width: 150rpx;">面积(M):</view>
				<view  style="-webkit-flex: 1;flex: 1;">{{item.size}}</view>
			</view>
			<view v-if="item.eva_unit_price!=0" class="uni-flex uni-row">
				<view  style="min-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 class="uni-flex uni-row">
				<view  style="width: 150rpx;">询价员:</view>
				<view  style="-webkit-flex: 1;flex: 1;">{{item.response_username}}</view>
			</view>-->
			<view class="uni-flex uni-row">
				<view  style="width: 150rpx;">询价时间:</view>
				<view  style="-webkit-flex: 1;flex: 1;">{{item.create_time}}</view>
			</view>
			<view v-if="item.maker!=null" class="uni-flex uni-row">
				<view  style="width: 150rpx;">报告制作人:</view>
				<view  style="-webkit-flex: 1;flex: 1;">{{item.maker}}</view>
			</view>
			<view v-if="item.estimate_no!=null" class="uni-flex uni-row">
				<view  style="width: 200rpx;">电子档提取码:</view>
				<view  style="-webkit-flex: 1;flex: 1;">{{item.estimate_no}}</view>
			</view>
		</view>
		<view class="uni-flex uni-row uni-mb-4">
			<view v-if="item.status == 2" class="flex-item "><button size="mini" style="width: 180rpx;background-color: #CC985E;" type="primary" @click="newSurvey(item)">发起查勘</button></view>

<!--			<view v-if="item.status == 4 || item.status == 5" class="flex-item "><view class="btn"><text @click="createReport(item)">申请生成报告</text></view></view>-->
			<!-- <view class="flex-item "><view class="btn"><text @click="cancelInquiry()">撤单</text></view></view>
			<view class="flex-item "><view class="btn"><text @click="uploadCertificate()">上传产证</text></view></view>
			<view class="flex-item "><view class="btn"><text @click="adjustInquiry()">调价</text></view></view> -->

		</view>
		<!-- <view class="line2" v-if="findex !== list.length - 1"></view> -->
		</uni-card>
	</view>
	<uni-popup ref="filterPopup" type="right">
			  <view>

				<uni-card  @click="closePopup">
				  <uni-data-checkbox mode="tag" selectedColor="red" @change="filterInquiry"
				  	v-model="filterStatus"
				  	:localdata="inquiryStatus">
				  </uni-data-checkbox>
				  </uni-card>
	    </view>
	</uni-popup>
  </view>
</template>

<script>
import { getUserID } from '@/utils/auth'
import { waitReturnPrice,getOfficialResultStatus } from '../../api/inquiry/inquiry'


  export default {
    data() {
      return {
		keyword:'',
		filterStatus:'0',
		pageInfo:null,
		inquiryRequest:{
			"page": "1",
			"limit": "20",
			"bank_id": "",
			"status": "0",
			"type": "",
			 "start_time": this.getPastDate(10),
			 "end_time": this.getPastDate(0),
			"eva_purpose": "",
			"search_text": "",
			"user_id": getUserID(),
			"response_username": "",
			"start_time_length": "",
			"end_time_length": "",
		},
		inquiryStatus:[
			{
				"value": "0",
				"text": "所有 "
			},
			{
				"value": "-1",
				"text": "已撤单（未申请出报告）"
			},
			{
				"value": "-2",
				"text": "已撤单（已申请出报告）"
			},
			{
				"value": "1",
				"text": "待回价"
			},
			{
				"value": "2",
				"text": "待业务确认"
			},
			{
				"value": "3",
				"text": "查勘待派单"
			},
			{
				"value": "4",
				"text": "查勘待完成"
			},
			{
				"value": "5",
				"text": "待生成报告"
			},
			{
				"value": "6",
				"text": "报告制作中"
			},
			{
				"value": "7",
				"text": "报告待审核"
			},
			{
				"value": "8",
				"text": "报告已完成"
			}
		],
		currentItem:{},
        list: [],
		total:0,

      }
    },
	mounted() {
	    this.getWaitReturnPriceList({});
	},
	computed: {
		startDate() {
			return this.getDate('start');
		},
		endDate() {
			return this.getDate('end');
		}
	},

	 created() {
		//this.filterStatus ="1"
		//console.log('created', this.filterStatus )
	  },

    methods: {

		  closePopup() {
		      this.$refs.filterPopup.close()
		} ,

		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}`;
		},

		filterInquiry(e) {
			this.inquiryRequest.status = this.filterStatus
			this.getWaitReturnPriceList(this.inquiryRequest);
			/*
			if(this.filterStatus == "0") {
				let item = {"status=0": this.filterStatus}
				this.getWaitReturnPriceList({});
			}else {
				let item = {"status": this.filterStatus}
				this.getWaitReturnPriceList(item);
			}*/
			this.$refs.filterPopup.close()
		},
		cancelFilter(){
			this.$refs.filterPopup.close()
		},

		openFilter(){
			this.$refs.filterPopup.open()
		},
		createReport(item) {
			this.$tab.navigateTo(`/pages/inquiry/createReportApply?order_no=${item.order_no}&inquiry_id=${item.quot_id}&city_id=${item.city_id}&bank_name=${item.bank_name}&inquiryStatus=${item.status}`)
		},
		newSurvey(item) {
			console.log('newSurvey: item:',item,item.quot_id)
			this.$tab.navigateTo(`/pages/inquiry/surveyApply?order_no=${item.order_no}&inquiry_id=${item.quot_id}&city_id=${item.city_id}`)
		},
		cancelInquiry() {

		},
		uploadCertificate() {

		},
		adjustInquiry() {

		},
		search() {
			let item = {"search_text": this.keyword}
			this.inquiryRequest.search_text = this.keyword
			let start_date = new Date(this.inquiryRequest.start_time)
			let end_date = new Date(this.inquiryRequest.end_time)
			let gap = start_date.getTime() - end_date.getTime()
			if( gap > 0)
			{
				uni.showToast({
					title:"询价开始时间不能大于当前时间！"
				})
				return
			}

			this.getWaitReturnPriceList(item);

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

		// 跳到详情页
		goToDetail( item ) {
			let evaList = this.evaluatePurposeList;
			this.$tab.navigateTo('/pages/inquiry/detail?inquir_detail_item='+ encodeURIComponent(JSON.stringify(item)) );
		},

		// 日期计算函数，获取当前时间，往前days天的日期，格式为 2024-1-1
		getPastDate(days) {
		    const currentDate = new Date();
		    const pastDate = new Date(currentDate.getTime() - days * 24 * 60 * 60 * 1000); // 一天的毫秒数
		    const year = pastDate.getFullYear();
		    const month = ("0" + (pastDate.getMonth() + 1)).slice(-2); // 月份从0开始，所以需要+1，并且前面补0
		    const day = ("0" + pastDate.getDate()).slice(-2); // 日前面补0

		    return `${year}-${month}-${day}`;
		},

		//询价待回价
		getWaitReturnPriceList(item) {
		  let params = Object.assign({}, params, item)
		  params = this.inquiryRequest
		  params.user_id = getUserID()
		  this.pageInfo = this.inquiryRequest//params;
		  //console.log("getWaitReturnPriceList:",params,item)
		  uni.showLoading({})
	//	  console.log('getWaitReturnPriceList param',params )
		  waitReturnPrice(params).then(res => {
			window.console.log('waitReturnPrice:',res)
			this.initialStatus( res.data.data )
		    this.list = res.data.data
			this.total = res.data.count

			if(this.total == 0 )
			{
				 uni.showToast({title:'找不到询价数据！\r\n 您可调整[ 询价开始日期 ]，扩大您的搜索范围。', icon:'error',duration:3000})
			}
			uni.hideLoading()
		  }).catch((r) => {
			console.log('waitReturnPrice Error:',r)
		  })
		},

		initialStatus( list )
		{
			console.log( 'list length:',list.length)
			for( let i=0; i<list.length; ++i)
			{
				let item = list[i];
				//console.log('for ',item.status_str)
				if( item.bank_name == "中国银行" && item.status_str == "报告已完成")
				{
					setTimeout(() => {
					  // 在这里执行你的函数
					  let params = { pre_estimate_no:item.estimate_no }
					  getOfficialResultStatus(params).then( res =>{
								list[i].status_str  = this.getStatueStr(  item.bank_name, item.status_str , res.data ) ;
								console.log("initialStatus : response getOfficalresut status",item.status_str  )
								this.$forceUpdate() ;
						})
					}, 200);
				}
			}

		},

		// 获取处理状态数据
		getStatueStr( bankNameStr , statusStr,handle_str ) {
			// 只有中国银行有是否提交报告
			// if( '中国银行'== bankNameStr)
			//   {
			// 	  if( '报告已完成' == statusStr )
			// 	  {
			// 		  if( 1 == handle_str)
			// 		  {
			// 			  return statusStr+'(已传中行)'
			// 		  }
			// 		  else{
			// 			   return statusStr+'(未传中行)'
			// 		  }
			// 	  }
			//   }
			//   else{
				  return statusStr;
			  // }
		},

		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}`)
		},


		onChangeSelect(event, name, index, item) {
			if (!event) {
				return
			}
			switch (name) {
				case 'InquirDate':
				    this.inquiryRequest.start_time = event.detail.value
					this.search()
					break;
				case 'InquiryEndDate':
					console.log('end date', event.detail.value)
					this.inquiryRequest.end_time = event.detail.value
					this.search()
				break;
			}
		},

		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`)
			}
		}

    }
  }
</script>

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

  .inquiry-container {
    margin-bottom: 100rpx;
    padding: 20rpx;
  }
  .toolbar {
	 margin-bottom: 50rpx;
	 padding: 10rpx;
  }
  .btn {
  	  color: #007aff;

	  margin: 10rpx;
	  border-color: blue;
	  background-color: #F5F5F5;
  }
  .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;
  }
  .button-sp-area {
      margin: 0 auto;
      width: 80%;
  }
  .mini-btn {
  	margin-right: 20rpx;
  }
</style>
