<!-- 主动补货任务查询 -->
<template>
<div>
	<p-card title="波次补货">
		<template slot="extra">
			<p-button type="primary" icon="upload" @click="extract" style="margin-right:15px;">提取</p-button>
			<p-button type="primary" icon="delete" @click="delShow" style="margin-right:15px;">删除</p-button>
			<p-button type="primary" icon="save" @click="hanlderSubmit">保存</p-button>
		</template>
		<div class="plg-inner-form-mb">
		 <p-grid-form
			  ref="search"
		      :items="items"
		      @submit="hanlderSubmit"
		    />
		</div>
		 <p-table-edit-pro
			ref="table"
			autoWidth
			 editor-row-table
			:columns="columns"
			:data-source.sync="data"
			rowKey="id"
			dragle
			:row-select-type="2"
			@colChange='colChange'
			@change='changePagination'
			@checkRow="checkRow"
			:pagination="pagination"
			:loading = "loading"
		    bordered>

		 </p-table-edit-pro>


	</p-card>
</div>
</template>

<script>
	// 设置默认仓库
import { setwarehouse }  from  '@/views/kzyWMS/SearchModal/mixins_warehouse.js';
import { items,columns } from  '../js/accord.js';
import { search_page,initiative_save } from '../api'
export default {
	mixins:[setwarehouse],
	data() {
		return {
			items,
			columns,
			data: [],
			selectData:[],
			loading: false,
			pagination: {
			  total: 0,
			  pageSize: 10, //每页中显示10条数据
			  current: 1,
			  showSizeChanger: true,
			  pageSizeOptions: ['10', '15', '20'], //每页中显示的数据
			  showTotal: (total) => `共有 ${total} 条数据`, //分页中显示总的数据
			},
		}
	},
	props: {
		waveId: {
			type: String,
			default: ''
		}
	},
	methods:{
		getData(val){
			val = {
				pageNum : this.pagination.current,
				pageSize: this.pagination.pageSize,
				num: 'waveReplenishQuery',
				params:{...val,waveId:this.waveId,warehouseId: this.$refs.search.form.getFieldsValue().warehouseId}
			}
			this.loading = true;
			search_page(val).then(res => {
			  if (res.success) {
				if(res.data){
						let temp = res.data.list;
						this.data = temp.map(item=>{
							return ({
								pieceQuantity:0, //实补数量（混入后台未定义）
								pieceNum:0,		//实补件数（混入后台未定义）
								...item,
							})
						});
						this.pagination.total = Number(res.data.totalCount);
				}
			    this.loading = false;
			  } else {
			    this.loading = false;
			    this.$exception(res);
			  }
			}).finally(()=>{
			    this.loading = false;
			})
		},
		// 右上角保存按钮
		// 保存 提交
		hanlderSubmit(event) {
			 // event.preventDefault();
			if(this.selectData.length){
				this.$refs.search.form.validateFields((err, values) => {
			        if (!err) {
			          console.log('验证通过: ', values);
					  // this.getData(values);
					  let params ={	warehouseId:this.$refs.search.form.getFieldsValue().warehouseId,
									details:[...this.selectData],
									};
					  initiative_save(params).then(res=>{
						  if(res.success){
							  this.$message.success('保存成功！');
							  this.$emit('saveFun')
						  } else {
							  this.$exception(res);
						  }
					  })
			        }
			      });
			} else {
				this.$message.warning('请选择数据！')
			}
		},
		// 重置
		hanlderRest(event,form){
			this.$refs.search.form.resetFields();
		},
		changePagination(pagination, filters, sorter) { // 分页变化
			this.pagination = pagination;
			this.getData();  //分页查询所有列表
		},
		checkRow(selectId,selectRows){
			console.log("selectId,selectRows：",selectId,selectRows);
			this.selectData = selectRows.map(item=>{
				return({
					id:item.id,
					cargoOwnerId: item.cargoOwnerId, //货主ID
					itemId: item.itemId,	//商品ID
					pieceQuantity: item.pieceNum*item.caseConversionRate, //补货数量
					warehouseAreaCategory:item.warehouseCategoryCode,	//库别
				})
			});
			console.log("准备保存数组：",this.selectData);
		},
		colChange({ name, value, index,label, record, modalRowData,config, vNode , options, moment, event}) {
			console.log("改变单元格数据：",name, value, index,label, record, modalRowData);
			this.selectData.forEach(item=>{
				if(item.id==record.id){
					item.pieceQuantity = value*record.caseConversionRate;
				}
			})
			console.log("this.selectData:",this.selectData);
		},
		// 提取
		extract(){
			this.getData({...this.$refs.search.form.getFieldsValue()});
		},
		// 仅前台显示删除，后台不做删除
		delShow(){
			if(this.selectData.length){
				for(let item in this.data){
					for(let sub of this.selectData){
						if(this.data[item].id == sub.id){
							// delete this.data[item];
							this.data.splice(item,1);
						}
					}
				}
				console.log("仅前台显示数据：",this.data);
			} else {
				this.$message.warning('请选择数据！');
			}
		}
	},	// methods
	mounted(){
		this.getData({...this.$refs.search.form.getFieldsValue()});
	}
}
</script>

<style>
</style>
