<template>
	<view>
		<view class="head">
			<view class="list">
				<view class="lf-row-between">
					<view>供应商</view>
					<view class="lf-icon" @click="$url('/pages/classification/supplier')">
						<u-icon name="plus-circle" size="42"></u-icon>
					</view>
				</view>
			</view>
			<view class="list" v-if="show_material">
				<view class="lf-row-between">
					<view>采购清单</view>
					<view class="lf-icon" @click="$url('/pages/classification/material?type=1')">
						<u-icon name="plus-circle" size="42"></u-icon>
					</view>
				</view>
				<view class="lf-font-24 lf-color-gray">添加物资后可以编辑数量</view>
			</view>
		</view>
		<!-- 修饰条 -->
		<self-line></self-line>
		<!-- 收货时间 -->
		<view class="lf-row-between lf-p-30 lf-p-l-32 lf-p-r-32 lf-font-28">
			<view class="lf-color-black">收货时间</view>
			<picker mode="date" :value="date" @change="pickerChange">
				<view class="lf-color-555 lf-text-right" style="width: 400rpx;">{{ date || '请选择收货时间...' }}</view>
			</picker>
		</view>
		<self-line></self-line>
		<!-- 物料table -->
		<view class="lf-p-32 lf-p-t-30 lf-p-b-30 lf-w-100 lf-h-maxcontent lf-border-box">
			<view class="lf-font-32 lf-font-bold">物资明细</view>
			<view v-for="(value, key) in render_material_list" :key="key" class="lf-m-t-20" v-if="value.material_list.length">
				<view class="lf-m-b-10 lf-row-between">
					<text>{{ value.supplier_name }}</text>
					<text @click="removeSupplier(key)" class="lf-color-price">删除</text>
				</view>
				<wyb-table :headers="value.headers" 
					:contents="value.material_list" 
					contentBgColor="#ecfaf5" 
					:first-line-fixed="true" 
					@onInputChange="onInputChange" 
					width="max-content" height="800rpx"
					@onButtonClick="onButtonClick"></wyb-table>
			</view>
		</view>
		<!-- 操作按钮 -->
		<view style="height: 100rpx;"></view>
		<view class="fixed-bottom">
			<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 {
				// date: this.$shared.recordTime(new Date(), '-', 'date'), // 选择时间, 默认今天
				date: '',
				material_list: {}, // 供应商，物资列表，不被渲染，在底层逻辑运作
				show_material: false, // 是否显示采购清单按钮
				render_material_list: {} // 渲染出来的物资列表
			}
		},
		onLoad(){
			// 监听MaterialList被操作
			uni.$on('addMaterialList', res => {
				this.material_list = res;
			})
		},
		onShow(){
			let material_list = JSON.stringify(this.material_list);
			material_list = JSON.parse(material_list);
			for(let i in material_list){
				// table 标题处理
				material_list[i].headers = [{
					key: 'material_name',
					label: '物资名称'
				},{
					key: 'spec_name',
					label: '规格'
				},{
					key: 'brand',
					label: '品牌'
				},{
					key: 'quality_level',
					label: '品级'
				},{
					label: '编号',
					key: 'm_sn'
				},{
					label: '供应商',
					key: 'supplier_name'
				},{
					key: 'tax_price',
					label: '含税价'
				},{
					key: 'non_tax_price',
					label: '不含税价'
				},{
					key: 'purchase_limit',
					label: '起购数'
				},{
					key: 'purchase_number',
					label: '采购数量'
				},{
					key: 'operation',
					label: '操作'
				}];
				// table 内容处理
				let list_arr = [];
				for(let j in material_list[i].material_list){
					material_list[i].material_list[j].purchase_number = {edit: true, value: ''};
					material_list[i].material_list[j].operation = {button: true, key: 'delete', value: '删除'};
					material_list[i].material_list[j].supplier_name = material_list[i].supplier_name;
					material_list[i].material_list[j].tax_price = material_list[i].material_list[j].tax_price;
					material_list[i].material_list[j].non_tax_price = material_list[i].material_list[j].non_tax_price;
					// material_list[i].material_list[j].star_num = material_list[i].material_list[j].purchase_limit;
					list_arr.push(material_list[i].material_list[j]);
				}
				material_list[i].material_list = list_arr;
			}
			this.render_material_list = material_list;
			this.show_material = Object.keys(this.material_list).length > 0;
			console.log("show..material_list", this.material_list);
			console.log("show...render_material_list", this.render_material_list)
		},
		methods: {
			// table-input值被改变
			onInputChange(event){
				console.log("检测到table input被更改", event);
				let supplier_id = event.lineData.supplier_id; // 取出第一层，供应商id
				let material_index = event.contentIndex; // 取出第二层，物资下标
				let detailValue = event.detailValue; // 取出table input被输入的值
				let supplier_item = this.render_material_list[supplier_id]; // 取出所在供应商
				let material_item = supplier_item.material_list[material_index]; // 取出物资
				if(material_item.purchase_limit <= detailValue){
					material_item.purchase_number.value = detailValue; // 将输入的值赋值给物资
				}else{
					this.$msg('采购数量须大于起购数量');
					material_item.purchase_number.value = material_item.purchase_limit;
					
					// uni.showModal({
					// 	title: '温馨提示',
					// 	content: '采购数量必须大于起购数量',
					// 	showCancel: false,
					// 	success: result => {
					// 		material_item.purchase_number.value = material_item.purchase_limit;
					// 	}
					// })
				}
				console.log("render_material_list_change", this.render_material_list);
			},
			// table 操作按钮被点击
			onButtonClick(event){
				console.log("event", event);
				if(event.content.key == 'delete'){
					let supplier_id = event.lineData.supplier_id; // 取出第一层，供应商id
					let item_id = event.lineData.item_id; // 取出第二层，item_id
					let material_index = event.lineData.contentIndex; // 取出第二层，物资下标
					this.render_material_list[supplier_id].material_list.splice(material_index, 1);
					delete this.material_list[supplier_id].material_list[item_id];
				}
			},
			// 时间选择
			pickerChange(event){
				this.date = event.detail.value;
			},
			// 移除供应商
			removeSupplier(key){
				uni.showModal({
					title: '温馨提示',
					content: '删除供应商后所添加的物资将随之移除，确定继续吗？',
					confirmColor: '#FF0000',
					cancelColor: '#11D189',
					success: result => {
						if(result.confirm){
							// 移除render_material_list
							let render_material_list = {...this.render_material_list};
							delete render_material_list[key];
							this.render_material_list = render_material_list;
							// 移除material_list
							delete this.material_list[key];
							// 校验还有没有供应商，没有则不能显示选择物资按钮
							this.show_material = Object.keys(this.material_list).length > 0;
						}
					}
				})
			},
			// 保存并发单
			save(_t){
				let material_list = this.render_material_list;
				let list = [];
				let is_empty = true; // 物资数据是否为空
				let is_right = true; // 采购份数为正常的情况
				for(let i in material_list){
					if(Object.keys(material_list[i].material_list).length){
						is_empty = false;
						let material = material_list[i].material_list;
						// 数组写法
						let arr = material.map(item => {
							if(item.purchase_limit > item.purchase_number.value){
								is_right = false;
							};
							return {
								m_id: item.material_id,
								m_spec_id: item.spec_id,
								tax_price: item.tax_price,
								non_tax_price: item.non_tax_price,
								purchase_number: Number(item.purchase_number.value) || item.purchase_limit
							}
						});
						// 对象写法: 
						// let arr = [];
						// for(let j in material){
						// 	arr.push({
						// 		m_id: material[j].material_id,
						// 		m_spec_id: material[j].spec_id,
						// 		tax_price: material[j].tax_price,
						// 		non_tax_price: material[j].non_tax_price,
						// 		purchase_number: material[j].purchase_number.value || 0
						// 	});
						// }
						list.push({
							supplier_id: material_list[i].id,
							material: arr
						})
					}
				}
				
				console.log("list", list)
				let deadline = this.date;
				if(!deadline){
					this.$msg('请选择收货时间');
					return;
				}
				if(is_empty){
					this.$msg('您未选择物资');
					return;
				}
				if(!is_right){
					this.$msg('采购数量需大于起购数量');
					return;
				}
				// let state = ['待发单', '待审核'][_t];
				this.$http(this.API.API_CANTEEN_PURCHASEAPPLY, {
					order: list,
					state: _t,
					deadline: deadline
				}).then(res => {
					console.log("save", res);
					this.$msg('操作成功');
				})
			}
		}
	}
</script>

<style lang="scss" scoped="scoped">
	.lf-m-t-5{
		margin-top: 5rpx;
	}
	.head{
		width: 750rpx;
		height: max-content;
		padding: 0 32rpx;
		box-sizing: border-box;
		.list{
			width: 100%;
			border-bottom: 1rpx solid #e5e5e5;
			padding: 30rpx 0;
			font-size: 28rpx;
			&:last-child{
				border-bottom: none;
			}
			.lf-icon{
				padding: 2rpx 10rpx;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
	}
	.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: #FFFFFF;
		.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>
