<template>
	<view>
		<view class="view-bg">
			<text class="title-txt">工程用料统计</text>
			<view>
				<view class="uni-list-cell">
					<view class="uni-list-cell-left">
						工程周期
					</view>
					<view class="uni-list-cell-db">
						<view v-if="this.isEmpty(this.projectValue.dictValue)" style="margin-top: 10;"
							@click="pickerShow('gczq')">
							<span style="color: darkgray;">请选择工程周期</span>
						</view>
						<view v-else style="margin-top: 10;" @click="pickerShow('gczq')">
							<span style="color: black;">{{projectValue.dictLabel}}</span>
						</view>
					</view>

				</view>
				<view class="uni-list-cell">
					<view class="uni-list-cell-left">
						行政区
					</view>
					<view class="uni-list-cell-db">
						<view v-if="this.isEmpty(this.XZQValue.dictValue)" style="margin-top: 10;"
							@click="pickerShow('xzq')">
							<span style="color: darkgray;">请选择行政区</span>
						</view>
						<view v-else style="margin-top: 10;" @click="pickerShow('xzq')">
							<span style="color: black;">{{XZQValue.dictLabel}}</span>
						</view>
					</view>


				</view>
				<view class="uni-list-cell">
					<view class="uni-list-cell-left">
						小区
					</view>
					<view class="uni-list-cell-db">
						<view v-if="this.isEmpty(this.XQValue.id)" style="margin-top: 10;" @click="pickerShow('xq')">
							<span style="color: darkgray;">请选择小区</span>
						</view>
						<view v-else style="margin-top: 10;" @click="pickerShow('xq')">
							<span style="color: black;">{{XQValue.name}}</span>
						</view>
					</view>
				</view>
				<view class="uni-list-cell">
					<view class="uni-list-cell-left">
						材料
					</view>
					<view class="uni-list-cell-db">
						<view v-if="this.isEmpty(this.materialValue.name)" style="margin-top: 10;"
							@click="pickerShow('cz')">
							<span style="color: darkgray;">请选择材料</span>
						</view>
						<view v-else style="margin-top: 10;" @click="pickerShow('cz')">
							<span style="color: black;">{{materialValue.name}}</span>
						</view>
					</view>
				</view>
				<view class="uni-list-cell">
					<view class="uni-list-cell-left">
						规格
					</view>
					<view class="uni-list-cell-db">
						<view v-if="this.isEmpty(this.specificationValue.name)" style="margin-top: 10;"
							@click="pickerShow('gg')">
							<span style="color: darkgray;">请选择规格</span>
						</view>
						<view v-else style="margin-top: 10;" @click="pickerShow('gg')">
							<span style="color: black;">{{specificationValue.name}}</span>
						</view>
					</view>
				</view>

			</view>
			<uni-divider></uni-divider>
			<uni-table ref="table">
				<uni-tr>
					<uni-th width="20" align="left"></uni-th>
					<uni-th width="20" align="right">预计用量</uni-th>
					<uni-th width="20" align="right">实际用量</uni-th>
				</uni-tr>
				<uni-tr v-for="(item, index) in materialUsageList" :key="index">
					<uni-td>
						<view class="name">{{ item.materialQuality }}</view>
					</uni-td width="20" align="left">
					<uni-td width="20" align="right">{{ item.estimatedSize }}</uni-td>
					<uni-td width="20" align="right">{{ item.realitySize }}</uni-td>
				</uni-tr>
			</uni-table>
		</view>

		<view class="view-bg">
			<text class="title-txt">工程进度统计</text>
			<view>
				<view class="uni-list-cell">
					<view class="uni-list-cell-left">工程周期</view>
					<view class="uni-list-cell-db">
						<view v-if="this.isEmpty(this.projectValue2.dictValue)" style="margin-top: 10;"
							@click="pickerShow('gczq2')">
							<span style="color: darkgray;">请选择工程周期</span>
						</view>
						<view v-else style="margin-top: 10;" @click="pickerShow('gczq2')">
							<span style="color: black;">{{projectValue2.dictLabel}}</span>
						</view>
					</view>
				</view>
				<view class="uni-list-cell">
					<view class="uni-list-cell-left">
						行政区
					</view>
					<view class="uni-list-cell-db">
						<view v-if="this.isEmpty(this.XZQValue2.dictValue)" style="margin-top: 10;"
							@click="pickerShow('xzq2')">
							<span style="color: darkgray;">请选择行政区</span>
						</view>
						<view v-else style="margin-top: 10;" @click="pickerShow('xzq2')">
							<span style="color: black;">{{XZQValue2.dictLabel}}</span>
						</view>
					</view>


				</view>
				<view class="uni-list-cell">
					<view class="uni-list-cell-left"> 小区</view>
					<view class="uni-list-cell-db">
						<view v-if="this.isEmpty(this.XQValue2.id)" style="margin-top: 10;" @click="pickerShow('xq2')">
							<span style="color: darkgray;">请选择小区</span>
						</view>
						<view v-else style="margin-top: 10;" @click="pickerShow('xq2')">
							<span style="color: black;">{{XQValue2.name}}</span>
						</view>
					</view>

				</view>
				<view class="uni-list-cell">
					<view class="uni-list-cell-left">楼栋</view>
					<view class="uni-list-cell-db">
						<view v-if="this.isEmpty(this.LDValue2.id)" style="margin-top: 10;" @click="pickerShow('ld2')">
							<span style="color: darkgray;">请选择楼栋</span>
						</view>
						<view style="margin-top: 10;" @click="pickerShow('ld2')">
							<span style="color: black;">{{LDValue2.name}}</span>
						</view>
					</view>

				</view>
				<view class="uni-list-cell">
					<view class="uni-list-cell-left">单元</view>
					<view class="uni-list-cell-db">
						<view v-if="this.isEmpty(this.DYValue2.id)" style="margin-top: 10;" @click="pickerShow('dy2')">
							<span style="color: darkgray;">请选择单元</span>
						</view>
						<view v-else style="margin-top: 10;" @click="pickerShow('dy2')">
							<span style="color: black;">{{DYValue2.name}}</span>
						</view>
					</view>

				</view>

			</view>
			<uni-divider></uni-divider>
			<view class="view_bg_build" v-if="roomList.length > 0">
 
				<view v-for="(item,index) in roomList" :key="index">
					<uni-grid :border="false" :column="roomCount" :highlight="true" @change="change">
						<uni-grid-item v-for="(item2, index2) in roomList[index]" :index="index2" :key="index2">
							<view :style="{backgroundColor: getColorByStatus(item2.roomStatus)}">
								<text class="grid-text">{{item2.roomName}}</text>
							</view>
						</uni-grid-item>
					</uni-grid>
				</view>
			</view>
			<view  v-if="roomList.length == 0">暂无数据</view>
			<view style="align-items: center;"> 
				<view style="display: flex;align-items: center;">
					<image class="block-iv" src="../../static/images/ic_legend.png"></image>
					<text style="margin-right: 20px;margin-left: 5px;">图例: </text> 
					<div class="block-iv" style="background-color: #80d9ae;"></div>
					<text  style="margin-right: 20px;margin-left: 5px;">完工</text> 
					<div class="block-iv" style="background-color: #5bd3ff;"></div>
					<text  style="margin-right: 20px;margin-left: 5px;">未完工</text> 
					<div class="block-iv" style="background-color: #ffc881;"></div>
					<text  style="margin-right: 20px;margin-left: 5px;">未开工</text> 
				</view>

			</view>
		</view>

		<SelectPicker :list="selectList" @change="changeSelect" v-if="open" @close="close" titleKey="name"
			subtitleKey="id" v-model="name"></SelectPicker>

		<SelectPicker :list="dictOptions" @change="changeSelect" v-if="openDict" @close="closeDict" titleKey="dictLabel"
			subtitleKey="dictValue" v-model="dictLabel"></SelectPicker>
	</view>
