<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.nickName + '/' + item.nickName" :value="item.userId"> </el-option>
        </el-select>
      </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="ID" width="55" align="center" prop="id" />
      <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="placeName" />
      <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 } from '@/api/patrol/placeInspectionlog'
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,
        inspectionUserId: null,
        placeId: null,
      },
      //用户列表
      useroptions: [],
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 远程搜索
    remoteMethod(queryString) {
      this.loading = true
      getInspectionUser({
        userName: queryString,
      }).then((response) => {
        this.useroptions = response.rows
        this.loading = false
      })
    },
    /** 查询巡检记录列表 */
    getList() {
      this.loading = true
      listLog(this.queryParams)
        .then((response) => {
          this.inspectionList = response.rows
          this.total = response.total
          this.loading = false
          this.useroptions = response.rows
        })
        .catch(() => {
          this.loading = false
        })
      getInspectionUser({
        userName: null,
      }).then((response) => {
        this.useroptions = response.rows
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
    },

    /** 搜索按钮操作 */
    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
    },
  },
}
</script>
