<template>
  <view class="survey-container">
	<view>
		<!-- 分享示例 -->
		<uni-popup ref="surveyType" type="share" safeArea backgroundColor="#fff">
			<button class="button"  @click="navSurvey('house')"><text class="button-text">住宅查勘模板</text></button>
			<button class="button"  @click="navSurvey('shop')"><text class="button-text">商业查勘模板</text></button>
			
		</uni-popup>
	</view>
	<view><uni-easyinput class="uni-mt-5" suffixIcon="search" v-model="keyword" placeholder="输入物业名或单号" @input="inputClick" @iconClick="searchClick"></uni-easyinput></view>
	<view v-for="(item, findex) in list" :key="findex">
		<uni-card  >
		<view @click="handleClick(item)">	
		<view><button size="mini" style="margin-left: 10rpx; margin-top: 10rpx; height: 55rpx;text-align: center; background-color: #FF3333;" type="primary" @click="returnSurvey(item)">退单</button></view>
		    <view class="uni-flex uni-row" style="-webkit-justify-content: space-between;justify-content: space-between;">
				<view >{{item.order_no}}</view>
				<view class="btnSurvey" >查勘待跟进</view>
		    </view>
		    <view class="line" ></view>
		    <view class="detail">
				<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.city}}</view>
				</view>
				<view class="uni-flex uni-row">
					<view  style="width: 150rpx;">小片区:</view>
					<view  style="-webkit-flex: 1;flex: 1;">{{item.area}}</view>
				</view>
				<view class="uni-flex uni-row">
					<view  style="width: 150rpx;">物业类型:</view>
					<view  style="-webkit-flex: 1;flex: 1;">{{item.type_str}}</view>
				</view>
				<view class="uni-flex uni-row">
					<view  style="width: 150rpx;">银行:</view>
					<view  style="-webkit-flex: 1;flex: 1;">{{item.bank_name}}</view>
				</view>
			<view class="uni-flex uni-row">
				<view  style="width: 150rpx;">查勘员:</view>
				<view  style="-webkit-flex: 1;flex: 1;">{{item.surver_username}}</view>
			</view>
			<!--<view class="uni-flex uni-row">
				<view  style="width: 150rpx;">询价员:</view>
				<view  style="-webkit-flex: 1;flex: 1;">{{item.appraiser_name}}</view>
			</view> -->
				<view class="uni-flex uni-row">
					<view  style="width: 150rpx;">业务员:</view>
					<view  style="-webkit-flex: 1;flex: 1;">{{item.buss_username}}</view>
				</view>
				<view class="uni-flex uni-row">
					<view  style="width: 150rpx;">指派时间:</view>
					<view  style="-webkit-flex: 1;flex: 1;">{{item.assign_time}}</view>
				</view>
		    </view>
		</view>		
		<view style="margin-left: -20rpx; text-align: left; display: flex;justify-content: space-between;">
			<button size="mini" style="margin-top: 10rpx; height: 55rpx;text-align: center; background-color: #CC985E;" type="primary" @click="changeSurveyToAaskedConfirm(item)">退回待派单</button>
			<button size="mini" style="margin-top: 10rpx; height: 55rpx;text-align: center; background-color: #CC985E;" type="primary" @click="transferSurvey(item)">转 派</button>
		</view> 
		</uni-card>
	</view>
	<uni-popup ref="transferSurveyPopup" type="right">
		<view style="width: 300px;">
			<uni-section title="查勘转派"  type="line" padding>
				
			  <uni-data-checkbox mode="tag" selectedColor="red" 
				v-model="surveyUser"
				:localdata="surveyUserList">
			  </uni-data-checkbox>
		    <view class="line3"></view>
		    	<view class="button-sp-area">
		    		<button class='mini-btn' @click='cancelTransferSurvey()' type="primary" size='mini'>取消</button>
		    		<button class='mini-btn' @click='submitTransferSurvey()' type="primary" size='mini'>提交</button>
		    	</view>
		    </uni-section>	
	    </view>
	</uni-popup>
	<uni-popup ref="returnSurveyPopup" >
		<view style="width: 300px;">
			<uni-section title="查勘退单"  type="line" padding>
				<uni-easyinput  type="textarea" v-model="return_reason" placeholder="请输入退单理由"></uni-easyinput>
			    <view class="line3"></view>
				<view class="button-sp-area">
					<button class='mini-btn' @click='cancelReturnSurvey()' type="primary" size='mini'>取消</button>
					<button class='mini-btn' @click='submitReturnSurvey()' type="primary" size='mini'>提交</button>
				</view>
			</uni-section>	
	    </view>
	</uni-popup>
	<uni-popup ref="confirmPopup" type="dialog">
		<uni-popup-dialog mode="base" content="确认退回到待派单吗？"  :before-close="true" @close="closeConfirm" @confirm="confirmChangeToAsked"></uni-popup-dialog>
	</uni-popup>
  </view>