</template>

<script>
	import SelectPicker from '../../components/selectPicker/select_picker.vue'
	import {
		getDicts
	} from "@/api/system/dict/data";
	import {
		getAreaList,
		getBuildingList,
		getUnitList,
		getHousesList,
		getEnginMaterialQualityList,
		getEnginSpecificationsList,
		getMunicipalStatisticsData,
		getObtainRoomcCompletionInformationList,
	} from '@/api/common'

	export default {
		components: {
			SelectPicker
		},
		data() {
			return {
				villageIndex: 0,
				specificationIndex: 0,
				materialIndex: 0,
				villageIndex2: 0,
				buildIndex: 0,
				unitIndex: 0,
				open: false,
				openDict: false,
				selectList: [],
				dictOptions: [],
				XQList: [],
				LDList: [],
				DYList: [],
				projectValue: {},
				XZQValue: {},
				XQValue: {},


				XQList2: [],
				LDList2: [],
				DYList2: [],

				projectValue2: {},
				XZQValue2: {},
				XQValue2: {},
				LDValue2: {},
				DYValue2: {},
				specificationValue: {},
				specificationList: [],
				materialValue: {},
				materialList: [],
				materialUsageList: [],
				roomList: [],
				roomCount: 4
			}
		},
		created() {},
		methods: {
			isEmpty(str) {
				return (!str || 0 === str.length);
			},
			pickerShow(e) {
				this.type = e; //赋值类型
				if (e == 'gczq') { //工程周期
					this.openDict = true;
					getDicts("engin_cycle").then(response => {
						this.dictOptions = response.data;
					});
				} else if (e == 'xzq') {
					if (this.isEmpty(this.projectValue.dictValue)) {
						this.$modal.msg('请选择工程周期')

					} else {
						this.openDict = true;
						getDicts("district").then(response => {
							this.dictOptions = response.data;
						});
					}
				} else if (e == 'xq') { //小区
					if (this.isEmpty(this.XZQValue.dictValue)) {
						this.$modal.msg('请选择行政区')

					} else {
						this.open = true;
						this.selectList = this.XQList;
					}
				} else if (e == 'ld') { //楼栋

					if (this.isEmpty(this.XQValue.id)) {
						this.$modal.msg('请选择小区')

					} else {
						this.open = true;
						this.selectList = this.LDList;
					}

				} else if (e == 'dy') { //单元
					if (this.isEmpty(this.LDValue.id)) {
						this.$modal.msg('请选择楼栋')

					} else {
						this.open = true;
						this.selectList = this.DYList;
					}
				} else if (e == 'gczq2') { //工程周期
					this.openDict = true;
					getDicts("engin_cycle").then(response => {
						this.dictOptions = response.data;
					});
				} else if (e == 'xzq2') { //行政区2
					if (this.isEmpty(this.projectValue2.dictValue)) {
						this.$modal.msg('请选择工程周期')

					} else {
						this.openDict = true;
						getDicts("district").then(response => {
							this.dictOptions = response.data;
						});
					}
				} else if (e == 'xq2') { //小区2
					if (this.isEmpty(this.XZQValue2.dictValue)) {
						this.$modal.msg('请选择行政区')

					} else {
						this.open = true;
						this.selectList = this.XQList2;
					}
				} else if (e == 'ld2') { //楼栋2

					if (this.isEmpty(this.XQValue2.id)) {
						this.$modal.msg('请选择小区')

					} else {
						this.open = true;
						this.selectList = this.LDList2;
					}

				} else if (e == 'dy2') { //单元2
					if (this.isEmpty(this.LDValue2.id)) {
						this.$modal.msg('请选择楼栋')

					} else {
						this.open = true;
						this.selectList = this.DYList2;
					}
				} else if (e == 'gg') { //规格
					if (this.isEmpty(this.materialValue.id)) {
						this.$modal.msg('请选择材料')

					} else {
						this.open = true;
						this.selectList = this.materialList;

					}
				} else if (e == 'cz') { //材料
					this.open = true;
					getEnginMaterialQualityList("民用工程").then(response => {
						this.selectList = response.data;
					});
				}
			},
			changeSelect(item, index) {
				// this.index = index;
				// this.address = item.communityName;
				this.open = false;
				this.openDict = false;
				if (this.type == 'xzq') {
					this.XZQValue = item;

					//根据行政区id获取小区数据
					this.XQValue = ''; //重置小区数据
					this.LDValue = ''; //重置楼栋数据
					this.DYValue = ''; //重置单元数据
					this.FJValue = ''; //重置房间数据
					getAreaList(item.dictValue).then(res => {
						this.XQList = res.data;
					})
				} else if (this.type == 'gczq') {
					this.projectValue = item

				} else if (this.type == 'xq') {
					this.XQValue = item;
					//根据小区id 获取楼栋数据
					this.LDValue = ''; //重置楼栋数据
					this.DYValue = ''; //重置单元数据
					this.FJValue = ''; //重置房间数据
					// getBuildingList(item.id).then(res => {
					// 	this.LDList = res.data
					// 	// console.log("楼栋=", res)
					// })
					this.queryMaterialsUsedList();
				} else if (this.type == 'ld') {
					this.LDValue = item;
					//根据楼栋ID 获取单元数据
					this.DYValue = ''; //重置单元数据
					this.FJValue = ''; //重置房间数据
					getUnitList(item.id).then(res => {
						this.DYList = res.data
						// console.log("单元=", res)
					})

				} else if (this.type == 'dy') {
					this.DYValue = item;
				} else if (this.type == 'gczq2') {
					this.projectValue2 = item

				} else if (this.type == 'xzq2') {
					this.XZQValue2 = item;

					//根据行政区id获取小区数据
					this.XQValue2 = ''; //重置小区数据
					this.LDValue2 = ''; //重置楼栋数据
					this.DYValue2 = ''; //重置单元数据
					this.FJValue2 = ''; //重置房间数据
					getAreaList(item.dictValue).then(res => {
						this.XQList2 = res.data;
					})
				} else if (this.type == 'xq2') { //小区2
					this.XQValue2 = item;
					//根据小区id 获取楼栋数据
					this.LDValue2 = ''; //重置楼栋数据
					this.DYValue2 = ''; //重置单元数据
					this.FJValue2 = ''; //重置房间数据
					getBuildingList(item.id).then(res => {
						this.LDList2 = res.data
						// console.log("楼栋=", res)
					})
				} else if (this.type == 'ld2') {
					this.LDValue2 = item;
					//根据楼栋ID 获取单元数据
					this.DYValue2 = ''; //重置单元数据
					this.FJValue2 = ''; //重置房间数据
					getUnitList(item.id).then(res => {
						this.DYList2 = res.data
						// console.log("单元=", res)
					})

				} else if (this.type == 'dy2') {
					this.DYValue2 = item;
					this.queryProjectStatisticsList();
				} else if (this.type == 'gg') { //规格
					this.specificationValue = item;
					this.queryMaterialsUsedList();
				} else if (this.type == 'cz') { //材料
					this.materialValue = item;
					this.specificationValue = ''; //重置规格
					//根据材质ID 获取规格数据
					getEnginSpecificationsList(item.id).then(res => {
						this.materialList = res.data
					})
				}
			},
			//关闭弹窗
			close(e) {
				this.open = false
			},
			//关闭弹窗
			closeDict(e) {
				this.openDict = false
			},
			queryMaterialsUsedList() {
				if (this.isEmpty(this.XQValue.id)) {
					this.$modal.msg('请选择小区')
					return;
				}
				if (this.isEmpty(this.specificationValue.id)) {
					this.$modal.msg('请选择规格')
					return;
				}
				let param = {
					enginCycle: this.projectValue.dictValue, //工程周期
					areaId: this.XQValue.id,
					realityQuality: this.materialValue.id,
					realitySpecifications: this.specificationValue.id,
				};
				getMunicipalStatisticsData(param).then(res => {
					this.materialUsageList = res.data;
				})
			},
			queryProjectStatisticsList() {
				let param = {
					enginCycle: this.projectValue.dictValue, //工程周期
					areaId: this.XQValue2.id,
					buildingId: this.LDValue2.id,
					unitId: this.DYValue2.id,
				};
				getObtainRoomcCompletionInformationList(param).then(res => {
					this.roomList = res.data[0].roomStatusVoList;
					this.roomCount = res.data[0].roomStatusVoList.length;
				})
			},
			getColorByStatus(status) {
				if (this.isEmpty(status)) {
					return "#fff";
				}
				if (status == '未完工') {
					return "#5bd3ff";
				} else if (status == '未开工') {
					return "#ffc881";
				} else if (status == '完工') {
					return "#80d9ae";
				}
			}
		}
	}
