<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="设备唯一标识(IMEI/DeviceId)" prop="imei">
        <el-input
          id="imei" v-model="queryParams.imei"
          placeholder="请输入设备唯一标识(IMEI/DeviceId)"
          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">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['assets:remark:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['assets:remark:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['assets:remark:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['assets:remark:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="remarkList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="设备唯一标识(IMEI/DeviceId)" align="center" prop="imei" />
      <el-table-column label="卷被开" align="center" prop="jbk" />
      <el-table-column label="卷被关" align="center" prop="jbg" />
      <el-table-column label="卷膜1开" align="center" prop="jm1k" />
      <el-table-column label="卷膜1关" align="center" prop="jm1g" />
      <el-table-column label="卷膜2开" align="center" prop="jm2k" />
      <el-table-column label="卷膜2关" align="center" prop="jm2g" />
      <el-table-column label="卷膜3开" align="center" prop="jm3k" />
      <el-table-column label="卷膜3关" align="center" prop="jm3g" />
      <el-table-column label="温度1(℃)" align="center" prop="temp1" />
      <el-table-column label="湿度1(%RH)" align="center" prop="humi1" />
      <el-table-column label="温度2(℃)" align="center" prop="temp2" />
      <el-table-column label="湿度2(%RH)" align="center" prop="humi2" />
      <el-table-column label="温度3(℃)" align="center" prop="temp3" />
      <el-table-column label="湿度3(%RH)" align="center" prop="humi3" />
      <el-table-column label="温度4(℃)" align="center" prop="temp4" />
      <el-table-column label="湿度4(%RH)" align="center" prop="humi4" />
      <el-table-column label="数据版本号" align="center" prop="version" />
      <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-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['assets:remark:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['assets:remark:remove']"
          >删除</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"
    />

    <!-- 添加或修改dtu设备备注对话框 -->
    <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="设备唯一标识(IMEI/DeviceId)" prop="imei">
          <el-input id="imei" v-model="form.imei" placeholder="请输入设备唯一标识(IMEI/DeviceId)" />
        </el-form-item>
        <el-form-item label="卷被开" prop="jbk">
          <el-input id="jbk" v-model="form.jbk" placeholder="请输入卷被开" />
        </el-form-item>
        <el-form-item label="卷被关" prop="jbg">
          <el-input id="jbg" v-model="form.jbg" placeholder="请输入卷被关" />
        </el-form-item>
        <el-form-item label="卷膜1开" prop="jm1k">
          <el-input id="jm1k" v-model="form.jm1k" placeholder="请输入卷膜1开" />
        </el-form-item>
        <el-form-item label="卷膜1关" prop="jm1g">
          <el-input id="jm1g" v-model="form.jm1g" placeholder="请输入卷膜1关" />
        </el-form-item>
        <el-form-item label="卷膜2开" prop="jm2k">
          <el-input id="jm2k" v-model="form.jm2k" placeholder="请输入卷膜2开" />
        </el-form-item>
        <el-form-item label="卷膜2关" prop="jm2g">
          <el-input id="jm2g" v-model="form.jm2g" placeholder="请输入卷膜2关" />
        </el-form-item>
        <el-form-item label="卷膜3开" prop="jm3k">
          <el-input id="jm3k" v-model="form.jm3k" placeholder="请输入卷膜3开" />
        </el-form-item>
        <el-form-item label="卷膜3关" prop="jm3g">
          <el-input id="jm3g" v-model="form.jm3g" placeholder="请输入卷膜3关" />
        </el-form-item>
        <el-form-item label="温度1(℃)" prop="temp1">
          <el-input id="temp1" v-model="form.temp1" placeholder="请输入温度1(℃)" />
        </el-form-item>
        <el-form-item label="湿度1(%RH)" prop="humi1">
          <el-input id="humi1" v-model="form.humi1" placeholder="请输入湿度1(%RH)" />
        </el-form-item>
        <el-form-item label="温度2(℃)" prop="temp2">
          <el-input id="temp2" v-model="form.temp2" placeholder="请输入温度2(℃)" />
        </el-form-item>
        <el-form-item label="湿度2(%RH)" prop="humi2">
          <el-input id="humi2" v-model="form.humi2" placeholder="请输入湿度2(%RH)" />
        </el-form-item>
        <el-form-item label="温度3(℃)" prop="temp3">
          <el-input id="temp3" v-model="form.temp3" placeholder="请输入温度3(℃)" />
        </el-form-item>
        <el-form-item label="湿度3(%RH)" prop="humi3">
          <el-input id="humi3" v-model="form.humi3" placeholder="请输入湿度3(%RH)" />
        </el-form-item>
        <el-form-item label="温度4(℃)" prop="temp4">
          <el-input id="temp4" v-model="form.temp4" placeholder="请输入温度4(℃)" />
        </el-form-item>
        <el-form-item label="湿度4(%RH)" prop="humi4">
          <el-input id="humi4" v-model="form.humi4" placeholder="请输入湿度4(%RH)" />
        </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 { listRemark, getRemark, delRemark, addRemark, updateRemark } from "@/api/assets/remark"

export default {
  name: "Remark",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // dtu设备备注表格数据
      remarkList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        imei: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        imei: [
          { required: true, message: "设备唯一标识(IMEI/DeviceId)不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询dtu设备备注列表 */
    getList() {
      this.loading = true
      listRemark(this.queryParams).then(response => {
        this.remarkList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        imei: null,
        jbk: null,
        jbg: null,
        jm1k: null,
        jm1g: null,
        jm2k: null,
        jm2g: null,
        jm3k: null,
        jm3g: null,
        temp1: null,
        humi1: null,
        temp2: null,
        humi2: null,
        temp3: null,
        humi3: null,
        temp4: null,
        humi4: null,
        version: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        extJson: 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 = "添加dtu设备备注"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getRemark(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改dtu设备备注"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRemark(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addRemark(this.form).then(response => {
                if (response.code===200) {
                  this.$modal.msgSuccess("新增成功")
                } else {
                  this.$modal.msgError("新增失败")
                }
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除dtu设备备注编号为"' + ids + '"的数据项？').then(function() {
        return delRemark(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('assets/remark/export', {
        ...this.queryParams
      }, `remark_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
