<template>
	<view class="project-select">
		<view class="common-list">
			<view class="common-item" v-for="(item,index) in list" :key="item.id" @click="selectProject(item)">
				<view class="title">{{item.name}}</view>
				<view class="date">{{item.createDate}}</view>
				<view class="space">{{JSON.parse(item.address).join('-')}}</view>
				<view class="state">
					<view class="create">
						<uni-icons class="inherit-icons" type="person-filled" color="#08979c" size="18"></uni-icons>
						<text class="name hui-ellipsis">{{item.organizationName || '-'}}</text>
					</view>
					<view class="tag">
						<view class="status-tag primary" v-for="(node,index) in item.projectListIdentity"
							:key="item.id">
							{{node.name}}
						</view>
					</view>
				</view>
				<view class="icon" v-if="project.id === item.id">
					<uni-icons type="checkmarkempty" color="#08979c" size="30"></uni-icons>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getProjectList,
		selectProject
	} from '@/request/api/project'
	export default {
		data() {
			return {
				list: [],
				project: {}
			}
		},
		onShow() {
			this.project = this.$store.getters.project;
		},
		onLoad() {
			this.init();
		},
		methods: {
			init() {
				getProjectList().then(res => {
					if (res.code == 200) {
						this.list = res.data;
					}
				})
			},
			selectProject(item) {
				uni.showActionSheet({
					itemList: item.projectListIdentity.map(node => node.name),
					success: res => {
						uni.showLoading()
						selectProject(item.id).then(res => {
							if (res.code === 200) {
								let node = item.projectListIdentity[res.tapIndex];
								this.$store.dispatch('app/changeProject', item);
								this.$store.dispatch('app/changeIdentity', node);
								uni.setStorageSync('vuex_state', this.$store.state);
								this.$toast('切换成功');
								setTimeout(() => {
									uni.hideLoading();
									uni.navigateBack();
								}, 400)
							} else {
								uni.hideLoading();
							}
						})

					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.project-select {
		padding: 30rpx 0;
	}
</style>