<template>
	<view class="index-container" :style="{ height: `${windowHeight}px` }" @touchmove.stop.prevent="() => {}">
		<z-paging ref="paging" use-virtual-list @query="getListData" bg-color="#F3F3F3" :loading-more-enabled="false">
			<template #top>
				<custom-nav-bar :left="true" title="地块管理">
					<template v-slot:right>
						<view class="fixed">
							<view align="right" class="iconfont icon-jia icon" v-hasPermi="['system:land:add']"
								@click="onAdd">
							</view>
						</view>
					</template>
				</custom-nav-bar>
				<!--搜索框-->
				<view class="search">
					<u-search placeholder="请输入地块名称/编号" @search="onSearchValue" @custom="onSearchValue"
						@clear="onSearchValue(null)" shape="square"></u-search>
					<uni-forms ref="form" :model="form" labelWidth="0px" >
						<uni-forms-item label="" name="">
							<cxk-dropdown-tree :dataOptions="treeList" labelField="label" valueField="id"
								:defaultCheckedKeys="defaultCheckedKeys" @change="handleTreeChange"
								style="width: 78%;float: left;margin-right: 2px;"></cxk-dropdown-tree>
							<u-checkbox-group style="width: 20%;float: right;" icon-placement="left"
								@change="handleChange">
								<u-checkbox active-color="#35842E" label="查全部" name="all"></u-checkbox>
							</u-checkbox-group>
						</uni-forms-item>

					</uni-forms>
				</view>
				
				<view class="tip-intel">
					<view v-show="list.length > 0">地块数量：({{ list.length }}块)</view>
				</view>
			</template>
			<template #cell="{ item }">
				<view class="card">
					<uni-collapse>
						<uni-collapse-item title-border="none" :border="false" :show-arrow="false" :open="true">
							<template v-slot:title>
								<view class="cards-title">
									<view>
										<view class="iconfont icon-nongtian icon"></view>
										<view class="text">
											<view>地块编号：{{item.landNum}}</view>
										</view>
										<view>
											<uni-tag class="bg-green" text="启用" :circle="true"
												v-if="item.status == 0"></uni-tag>
											<uni-tag class="bg-red" text="禁用" :circle="true"
												v-if="item.status == 1"></uni-tag>
										</view>
									</view>
									<view class="title-r">
										<u-button type="success" size="mini" text="编辑" v-hasPermi="['system:land:edit']" @click="edit(item)"></u-button>
									</view>
								</view>
							</template>
							<view class="content">
								<view class="cards-content">
									<view class="txt-list" >
										<view class="list1">
											<view><text>地块名称：</text>{{item.landName}}</view>
											<view><text>地块面积：</text>{{item.landName}}</view>
											<view><text>负责人：</text><text
													v-if="item.user && item.user.userName">{{item.user.userName}}</text>
											</view>
											<view><text>所属公司：</text><text
													v-if="item.companyGroup && item.companyGroup.companyFullName">{{item.companyGroup.companyFullName}}</text>
											</view>
										</view>
										<view class="right">
											<view class="iconfont icon-you">
											</view>
										</view>
									</view>
								</view>
							</view>
						</uni-collapse-item>
					</uni-collapse>
				</view>
			</template>
		</z-paging>
	</view>