</script>

<style>
	.marginLeft5 {
		margin-left: 5px;
	}

	.title-txt {
		font-size: 15px;
		font-weight: bold;
		color: black;
	}

	.content-txt {
		font-size: 10px;
		font-weight: bold;
		color: black;
	}

	.view-bg {
		margin-left: 8px;
		margin-right: 8px;
		margin-top: 8px;
		padding: 10px;
		background-color: #fff;
		border-radius: 5px;
	}

	.uni-list
	/* {
		background-color: #FFFFFF;
		position: relative;
		width: 100%;
		display: flex;
		flex-direction: column;
	} */

	.uni-list-cell-db,
	.uni-list-cell {
		position: relative;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}

	.uni-list-cell-left {
		white-space: nowrap;
		font-size: 28rpx;
	}

	.centered {
		text-align: center;
		background-color: #e2f4ff;

	}

	.text {
		background-color: #e2f4ff;
		height: 20rpx;
	}

	.view_bg_build {
		background-color: #e2f4ff;
		padding: 10rpx;
		margin: 20rpx;
	}

	.grid-text {
		font-size: 14px;
		color: #000000;
		text-align: center;
		padding-left: 30rpx;
		padding-right: 30rpx;
		padding-top: 20rpx;
		padding-bottom: 20rpx;
	}

	.name {
		flex: 0;
		font-size: 14px;
		color: #000000;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.table-item {
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}
	.block-iv{
		width: 10px;
		height: 10px;
	}
</style>