<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="报修单号" prop="orderCode">
				<el-input
					v-model="queryParams.orderCode"
					placeholder="请输入报修单号"
					clearable
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="设备编码" prop="equipmentCode">
				<el-input
					v-model="queryParams.equipmentCode"
					placeholder="请输入设备编码"
					clearable
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="故障时间" prop="orderBreakdownTime">
        <el-date-picker
          v-model="queryParams.orderBreakdownTimeArray"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          :picker-options="pickerOptions1">
        </el-date-picker>
			</el-form-item>
			<el-form-item label="报修来源" prop="orderSource">
        <el-select v-model="form.orderSource" style="width:195px">
          <el-option
            v-for="dict in dict.type.device_order_source"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
			</el-form-item>
			<el-form-item label="报修时间" prop="orderTime">
        <el-date-picker
          v-model="queryParams.orderTimeArray"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          :picker-options="pickerOptions2">
        </el-date-picker>
			</el-form-item>
			<el-form-item label="报修人" prop="orderRepairman">
				<el-input
					v-model="queryParams.orderRepairman"
					placeholder="请输入报修人"
					clearable
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="联系方式" prop="orderConnection">
				<el-input
					v-model="queryParams.orderConnection"
					placeholder="请输入联系方式"
					clearable
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button
					type="primary"
					plain
					icon="el-icon-plus"
					size="mini"
					@click="handleAdd"
					v-hasPermi="['device:faultReport:add']"
				>添加报修</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button
					type="success"
					plain
					icon="el-icon-edit"
					size="mini"
					:disabled="single"
					@click="handleUpdate"
					v-hasPermi="['device:faultReport:edit']"
				>修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button
					type="danger"
					plain
					icon="el-icon-delete"
					size="mini"
					:disabled="multiple"
					@click="handleDelete"
					v-hasPermi="['device:faultReport:remove']"
				>删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button
					type="warning"
					plain
					icon="el-icon-download"
					size="mini"
					@click="handleExport"
					v-hasPermi="['device:faultReport:export']"
				>导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="faultReportList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="报修单号" align="center" prop="orderCode" width="120"/>
			<el-table-column label="设备编码" align="center" prop="equipmentCode" width="100"/>
			<el-table-column label="故障描述" align="center" prop="orderDesc" width="200"/>
			<el-table-column label="故障时间" align="center" prop="orderBreakdownTime" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.orderBreakdownTime) }}</span>
				</template>
			</el-table-column>
			<el-table-column label="报修来源" align="center" prop="orderSource" />
			<el-table-column label="报修时间" align="center" prop="orderTime" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.orderTime) }}</span>
				</template>
			</el-table-column>
			<el-table-column label="报修人" align="center" prop="orderRepairman" />
			<el-table-column label="联系方式" align="center" prop="orderConnection" width="120"/>
			<el-table-column label="处理状态" align="center" prop="orderStatus" />
			<el-table-column label="关联计划" align="center" prop="orderRelevance" />
			<el-table-column label="故障图片" align="center" prop="orderPicture" />
			<el-table-column label="创建人" align="center" prop="craeteBy" />
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button
						size="mini"
						type="text"
						icon="el-icon-edit"
						@click="handleUpdate(scope.row)"
						v-hasPermi="['device:faultReport:edit']"
					>修改</el-button>
					<el-button
						size="mini"
						type="text"
						icon="el-icon-delete"
						@click="handleDelete(scope.row)"
						v-hasPermi="['device:faultReport:remove']"
					>删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination
			v-show="total>0"
			:total="total"
			:page.sync="queryParams.pageNum"
			:limit.sync="queryParams.pageSize"
			@pagination="getList"
		/>

		<!-- 添加或修改故障报修对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="100px">
				<el-row :gutter="10" class="mb8">
          <el-col :span="8">
						<el-form-item label="设备编码" prop="equipmentCode">
              <el-input v-model="form.equipmentCode" placeholder="请选择辅助设备" >
                <el-button slot="append" @click="handleSelectEquipment" icon="el-icon-search"></el-button>
          			  </el-input>
          				<ItemSelect ref="itemSelect" @onSelected="onItemSelectedEquipment" > </ItemSelect>
			  		</el-form-item>
          </el-col>
          <el-col :span="8">
						<el-form-item label="故障时间" prop="orderBreakdownTime">
	            <el-date-picker
	              clearable
                v-model="form.orderBreakdownTime"
                type="datetime"
                placeholder="选择日期时间">
              </el-date-picker>
						</el-form-item>
          </el-col>
					<el-col :span="8">
						<el-form-item label="报修来源" prop="orderSource">
					    	<el-input v-model="form.orderSource" placeholder="请输入报修来源" />
						</el-form-item>
          </el-col>
        </el-row>
				<el-row :gutter="10" class="mb8">
          <el-col :span="8">
						<el-form-item label="报修时间" prop="orderTime">
							<el-date-picker clearable
								v-model="form.orderTime"
								type="date"
								value-format="yyyy-MM-dd"
								placeholder="请选择报修时间">
							</el-date-picker>
						</el-form-item>
          </el-col>
          <el-col :span="8">
						<el-form-item label="报修人" prop="orderRepairman">
					    <el-input v-model="form.orderRepairman" placeholder="请输入报修人" />
						</el-form-item>
          </el-col>
					<el-col :span="8">
						<el-form-item label="联系方式" prop="orderConnection">
							<el-input v-model="form.orderConnection" placeholder="请输入联系方式" />
						</el-form-item>
          </el-col>
        </el-row>
			  <el-row>
					<el-form-item label="故障描述" prop="orderDesc">
						<el-input v-model="form.orderDesc" placeholder="请输入故障描述" />
					</el-form-item>
				</el-row>
				<el-form-item label="故障图片" prop="orderPicture">
					<el-input v-model="form.orderPicture" placeholder="请输入故障图片" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import { listFaultReport, getFaultReport, delFaultReport, addFaultReport, updateFaultReport,getEquipmentList } from "@/api/device/faultReport";
