<template>
	<div class="common-page device-manage">

		<div class="option page-row">
			<!--工具栏-->
			<div class="head-container">
				<!-- 搜索 -->
				<label class="el-form-item-label" style="font-weight: 500;">车牌号</label>
				<el-input v-model="query.number" clearable size="small" placeholder="请输入车牌号" style="width:180px;"/>

				<label class="el-form-item-label" style="font-weight: 500;">所属车队</label>
				<el-select v-model="query.road" placeholder="请选择所属车队" style="width:180px;">
					<el-option v-for="(obj,index) in selectList" :label="obj.name" :value="obj.name" :key="index"></el-option>
				</el-select>

				<label class="el-form-item-label" style="font-weight: 500;">车辆类型</label>
				<el-select v-model="query.carclass" placeholder="请选择卡车类型" style="width:180px;">
					<el-option v-for="(obj,index) in carTypeData" :label="obj.name" :value="obj.name" :key="index"></el-option>
				</el-select>
				<el-button size="mini" type="success" icon="el-icon-search" @click="toSearch">搜索</el-button>
				<el-button size="mini" icon="el-icon-refresh" @click="clearLimit">重置</el-button>
			</div>
		</div>

    	<!-- 表格 -->
		<div class="panel-bottom page-row">
			<h3>车辆信息</h3>
			<div class="ctin-box">
				<div class="content-within">
					<div class="content-fix">
						<div class="toolbar">
							<el-button size="mini" type="primary" icon="el-icon-plus" @click="toAdd">新增</el-button>
						</div>
						<div class="content">
							<el-table :data="tableData" v-loading="loading" border style="width:auto" :row-class-name="tableRowClassName">
								<el-table-column prop="number" label="车牌号" align="center"></el-table-column>
								<el-table-column prop="carclass" label="卡车类型" align="center"></el-table-column>
								<el-table-column prop="road" label="车队名称" align="center"></el-table-column>
								<el-table-column prop="model" label="车辆型号" align="center"></el-table-column>
								<el-table-column prop="weight" label="运重(T)" align="center"></el-table-column>
								<el-table-column prop="status" label="车辆状态" align="center" :formatter="statusChange"></el-table-column>
								<el-table-column prop="alarm" label="超速预警(km/h)" align="center"></el-table-column>
								<el-table-column prop="month" label="保养周期(月)" align="center"></el-table-column>
								<el-table-column prop="year" label="车辆年限" align="center"></el-table-column>
								<el-table-column prop="time" label="最新一次保养时间" align="center" :formatter="timeChange"></el-table-column>
								<el-table-column prop="equipmentName" label="终端设备名称" align="center"></el-table-column>
								<el-table-column label="操作" align="center" fixed="right">
									<template slot-scope="scope">
										<el-button size="mini" type="primary" icon="el-icon-edit" @click="toEdit(scope.row)"></el-button>
										<el-button size="mini" type="danger" icon="el-icon-delete" @click="toDelete(scope.row)"></el-button>
									</template>
								</el-table-column>
							</el-table>
							<!--分页组件-->
							<el-pagination :total="total" :current-page="page" style="margin-top: 8px;" layout="total, prev, pager, next, sizes" @size-change="sizeChange" @current-change="pageChange" />
						</div>
					</div>
				</div>
			</div>
		</div>

		<!-- 表单渲染 -->
		<el-dialog append-to-body :close-on-click-modal="true" :before-close="cancelForm" :visible.sync="form.visible" :title="form.title" width="700px">
			<el-form :model="form.item" :rules="rules" ref="form1" :inline="true" size="small" label-width="150px">
				<el-form-item label="车牌号" prop="number">
					<el-input v-model="form.item.number" style="width:160px;" placeholder="请输入车牌号" :disabled="form.reqType == 'edit'"/>
				</el-form-item>
				<el-form-item label="卡车类型" prop="carclass">
					<el-select v-model="form.item.carclass" placeholder="请选择卡车类型" style="width:160px;" :disabled="form.reqType == 'edit'">
						<el-option v-for="(obj,index) in carTypeData" :label="obj.name" :value="obj.name" :key="index"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="车队名称" prop="road">
					<el-select v-model="form.item.road" placeholder="请选择所属车队名称" style="width:160px;">
						<el-option v-for="(obj,index) in selectList" :label="obj.name" :value="obj.name" :key="index"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="车辆型号" prop="model">
					<el-select v-model="form.item.model" placeholder="请选择车辆型号" style="width:160px;">
						<el-option v-for="(obj,index) in carModelData" :label="obj.model" :value="obj.model" :key="index"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="运重(T)" prop="weight">
					<el-input v-model="form.item.weight" style="width:160px;" placeholder="请输入运重(T)"/>
				</el-form-item>
				<el-form-item label="车辆状态" prop="status">
					<el-select v-model="form.item.status" placeholder="请选择车辆状态" style="width:160px;">
						<el-option label="正常" value="0"></el-option>
						<el-option label="故障" value="1"></el-option>
						<el-option label="停用" value="2"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="超速预警(km/h)" prop="alarm">
					<el-input v-model="form.item.alarm" style="width:160px;" placeholder="请输入超速预警(km/h)"/>
				</el-form-item>
				<el-form-item label="保养周期(月)" prop="month">
					<el-input v-model="form.item.month" style="width:160px;" placeholder="请输入保养周期(月)"/>
				</el-form-item>
				<el-form-item label="车辆年限" prop="year">
					<el-input v-model="form.item.year" style="width:160px;" placeholder="请输入车辆年限"/>
				</el-form-item>
				<el-form-item label="最新一次保养时间" prop="time">
					<el-date-picker v-model="form.item.time" type="date" placeholder="选择日期" style="width:160px;"></el-date-picker>
				</el-form-item>
				<el-form-item label="终端设备名称" prop="equipmentName">
					<el-select v-model="form.item.equipmentName" placeholder="请选择终端设备" style="width:200px;">
						<el-option v-for="(obj,index) in terminalList" :label="obj.name" :value="obj.name" :key="index"></el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="text" @click="cancelForm">取消</el-button>
				<el-button :loading="form.status.cu === 2" type="primary" @click="submitForm('form1', form.item)">确认</el-button>
			</div>
		</el-dialog>

	</div>
