<template>
  <section class="app-container">
    <el-row>
      <el-col :span="4">
        <region></region>
      </el-col>
      <el-col :span="20">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="名称" prop="jobName">
            <el-input v-model="queryParams.deviceName" placeholder="请输入名称" clearable size="small" style="width: 240px"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="cyan" 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 size="mini" type="primary" icon="el-icon-plus" @click="handleAdd" v-hasPermi="['system:device:add']">增加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
              v-hasPermi="['monitor:job:remove']">批量删除</el-button>
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleClean" v-hasPermi="['monitor:job:remove']">清空
            </el-button>
          </el-col> -->
          <!-- <el-col :span="1.5">
            <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['monitor:job:export']">导出
            </el-button>
          </el-col> -->
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table :data="tableData" style="width: 100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center">
          </el-table-column>
          <el-table-column prop="deviceId" align="center" label="设备ID">
          </el-table-column>
          <el-table-column prop="deviceName" align="center" label="设备名称">
          </el-table-column>
          <el-table-column prop="deviceCode" align="center" label="设备编码">
          </el-table-column>
          <el-table-column prop="deviceType" align="center" label="类型">
          </el-table-column>
          <el-table-column prop="deviceModel" align="center" label="装置模型">
          </el-table-column>
          <el-table-column prop="provinceCode" align="center" label="省份">
          </el-table-column>
          <el-table-column prop="cityCode" align="center" label="市">
          </el-table-column>
          <el-table-column prop="areaCode" align="center" label="区">
          </el-table-column>
          <el-table-column prop="status" align="center" label="在线状态">
          </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-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:device:edit']">修改
              </el-button>
              <el-button class="text-danger" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                v-hasPermi="['system:device:edit']">删除</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-col>
    </el-row>
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="设备ID" prop="deviceId">
          <el-input v-model="form.deviceId" placeholder="请输入设备ID" />
        </el-form-item>
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备编码" prop="deviceCode">
          <el-input v-model="form.deviceCode" placeholder="请输入设备编码" />
        </el-form-item>
        <el-form-item label="设备类型" prop="deviceType">
          <el-input v-model="form.deviceType" placeholder="请输入设备类型" />
        </el-form-item>
        <el-form-item label="设备型号" prop="deviceModel">
          <el-input v-model="form.deviceModel" placeholder="请输入设备型号" />
        </el-form-item>
        <el-form-item label="产品" prop="productId">
          <el-select v-model="form.productId" clearable placeholder="请选择">
            <el-option v-for="item in productSelectList" :key="item.id" :label="item.productName" :value="item.productId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="国家行政区 省" prop="provinceCode">
          <el-input v-model="form.provinceCode" />
        </el-form-item>
        <el-form-item label="国家行政区 市" prop="cityCode">
          <el-input v-model="form.cityCode" />
        </el-form-item>
        <el-form-item label="国家行政区 区" prop="areaCode">
          <el-input v-model="form.areaCode" />
        </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>
  </section>
</template>

<script>
import Region from "@/components/Region";
import {
  list,
  Info,
  add,
  update,
  del,
} from "@/api/hardware/device";
import { select as productSelect } from "@/api/hardware/product";
export default {
  name: "DeviceList",
  components: {
    Region,
  },
  data () {
    return {
      // 产品下拉列表
      productSelectList: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表格数据
      tableData: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      rules: {
        deviceId: [
          { required: true, message: "设备ID不能为空", trigger: "blur" },
        ],
        deviceName: [
          { required: true, message: "设备名称不能为空", trigger: "blur" },
        ],
        deviceType: [
          { required: true, message: "设备编码不能为空", trigger: "blur" },
        ],
        productId: [
          { required: true, message: "产品不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created () {
    productSelect().then(({ code, msg, select }) => {
      if (code == 200) {
        this.productSelectList = select;
      } else {
        this.$message.error(msg);
      }
    })
    this.getList();
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 获取设备数据列表
    getList () {
      list(this.queryParams).then((response) => {
        this.tableData = response.rows;
        this.total = response.total;
      });
    },

    // 取消按钮
    cancel () {
      this.open = false;
      // this.reset();
    },

    // 表单重置
    reset () {
      this.form = {
        deviceId: "",
        deviceType: "",
        deviceName: "",
        status: "0",
      };
      this.resetForm("form");
    },

    // 多选框选中数据
    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();
      this.form = row;
      this.open = true;
      this.title = "修改设备";
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          // debugger
          if (this.form.id != undefined) {
            update(this.form).then(res => {
              if (200 == res.code) {
                this.msgSuccess(this.title + "成功");
                this.open = false;
                this.getList();
              } else {
                this.msgSuccess(res.msg);
              }

            });
          } else {
            add(this.form).then(res => {
              if (200 == res.code) {
                this.msgSuccess(this.title + "成功");
                this.open = false;
                this.getList();
              } else {
                this.msgSuccess(res.msg);
              }
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return del(ids);
      }).catch(() => { }).then(res => {
        if (200 == res.code) {
          this.getList();
          this.msgSuccess("删除成功");
        } else {
          this.msgSuccess(res.msg);
        }
      }).catch(res => { })
    },

    // 页码尺寸改变
    handleSizeChange (val) {
      this.queryForm.pagesize = val;
      this.getList();
    },
    // 页码改变
    handleCurrentChange (val) {
      this.queryForm.page = val;
      this.getList();
    },
    // handleSelectionChange(val) {
    //   this.multipleSelection = val;
    // },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`);
    },
    // 对话框的关闭
    handleClose (done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => { });
    },
  },
};
</script>


<style></style>
