<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small"  :inline="true" v-show="showSearch" label-width="180px">

      <el-row>
        <el-row :gutter="20">
          <el-col :span="6"><el-form-item label="大棚名称" prop="agriName">
            <el-input
              id="agriName" v-model="queryParams.agriName"
              placeholder="请输入大棚名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="imei" prop="imei">
            <el-input
              id="imei" v-model="queryParams.imei"
              placeholder="请输入imei"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="功能名称" prop="funcCode">
            <el-select id="funcCode" v-model="queryParams.funcCode" placeholder="请选择功能名称" clearable>
              <el-option
                v-for="dict in dict.type.sys_func_code"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="操作类型" prop="opType">
            <el-select id="opType" v-model="queryParams.opType" placeholder="请选择操作类型" clearable>
              <el-option
                v-for="dict in dict.type.sys_status_list"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item></el-col>

        </el-row>

      </el-row>

      <el-row :gutter="20">
        <el-col :span="6"> <el-form-item label="操作来源" prop="opSource">
          <el-select id="opSource" v-model="queryParams.opSource" placeholder="请选择操作来源" clearable>
            <el-option
              v-for="dict in dict.type.sys_op_source"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item></el-col>
        <el-col :span="6"><el-form-item label="是否成功获取锁" prop="lockAcquired">
          <el-select id="lockAcquired" v-model="queryParams.lockAcquired" placeholder="请选择是否成功获取锁" clearable>
            <el-option
              v-for="dict in dict.type.sys_exec_result"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item></el-col>
        <el-col :span="6"> <el-form-item label="是否收到设备回执" prop="ackReceived">
          <el-select id="ackReceived" v-model="queryParams.ackReceived" placeholder="请选择是否收到设备回执" clearable>
            <el-option
              v-for="dict in dict.type.sys_result"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item></el-col>
        <el-col :span="6"><el-form-item label="MQTT发布成功" prop="ackSuc">
          <el-select id="ackSuc" v-model="queryParams.ackSuc" placeholder="请选择MQTT发布成功" clearable>
            <el-option
              v-for="dict in dict.type.sys_exec_result"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item></el-col>
      </el-row>


      <el-row :gutter="20">
        <el-col :span="6">  <el-form-item label="设备控制锁是否释放成功" prop="isLockSuc">
          <el-select id="isLockSuc" v-model="queryParams.isLockSuc" placeholder="请选择设备控制锁是否释放成功" clearable>
            <el-option
              v-for="dict in dict.type.sys_result"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        </el-col>
        <el-col :span="6"> <el-form-item label="是否触发定时任务" prop="isTask">
          <el-select id="isTask" v-model="queryParams.isTask" placeholder="请选择是否触发定时任务" clearable>
            <el-option
              v-for="dict in dict.type.sys_result"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item></el-col>
        <el-col :span="6"><el-form-item label="最终执行结果" prop="execResult">
          <el-select id="execResult" v-model="queryParams.execResult" placeholder="请选择最终执行结果" clearable>
            <el-option
              v-for="dict in dict.type.sys_exec_result"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item></el-col>
        <el-col :span="6"> <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-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['control:controllog:export']"
          >导出</el-button>
        </el-form-item></el-col>
      </el-row>


    </el-form>


    <el-table v-loading="loading" border :data="controllogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column width="180" show-overflow-tooltip label="主键ID" align="center" prop="id" />
      <el-table-column width="100" show-overflow-tooltip label="大棚名称" align="center" prop="agriName" />
      <el-table-column width="180" show-overflow-tooltip label="imei" align="center" prop="imei" />
      <el-table-column width="100" show-overflow-tooltip label="功能名称" align="center" prop="funcCode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_func_code" :value="scope.row.funcCode"/>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="操作类型" align="center" prop="opType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_status_list" :value="scope.row.opType"/>
        </template>
      </el-table-column>
      <el-table-column width="100" show-overflow-tooltip label="操作来源" align="center" prop="opSource">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_op_source" :value="scope.row.opSource"/>
        </template>
      </el-table-column>
      <el-table-column width="120" show-overflow-tooltip label="payload" align="center" prop="payload" />
      <el-table-column show-overflow-tooltip label="是否成功获取锁" align="center" prop="lockAcquired">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_exec_result" :value="scope.row.lockAcquired"/>
        </template>
      </el-table-column>
      <el-table-column width="160" show-overflow-tooltip label="锁持有者" align="center" prop="lockHolder" />
      <el-table-column show-overflow-tooltip label="当前任务队列情况" align="center" prop="taskStatus" />
      <el-table-column show-overflow-tooltip label="是否收到设备回执" align="center" prop="ackReceived">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_result" :value="scope.row.ackReceived"/>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="MQTT发布是否成功" align="center" prop="ackSuc">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_exec_result" :value="scope.row.ackSuc"/>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="设备控制锁是否释放成功" align="center" prop="isLockSuc">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_result" :value="scope.row.isLockSuc"/>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="是否触发定时任务" align="center" prop="isTask">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_result" :value="scope.row.isTask"/>
        </template>
      </el-table-column>
      <el-table-column width="120" show-overflow-tooltip label="限位时间" align="center" prop="runTime" />
      <el-table-column width="160" show-overflow-tooltip label="未触发原因" align="center" prop="noTaskReason" />
      <el-table-column width="180" show-overflow-tooltip label="完整回执" align="center" prop="ack" />
      <el-table-column show-overflow-tooltip label="最终执行结果" align="center" prop="execResult">
        <template slot-scope="scope" v-if="scope.row.isTask===1">
          <dict-tag :options="dict.type.sys_exec_result" :value="scope.row.execResult"/>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="未执行/跳过原因" align="center" prop="skipReason" />
      <el-table-column show-overflow-tooltip label="device:latest 状态快照" align="center" prop="latestState" />
      <el-table-column show-overflow-tooltip label="备注" align="center" prop="remark" />
      <el-table-column width="100" show-overflow-tooltip label="操作人" align="center" prop="createBy" />
      <el-table-column width="180" show-overflow-tooltip label="操作时间" align="center" prop="createTime" >
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
    </el-table>

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

<script>
import { listControllog } from "@/api/control/controllog"

export default {
  name: "Controllog",
  dicts: ['sys_exec_result', 'sys_func_code', 'sys_result', 'sys_op_source', 'sys_status_list'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备控制操作日志表格数据
      controllogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        agriName: null,
        imei: null,
        funcCode: null,
        opType: null,
        opSource: null,
        lockAcquired: null,
        ackReceived: null,
        ackSuc: null,
        isLockSuc: null,
        isTask: null,
        execResult: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询设备控制操作日志列表 */
    getList() {
      this.loading = true
      listControllog(this.queryParams).then(response => {
        this.controllogList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        agriName: null,
        imei: null,
        funcCode: null,
        opType: null,
        opSource: null,
        payload: null,
        lockAcquired: null,
        lockHolder: null,
        taskStatus: null,
        ackReceived: null,
        ackSuc: null,
        isLockSuc: null,
        isTask: null,
        runTime: null,
        noTaskReason: null,
        ack: null,
        execResult: null,
        skipReason: null,
        latestState: null,
        remark: null,
        version: null,
        createBy: 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.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('control/controllog/export', {
        ...this.queryParams
      }, `controllog_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
