<template>
	<div>
		<el-dialog title="配车" :visible.sync="dialogVisible" width="1000px" height="400px"  @close="close">

			<el-form :model="distribution" label-width="100px" inline="">
				<el-form-item label="车号">
					<el-select v-model="truckNo" clearable allow-create filterable default-first-option @change="change" style="width: 200px;">
						<el-option v-for="item in trucks" :key="item.id" :label="item.truckNo" :value="item.truckNo">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="司机姓名">
					<el-input v-model="distribution.driverName" placeholder="司机姓名" style="width: 200px;"></el-input>
				</el-form-item>
				<el-form-item label="联系方式">
					<el-input v-model="distribution.driverPhone" placeholder="联系方式" style="width: 200px;"></el-input>
				</el-form-item>
				<el-form-item label="总重量">
					<el-input v-model="sumWeight" placeholder="总重量" style="width: 200px;"></el-input>
				</el-form-item>
				<el-form-item label="总数量">
					<el-input v-model="sumCount" placeholder="总数量" style="width: 200px;"></el-input>
				</el-form-item>
				<el-form-item label="过磅重量" v-if="false">
					<el-input v-model="distribution.suttle" placeholder="过磅重量" style="width: 200px;"></el-input>
				</el-form-item>
				<el-form-item label="提货单位" >
					<el-input v-model="distribution.deliveryCompany" placeholder="提货单位" style="width: 200px;"></el-input>
				</el-form-item>

				<el-form-item label=" ">
					<el-button size="medium" type="success" @click="handleSave">确定</el-button>
					<el-button size="medium" type="success" @click="handleCancel">取消</el-button>
					<el-button size="medium" type="primary" @click="handlePrint">打印配车单</el-button>
				</el-form-item>
			</el-form>


			<el-table :data="tableData" border style="width: 99%;" ref="multipleTable" :row-class-name="tableRowClassName"
			 @select="handleTableSelect" @select-all = "selectAll">
				<el-table-column type="selection" width="40" header-align="center" align="center">
				</el-table-column>
				<el-table-column prop="layer" label="层数" width="60" show-overflow-tooltip header-align="center" align="center">
				</el-table-column>
				<el-table-column prop="stack.name" label="垛位" width="60" show-overflow-tooltip header-align="center" align="center">
				</el-table-column>
				<el-table-column prop="material.name" label="物料" width="100" show-overflow-tooltip header-align="center" align="center">
				</el-table-column>
				<el-table-column prop="material.standard" label="钢种" width="100" show-overflow-tooltip header-align="center" align="center">
				</el-table-column>
				<el-table-column prop="plateNo" label="钢板号" width="200" show-overflow-tooltip header-align="center">
				</el-table-column>
				<el-table-column prop="model" label="规格型号" width="120" show-overflow-tooltip header-align="center" align="center">
				</el-table-column>
				<el-table-column prop="count" label="数量" width="120" show-overflow-tooltip align="right" header-align="center">
				</el-table-column>
				<el-table-column prop="weight" label="重量" width="120" show-overflow-tooltip align="right" header-align="center">
				</el-table-column>
				<el-table-column prop="customer.name" label="客户" width="300" show-overflow-tooltip header-align="center">
				</el-table-column>
				<el-table-column prop="receiveAddress" label="收货地址" width="300" show-overflow-tooltip header-align="center">
				</el-table-column>
				<el-table-column prop="wgtDcnMtcCd" label="计量方式" width="100" show-overflow-tooltip header-align="center">
				</el-table-column>
				<el-table-column prop="edgeTy" label="切边类型" width="100" show-overflow-tooltip header-align="center">
				</el-table-column>
				<el-table-column prop="productionPlace" label="产地" width="250" show-overflow-tooltip header-align="center">
				</el-table-column>
			</el-table>
		</el-dialog>
	</div>
</template>

