<template>
	<view>
		<view class="list" @click="$url('/pages/classification/warehouse')">
			<view class="lf-font-28 lf-color-black lf-row-between">
				<view>出库清单</view>
				<view class="lf-icon">
					<u-icon name="plus-circle" size="42"></u-icon>
				</view>
			</view>
			<view class="lf-font-24 lf-color-gray lf-m-t-5">添加物资后可以编辑数量</view>
		</view>
		<!-- 修饰条 -->
		<self-line></self-line>
		<!-- 物料table -->
		<view class="list">
			<view class="lf-font-32 lf-color-black lf-font-bold lf-m-b-20">物资明细</view>
			<wyb-table :headers="headers" :contents="contents" 
				contentBgColor="#ecfaf5" 
				:first-line-fixed="true" 
				@onInputChange="onInputChange"
				@onButtonClick="onButtonClick"
				width="max-content" height="80vh"
				v-if="contents.length"></wyb-table>
		</view>
		<view style="height: 140rpx;"></view>
		<!-- 操作按钮 -->
		<view class="fixed-bottom lf-p-l-32 lf-p-r-32" style="justify-content: space-between;" v-if="enter_type == 1">
			<view></view>
			<!-- <button class="btn btn2" @click="editOrder()">立即申请</button> -->
			<button class="btn btn2" @click="save(1)">立即申请</button>
		</view>
		<view class="fixed-bottom" v-else>
			<button class="btn btn1" @click="save(0)">临时保存</button>
			<button class="btn btn2" @click="save(1)">直接申请</button>
		</view>
	</view>
</template>