</template>

<script>
import { waitSurvey, transferSurveyUser, returnSurvey, getRole,returnSurveyToAsked} from '../../api/survey/survey'
import { toast } from '@/utils/common'
  export default {
	name:'WaitSurveyList',
    data() {
      return {
		return_reason:'',  
		keyword:'',
		currentItem:{},
        list: [],
		surveyUserList:[],
		surveyUser: -1
      }
    },
	mounted() {
	    this.getWaitSurveyList();
	},
    methods: {
		initSurveyor() {
			getRole({ roleCode: 'Surveyor' }).then(res => {
		        if (res.code === 1) {
		         
		          let surveyList = res.data
				  
				  this.surveyUserList = surveyList.map(item=>{
					  return {value:item.userId,text:item.userNickName}
				  })
		         
		        }
		      })
		
		},	  
		cancelTransferSurvey() {
			this.$refs.transferSurveyPopup.close()
		},
		submitTransferSurvey() {
			if(this.surveyUser == -1) {
				toast('请选择查勘员')
				return
			}
			let surveyNickName = ''
			for(let i = 0 ; i<this.surveyUserList.length;i++){
				if(this.surveyUserList[i].value == this.surveyUser){
					surveyNickName = this.surveyUserList[i].text
					break
				}
			}
			let param = {survey_id:this.currentItem.survey_id,
			             surveyName: surveyNickName,
						 surveyUserId:this.surveyUser}
			
			transferSurveyUser(param).then(res => {
			  
				  toast(res.msg)
				  this.$refs.transferSurveyPopup.close()
				  this.searchClick()
			}).catch((e) => {
						toast(e)
						this.$refs.transferSurveyPopup.close()
			})	
		},
		cancelReturnSurvey() {
			this.$refs.returnSurveyPopup.close()
		},
		submitReturnSurvey() {
			if(this.return_reason == ''){
				toast('请填写退单理由')
				return
			}
			let param = {survey_id:this.currentItem.survey_id,
			             return_reason:this.return_reason}
			returnSurvey(param).then(res => {
			  
				  toast(res.msg)
				  this.$refs.returnSurveyPopup.close()
				  this.searchClick()
			}).catch((e) => {
						toast(e)
						this.$refs.returnSurveyPopup.close()
			})			 
		},
		inputClick(inputword) {
			
		},
		searchClick() {
			if(this.keyword){
				let param = {keyword:this.keyword}
				this.getWaitSurveyList(param); 
			}else {
				this.getWaitSurveyList();
			}
		},
		//查勘待跟进
		getWaitSurveyList(param = {}) {
		  let item = { page:1,limit:50}
		  param = Object.assign({}, param, item)	  
		  waitSurvey(param).then(res => {
		    this.list = res.data.list
		  }).catch(() => {
			console.log('waitSurvey Error')
		  })
		},
		transferSurvey(item){
			this.currentItem = item
			if(this.surveyUserList == false) {
				this.initSurveyor()
			}
			this.$refs.transferSurveyPopup.open()
		},
		changeSurveyToAaskedConfirm(item) {
			this.currentItem = item
			this.$refs.confirmPopup.open()
			
		},
		closeConfirm(){
			this.$refs.confirmPopup.close()
		},
		confirmChangeToAsked() {
			this.changeSurveyToAasked(this.currentItem)
			this.$refs.confirmPopup.close()
		},
		changeSurveyToAasked(item) {
			
			let param = {survey_id:item.survey_id}
			returnSurveyToAsked(param).then(res => {
			 toast(res.msg)
				  
				  this.searchClick()
			 
			}).catch((e) => {
						toast(e)
			})	
		},
		returnSurvey(item) {
			this.currentItem = item
			this.return_reason = ''
			this.$refs.returnSurveyPopup.open()
		},
		handleClick(item) {
			window.console.log(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`)
			}
		}
		
    }
  }
</script>

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

  .survey-container {
    margin-bottom: 100rpx;
    padding: 20rpx;
  }
  
  .btnSurvey {
	  color: #ff6a00;
  }
	.btn {
	  color: #007aff;
	  
	  margin: 10rpx;
	  border-color: blue;
	  background-color: #F5F5F5;
	}
  .line {
    width: 100%;
    height: 5rpx;
    background-color: #E6E6E6;
  }
  .line2 {
    width: 100%;
    height: 5rpx;
    background-color: #F5F5F5;
  }
  .line3 {
    width: 100%;
    height: 30rpx;
  }
  .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: 30rpx;
  	text-align: center;
  	line-height: 200rpx;
  }
  .button-sp-area {
  	margin: 0 auto;
  	width: 80%;
  	text-align: center;
  }
  .mini-btn {
  	margin-right: 20rpx;
  }
</style>