<script>
	import axios from '@/axios';
	import {
		mapState
	} from 'vuex'
	export default {
		computed: {
			sumWeight: function() {
				let result = 0
				for (let i = 0; i < this.tableData.length; i++) {
					//console.log(this.distribution.listDetail[i].weight)
					result = Number(result) + Number(this.tableData[i].weight)
				}
				return result.toFixed(3)
			},
			sumCount: function() {
				let result = 0
				for (let i = 0; i < this.tableData.length; i++) {
					//console.log(this.distribution.listDetail[i].count)
					result = Number(result) + Number(this.tableData[i].count)
				}
				return result.toFixed(0)
			},
			...mapState({
				serverName: state => state.app.serverName
			})
		},
		props: {
			selectRows: {
				type: Array
			},
		},
		data() {
			return {
				trucks: [],
				truckNo: '',
				tableData: [],
				currentPage: 1,
				totalRows: 0,
				pageSizes: [10, 20, 30, 40],
				pageSize: 10,
				query: '',
				isShow: false,
				dialogVisible: false,
				distribution: {
					truckNo: '',
					driverName: '',
					driverPhone: '',
					sumWeight: '',
					sumCount: '',
					suttle: 0,
					deliveryCompany:'',
					listDetail: []
				},
				selection: []
			};
		},
		methods: {
			getTableData() {
				var url = 'MaintCustomer/query.do'
				var param = {
					page: this.currentPage,
					rows: this.pageSize,
					query: this.query
				}
				axios.get(url, param).then(response => {
					if (response.data.code == 0) {
						this.tableData = response.data.data.list
						this.totalRows = response.data.data.total
					} else {
						this.$message({
							type: 'error',
							message: '失败！' + response.data.msg,
						});
					}
				});
			},
			getTrucks() {
				var url = 'WareDistribution/queryWaitTrucks.do'
				var param = {}
				axios.get(url, param).then(response => {
					if (response.data.code == 0) {
						for (let i = 0; i < response.data.data.length; i++) {
							if (response.data.data.listDetail) {
								response.data.data.listDetail = []
							}
						}
						this.trucks = response.data.data
						//	console.log(JSON.stringify(this.trucks))

					} else {
						this.$message({
							type: 'error',
							message: '失败！' + response.data.msg,
						});
					}
				});
			},
			// 修改每页行数
			handleSizeChange(val) {
				this.pageSize = val
				this.getTableData()
			},
			// 修改当前页事件
			handleCurrentChange(val) {
				this.currentPage = val
				this.getTableData()
			},
			sendToParent: function(row) {
				this.$emit('receviceFromChild', row);
				this.dialogVisible = false
			},
			show() {
				this.tableData = []
				this.distribution = {}
				this.dialogVisible = true
				//this.getTableData()
				this.getTrucks()
				//console.log(JSON.stringify(this.selectRows))
				for (let i = 0; i < this.selectRows.length; i++) {
					this.selectRows[i].lockFlag = '0'
					this.tableData.push(this.selectRows[i])
				}
				//this.tableData = this.tableData.concat()
				//console.log(JSON.stringify(this.selectRows))
			},
			search() {
				this.currentPage = 1
				this.getTableData()

			},
			clear() {
				this.distribution = {
					truckNo: '',
					driverName: '',
					driverPhone: '',
					sumWeight: this.sumWeight,
					sumCount: this.sumCount,
					suttle: 0,
					listDetail: []
				}
				this.tableData = []
			},

			change(val) {

				if (val == '') {
				} else {
					for (let i = 0; i < this.trucks.length; i++) {
						if (this.trucks[i].truckNo == val) {
							this.distribution = this.trucks[i]
						}
					}
					this.tableData = []
					var tempPltNo = [];
					// console.log("iiiiiiiiii"+JSON.stringify(this.distribution))
					// if(typeof(this.distribution.listDetail) != "undefined"){
					// 	alert("kk")
						
					// }
					
					for (let i = 0; i < this.selectRows.length; i++) {
							this.tableData.push(this.selectRows[i])
							console.log(JSON.stringify(this.selectRows[i]))
							tempPltNo.push(this.selectRows[i].plateNo);
					}
					for (let i = 0; i < this.distribution.listDetail.length; i++) {
							// alert(JSON.stringify(this.distribution.listDetail[i].store))
						if(tempPltNo.indexOf(this.distribution.listDetail[i].store.plateNo) === -1){	
							this.tableData.push(this.distribution.listDetail[i].store)
						}	
					}
					

					this.distribution.sumWeight = this.sumWeight
					this.distribution.sumCount = this.sumCount
					this.truckNo = this.distribution.truckNo
				}
			},
			handleSave() {
				//console.log(JSON.stringify(this.distribution))
				this.distribution.sumWeight = this.sumWeight
				this.distribution.sumCount = this.sumCount
				this.distribution.truckNo = this.truckNo
				if(this.truckNo.length == 0){
					this.$message({
						type: 'error',
						message: '请选择车辆！'
					});
					return;
				}
				var url = 'WareDistribution/save.do'
				var param = {
					json: JSON.stringify(this.distribution),
					jsonList: JSON.stringify(this.selectRows)
				}
				this.dialogVisible = false
				axios.post(url, param).then(response => {
					if (response.data.code == 0) {

						//this.dialogVisible = false
						this.selectRows    = []
					} else {
						this.$message({
							type: 'error',
							message: '失败！' + response.data.msg,
						});
					}
				});
			},
			close() {
				this.distribution = {
					truckNo: '',
					driverName: '',
					driverPhone: '',
					sumWeight: 0,
					sumCount: 0,
					suttle: 0,
					listDetail: []
				}
				this.tableData = []
				this.truckNo = ''
				this.$emit('receviceFromChild')

			},

			handlePrint() {
				if (this.distribution.truckNo == null || this.distribution.truckNo == '') {
					this.$message({
						type: 'error',
						message: '请选择车辆！'
					});
					return
				}
				// window.open(this.serverName + 'ureport/preview?_u=file:distribution.ureport.xml&id=' + this.distribution.id)
				window.open(this.serverName + 'ureport/preview?_u=file:distribution.ureport.xml&id=' + this.distribution.id)
			},

			handleTableSelect(selection, row) {
				if (row.lockFlag == '0') {
					this.$message({
						type: 'error',
						message: '该行未配货',
					});
					this.$refs.multipleTable.toggleRowSelection(row, false);
				} else {
					this.selection = selection
				}
			},
			selectAll(selection){
				for(var i = 0 ; i < selection.length;i++){
					if (selection[i].lockFlag == '0') {

						this.$message({
							type: 'error',
							message: '存在未配货记录',
						});
						return;
					}
					
				}

				this.selection = selection;
			},
			handleCancel() {
				var url = 'WareDistribution/cancel.do'
				if(this.selection.length == 0){
					this.$message({
						type: 'error',
						message: '请选择记录',
					});
					return;
				}
				var param = {
					json: JSON.stringify(this.distribution),
					jsonList: JSON.stringify(this.selection)
				}
				axios.post(url, param).then(response => {
					if (response.data.code == 0) {
						this.dialogVisible = false
					} else {
						this.$message({
							type: 'error',
							message: '失败！' + response.data.msg,
						});
					}
				});
			},
			

			tableRowClassName({
				row,
				rowIndex
			}) 
			{
				if (row.lockFlag == '1') {
					return 'success-row';
				}
				return '';
			}

		},

	}
</script>
<style>
	.el-table .success-row {
		background: #f0f9eb;
	}
</style>