import ItemSelect  from "./single.vue";
export default {
	name: "FaultReport",
	components: {ItemSelect},
  dicts: ['device_order_source'],
	data() {
		return {
		  equipmentCodeOption:[],
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 故障报修表格数据
			faultReportList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				orderCode: null,
				equipmentCode: null,
				orderDesc: null,
				orderBreakdownTime: null,
				orderSource: null,
				orderTime: null,
				orderHandle: null,
				orderRepairman: null,
				orderConnection: null,
				orderStatus: null,
				orderRelevance: null,
				orderPicture: null,
				attr1: null,
				attr2: null,
				attr3: null,
				craeteBy: null,
				orderBreakdownTimeArray: [],
				orderTimeArray: []
			},
      // 日期范围选择快捷
      pickerOptions1: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      pickerOptions2: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
			// 表单参数
			form: {},
			// 表单校验
			rules: {
			}
		};
	},
	created() {
		this.getList();
	},
	methods: {
    //新增 设备选择弹出框
    handleSelectEquipment(){
      this.$refs.itemSelect.showFlag = true;
    },
    //新增 设备选择弹出框
    onItemSelectedEquipment(obj){
      this.form.equipmentCode = obj;
    },
		/** 查询故障报修列表 */
		getList() {
			this.loading = true;
			listFaultReport(this.queryParams).then(response => {
				this.faultReportList = response.rows;
				this.total = response.total;
				this.loading = false;
			});
		},
		// 取消按钮
		cancel() {
			this.open = false;
			this.reset();
		},
		// 表单重置
		reset() {
			this.form = {
				orderId: null,
				orderCode: null,
				equipmentCode: null,
				orderDesc: null,
				orderBreakdownTime: null,
				orderSource: null,
				orderTime: null,
				orderHandle: null,
				orderRepairman: null,
				orderConnection: null,
				orderStatus: null,
				orderRelevance: null,
				orderPicture: null,
				attr1: null,
				attr2: null,
				attr3: null,
				delFlag: null,
				craeteBy: null,
				createTime: null,
				updateBy: null,
				updateTime: null
			};
			this.resetForm("form");
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm");
			this.handleQuery();
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.orderId)
			this.single = selection.length!==1
			this.multiple = !selection.length
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset();
			this.open = true;
			this.title = "添加故障报修";
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset();
			const orderId = row.orderId || this.ids
			getFaultReport(orderId).then(response => {
				this.form = response.data;
				this.open = true;
				this.title = "修改故障报修";
			});
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					if (this.form.orderId != null) {
						updateFaultReport(this.form).then(response => {
							this.$modal.msgSuccess("修改成功");
							this.open = false;
							this.getList();
						});
					} else {
						addFaultReport(this.form).then(response => {
							this.$modal.msgSuccess("新增成功");
							this.open = false;
							this.getList();
						});
					}
				}
			});
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const orderIds = row.orderId || this.ids;
			this.$modal.confirm('是否确认删除故障报修编号为"' + orderIds + '"的数据项？').then(function() {
				return delFaultReport(orderIds);
			}).then(() => {
				this.getList();
				this.$modal.msgSuccess("删除成功");
			}).catch(() => {});
		},
		/** 导出按钮操作 */
		handleExport() {
			this.download('device/faultReport/export', {
				...this.queryParams
			}, `faultReport_${new Date().getTime()}.xlsx`)
		}
	}
};
</script>
