<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.carbreak" placeholder="请选择故障类型" style="width:180px;">
					<el-option v-for="(obj,index) in guzhangleixingData" :label="obj.breakclass" :value="obj.breakclass" :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="driver" label="司机" align="center"></el-table-column>
								<el-table-column prop="carbreak" label="故障类型" align="center"></el-table-column>
								<el-table-column prop="breakRemarks" label="故障详情" align="center"></el-table-column>
								<el-table-column prop="createTime" label="故障日期" align="center"></el-table-column>
								<el-table-column prop="status" label="车辆状态处理" align="center" :formatter="statusChange"></el-table-column>
								<el-table-column prop="updateBy" label="故障处理人" align="center"></el-table-column>
								<el-table-column prop="updateTime" label="处理时间" align="center"></el-table-column>
								<el-table-column label="操作" align="center" fixed="right">
									<template slot-scope="scope">
										<el-button size="mini" type="primary" @click="toEdit(scope.row)" v-if="scope.row.status == 1">处理故障</el-button>
										<!-- <el-button size="mini" type="danger" icon="el-icon-delete" @click="toDelete(scope.row)"></el-button> -->
										<span v-if="scope.row.status == 0">已处理故障</span>
									</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" v-show="form.reqType == 'add'">
					<el-select v-model="form.item.number" placeholder="请选择车牌号" style="width:280px;">
						<el-option v-for="(obj,index) in carInformationData" :label="obj.number" :value="obj.number" :key="index"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="司机" prop="driver" v-show="form.reqType == 'add'">
					<el-select v-model="form.item.driver" placeholder="请选择司机" style="width:280px;">
						<el-option v-for="(obj,index) in sijiData" :label="obj.name" :value="obj.name" :key="index"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="故障类型" prop="carbreak" v-show="form.reqType == 'add'">
					<el-select v-model="form.item.carbreak" placeholder="请选择故障类型" style="width:280px;">
						<el-option v-for="(obj,index) in guzhangleixingData" :label="obj.breakclass" :value="obj.breakclass" :key="index"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="故障详情" prop="breakRemarks" v-show="form.reqType == 'add'">
					<el-input type="textarea" v-model="form.item.breakRemarks" style="width:280px;"></el-input>
				</el-form-item>
				<el-form-item label="车辆状态处理" prop="status" v-show="form.reqType == 'edit'">
					<el-select v-model="form.item.status" placeholder="请选择车辆状态处理" style="width:280px;">
						<el-option label="正常" value="0"></el-option>
						<el-option label="故障" value="1"></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: 'CarFault',
	components: {},
	data() {
		return {
			loading: false,
			page: 1,
			size: 10,
			total: 0,
			query:{},
			tableData: [],
			carInformationData:[],
			guzhangleixingData:[],
			sijiData:[],
			form: {
				title:'新增车辆故障',
				visible:false,
				reqType:'add',
				status:{cu:0},
				item:{},
			},
			rules: {
				number: [
					{required: true, message: '请输入车牌号', trigger: 'blur' }
				],
			},
		}
	},
	mounted() {
		this.loadData();
	},
	methods: {
		loadData() {
			var sort = 'createTime,desc';
			var param = this.query;
			param.page = this.page - 1;
			param.size = this.size;
			param.sort = sort;
			
			this.$nextTick(()=>{
				//获取故障表
				HttpReq.truckDispatching.carFaultQuery(param).then((res) => {
					if(res.code == 200){
						this.loading = false;
						this.tableData = res.data.content;
						this.total = res.data.totalElements;
						let faultCarNumber1 = [];
						res.data.content.forEach((item,index)=>{
							if(item.status == '1'){
								faultCarNumber1.push(item.number);
							}
						})
						let set1 = new Set();
						faultCarNumber1.forEach((item)=>{
							set1.add(item);
						})
						let lastArray1 = Array.from(set1);
						lastArray1.forEach((item)=>{
							HttpReq.truckDispatching.carInformationQuery({number:item}).then((res1) => {
								if(res1.code == 200){
									HttpReq.truckDispatching.carInformationUpdate({id:res1.data.content[0].id,status:1}).then((res2) => {})
								}
							})
						})
					}
				})
				//获取车辆信息
				HttpReq.truckDispatching.carInformationQuery({size:9999}).then((res) => {
					if(res.code == 200){
						this.carInformationData = res.data.content;
					}
				})
				//获取故障类型信息
				HttpReq.truckDispatching.carFaultTypeQuery({size:9999}).then((res) => {
					if(res.code == 200){
						this.guzhangleixingData = 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 = {};
		},
		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('确认删除该条数据吗?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then((e) => {
				this.reqRemoveProject([id])
			}).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};
			lastData.status = 1;
			HttpReq.truckDispatching.carFaultAdd(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.carInformationQuery({number:lastData.number}).then((res1) => {
				if(res1.code == 200){
					HttpReq.truckDispatching.carInformationUpdate({id:res1.data.content[0].id,status:0}).then((res2) => {})
				}
			})
			//修改故障表的数据
			HttpReq.truckDispatching.carFaultUpdate(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){
			HttpReq.truckDispatching.carFaultDel(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();
			})
		},
		//车辆状态转换
		statusChange(row){
			return row.status == '0' ? '正常' : '故障' ;
		},
	},
	watch:{
		'form.item.number':function(){
			HttpReq.truckDispatching.driverManagementPCQuery({car:this.form.item.number}).then((res) => {
				if(res.code == 200){
					this.sijiData = 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>