</template>
<script>
	import {
		checkPermi
	} from "@/utils/permission"
	import store from "@/store"
	import plugin from "@/plugins"
	export default {
		data() {
			return {
				isFirst: true,
				user: store.state.user.user,
				windowHeight: null,
				defaultCheckedKeys: "",
				paging: null,
				form: null,
				list: [],
				all: false,
				treeList: [],
				companyGroupId: null,
				query: {
					companyGroupIds: "",
					landName: null
				},
				selectCompanyObj: {}
			}
		},
		created() {
			this.windowHeight = uni.getSystemInfoSync().windowHeight;
		},
		onLoad() {
			let companyId = null;
			if (getApp().ijs.land) {
				companyId = getApp().ijs.land.companyGroup.companyId
			}
			this.getCompanyTree(companyId);
		},
		onShow() {
			if (this.paging != null) {
				this.$refs.paging.reload()
			}
			this.getListData();
		},
		methods: {
			/** 查询公司下拉树结构 */
			getCompanyTree(companyId) {
				const obj = {
					status: 0,
					delFlag: "0",
					companyTypes: [1, 2, 3],
				}
				store
					.dispatch("companyTreeSelect", obj)
					.then(res => {
						this.treeList = res.data;
						if (this.treeList && this.treeList.length) {
							if (companyId) {
								this.defaultCheckedKeys = companyId + "";
							} else {
								let companyList = this.treeList[0].children.filter(x => x.id != 100);
								if (companyList && companyList.length) {
									this.defaultCheckedKeys = companyList[0].id + "";
								}
							}
						}
					})
					.catch(error => {
						plugin.modal.msgError(error)
					})
					.finally(() => {
						plugin.modal.closeLoading()
					})
			},
			getListData() {
				if (!checkPermi(["system:land:list"])) {
					plugin.modal.msgError("抱歉暂无权限")
					return
				}
				if (!this.query.companyGroupIds || this.query.companyGroupIds == "" || this.query.companyGroupIds ==
					"101") {
					return;
				}
				plugin.modal.loading("获取地块列表...")
				if (this.query.landName == null || this.query.landName == "") {
					delete this.query["landName"]
				}
				store
					.dispatch("getLandList", this.query)
					.then(res => {
						this.list = res.data;
						this.$refs.paging.complete(this.list)
					})
					.catch(error => {
						plugin.modal.msgError(error)
					})
					.finally(() => {
						plugin.modal.closeLoading()
					})
			},
			onAdd() {
				if (!this.selectCompanyObj) {
					plugin.modal.msgError("请选择要添加地块的公司")
					return;
				}
				if (checkPermi(["system:land:add"])) {
					uni.navigateTo({
						url: "/pages/work/land/create-new?companyObj=" + encodeURIComponent(JSON.stringify(this
							.selectCompanyObj))
					})
				} else {
					plugin.modal.msgError("抱歉暂无权限")
				}
			},
			onItemClick(item) {
				uni.navigateTo({
					url: "/pages/work/land/index-detail?item=" + encodeURIComponent(JSON.stringify(item))
				})
			},
			onSearchValue(e) {
				this.query.landName = e;
				this.getListData();
			},
			handleTreeChange(values, currentItem) {
				this.query.companyGroupIds = "";
				this.companyGroupId = values;
				this.isFirst = false;
				this.selectCompanyObj = currentItem;
				if (this.all) {
					this.getCompanyGroupIds();
				}
				this.query.companyGroupIds += values;
				this.getListData();
			},
			getCompanyGroupIds() {
				if (this.selectCompanyObj && this.selectCompanyObj.childrenKeys) {
					this.selectCompanyObj.childrenKeys.forEach(property => {
						if (property) {
							this.query.companyGroupIds += property + ",";
						}
					});
				}
			},
			edit(item) {
				if (checkPermi(["system:land:edit"])) {
					uni.navigateTo({
						url: "/pages/work/land/create-new?item=" + encodeURIComponent(JSON.stringify(item))
					})
				} else {
					plugin.modal.msgError("抱歉暂无权限")
				}
			},
			handleChange(e) {
				this.all = e && e.length ? true : false;
				this.query.companyGroupIds = "";
				if (this.all) {
					this.getCompanyGroupIds();
				}
				if (this.companyGroupId) {
					this.query.companyGroupIds += this.companyGroupId;
				}
				this.getListData();
			},
			// 缓存数据
			setStorage(type, data) {
				uni.setStorageSync(type, {
					userId: this.user.userId,
					data: data,
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.cards-title .icon {
		margin-right: 0;
	}

	.cards-title uni-view .text {
		color: #333;
	}

	// 表单
	::v-deep.uni-forms-item__label {
	display: none !important;
	}
	.u-search {
		padding: 10px 10px 0 10px;
	}
	.u-checkbox {
		margin: 0 !important;
		height: 35px;
	}
	::v-deep.u-search__action--active {
		width: 20% !important;
		text-align: left !important;
	}
	.uni-forms-item.is-direction-left {
		margin: 0 !important;
	}
</style>