<template>
	<view class="content-box">

		<uni-nav-bar fixed="true" statusBar="true" left-icon="back" title="搜索" @clickLeft="back">
			<!--右菜单-->
			<template slot="right">
				<u-button type="success" size="small" class="u-reset-button" @click="openTreePicker"
					text="选择分类"></u-button>
			</template>
		</uni-nav-bar>


		<view class="top-wrapper">
			<view class="searchBox rx-bc">
				<input v-model="keyWord" placeholder="请输入关键字搜索" class="searchInput" />

				<view>
					<u-button @click="getList" type="success" size="small" class="u-reset-button" text="搜索">
					</u-button>
				</view>

			</view>
		</view>

		<view class="list_box">
			<u-list>


				<checkbox-group v-for="(item, index) in list" :key="index" @change="e => selectVal(e, item, index)">
					<label class="listBox rx-bs">

						<view class="listBox-sel">
							<checkbox :value="item.code"
								:class="item.rootCategoryLevelId == 1 &&  !item.checked ? 'checkedClass': ''"
								color="#fff" :disabled="item.disabled" :checked="item.checked" />
						</view>

						<view class="listBox-con">
							<view class="listBox-top rx-bc">
								<view> {{ item.name }}</view>
								<view class="code">{{ item.code}}</view>
							</view>

							<view class="listBox-bottom rx">
								<view v-for="(itm, index) in tableH(item.rootCategoryLevelId)" :key="index"
									class="items">
									{{ itm.label }}：{{ item[itm.prop] }}
								</view>

								<view class="items">
									可用库存：{{ item.availableCountBase }} {{item.unit}}
								</view>

							</view>

						</view>


					</label>

				</checkbox-group>



				<view v-if='list.length == 0' style='margin-top: 20vh;'>
					<u-empty iconSize='150' textSize='32' text='暂无数据'>
					</u-empty>
				</view>

			</u-list>



		</view>


		<view class="bottom-wrapper rx-bc">
			<view>
				<checkbox v-if="!seletedAll" color="#fff" :checked="seletedAll" @tap="_seletedAll">全选</checkbox>
				<checkbox class="select-all" color="#fff" v-else :checked="seletedAll" @tap="_seletedAll">取消全选
				</checkbox>
			</view>
			<view>
				<u-button type="success" size="small" class="u-reset-button" :disabled="!checkListLen" @click="jumpAdd">
					<view> 选择( {{ checkListLen }} ) </view>
				</u-button>
			</view>
		</view>

		<ba-tree-picker ref="treePicker" key="verify" :multiple="false" @select-change="confirm" title="选择分类"
			:localdata="classificationList" valueKey="id" textKey="name" childrenKey="children" />

		<matterPop v-if='matterShow' :matterId='matterId' :DTOList='DTOList' @close='close' @mattSave='mattSave'>
		</matterPop>

	</view>
</template>