<script>
	import wybTable from '@/components/wyb-table/wyb-table';
	
	export default {
		components: {
			wybTable
		},
		data(){
			return {
				headers: [{
					label: '物资名称',
					key: 'material_name'
				},{
					label: '规格',
					key: 'spec_name'
				},{
					label: '单位',
					key: 'unit'
				},{
					label: '分类',
					key: 'category'
				},{
					key: 'brand',
					label: '品牌'
				},{
					key: 'quality_level',
					label: '品级'
				},{
					label: '编号',
					key: 'm_sn'
				},{
					label: '供应商',
					key: 'supplier_name'
				},{
					label: '批次',
					key: 'batch_sn'
				},{
					key: 'stock_number',
					label: '库存数量'
				},{
					key: 'delivery_number',
					label: '出库数量'
				},{
					key: 'operation',
					label: '操作'
				}],
				contents: [],
				warehouse_list: {},
				enter_type: '', // 1 编辑，2 复用
				o_sn: '',
				isStock: true
			}
		},
		onLoad(e){
			// 监听warehouse_list被操作
			uni.$on('addWarehouseList', res => {
				this.warehouse_list = res;
			})
			if(e){
				this.o_sn = e.o_sn
				this.enter_type = e.enter_type
				if(e.enter_type == 1 || e.enter_type == 2){
					this.getData();
				}
			}
		},
		onShow(){
			this.transformList();
		},
		methods: {
			transformList(){
				let contents = [];
				let warehouse_list = JSON.stringify(this.warehouse_list);
				warehouse_list = JSON.parse(warehouse_list);
				for(let i in warehouse_list){
					warehouse_list[i].delivery_number = {edit: true, value: warehouse_list[i].delivery_number || 1};
					warehouse_list[i].operation = {button: true, key: 'delete', value: '删除'};
					warehouse_list[i].unit = warehouse_list[i].unit.unit_name;
					warehouse_list[i].category = warehouse_list[i].category.m_cate_name;
					warehouse_list[i].stock_number = warehouse_list[i].stock;
					contents.push(warehouse_list[i]);
				}
				this.contents = contents;
				console.log("show.warehouse_list...", this.warehouse_list)
				console.log("transformList...", contents);
			},
			// 复用 & 编辑
			getData(){
				// TODO 此处应该调此接口 出库单复用
				// this.$http(this.API.API_CANTEEN_WAREHOUSEOUTREUSE, {
				this.$http(this.API.API_CANTEEN_OUTDETAIL, {
					o_sn: this.o_sn
				}).then(res => {
					// let list = res.data || [];
					let list = res.data.sheet || [];
					let warehouse_list = {};
					list.map(item => {
						console.log('======',item)
						console.log('===库存===',item.warehouse.stock)
						warehouse_list[item.id] = {
							batch_sn: item?.purchase?.batch_sn || '',
							brand: item?.material?.brand || '',
							checked: true,
							stock: item.warehouse.stock,
							m_sn: item?.material?.m_sn || '',
							material_id: item?.material?.id || 0,
							material_name: item?.material?.m_name || '',
							order_id: item.id,
							purchase_id: item.purchase_id,
							quality_level: item?.material?.quality_level || '',
							spec_id: item?.spec?.id || 0,
							spec_name: item?.spec?.name || '',
							supplier_name: item?.purchase?.supplier?.supplier_name || '',
							delivery_number: item.out_number || 1,
							unit: item?.material?.unit || {},
							category: item?.material?.category || {}
						};
					})
					this.warehouse_list = warehouse_list;
					this.transformList();
				})
			},
			//编辑出库 todo
			editOrder() {
				this.$http(this.API.API_EDITOUTORDER, {
					data: list
				}).then(res => {
					console.log("save", res);
					this.$msg('操作成功').then(() => this.$toBack());
				})
			},
			// table输入框
			onInputChange(event){
				console.log("检测到table input被更改", event);
				this.contents[event.contentIndex][event.key].value = event.detailValue;
			},
			// table操作按钮被点击
			onButtonClick(event){
				if(event.content.key == 'delete'){
					let material_index = event.contentIndex;
					let material_name = event.lineData.material_name;
					let spec_name = event.lineData.spec_name;
					let order_id = event.lineData.order_id;
					uni.showModal({
						title: '温馨提示',
						content: `确定删除 ${material_name}-${spec_name} 吗？`,
						cancelColor: '#11D189',
						confirmColor: '#FF0000',
						success: result => {
							if(result.confirm){
								this.contents.splice(material_index, 1);
								delete this.warehouse_list[order_id];
							}
						}
					})
				}
			},
			// 保存，出库
			save(_t){
				if(this.contents.length <= 0) {
					return this.$msg('您未选择物资')
				}
				let is_empty = false;
				let list = this.contents.map(item => {
					if(!item.delivery_number.value){
						is_empty = true;
					}
					if(Number(item.delivery_number.value) > item.stock) {
						console.log('循环几次')
						uni.showModal({
							title: '温馨提示',
							content: `物资：${item.material_name}，仅剩${item.stock}单位库存，已超出您申请的数量，是否直接修改成出库${item.stock}单位`,
							confirmColor: '#11D189',
							success: result => {
								if(result.confirm){
									this.isStock = true
									item.delivery_number.value = item.stock
									return {
										m_id: item.material_id,
										m_spec_id: item.spec_id,
										purchase_id: item.purchase_id,
										out_number: Number(item.delivery_number.value),
										warehouse_id: item.order_id || 0,
										tax_price: item.tax_price || '', // TODO 添加物资页面未返回价格，默认空
										non_tax_price: item.non_tax_price || '',
										m_unit_id: item.m_unit_id || 0
									}
								}else {
									item.delivery_number.value = 1
									this.isStock = true
									return {
										m_id: item.material_id,
										m_spec_id: item.spec_id,
										purchase_id: item.purchase_id,
										out_number: Number(item.delivery_number.value),
										warehouse_id: item.order_id || 0,
										tax_price: item.tax_price || '', // TODO 添加物资页面未返回价格，默认空
										non_tax_price: item.non_tax_price || '',
										m_unit_id: item.m_unit_id || 0
									}
								}
							}
						})
						this.isStock = false
						return
					}else {
						return {
							m_id: item.material_id,
							m_spec_id: item.spec_id,
							purchase_id: item.purchase_id,
							out_number: Number(item.delivery_number.value) || 1,
							warehouse_id: item.order_id || 0,
							tax_price: item.tax_price || '', // TODO 添加物资页面未返回价格，默认空
							non_tax_price: item.non_tax_price || '',
							m_unit_id: item.m_unit_id || 0
						}
					}
				});
				if(this.isStock) {
					if(is_empty){
						uni.showModal({
							title: '温馨提示',
							content: '您有物资未填写出库数量, 请填写',
							showCancel: false,
							confirmColor: '#11D189'
						})
						return;
					}
					let state = ['待确认', '已确认'][_t]; // 传入状态
					let _api = this.API.API_CANTEEN_WAREHOUSEOUT;
					let _data = {
						data: list,
						state: state
					}
					if(this.enter_type == 1){
						_api = this.API.API_EDITOUTORDER;
						_data.o_sn = this.o_sn;
					}
					this.$http(_api, _data).then(res => {
						this.$msg('操作成功').then(() => this.$toBack());
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped="scoped">
	.lf-m-t-5{
		margin-top: 5rpx;
	}
	.lf-icon{
		padding: 2rpx 10rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.list{
		padding: 30rpx 32rpx;
		width: 100%;
		height: max-content;
		box-sizing: border-box;
	}
	.fixed-bottom{
		position: fixed;
		bottom: 0rpx;
		left: 0rpx;
		z-index: 99;
		width: 750rpx;
		height: 98rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		border-top: 1rpx solid #E5E5E5;
		background-color: #fff;
		.btn{
			width: 320rpx;
			height: 82rpx;
			border-radius: 41rpx;
			margin: 0;
			padding: 0;
			font-size: 32rpx;
			display: flex;
			justify-content: center;
			align-items: center;
		}
		.btn1{
			border: 2rpx solid #555555;
			opacity: .5;
		}
		.btn2{
			background: #11D189;
			color: #FFFFFF;
			margin-left: 50rpx;
		}
	}
</style>
