<template>
	<div class="hui-flex hui-dialog select-project-index">
		<div class="hui-flex-box hui-dialog-content">
			<el-table :data="tableData" row-key="id" border height="100%">
				<el-table-column label="序号" width="50">
					<template slot-scope="scope">
						<div style="text-align: center;">{{scope.$index + 1}}</div>
					</template>
				</el-table-column>
				<el-table-column label="项目名称" prop="projectName"></el-table-column>
				<el-table-column label="所属企业" prop="organizationName"></el-table-column>
				<el-table-column label="项目身份">
					<template slot-scope="scope">
						<div class="hui-table-tag" style="display: flex;align-items: center;">
							<div class="hui-tag hui-tag-success" v-if="scope.row.identityId === 1">客户</div>
							<div class="hui-tag hui-tag-success" v-else-if="scope.row.identityId === 2">中介</div>
							<div class="hui-tag hui-tag-success" v-else-if="scope.row.identityId === 3">
								公司成员
							</div>
							<div class="hui-tag hui-tag-success" v-else-if="scope.row.identityId === 4">
								关联公司
							</div>
							<div class="hui-tag hui-tag-success" v-else-if="scope.row.identityId === 5">
								经纪公司
							</div>
							<div class="hui-tag hui-tag-success" v-else-if="scope.row.identityId === 6">
								所有者
							</div>
						</div>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="100">
					<template slot-scope="scope">
						<div class="hui-table-operation">
							<span class="table-operation"
								v-if="$store.getters.project.id != scope.row.projectId || $store.getters.identityId != scope.row.identityId"
								@click="changeProject(scope.row)">
								切换项目
							</span>
						</div>
					</template>
				</el-table-column>
				<template slot="empty">
					<empty description="暂无数据"></empty>
				</template>
			</el-table>
		</div>
	</div>
</template>

<script>
	import {
		selectProject,
		getOrganizedProjectList,
		selectOrangaized
	} from '@/httpApi/loginRegister'
	import {
		bindProjectDetail,
		getOrganizationDetailById
	} from '@/httpApi/organization'
	import {
		getProjectDetailById,
		getProjectListByPage
	} from '@/httpApi/space'
	import {
		getIdentityResource
	} from '@/httpApi/system'
	import {
		setComment
	} from '@/uitls/auth';
	export default {
		data() {
			return {
				tableData: []
			}
		},
		created() {
			let user = this.$store.getters.user;
			if (user.userId === 1) {
				getProjectListByPage({
					currPage: 1,
					pageSize: 100
				}).then(res => {
					if (res.state) {
						this.tableData = res.data.dataList.map(node => {
							let organization = user.organizationList.find(item => item.id === node
								.organizationId);
							return {
								projectId: node.id,
								projectName: node.name,
								organizationId: node.organizationId,
								organizationName: !organization ? '' : organization.name,
								identityId: 6
							}
						})
					}
				})
			} else {
				this.init();
			}
		},
		methods: {
			init() {
				bindProjectDetail({
					userId: this.$store.getters.user.userId
				}).then(res => {
					if (res.state) {
						let clientData = res.data;
						bindProjectDetail({
							bindOrganizationId: this.$store.getters.organization.id
						}).then(res => {
							if (res.state) {
								let organizationData = res.data;
								if (this.$store.getters.user.phone == this.$store.getters.organization
									.contactTel) {
									getOrganizedProjectList(this.$store.getters.organization.id).then(
										res => {
											if (res.state) {
												let data = res.data || [];
												let obj = data.map(node => {
													return {
														projectId: node.id,
														projectName: node.name,
														organizationId: node.organizationId,
														organizationName: node
															.organizationName,
														identityId: 6
													}
												})
												let list = clientData.concat(organizationData,
													obj).sort((a, b) => a.projectId - b.projectId);
												this.tableData = list.filter((item, index) => {
													return list.findIndex(t => t.projectId ===
														item.projectId && t.identityId ===
														item.identityId) === index;
												})
											}
										});
								} else {
									this.tableData = clientData.concat(organizationData).sort((a, b) => a
										.projectId - b.projectId);
								}
							}
						})
					}
				})
			},
			changeProject(item) {
				if (item.identityId == 6 || item.identityId == 3) {
					getOrganizationDetailById(item.organizationId).then(res => {
						if (res.state) {
							this.$store.dispatch('app/changeOrganization', res.data);
							selectOrangaized(res.data)
						}
					})
				}
				getProjectDetailById(item.projectId).then(res => {
					if (res.state) {
						let data = res.data;
						selectProject(data.id).then(res => {
							if (res.state) {
								let user = res.data;
								this.$store.dispatch('app/changeiIdentityId', item.identityId);
								this.$store.dispatch('projectBase/changeProject', data);
								localStorage.setItem('projectId', data.id);
								this.$store.dispatch('app/changeUser', user);
								if (item.identityId === 3 || user.userId === 1) {
									this.$store.dispatch('app/changeMenuData', user.resource ? JSON.parse(
										user.resource) : []);
									setComment(user.menu ? user.menu : JSON.stringify([]));
									this.$emit('callback');
									this.$router.push({
										path: '/',
										replace: true
									})
									this.$message.success('切换成功');
								} else {
									getIdentityResource({
										identityId: item.identityId,
										type: data.type
									}).then(node => {
										if (node.state) {
											let role = node.data[0] || {};
											this.$store.dispatch('app/changeMenuData', role
												.resource ? JSON.parse(role.resource) : []);
											setComment(role.menus ? role.menus : JSON.stringify(
												[]));
											this.$emit('callback');
											this.$router.push({
												path: '/',
												replace: true
											})
											this.$message.success('切换成功');
										} else {
											this.loginLoading = false;
										}
									})
								}

							} else {
								this.$message.error('切换失败');
							}
						});
					}
				})
			},
			submit() {

			}
		}
	}
</script>

<style lang="scss">
	.select-project-index {
		.select-project-item {
			flex: 1;
			height: 0;
			display: flex;
			flex-direction: column;

			.select-project-title {
				padding: 10px 15px;
				position: relative;

				&::before {
					content: '';
					width: 4px;
					height: 18px;
					left: 4px;
					top: 11px;
					background: $--color-primary;
					position: absolute;
				}
			}
		}
	}
</style>