<!-- 装备管理 -->
<template>
  <Container :query-form="queryForm" class="three-model-container">
    <!-- 查询表单 -->
    <template #query-form>
      <el-form-item label="装备名称">
        <el-input
          v-model="queryForm.modelName"
          placeholder="输入装备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="装备类型">
        <el-select v-model="queryForm.modelType" placeholder="全部" clearable>
          <el-option label="第一" value="building"/>
          <el-option label="第二" value="equipment"/>
          <el-option label="第三" value="vehicle"/>
        </el-select>
      </el-form-item>
    </template>

    <!-- 顶部操作 -->
    <template #header-actions>
      <el-button type="primary" icon="el-icon-search" @click="handleQuery">查询</el-button>
      <el-button @click="resetQuery">重置</el-button>
      <el-button type="primary" icon="el-icon-plus" @click="openAdd">新建</el-button>
    </template>

    <!-- 表格 -->
    <div class="table-container">
      <el-table
        ref="table"
        :data="tableData"
        border
        stripe
        size="mini"
        highlight-current-row
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"/>
        <el-table-column type="index" label="序号" width="60"/>
        <el-table-column prop="zbTypeName" label="所属装备类型" width="120" align="center"/>
        <el-table-column prop="zbModelName" label="所属装备型号名称" width="180" align="center" show-overflow-tooltip/>
        <el-table-column prop="zbName" label="装备名称" min-width="180" show-overflow-tooltip/>
        <el-table-column prop="zbCode" label="装备编号" width="120" align="center"/>
        <el-table-column prop="ipAddress" label="IP地址" width="120" align="center"/>
        <el-table-column prop="port" label="端口" width="100" align="center"/>
        <el-table-column prop="communicationType" label="通讯方式" width="100" align="center">
          <template slot-scope="{ row }">
            <span v-if="row.communicationType === '0'">PDXP</span>
            <span v-if="row.communicationType === '1'">UDP</span>
            <span v-else>TCP</span>
          </template>
        </el-table-column>
        <el-table-column prop="connectionStatus" label="连通状态" width="100" align="center">
          <template slot-scope="{ row }">
            <span v-if="row.connectionStatus === '0'">断开</span>
            <span v-if="row.connectionStatus === '1'">连接</span>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" align="center" sortable/>
        <el-table-column prop="userName" label="创建人" align="center" sortable/>
        <el-table-column label="操作" fixed="right" align="center">
          <template slot-scope="{ row }">
            <div class="action-bar">
              <el-link type="primary" :underline="false" @click="handleEdit(row)">编辑</el-link>
              <el-link type="danger" :underline="false" @click="handleDelete(row)">删除</el-link>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          background
          :current-page="pagination.currentPage"
          :page-sizes="[10, 20, 50]"
          :page-size="pagination.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <!-- 底部操作 -->
    <!--<template #footer-actions>-->
    <!--  <el-button icon="el-icon-download" @click="exportList">导出清单</el-button>-->
    <!--  <span class="footer-tips">选中 {{ selectedRows.length }} 项</span>-->
    <!--</template>-->

    <!-- 新增装备弹窗 -->
    <el-dialog
      :title="addOrUpdateTitle"
      :visible.sync="addVisible"
      width="720px"
      :close-on-click-modal="false"
      class="model-dialog"
      @closed="onAddClosed"
    >
      <el-form ref="addFormRef" :model="addForm" :rules="addRules" label-width="110px" size="small">
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item label="所属装备类型" prop="zbTypeId">
              <el-select v-model="addForm.zbTypeId" placeholder="请选择所属装备类型">
                <el-option v-for="(item, index) in zbTypeList" :key="index" :label="item.zbTypeName" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属装备型号" prop="zbModelId">
              <el-select v-model="addForm.zbModelId" placeholder="请输入所属装备型号名称">
                <el-option v-for="(item, index) in zbModelList" :key="index" :label="item.zbModelName" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="装备名称" prop="zbName">
              <el-input v-model="addForm.zbName" placeholder="请输入装备名称" />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="装备编号" prop="zbCode">
              <el-input v-model="addForm.zbCode" placeholder="请输入装备编号" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="IP地址" prop="ipAddress">
              <el-input
                v-model.trim="addForm.ipAddress"
                placeholder="请输入IP地址"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="端口号" prop="port">
              <el-input
                v-model.trim="addForm.port"
                placeholder="请输入端口号"
              />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="通讯方式" prop="communicationType">
              <el-select v-model="addForm.communicationType">
                <el-option value="0" label="PDXP">PDXP</el-option>
                <el-option value="1" label="UDP">UDP</el-option>
                <el-option value="2" label="TCP">TCP</el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="connectionStatus">
              <el-select v-model="addForm.connectionStatus">
                <el-option :value="0" label="断开"></el-option>
                <el-option :value="1" label="连接"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="addVisible=false">取 消</el-button>
        <el-button type="primary" @click="handleAddSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </Container>