</template>

<script>
import { Tools, HttpReq, Dates} from '@/assets/js/common.js';

export default {
	name: 'CarInformation',
	data() {
		return {
			loading: false,
			page: 1,
			size: 10,
			total: 0,
			query:{},
			tableData: [],
			selectList:[],
			terminalList:[],
			carTypeData:[],
			carModelData:[],
			form: {
				title:'新增车辆',
				visible:false,
				reqType:'add',
				status:{cu:0},
				item:{
					weight:'',
					alarm:'',
					model:'',
				},
			},
			rules: {
				number: [
					{required: true, message: '请输入车牌号', trigger: 'blur' }
				],
				equipmentName: [
					{required: true, message: '请选择终端设备', trigger: 'change' }
				],
				road: [
					{required: true, message: '请选择所属车队名称', trigger: 'change' }
				],
			},
		}
	},
	mounted() {
		this.$nextTick(() => {
			this.loadData();
		})
	},
	methods: {
		loadData() {
			var sort = 'id,desc';
			var param = this.query;
			param.page = this.page - 1;
			param.size = this.size;
			param.sort = sort;
			
			this.$nextTick(()=>{
				//获取车辆信息
				HttpReq.truckDispatching.carInformationQuery(param).then((res) => {
					if(res.code == 200){
						this.loading = false;
						this.tableData = res.data.content;
						this.total = res.data.totalElements;
					}
				})
				//获取车队
				HttpReq.truckDispatching.carFleetQuery({size:9999}).then((res) => {
					if(res.code == 200){
						this.selectList = res.data.content;
					}
				})
				//获取终端信息
				HttpReq.truckDispatching.selectZhongDuanDeviceQuery({size:9999}).then((res) => {
					this.terminalList = res;
				})
				//获取车辆类型
				HttpReq.truckDispatching.carTypeQuery({size:9999}).then((res) => {
					if(res.code == 200){
						this.carTypeData = res.data.content;
					}
				})
				//获取车辆型号
				HttpReq.truckDispatching.carModelQuery({size:9999}).then((res) => {
					if(res.code == 200){
						this.carModelData = res.data.content;
					}
				})
			})

		},
		tableRowClassName({row, rowIndex}) {
			if (rowIndex === 1) {
				return 'warning-row';
			} else if (rowIndex === 3) {
				return 'success-row';
			}
			return '';
		},
		// 点击搜索
		toSearch() {
			this.page = 1;
			this.loadData();
		},
		// 重置搜索
		clearLimit(){
			this.query = {};
			this.loadData()
		},
		cancelForm(a, b, c){
			this.form.visible = false;
		},
		toAdd() {
			// 请除表单验证
			this.$refs['form1'] && this.$refs['form1'].clearValidate();
			this.form.title = '新增车辆';
			this.form.status.cu = 0;
			this.form.visible = true;
			this.form.reqType = 'add';
			this.form.item = {
				weight:'',
				alarm:'',
				model:'',
			};
		},
		toEdit(item) {
			// 请除表单验证
			this.$refs['form1'] && this.$refs['form1'].clearValidate();
			this.form.title = '修改车辆';
			this.form.status.cu = 0;
			this.form.visible = true;
			this.form.item = {...item};
			this.form.reqType = 'edit';
		},
		toDelete(item) {
			var id = item.id;
			this.$confirm( item.carclass == '铲车' ? '该车为铲车，如要删除，请在删除以后为采点管理中对应的采点分配新的铲车！' : '确认删除该条数据吗?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: item.carclass == '铲车' ? 'error' : 'warning'
			}).then((e) => {
				this.reqRemoveProject([id],item)
			}).catch((e) => {})
		},
		pageChange(e) {
			this.page = e
			this.loadData()
		},
		sizeChange(e) {
			this.page = 1
			this.size = e
			this.loadData()
		},
		submitForm(validateName, item){
			if(!this.form.status.cu){
				this.form.status.cu = 2;
				this.$refs[validateName].validate(valid => {
					if(valid){
						if(this.form.reqType === 'add'){
							this.reqAddItem(this.form, item)
						}else{
							this.reqUpdateItem(this.form, item)
						}
					}else{
						this.form.status.cu = 0
					}
				});
			};
		},
		//新增车辆
		reqAddItem(form, item){
			let lastData = {...item};
			HttpReq.truckDispatching.carInformationAdd(lastData).then((res) => {
				form.visible = false;
				if(res.code == 200){
					this.$notify({
						title: '车辆新增成功！',
						type: 'success',
						duration: 2500
					});
					this.loadData()
				}else{
					this.$notify({
						title: res.msg,
						type: 'error',
						duration: 2500
					})
				}
			}).catch(function(error) {
				form.status.cu = 0
			});
		},
		//修改车辆
		reqUpdateItem(form, item){
			let lastData = {...item};
			HttpReq.truckDispatching.carInformationUpdate(lastData).then((res) => {
				form.visible = false;
				if(res.code == 200){
					this.$notify({
						title: '车辆修改成功！',
						type: 'success',
						duration: 2500
					});
					this.loadData()
				}else{
					this.$notify({
						title: res.msg,
						type: 'error',
						duration: 2500
					})
				}
			}).catch(function(error) {
				form.status.cu = 0
			});
		},
		//删除车辆
		reqRemoveProject(item,items){
			HttpReq.truckDispatching.carInformationDel(item).then((res) => {
				if(res.status == 400){
					this.$notify({
						title: res.msg,
						type: 'error',
						duration: 2500
					})
				}else{
					this.$notify({
						title: '删除成功！',
						type: 'success',
						duration: 2500
					});
				}
				this.loadData();
			})
			
			if(items.carclass == '铲车'){
				HttpReq.truckDispatching.RegionalCaiDianQuery({areashovel:items.number}).then((res) => {
					if(res.code == 200 && res.data.totalElements != 0){
						this.$nextTick(()=>{
							this.$notify({
								title: '请在采点管理中为 ' + res.data.content[0].areaName + '/' + res.data.content[0].name + ' 添加新的铲车！',
								type: 'warning',
								duration: 20000
							});
						})
						let lastData = {...res.data.content[0]};
						lastData.areashovel = '';
						HttpReq.truckDispatching.RegionalCaiDianUpdate(lastData).then((res) => {})
					}
				})
			}

			//清空人员管理中的车牌号
			HttpReq.truckDispatching.driverManagementPCQuery({car:items.number,size:9999}).then((res) => {
				if(res.code == 200 && res.data.totalElements != 0){
					this.$nextTick(()=>{
						this.$notify({
							title: '该车辆在人员管理中有绑定的人员，请尽快为该人员绑定新的车辆！',
							type: 'warning',
							duration: 20000
						});
					})
					res.data.content.forEach((item,index)=>{
						let lastData = {...item};
						lastData.car = '';
						HttpReq.truckDispatching.driverManagementPCUpdate(lastData).then((resDel) => {})
					})
				}
			})
			
		},
		//车辆状态转换
		statusChange(row){
			return row.status == '0' ? '正常' : row.status == '1' ? '故障' : row.status == '2' ? '停用' : '' ;
		},
		//保养时间转换
		timeChange(row){
			if(!row.time){
				return ''
			}else{
				return row.time == '' ?  '' : this.currentTime(row.time);
			}
		},
		currentTime(time){
			var date = new Date(time);
			var year = date.getFullYear();        //年 ,从 Date 对象以四位数字返回年份
			var month = date.getMonth() + 1;      //月 ,从 Date 对象返回月份 (0 ~ 11) ,date.getMonth()比实际月份少 1 个月
			var day = date.getDate();             //日 ,从 Date 对象返回一个月中的某一天 (1 ~ 31)
			//修改月份格式
			if (month >= 1 && month <= 9) {
				month = "0" + month;
			}
			//修改日期格式
			if (day >= 0 && day <= 9) {
				day = "0" + day;
			}
			//获取当前系统时间  格式(yyyy-mm-dd hh:mm:ss)
			let currentFormatDate = year + "-" + month + "-" + day;
			return currentFormatDate;
		},
	},
	watch:{
		'form.item.model':function(){
			HttpReq.truckDispatching.carModelQuery({model:this.form.item.model}).then((res) => {
				if(res.code == 200){
					this.form.item.weight = res.data.content[0].weight;
					this.form.item.alarm = res.data.content[0].alarm;
				}
			})
		},
		'form.item.carclass':function(){
			//获取车辆型号
			HttpReq.truckDispatching.carModelQuery({size:9999,carclass:this.form.item.carclass}).then((res) => {
				if(res.code == 200){
					this.carModelData = res.data.content;
				}
			})
		},
	},
}
</script>

<style rel="stylesheet/scss" lang="scss" scope>
	.el-table .warning-row {
		background: oldlace;
	}
	.el-table .success-row {
		background: #f0f9eb;
	}
	
	.device-manage{
		.panel-bottom{
			.el-button{margin:2px 0;padding:6px 15px;font-size:14px;}
			.btn-groub2{
				display:inline-block;
				.el-button{margin:2px 0;padding:6px 15px;font-size:15px;}
				.el-button--warning {
					background-color: #e6a23c;
					border-color: #e6a23c;
				}
				.el-button--warning:focus,.el-button--warning:hover{background:#ebb563;border-color:#ebb563;}
			}
		}
	}
	
	.ecahrt1-dialog{
		display:flex;align-items:center;
		.el-dialog{
			margin-top:inherit !important;display:table !important;width:fit-content;
			.el-form-item{margin-right:0;}
		}

		.el-dialog__header{height:0;padding:0;display:block;}
		.form-body{
			border:1px solid #999;width:800px;height:500px;position:relative;
			>div{position:absolute;top:0;left:0;height:100%;width:100%;}
		}
	}
</style>
