<template>
  <div class="app-container">
    <div class="btn-condition">
      <div class="condition-left" v-show="showSearch">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
        >
          <el-form-item label="网关名称" prop="gwName">
            <el-input
              v-model="queryParams.gwName"
              placeholder="请输入网关名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="网关状态"
              clearable
            >
              <el-option
                v-for="dict in dict.type.gateway_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div class="primary-btn">
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
        </div>
        <div class="cancel-btn">
          <el-button
            type="cancel"
            icon="el-icon-refresh"
            size="mini"
            @click="resetQuery"
            >重置</el-button
          >
        </div>
      </div>
    </div>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <div class="primary-btn" v-hasPermi="['device:gw:add']">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            >新增</el-button
          >
        </div>
      </el-col>
      <el-col :span="1.5">
        <div class="success-btn" v-hasPermi="['device:gw:edit']">
          <el-button
            type="success"
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            >修改</el-button
          >
        </div>
      </el-col>
      <el-col :span="1.5">
        <div class="delete-btn" v-hasPermi="['device:gw:remove']">
          <el-button
            type="delete"
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            >删除</el-button
          >
        </div>
      </el-col>
      <!-- <el-col :span="1.5">
        <div class="warning-btn" v-hasPermi="['system:post:export']">
          <el-button
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            >导出</el-button
          >
        </div>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="postList"
      @selection-change="handleSelectionChange"
      stripe
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="网关名称" align="center" prop="gwName" />
      <el-table-column label="网关IP" align="center" prop="gwIp" />
      <el-table-column label="安装位置" align="center" prop="gwAddr" />
      <el-table-column label="端口" align="center" prop="port" />
      <el-table-column label="采集周期" align="center" prop="collectionLoop" />
      <el-table-column label="连接时间" align="center" prop="connectTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.connectTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="卡号" align="center" prop="internetCard" />
      <el-table-column label="运营商" align="center" prop="operatorType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.operator_type"
            :value="scope.row.operatorType"
          />
        </template>
      </el-table-column>
      <el-table-column label="通信方式" align="center" prop="communicationType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.communication_type"
            :value="scope.row.communicationType"
          />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.gateway_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <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="['device:gw:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['device:gw: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"
    />

    <!-- 添加或修改网关对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body>
      <el-form ref="form" :model="form" :rules="rules">
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-form-item label="网关名称" prop="gwName">
              <el-input v-model="form.gwName" placeholder="请输入网关名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="网关IP" prop="gwIp">
              <el-input
                v-model="form.gwIp"
                placeholder="请输入网关Ip"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-form-item label="安装地址" prop="gwAddr">
              <el-input
                v-model="form.gwAddr"
                placeholder="请输入安装地址"
                clearable
              /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="端口" prop="port">
              <el-input
                v-model="form.port"
                placeholder="请输入端口"
                clearable
              /> </el-form-item
          ></el-col>
        </el-row>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-form-item label="采集周期" prop="collectionLoop">
              <el-input
                v-model="form.collectionLoop"
                placeholder="请输入采集周期"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="卡号" prop="internetCard">
              <el-input
                v-model="form.internetCard"
                placeholder="请输入卡号"
                clearable
              /> </el-form-item
          ></el-col>
        </el-row>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-form-item label="运营商" prop="operatorType">
              <el-select
                v-model="form.operatorType"
                placeholder="选择运营商"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.operator_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="通信方式" prop="communicationType">
              <el-select
                v-model="form.communicationType"
                placeholder="选择通信方式"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.communication_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select> </el-form-item
          ></el-col>
        </el-row>
        <el-row type="flex" justify="center" style="margin-bottom: 34px">
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
              /> </el-form-item
          ></el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  gatewayList,
  getGateway,
  delGateway,
  addGateway,
  updateGateway,
} from "@/api/device/gateway";

export default {
  name: "gateway",
  dicts: ["gateway_status", "operator_type", "communication_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 网关表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gwName: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        gwName: [
          { required: true, message: "网关名称不能为空", trigger: "blur" },
        ],
        gwIp: [{ required: true, message: "网关IP不能为空", trigger: "blur" }],
        gwAddr: [
          { required: true, message: "安装位置不能为空", trigger: "blur" },
        ],
        port: [{ required: true, message: "端口不能为空", trigger: "blur" }],
        collectionLoop: [
          { required: true, message: "采集周期不能为空", trigger: "blur" },
        ],
        internetCard: [
          { required: true, message: "卡号不能为空", trigger: "blur" },
        ],
        operatorType: [
          { required: true, message: "请选择运营商", trigger: "blur" },
        ],
        communicationType: [
          { required: true, message: "请选择通信方式", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询网关列表 */
    getList() {
      this.loading = true;
      gatewayList(this.queryParams).then((response) => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.form.id = "";
    },
    // 表单重置
    reset() {
      this.form = {
        gwName: undefined,
        gwIp: "",
        gwAddr: "",
        port: "",
        collectionLoop: "",
        internetCard: "",
        operatorType: "",
        communicationType: "",
        remark: undefined,
      };
      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 = "添加网关信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form.id = "";
      const gwId = row.id || this.ids;
      console.log("");
      getGateway(gwId).then((response) => {
        // this.form = response.data;
        this.form = {
          id: response.data.id,
          gwName: response.data.gwName,
          gwIp: response.data.gwIp,
          gwAddr: response.data.gwAddr,
          port: response.data.port,
          collectionLoop: response.data.collectionLoop,
          internetCard: response.data.internetCard,
          operatorType:
            response.data.operatorType !== null
              ? response.data.operatorType.toString()
              : null,
          communicationType:
            response.data.communicationType !== null
              ? response.data.communicationType.toString()
              : null,
          remark: response.data.remark,
        };
        this.open = true;
        this.title = "修改网关信息";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateGateway(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log("新增参数", this.form);
            addGateway(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 delGateway(Ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "device/gateway/export",
        {
          ...this.queryParams,
        },
        `post_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped lang="scss">
/* 媒体查询，适配大于2000px分辨率的大屏样式 - element样式 */
@media (max-width: 2000px) {
  ::v-deep .el-dialog {
    width: 700px !important;
  }

  ::v-deep .el-dialog .el-form-item__label {
    width: 110px !important;
  }
  ::v-deep .el-dialog .el-form-item {
    display: flex;
    flex-direction: row;
    align-items: center;
  }
}
@media (min-width: 2000px) {
  ::v-deep .el-dialog {
    width: 7.5rem !important;
  }

  ::v-deep .el-dialog .el-form-item__label {
    width: 1.1rem !important;
  }
  ::v-deep .el-dialog .el-form-item {
    display: flex;
    flex-direction: row;
    align-items: center;
  }
}
</style>