</template>

<script>
import {getDataList, addEqu, editEqu, delEqu, getEquDetails, getAllEquTypes, getAllEquModels} from '@/api/equipment'

export default {
  name: 'equipment',
  data() {
    return {
      addOrUpdateTitle: '新建',
      // 查询
      queryForm: {modelName: '', modelType: '', modelCategory: ''},

      // 列表&分页
      tableData: [],
      allData: [],
      selectedRows: [],
      pagination: {currentPage: 1, pageSize: 10, total: 0},

      // 新增弹窗
      addVisible: false,
      addForm: {
        id: '',               // 主键（编辑时必填）
        zbTypeId: '',       // 所属装备类型
        zbModelId: '',      // 所属装备型号名称
        zbName: '',        // 装备名称
        zbCode: '',           // 装备编号
        ipAddress: '',        // IP地址
        port: '',             // 端口号
        communicationType: '',// 通讯方式（默认PDXP）
      },
      addRules: {
      },
      zbTypeList: [],
      zbModelList: [],
    }
  },
  created() {
    this.handleQuery()
    this.getTypes()
    this.getModels()
  },
  methods: {
    // 获取所有装备类型
    getTypes() {
      getAllEquTypes().then(res => {
        if (res.code !==0) {
          return
        }
        this.zbTypeList = res.data
      })
    },
    // 获取所有装备型号
    getModels() {
      getAllEquModels().then(res => {
        if (res.code !==0) {
          return
        }
        this.zbModelList = res.data
      })
    },
    /* ====== 查询&分页 ====== */
    handleQuery() {
      let param = {
        pageNo: this.pagination.currentPage,
        pageSize: this.pagination.pageSize,
        // ...this.queryForm
      }
      getDataList(param).then(res => {
        if (res.code !== 0) {
          return
        }
        this.tableData = res.data.rows || []
        this.pagination.total = res.data.total
        console.log(res)
      }).catch(() => {
      })
    },
    resetQuery() {
      this.queryForm = {modelName: '', modelType: '', modelCategory: ''}
      this.pagination.currentPage = 1
      this.handleQuery()
    },
    handleSizeChange(size) {
      this.pagination.pageSize = size
      this.pagination.currentPage = 1
      this.handleQuery()
    },
    handleCurrentChange(p) {
      this.pagination.currentPage = p
      this.handleQuery()
    },

    /* ====== 表格操作 ====== */
    handleSelectionChange(val) {
      this.selectedRows = val
    },
    handleEdit(row) {
      this.addForm = {...row}
      // getEquDetails(row.id).then(res => {
      //   console.log(res)
      // })
      this.addOrUpdateTitle = '编辑'
      this.addVisible = true
    },
    togglePublish(row) {
      row.modelCategory = row.modelCategory === 'published' ? 'draft' : 'published'
      this.$message.success('状态已更新')
    },
    handleDelete(row) {
      // this.allData = this.allData.filter(r => r.code !== row.code)
      delEqu(row.id).then(res => {
        if (res.code !== 0) {
          return
        }
        this.$message.success('删除成功')
        this.handleQuery()
      }).catch(() => {
      })
    },
    /* ====== 新增或修改弹窗 ====== */
    openAdd() {
      this.addForm = {
        zbTypeId: '',       // 所属装备类型
        zbModelId: '',      // 所属装备型号名称
        zbName: '',        // 装备名称
        zbCode: '',           // 装备编号
        ipAddress: '',        // IP地址
        port: '',             // 端口号
        communicationType: '',// 通讯方式
        connectionStatus: '',
      }
      this.addOrUpdateTitle = '新建'
      this.addVisible = true
      this.$nextTick(() => {
        this.$refs.addFormRef && this.$refs.addFormRef.clearValidate()
        this.$refs.uploader && this.$refs.uploader.clearFiles()
      })
    },
    onAddClosed() {
      this.$refs.addFormRef && this.$refs.addFormRef.resetFields()
    },
    handleAddSubmit() {
      this.$refs.addFormRef.validate(valid => {
        if (!valid) return
        (this.addOrUpdateTitle === '编辑' ? editEqu : addEqu)(this.addForm).then(res => {
          if (res.code !== 0) {
            return
          }
          this.$message({
            message: '操作成功',
            type: 'info',
            duration: 500,
            onClose: () => {
              this.pagination.currentPage = 1
              this.addVisible = false
              this.handleQuery()
            }
          })
        })
      })
    },
  },
}
</script>

