<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>	
			<view class="uni-flex uni-row" style="-webkit-justify-content: space-between;justify-content: space-between;">
					<view >{{item.order_no}}</view>
					<view class="btnSurvey" @click="handleClick(item)">待派单</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.type_str}}</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.assign_time}}</view>
				</view> -->
			</view>
		</view>
		<view >
		    <button size="mini" style="margin-left: 10rpx; margin-top: 10rpx; height: 55rpx;text-align: center; background-color: #CC985E;" type="primary" @click="acceptSurvey(item)">接单</button>
		</view>
		</uni-card>
	</view>
  </view>
</template>

<script>
import { getUserID } from '@/utils/auth'	
import { appointSurvey,getRole,selectSurveyUser } from '../../api/survey/survey'
import { toast } from '../../utils/common';
  export default {
	name:'AppointSurveyList',
    data() {
      return {
		keyword:'',
		currentItem:{},
        list: []
      }
    },
	mounted() {
	    this.getAppointSurveyList();
	},
    methods: {
		inputClick(inputword) {
			
		},
		searchClick() {
			if(this.keyword == '' ){
				this.getAppointSurveyList();
			}else {
				let param = {keyword:this.keyword}
				this.getAppointSurveyList(param); 
			}
		},
		//查勘待派单
		getAppointSurveyList(param = {}) {
		  let item = { page:1,limit:50}
		  param = Object.assign({}, param, item)
		  appointSurvey(param).then(res => {
		    this.list = res.data.list
		  }).catch(() => {
			console.log('appointSurvey Error')
		  })
		},
		acceptSurvey(item) {
			this.currentItem = item
			let userId = getUserID()
			getRole({ roleCode: 'Surveyor' }).then(res => {
			    if (res.code === 1) {
			      let surveyList = res.data
				  let done = false
			      for(let i = 0; i < surveyList.length; i++){
					  if(userId == surveyList[i].userId) {
						  let param = {surveyName:surveyList[i].userNickName,
						               surveyPhone:surveyList[i].phonenumber,
									   surveyUserId:surveyList[i].userId,
									   survey_ids:[item.survey_id]}
						  selectSurveyUser(param).then(ret => {
						     toast('接单成功')
							 done = true
							 this.searchClick()
						  }).catch(() => {
						  			console.log('selectSurveyUser Error')
						  })
						break  
					  }
				  }
				  setTimeout(()=>{
					  if(!done){
						  toast('请在跟进中查看结果，有疑问请联系管理员')
					  }
				  },2000)
			    }
			  })	
		}
		
		
    }
  }
</script>

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

  .survey-container {
    margin-bottom: 100rpx;
    padding: 20rpx;
  }
  
  .btnSurvey {
	  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: 30rpx;
  	text-align: center;
  	line-height: 200rpx;
  }
</style>
