<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="巡检日期" prop="pollingData">
        <el-date-picker clearable v-model="queryParams.pollingData" type="date" value-format="yyyy-MM-dd" placeholder="请选择巡检日期"> </el-date-picker>
      </el-form-item>
      <el-form-item label="巡检人" prop="inspectionUserId">
        <el-select
          v-model="queryParams.inspectionUserId"
          filterable
          remote
          reserve-keyword
          placeholder="请输入关键词"
          :remote-method="remoteMethod"
          :loading="loading"
        >
          <el-option v-for="item in useroptions" :key="item.userId" :label="item.userName + '/' + item.nickName" :value="item.userId"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="报修单编号" prop="repairsId">
        <el-input v-model="queryParams.repairsId" 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">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inspectionList" @selection-change="handleSelectionChange">
      <el-table-column label="报修单编号" align="center" prop="repairsId" />
      <el-table-column label="巡检日期" align="center" prop="pollingData" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.pollingData, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="巡检人" align="center" prop="inspectionUserName" />
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="设备状态" align="center" prop="deviceState">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.deviceState == 0" type="success">正常</el-tag>
          <el-tag v-else type="danger">异常</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
    </el-table>

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

<script>
import { listLog, getLog, delLog, addLog, updateLog } from '@/api/patrol/patrolLog';
import { getInspectionUser } from '@/api/patrol/placeInspection';

export default {
  name: 'Inspection',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 巡检记录表格数据
      inspectionList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pollingData: null,
        userName: null,
        deviceId: null,
        deviceState: null,
        repairsId: null,
      },
      // 用户下拉选项
      useroptions: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [{ required: true, message: '巡检人不能为空', trigger: 'blur' }],
        deviceId: [{ required: true, message: '设备id不能为空', trigger: 'blur' }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询巡检记录列表 */
    getList() {
      this.loading = true;
      listLog(this.queryParams).then(response => {
        this.inspectionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      getInspectionUser({
        userName: null,
      }).then(response => {
        this.useroptions = response.rows;
      });
    },
    // 远程搜索
    remoteMethod(queryString) {
      this.loading = true;
      getInspectionUser({
        userName: queryString,
      }).then(response => {
        this.useroptions = response.rows;

        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        pollingData: null,
        userId: null,
        deviceId: null,
        deviceState: null,
        repairsId: 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();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = '添加巡检记录';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getLog(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) {
            updateLog(this.form).then(response => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addLog(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 delLog(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('删除成功');
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'inspection/inspection/export',
        {
          ...this.queryParams,
        },
        `inspection_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