<script>
	import baTreePicker from '@/components/ba-tree-picker/ba-tree-picker.vue'
	import {
		tableHeader
	} from '../../common.js'
	import {
		treeByPid,
		pageeLedgerMain,
		assetPage
	} from '@/api/pda/workOrder.js'
	import matterPop from './matterPop.vue'
	export default {
		components: {
			baTreePicker,
			matterPop
		},
		data() {
			return {

				keyWord: null,
				categoryLevelId: null,
				classificationList: [],
				treePickerShow: false,
				list: [],


				seletedAll: false, //全选状态
				memoList: [],

				isType: null,

				pid: null, // 上个页面id
				storageKey: null,

				matterShow: false, //物料 左弹框
				matterId: null,
				DTOList: []
			}
		},

		//选择的列表长度
		computed: {
			checkListLen() {
				return this.memoList.length
			}

		},

		onLoad(option) {
			this.pid = option.id
			this.isType = option.isType
			if (option.storageKey) {
				this.storageKey = option.storageKey
				this.memoList = (this.storageKey && uni.getStorageSync(this.storageKey)) || []
			}

			this.getTreeList()
		},

		onUnload() {
			if (this.storageKey) {
				uni.removeStorage(this.storageKey)
			}

		},



		methods: {
			_seletedAll() {

				if (!this.seletedAll) {
					this.seletedAll = true

					this.list.map(item => {
						this.$set(item, 'checked', true)
						const idx = this.memoList.findIndex(itm => itm.id === item.id)
						if (idx === -1) {
							this.memoList.push(item)
						}
					})



				} else {
					this.seletedAll = false
					this.list.map(item => {
						this.$set(item, 'checked', false)

						const idx = this.memoList.findIndex(itm => itm.id === item.id)
						if (idx > -1) {
							this.memoList.splice(idx, 1)
						}

					})
				}
			},

			openTreePicker() {
				this.$refs.treePicker._show()

			},
			tableH(type) {
				return tableHeader(type)
			},

			getTreeList() {
				let params = {
					ids: [1, 5, 7, 8, 10, 14]

				}
				treeByPid(params).then(res => {
					this.classificationList = res
					this.confirm([res[0].id], res[0].name)
				})
			},

			confirm(id, name) {
				this.categoryLevelId = id
				this.getList()
			},



			getList() {
				let param = {
					categoryLevelId: this.categoryLevelId,
					keyWord: this.keyWord,
					pageNum: 1,
					size: -1,

				}
				this.list = []
				if (this.isType == 'pick') {
					param.dimension = 1
					pageeLedgerMain(param).then(res => {
						this.list.push(
							...res.list.map(i => {
								const checked =
									this.memoList.findIndex(itm => itm.id === i.id) > -1

								const warehouseId = i.pathIds && i.pathIds.split(',')[0]

								return {
									checked,
									warehouseId,
									...i,
									instanceId: i.id,
									categoryId: i.assetId,

								}
							})
						)
					})

				} else if (this.isType == 'feed') {
					assetPage(param).then(res => {
						this.list.push(
							...res.list.map(i => {
								const checked =
									this.memoList.findIndex(itm => itm.id === i.id) > -1

								const warehouseId = i.pathIds && i.pathIds.split(',')[0]

								if (i.rootCategoryLevelId == 1) {
									i.mattList = []
								}

								return {
									checked,
									warehouseId,
									...i,
									instanceId: i.id,
									categoryId: i.assetId,

								}
							})
						)
					})

				}

			},

			//勾选
			selectVal(e, val, index) {
				if (this.isType == 'pick') {
					this.list[index].checked = !this.list[index].checked

					this.seletedAll = !this.list.some(item => !item.checked)

					const idx = this.memoList.findIndex(
						item => item.id === this.list[index].id
					)

					if (this.list[index].checked) {
						if (idx === -1) {
							this.memoList.push(this.list[index])
						}
					} else {
						if (idx > -1) {
							this.memoList.splice(idx, 1)
						}
					}
				} else if (this.isType == 'feed') {
					this.$set(this.list[index], 'checked', false)


					this.DTOList = val.inventoryDetailsDTOList
					this.matterId = val.id
					this.matterShow = true
				}




			},

			close() {
				this.matterShow = false
			},

			mattSave(maList, id) {

				this.list.forEach(e => {
					if (e.id == id) {
						this.$set(e, 'checked', maList.length == 0 ? false : true)
						e.mattList = maList
						this.matterShow = false
					}
				})

			},

			//跳转回添加页面
			jumpAdd() {
				uni.$emit('setSelectList', this.memoList, this.pid)
				uni.navigateBack()
			},
		}
	}
</script>

<style lang="scss" scoped>
	.content-box {
		height: 100vh;
		overflow: hidden;
		display: flex;
		flex-direction: column;
		background-color: $page-bg;
	}

	.searchBox {
		background-color: #dedede;
		height: 90rpx;
		padding: 0 20rpx;

		input {
			height: 70rpx;
			width: 540rpx;
			background: #f9f9f9 !important;
			padding-left: 10rpx;
			border-radius: 5rpx;
		}

	}


	.list_box {
		flex: 1;
		overflow: hidden;
		padding: 6rpx 0;

		.u-list {
			height: 100% !important;
		}
	}

	.bottom-wrapper {
		height: 80rpx;
		background: #fff;
		padding: 0 32rpx;

		/deep/ .uni-checkbox-input-checked {
			background-color: $theme-color !important;
			border-color: $theme-color !important;
		}
	}

	.listBox {
		margin-top: 8rpx;
		padding: 8rpx 24rpx;
		background: #fff;


		/deep/ .uni-checkbox-input-checked {
			background-color: $theme-color !important;
			border-color: $theme-color !important;
		}

		.listBox-con {
			width: 650rpx;
			font-weight: 400;
		}

		.listBox-top {
			margin-top: 6rpx;
			color: #090A0A;
			font-size: 28rpx;
			font-style: normal;
		}

		.listBox-bottom {
			color: #090A0A;
			font-size: 24rpx;
			font-style: normal;
			flex-wrap: wrap;

			.items {
				width: 50%;
				margin-top: 8rpx;
			}
		}
	}

	.checkedClass {
		/deep/ .uni-checkbox-input-checked {
			background-color: #fff !important;
			border: 1px solid #d1d1d1 !important;
		}
	}
</style>