<style>
/* 统一表格暗色风格，消除白色条纹和容器 */
.three-model-container .el-table,
.three-model-container .el-table__fixed,
.three-model-container .el-table__fixed-right {
  background: rgba(15, 31, 56, 0.86) !important;
}

.three-model-container .el-table__body-wrapper,
.three-model-container .el-table__fixed-body-wrapper {
  background: rgba(15, 31, 56, 0.86) !important;
}

.three-model-container .el-table td,
.three-model-container .el-table th.is-leaf {
  background: rgba(15, 31, 56, 0.86) !important;
  border-color: rgba(255, 255, 255, 0.08) !important;
  color: #e6efff;
}

.three-model-container .el-table th {
  background: rgba(30, 55, 95, 0.85) !important;
  color: #cfd9e6 !important;
}

.three-model-container .el-table--striped .el-table__body tr.el-table__row--striped td {
  background: rgba(15, 31, 56, 0.9) !important;
}

.three-model-container .el-table__body tr:hover > td {
  background: rgba(30, 64, 175, 0.25) !important;
}

.three-model-container .el-table__current-row > td {
  background: rgba(64, 150, 255, 0.2) !important;
}

.three-model-container .el-table__fixed::before,
.three-model-container .el-table__fixed-right::before,
.three-model-container .el-table::before {
  background-color: rgba(255, 255, 255, 0.08) !important;
}

.three-model-container .el-table__gutter,
.three-model-container .el-table__fixed-right .el-table__fixed-header-wrapper,
.three-model-container .el-table__fixed-right .el-table__fixed-body-wrapper,
.three-model-container .el-table__fixed .el-table__fixed-header-wrapper,
.three-model-container .el-table__fixed .el-table__fixed-body-wrapper {
  background: rgba(15, 31, 56, 0.86) !important;
}

.pagination-container {
  margin-top: 14px;
  display: flex;
  justify-content: flex-end;
}

.action-bar {
  display: flex;
  justify-content: space-around;
  width: 100%;
}

.model-dialog .el-dialog {
  background: #0b254a;
  border: 1px solid #364a64;
}

.model-dialog .el-dialog__title {
  color: #e6efff;
}

.model-dialog .el-dialog__header,
.model-dialog .el-dialog__footer {
  border-color: #364a64;
}

.model-dialog .el-upload-list__item {
  background: rgba(255, 255, 255, 0.04);
  border-color: #364a64;
  color: #e6efff;
}

.footer-tips {
  color: #94a3b8;
  margin-left: 10px;
}
</style>
