<template>
	<view>
		<mescroll-body top="30" bottom="40" @init="mescrollInit" @down="downCallback" @up="upCallback" :option="{}">
			<view class="common-list">
				<view class="common-item" v-for="(item,index) in list" :key="item.id"
					@click="$navigateTo('/pages/contractDetail/contractDetail?contractId='+item.id)">
					<view class="title">{{item.code}}</view>
					<view class="date">{{item.startDate}}至{{item.endDate}}</view>
					<view>
						<view class="space" v-for="(node,index) in item.roomMap" :key="index">{{node}}</view>
					</view>
					<view class="other">
						<view class="item">
							<view class="label">出租方</view>
							<view class="value">{{item.organizationName}}</view>
						</view>
						<view class="item">
							<view class="label">租赁方</view>
							<view class="value">{{item.tenantType === 1 ? item.merchantName: item.clientName}}</view>
						</view>
					</view>
					<view class="state">
						<view class="create">
							<uni-icons class="inherit-icons" type="staff-filled" color="#08979c" size="18"></uni-icons>
							<text class="name hui-ellipsis">{{item.operatorName || '-'}}</text>
						</view>
						<view class="tag">
							<view class="status-tag info" v-if="!item.status">待发送</view>
							<view class="status-tag warning" v-else-if="item.status === 1">待确认</view>
							<view class="status-tag success" v-else-if="item.status === 2">已生效</view>
						</view>
					</view>
					<view class="icon">
						<uni-icons custom-prefix="iconfont" :type="menu.iconClass" color="#08979c" size="30">
						</uni-icons>
					</view>
				</view>
			</view>
		</mescroll-body>
	</view>
</template>

<script>
	import {
		getContractListByPage
	} from '@/request/api/contract.js'
	import {
		bindProjectDetail
	} from '@/request/api/organization.js'

	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin], // 使用mixin
		data() {
			return {
				list: [],
				type: '',
				menu: {},
				clientId: '',
				merchantId: ''
			}
		},
		onLoad(body) {
			this.type = parseInt(body.type);
			this.menu = this.title();
			uni.setNavigationBarTitle({
				title: this.menu.title
			});
			uni.$on('reloadContract', () => {
				this.mescroll.resetUpScroll(false);
			})
		},
		methods: {
			title() {
				let str = {
					title: '',
					iconClass: 'icon-hetongguanli'
				};
				switch (this.type) {
					case 1:
						str = {
							title: '合同列表',
							iconClass: 'icon-hetongguanli'
						};
						break;
					case 2:
						str = {
							title: '公司合同',
							iconClass: 'icon-anli'
						};
						break;
					case 3:
						str = {
							title: '个人合同',
							iconClass: 'icon-gerenhetongchaxun'
						};
						break;
					default:
						break;
				}
				return str;
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				let postData = {
					currPage: page.num,
					pageSize: 10,
					projectId: this.$store.getters.project.id
				}
				if (this.type === 1) {
					postData['organizationId'] = this.$store.getters.organization.id;
					this.getContractList(postData);
				} else if (this.type === 2) {
					postData['statusList'] = [1, 2, 3, 4, 5];
					if (this.merchantId) {
						postData['merchantId'] = this.merchantId;
						this.getContractList(postData);
						return;
					}
					bindProjectDetail({
						bindOrganizationId: this.$store.getters.organization.id,
						projectId: this.$store.getters.project.id,
						identityId: 4
					}).then(res => {
						if (res.code === 200) {
							if (res.data.length == 0) return this.mescroll.endBySize(0, 0);
							this.merchantId = res.data[0].merchantId;
							postData['merchantId'] = this.merchantId;
							this.getContractList(postData);
						} else {
							this.mescroll.endErr();
						}
					}).catch(() => {
						//联网失败, 结束加载
						this.mescroll.endErr();
					})
				} else if (this.type === 3) {
					postData['statusList'] = [1, 2, 3, 4, 5];
					if (this.clientId) {
						postData['clientId'] = this.clientId;
						this.getContractList(postData);
						return;
					}
					bindProjectDetail({
						userId: this.$store.getters.user.userId,
						projectId: this.$store.getters.project.id,
						identityId: 1
					}).then(res => {
						if (res.code === 200) {
							if (res.data.length == 0) return this.mescroll.endBySize(0, 0);
							this.clientId = res.data[0].clientId;
							postData['clientId'] = this.clientId;
							this.getContractList(postData);
						} else {
							this.mescroll.endErr();
						}
					}).catch(() => {
						//联网失败, 结束加载
						this.mescroll.endErr();
					})
				}
			},
			getContractList(postData) {
				getContractListByPage(postData).then(res => {
					if (res.code === 200) {
						this.mescroll.endBySize(res.data.dataList.length, res.data.totalCount);
						if (postData.currPage == 1) this.list = []; //如果是第一页需手动制空列表
						let data = res.data.dataList;
						this.list = this.list.concat(data); //追加新数据
					} else {
						this.mescroll.endErr();
					}
				}).catch(() => {
					//联网失败, 结束加载
					this.mescroll.endErr();
				})
			}
		}
	}
</script>

<style>

</style>