<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="auto">
      <el-row :gutter="20">
        <el-form-item label="情报板路段" prop="boardRoadSection">
          <el-col :span="4">
            <treeselect v-model="queryParams.boardRoadSection" :options="roadGroupOptions"
                        placeholder="请选择归属路段" style="width: 200px" />
          </el-col>
        </el-form-item>
        <el-form-item label="情报板名称" prop="boardName">
          <el-input
            v-model="queryParams.boardName"
            placeholder="请输入情报板名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="情报板工桩号" prop="boardMileage">
          <el-input
            v-model="queryParams.boardMileage"
            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-row>

    </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="['board:info: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="['board:info: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="['board:info: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="['board:info:export']"-->
      <!--        >导出-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange" append-to-body>
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="唯一编号" align="center" prop="id"/>
      <el-table-column label="情报板名称" align="center" prop="boardName"/>
      <el-table-column label="情报板路段" align="center" prop="roadName"/>
      <el-table-column label="情报板方向" align="center" prop="boardDirection"/>
      <el-table-column label="情报板工桩号" align="center" prop="boardMileage"/>
      <el-table-column label="情报板尺寸" align="center" prop="boardSize">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.board_size" :value="scope.row.boardSize"/>
          {{ scope.row.boardSize }}
        </template>
      </el-table-column>
      <el-table-column label="情报板品牌" align="center" prop="boardBrand"/>
      <el-table-column label="情报板通讯协议" align="center" prop="boardCommunicationProtocol">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.board_protocol" :value="scope.row.boardCommunicationProtocol"/>
        </template>
      </el-table-column>
      <el-table-column label="情报板IP" align="center" prop="boardIp"/>
      <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="['board:info:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['board:info: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" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="auto">
        <el-form-item label="情报板名称" prop="boardName">
          <el-input v-model="form.boardName" placeholder="请输入情报板名称"/>
        </el-form-item>
        <el-form-item label="情报板路段" prop="boardRoadSection">
          <treeselect v-model="form.boardRoadSection" :options="roadGroupOptions" placeholder="请选择归属路段"
                      style="width: 200px"/>
        </el-form-item>
        <el-form-item label="情报板方向" prop="boardDirection">
          <el-input v-model="form.boardDirection" placeholder="请输入情报板方向"/>
        </el-form-item>
        <el-form-item label="情报板工桩号" prop="boardMileage">
          <el-input v-model="form.boardMileage" placeholder="请输入情报板工桩号"/>
        </el-form-item>
        <el-form-item label="情报板尺寸" prop="boardSize">
          <el-select
            v-model="form.boardSize"
            placeholder="请选择"
          >
            <el-option
              v-for="dict in dict.type.board_size"
              :key="dict.value"
              :label="dict.label + ' ' + dict.value"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="情报板品牌" prop="boardBrand">
          <el-input v-model="form.boardBrand" placeholder="请输入情报板品牌"/>
        </el-form-item>
        <el-form-item label="情报板通讯协议" prop="boardCommunicationProtocol">
          <el-select
            v-model="form.boardCommunicationProtocol"
            placeholder="全部"
          >
            <el-option
              v-for="dict in dict.type.board_protocol"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="情报板IP" prop="boardIp">
          <el-input v-model="form.boardIp" placeholder="请输入情报板IP"/>
        </el-form-item>
        <el-form-item label="情报板端口号" prop="boardPort">
          <el-input-number :min="0" :max="65535" v-model="form.boardPort"
                           placeholder="填0为默认端口"/>
        </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 {listInfo, getInfo, delInfo, addInfo, updateInfo} from "@/api/board/info";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import TextPreview from "@/views/board/component/TextPreview.vue";
import {roadGroupTreeSelect} from "@/api/board/roadgroup";

export default {
  name: "Info",
  dicts: ['board_size', 'board_protocol'],
  components: {TextPreview, Treeselect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 部门树选项
      roadGroupOptions: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 情报板信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        boardName: null,
        boardRoadSection: null,
        boardDirection: null,
        boardMileage: null,
        boardSize: null,
        boardBrand: null,
        boardCommunicationProtocol: null,
        boardIp: null,
        boardPort: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        boardName: [
          {required: true, message: "情报板名称不能为空", trigger: "blur"}
        ],
        boardRoadSection: [
          {required: true, message: "情报板路段不能为空", trigger: "blur"}
        ],
        boardDirection: [
          {required: true, message: "情报板方向不能为空", trigger: "blur"}
        ],
        boardMileage: [
          {required: true, message: "情报板工桩号不能为空", trigger: "blur"}
        ],
        boardSize: [
          {required: true, message: "情报板尺寸不能为空", trigger: "blur"}
        ],
        boardBrand: [
          {required: true, message: "情报板品牌不能为空", trigger: "blur"}
        ],
        boardCommunicationProtocol: [
          {required: true, message: "情报板通讯协议不能为空", trigger: "blur"}
        ],
        boardIp: [
          {required: true, message: "情报板IP不能为空", trigger: "blur"},
          {
            pattern: /^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])$/,
            message: "请输入正确的 IP",
            trigger: "blur"
          }
        ],
        boardPort: [
          {required: true, message: "情报板端口号不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getRoadTree();
  },
  methods: {
    /** 查询情报板信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        boardName: null,
        boardRoadSection: null,
        boardDirection: null,
        boardMileage: null,
        boardSize: null,
        boardBrand: null,
        boardCommunicationProtocol: null,
        boardIp: null,
        boardPort: 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 = "添加情报板信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(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) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(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 delInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('board/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    },
    /** 查询部门下拉树结构 */
    getRoadTree() {
      roadGroupTreeSelect().then(response => {
        this.roadGroupOptions = response.data;
      });
    },
  }
};
</script>
