<template>
	<view class="page">
		<view class="item_line" >
		</view>
		<view class="item item_line item_textarea" >
			<view class="itemname redMi">原因分析</view>
			<textarea class="textarea" placeholder="请输入原因分析" v-model="msgInfo.msg" @change="dataChange" ></textarea>
		</view>
		<view class="item item_line item_textarea" >
			<view class="itemname redMi">解决方案</view>
			<textarea class="textarea" placeholder="请输入解决方案" v-model="msgInfo.solution"   @change="dataChange" ></textarea>
		</view>
		<view class="item item_line">
			<view class="itemname">抄送人</view>
			<view class="itemtext" @click="selectPeople()" >
				<input class="iteminput InputRightMargin" disabled placeholder="请选择抄送人" :value="formatCsrName(targetList)"/>
				<img src="../../static/img/personlistIcon.png" class="dateIcon" />
			</view>
		</view>
		<view class="fixMargin"></view>
		<view class="goHome" @click='goMain("msgInfo")'>
			<img src="../../static/img/gohome.png">
		</view>
		<view class="fixBottom">
			<view class="uni-flex uni-row">
				<view style="-webkit-flex:1;flex: 1;">
					<button type="primary"  style="width: 100%;"  @click="submit()">确认提交</button>
				</view>
			</view>
		</view>
		<view class="mysearchlist" style="z-index: 10000;">
			<chooseUserMulti ref="mysearchlist" 
				:showsearch.sync="show_search_user"
				:itemInfo_.sync="search_value_user">
			</chooseUserMulti>
		</view>
	</view>
</template>

<script>
	import chooseUserMulti from '@/components/xinsoft-search-list/chooseUserMulti.vue'
	export default {
		components: {
			chooseUserMulti
		},
		data() {
			return {
				msgInfo:{
					id:'',
					msg:'',
					solution:'',
				},
				csr:[],
				targetList: [],
				show_search_user:false,
				search_value_user:[],
			}
		},
		onLoad(option) {
			var _this = this;
			this.msgInfo.id = option.id||'';
			if(option.id){
				var msgInfo =  uni.getStorageSync('msgInfo');
				if(!msgInfo){
					msgInfo = {}
				}
				msgInfo.info = this.msgInfo;
				uni.setStorage({
					key: 'msgInfo',
					data: msgInfo
				});
			}
			else{
				uni.getStorage({
					key: 'msgInfo',
					success: function (res) {
						_this.msgInfo = res.data.info||{};
						_this.targetList = res.data.targetList||[];
					}
				});
			}
		},
		watch:{
			search_value_user(newv){
				var detail = [];
				newv.forEach(item=>{
					let obj = {userId: item.userId, name: item.NAME};
					detail.push(obj);
				})
				if(newv.length>0){
					var msgInfo =  uni.getStorageSync('msgInfo');
					if(!msgInfo){
						msgInfo = {}
					}
					this.targetList=detail
					msgInfo.targetList = this.targetList
					uni.setStorage({
					    key: 'msgInfo',
					    data: msgInfo,
					    success: function () {
					    }
					});
					}
				}
			},
		methods: {
			selectPeople(){
				this.show_search_user=true;
				// uni.navigateTo({
				//     url: "selectPeople?flag="+flag
				// });
			},
			formatCsrName(data){
				var returnName = '';
				for(var i=0;i<data.length;i++){
					if(i == 3){
						returnName += data[i].name+'等等';
						 break;
					}else{
						returnName += data[i].name+','
					}
				}
				returnName = returnName.substring(0,returnName.length-1);
				return returnName;
			},
			dataChange(){
				var msgInfo =  uni.getStorageSync('msgInfo');
				if(!msgInfo){
					msgInfo = {}
				}
				msgInfo.info = this.msgInfo;
				uni.setStorage({
					key: 'msgInfo',
					data: msgInfo,
					success: function () {
					}
				});
			},
			submit(){
				var _this = this;
				if(!(this.msgInfo.msg)){
					uni.showToast({
							title: '请填写原因分析',
							duration: 2000,
							icon:"none"
						});
					return false;
				}
				if(!(this.msgInfo.solution)){
					uni.showToast({
							title: '请填写解决方案',
							duration: 2000,
							icon:"none"
						});
					return false;
				}
				var carbonCopyRecipients =[]
				if(this.targetList.length>0){
					this.targetList.forEach((vals,index)=>{
						carbonCopyRecipients.push(vals.userId);
					});
				}
				//提交回复内容
				this.$http.request({
				  url: '/apis/taskMain/replySave',
				  params:{
					  taskid:this.msgInfo.id,
					  solution:this.msgInfo.solution,
					  reason:this.msgInfo.msg,
					  ccList:carbonCopyRecipients
				  }
				}).then(res=>{
					uni.removeStorageSync('msgInfo');
					if(res.data.code == 0){
						uni.showToast({
							title: '操作成功',
							duration: 	1000,
							icon:"none"
						});
						setTimeout( function(){
							uni.navigateTo({
								url: "taskDetail?id="+_this.msgInfo.id
							});
						},1000)
					}else{
						uni.showToast({
							title: res.data.msg,
							duration: 	2000,
							icon:"none"
						});
					}
				}).catch(err=>{
					uni.showToast({
						title: '操作失败',
						duration: 	2000,
						icon:"none"
					});
				});
			}
		}
	}
</script>

