<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="command">
        <el-input
          v-model="queryParams.command"
          placeholder="请输入命令"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="MAC" prop="mac">
        <el-input
          v-model="queryParams.mac"
          placeholder="请输入mac"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="SN" prop="sn">
        <el-input
          v-model="queryParams.sn"
          placeholder="请输入sn"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-input
          v-model="queryParams.type"
          placeholder="请输入类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="回调状态" prop="callStatus">
        <el-select v-model="queryParams.callStatus" placeholder="请选择回调状态" clearable>
          <el-option
            v-for="dict in dict.type.onenet_call_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          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-table v-loading="loading" :data="commandLogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="请求url" align="center" prop="url" :show-overflow-tooltip="true"/>
      <el-table-column label="命令" align="center" prop="command" />
      <el-table-column label="类型" align="center" prop="type" />
      <el-table-column label="mac" align="center" prop="mac" />
      <el-table-column label="sn" align="center" prop="sn" />
      <el-table-column label="订单号" align="center" prop="orderNo" :show-overflow-tooltip="true"/>
      <el-table-column label="响应" align="center" prop="result" :show-overflow-tooltip="true"/>
      <el-table-column label="回调状态" align="center" prop="callStatus" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.onenet_call_status" :value="scope.row.callStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <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-view"
            @click="handleView(scope.row,scope.index)"
            v-hasPermi="['system:commandLog:query']"
          >详细</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="命令日志详细" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px">
        <el-col :span="24">
          <el-form-item label="请求url">{{ form.url }}</el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="命令">{{ form.command }}</el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item style="color: #12d2ac;font-weight: 700" label="类型">{{ form.type }}</el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="MAC">{{ form.mac }}</el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="SN">{{ form.sn }}</el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="响应">{{ form.result }}</el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="回调状态">
            <dict-tag :options="dict.type.onenet_call_status" :value="form.callStatus"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="创建时间">{{ parseTime(form.createTime) }}</el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCommandLog, getCommandLog, delCommandLog, addCommandLog, updateCommandLog } from "@/api/system/commandLog";

export default {
  name: "CommandLog",
  dicts:  ['onenet_call_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 命令日志表格数据
      commandLogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        url: null,
        command: null,
        mac: null,
        sn: null,
        orderNo: null,
        result: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    const sn = this.$route.params && this.$route.params.sn;
    if (sn != null) {
      this.queryParams.sn = sn;
    }
    const orderNo = this.$route.params && this.$route.params.orderNo;
    if (orderNo != null) {
      this.queryParams.orderNo = orderNo;
    }
    this.getList();
  },
  methods: {
    /** 查询命令日志列表 */
    getList() {
      this.loading = true;
      listCommandLog(this.queryParams).then(response => {
        this.commandLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        url: null,
        command: null,
        mac: null,
        result: null
      };
      this.resetForm("form");
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.open = true;
      this.form = row;
    },
    /** 搜索按钮操作 */
    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
      getCommandLog(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) {
            updateCommandLog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCommandLog(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 delCommandLog(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/commandLog/export', {
        ...this.queryParams
      }, `commandLog_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
