<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备类型" prop="deviceType">
        <el-select v-model="queryParams.deviceType" placeholder="请选择设备类型">
          <el-option v-for="item in deviceTypeList" :key="item.id" :label="item.typeName" :value="item.id"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备名称" prop="deviceName">
        <el-input v-model="queryParams.deviceName" placeholder="请输入设备名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="maintainList">
      <el-table-column label="维修单编号" align="center" prop="id" />
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="设备编码" align="center" prop="deviceNo" />
      <el-table-column label="巡检人" align="center" prop="inspectionUserName" />
      <el-table-column label="设备故障描述" align="center" prop="deviceErroDetail">
        <template slot-scope="scope">
          <span>{{ scope.row.deviceErroDetail || '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="维修单日期" align="center" prop="repairsDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.repairsDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="维修人" align="center" prop="maintainUserName" />
      <el-table-column label="维修情况描述" align="center" prop="maintainDetail" />
      <el-table-column label="维修日期" align="center" prop="maintainDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.maintainDate, '{y}-{m}-{d}') }}</span>
        </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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="维修单编号" prop="repairsId">
          <el-input v-model="form.repairsId" placeholder="请输入维修单编号" />
        </el-form-item>
        <el-form-item label="设备id" prop="deviceId">
          <el-input v-model="form.deviceId" placeholder="请输入设备id" />
        </el-form-item>
        <el-form-item label="设备编码" prop="deviceNo">
          <el-input v-model="form.deviceNo" placeholder="请输入设备编码" />
        </el-form-item>
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="巡检人id" prop="inspectionUserId">
          <el-input v-model="form.inspectionUserId" placeholder="请输入巡检人id" />
        </el-form-item>
        <el-form-item label="设备故障描述" prop="deviceErrorDetail">
          <el-input v-model="form.deviceErrorDetail" placeholder="请输入设备故障描述" />
        </el-form-item>
        <el-form-item label="维修单日期" prop="repairsDate">
          <el-date-picker clearable v-model="form.repairsDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择维修单日期"> </el-date-picker>
        </el-form-item>
        <el-form-item label="维修人" prop="maintainUser">
          <el-input v-model="form.maintainUserId" placeholder="请输入维修人" />
        </el-form-item>
        <el-form-item label="维修情况描述" prop="maintainDetail">
          <el-input v-model="form.maintainDetail" placeholder="请输入维修情况描述" />
        </el-form-item>
        <el-form-item label="维修日期" prop="maintainDate">
          <el-date-picker clearable v-model="form.maintainDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择维修日期"> </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" 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 { listMaintain, getMaintain, delMaintain, addMaintain, updateMaintain } from '@/api/repair/repairLog';
import { listDevice as listDeviceType } from '@/api/device/deviceType';

export default {
  name: 'Maintain',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维修历史表格数据
      maintainList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        repairsId: null,
        deviceId: null,
        deviceNo: null,
        deviceName: null,
        inspectionUserId: null,
        deviceErrorDetail: null,
        repairsDate: null,
        maintainUserId: null,
        maintainDetail: null,
        maintainDate: null,
        maintainType: null,
        deviceType: null,
      },
      orderStatesList: [
        { label: '派单中', value: 0 },
        { label: '维修中', value: 1 },
        { label: '已完成', value: 2 },
      ],
      deviceTypeList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        repairsId: [{ required: true, message: '维修单编号不能为空', trigger: 'blur' }],
      },
    };
  },
  created() {
    this.getList();
    listDeviceType().then(response => {
      this.deviceTypeList = response.rows;
    });
  },
  methods: {
    /** 查询维修历史列表 */
    getList() {
      this.loading = true;
      listMaintain(this.queryParams).then(response => {
        this.maintainList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        repairsId: null,
        deviceId: null,
        deviceNo: null,
        deviceName: null,
        inspectionUserId: null,
        deviceErrorDetail: null,
        repairsDate: null,
        maintainUserId: null,
        maintainDetail: null,
        maintainDate: null,
        maintainType: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = '添加维修历史';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getMaintain(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = '修改维修历史';
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMaintain(this.form).then(response => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addMaintain(this.form).then(response => {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除维修历史编号为"' + ids + '"的数据项？')
        .then(function () {
          return delMaintain(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('删除成功');
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'maintain/maintain/export',
        {
          ...this.queryParams,
        },
        `maintain_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
