<template>
  <div>
    <el-row>
      <el-col :span="2" style="padding-left: 10px; padding-right: 10px">
        <div class="grid-content bg-purple">
          <el-menu
            default-active="2"
            class="el-menu-vertical-demo"
            aria-disabled
          >
            <el-submenu index="1">
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>组织一</span>
              </template>
              <el-menu-item-group>
                <el-menu-item index="1-1">选项1</el-menu-item>
                <el-menu-item index="1-2">选项2</el-menu-item>
                <el-menu-item index="1-3">选项3</el-menu-item>
              </el-menu-item-group>
            </el-submenu>
            <el-submenu index="2">
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>组织二</span>
              </template>
              <el-menu-item-group>
                <el-menu-item index="1-1">选项1</el-menu-item>
                <el-menu-item index="1-2">选项2</el-menu-item>
                <el-menu-item index="1-3">选项3</el-menu-item>
              </el-menu-item-group>
            </el-submenu>
          </el-menu>
        </div>
      </el-col>
      <el-col :span="22">
        <el-form :inline="true" size="mini">
          <el-form-item style="width: 100px">
            <el-select v-model="value" clearable placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item style="width: 100px">
            <el-select v-model="value" clearable placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item style="width: 100px">
            <el-select v-model="value" clearable placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item style="width: 100px">
            <el-select v-model="value" clearable placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item style="width: 100px">
            <el-select v-model="value" clearable placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item style="width: 100px">
            <el-select v-model="value" clearable placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item style="width: 100px">
            <el-select v-model="value" clearable placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item style="width: 100px">
            <el-select v-model="value" clearable placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item style="width: 100px">
            <el-select v-model="value" clearable placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div>
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-plus"
            @click="handleAdd"
            v-hasPermi="['system:device:add']"
            >增加</el-button
          >
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:device:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:device:remove']"
            >删除</el-button
          >
          <el-button size="mini" type="primary" icon="el-icon-refresh-right"
            >刷新</el-button
          >
          <el-button size="mini" type="primary" icon="el-icon-search"
            >查看</el-button
          >
          <el-button size="mini" type="primary" icon="el-icon-download"
            >导出</el-button
          >
          <el-button size="mini" type="primary" icon="el-icon-search"
            >设备批量导入</el-button
          >
          <el-button size="mini" type="primary" icon="el-icon-search"
            >批量域名设置</el-button
          >
          <el-button size="mini" type="primary" icon="el-icon-edit"
            >配置工单处理人</el-button
          >
          <el-button size="mini" type="primary" icon="el-icon-edit"
            >运营状态变更</el-button
          >
          <el-button size="mini" type="primary" icon="el-icon-search"
            >查询</el-button
          >
          <el-button size="mini" type="primary" icon="el-icon-refresh-right"
            >重置</el-button
          >
        </div>
        <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="city" 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>
        <el-pagination
          style="float: right"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="queryParams.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </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="provinceCode">
          <el-input v-model="form.provinceCode" />
        </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>
  </div>
</template>

<script>
import {
  listDevice,
  getDevice,
  addDevice,
  updateDevice,
  delDevice,
} from "@/api/hardware/device";
export default {
  name: "Device",
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      labelPosition: "right",
      options: [
        {
          value: "选项1",
        },
        {
          value: "选项2",
        },
      ],
      // 表单参数
      form: {deviceId: ''},
      // 总条数
      total: 0,
      value: "",
      // 表格数据
      tableData: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceId: undefined,
        deviceType: undefined,
        deviceName: undefined,
        deviceCode: undefined,
        deviceModel: undefined,
        provinceCode: undefined,
        areaCode: undefined,
      },
      rules: {
        deviceId: [
          { required: true, message: "设备ID不能为空", trigger: "blur" },
        ],
        deviceName: [
          { required: true, message: "设备名称不能为空", trigger: "blur" },
        ],
        deviceType: [
          { required: true, message: "设备编码不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 获取设备数据列表
    getList() {
      listDevice().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.deviceId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      // debugger
      // this.deviceId=[]
      const deviceId = row.deviceId || this.ids;
      debugger
      getDevice(deviceId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          // debugger
          if (this.form.deviceId != undefined) {
            updateDevice(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
          addDevice(this.form).then((response) => {
            this.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const deviceIds = row.deviceId || this.ids;
      this.$confirm('是否确认删除岗位编号为"' + deviceIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDevice(deviceIds);
        }).then((res) => {
          console.log(res)
          this.getList();
          this.msgSuccess("删除成功");
        })
    },

    // 页码尺寸改变
    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>