<template>
	<view class="message-detail-index">
		<view class="message-detail-content">
			<text v-if="detail.id">{{detail.sendUserName}}{{detail.message}}</text>
		</view>
		<view class="message-btn-box" v-if="dataDetail.status === 1">
			<view class="message-btn red" @click="btnClick('拒绝',3)">拒绝</view>
			<view class="line"></view>
			<view class="message-btn primary" @click="btnClick('同意',2)">同意</view>
		</view>
	</view>
</template>

<script>
	import {
		getMessageDetailById,
		bindProject,
		getCustomerDetailById,
		updateCustomer,
		getAgentDetailById,
		updateAgent
	} from '@/request/api/message.js'
	export default {
		data() {
			return {
				messageId: '',
				detail: {},
				dataDetail: {}
			}
		},
		onLoad(body) {
			this.messageId = body.id;
			this.init();
		},
		methods: {
			init() {
				getMessageDetailById(this.messageId).then(res => {
					if (res.code == 200) {
						this.detail = res.data;
						this.detail = Object.assign(this.detail, JSON.parse(this.detail.json));
						this.initDetail();
					}
				})
			},
			initDetail() {
				if (this.detail.identityId === 1) { //客户
					getCustomerDetailById(this.detail.dataId).then(res => {
						if (res.code == 200) {
							this.dataDetail = res.data;
						}
					})
				} else if (this.detail.identityId === 2) { //经纪人
					getAgentDetailById(this.detail.dataId).then(res => {
						if (res.code == 200) {
							this.dataDetail = res.data;
						}
					})
				}
			},
			btnClick(msg, type) {
				uni.showModal({
					title: '有极提示',
					content: '是否' + msg + '加入该项目',
					success: (res) => {
						if (res.confirm) {
							if (this.detail.identityId === 1) { //客户
								updateCustomer({
									id: this.dataDetail.id,
									status: type
								}).then(res => {
									if (res.code == 200) {
										if (type === 2) return this.operationProject();
										this.initDetail();
										this.$toast('操作成功');
									}
								})
							} else if (this.detail.identityId === 2) { //经纪人
								updateAgent({
									id: this.dataDetail.id,
									status: type
								}).then(res => {
									if (res.code == 200) {
										if (type === 2) return this.operationProject();
										this.initDetail();
										this.$toast('操作成功');
									}
								})
							}
						}
					}
				});
			},
			operationProject() {
				let postData = {
					organizationId: this.detail.organizationId,
					projectId: this.detail.projectId,
					userId: this.$store.getters.user.userId,
					identityId: this.detail.identityId
				}
				if (this.detail.identityId === 1) { //客户
					postData['clientId'] = this.detail.dataId
				} else if (this.detail.identityId === 2) { //经纪人
					postData['agentId'] = this.detail.dataId
				}
				bindProject(postData).then(res => {
					if (res.code === 200) {
						this.$toast('操作成功');
						this.initDetail();
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.message-detail-index {
		background: $uni-white;
		padding: 20rpx;
		padding-bottom: 0;
		margin: 20rpx 30rpx;
		border-radius: 10rpx;

		.message-detail-content {
			min-height: 200rpx;
			padding-bottom: 40rpx;
		}

		.message-btn-box {
			width: 100%;
			display: flex;
			height: 80rpx;
			align-items: center;
			border-top: 1px solid $uni-border-3;

			.line {
				width: 1px;
				height: 40rpx;
				background: $uni-border-3;
			}

			.message-btn {
				text-align: center;
				flex: 1;
			}

			.red {
				color: $uni-error;
			}

			.primary {
				color: $uni-primary;
			}
		}

	}
</style>