<!--
 * @Descripttion: (端口数据列表/odf_ports)
 * @Author: (admin)
 * @Date: (2025-08-06)
-->
<template>
  <div>
    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
      <el-form-item label="机房名称" prop="roomName">
        <el-input v-model="queryParams.roomName" placeholder="请输入机房名称" />
      </el-form-item>
      <el-form-item label="机架名称" prop="rackName">
        <el-input v-model="queryParams.rackName" placeholder="请输入机架名称" />
      </el-form-item>
      <el-form-item label="机框名称" prop="frameName">
        <el-input v-model="queryParams.frameName" placeholder="请输入机框名称" />
      </el-form-item>
      <el-form-item label="连接状态" prop="status">
        <el-radio-group v-model="queryParams.status">
          <el-radio>全部</el-radio>
          <el-radio v-for="item in options.odf_ports_status" :key="item.dictValue" :value="item.dictValue">{{ item.dictLabel }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input v-model="queryParams.remarks" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="光衰值（dB）" prop="opticalAttenuation">
        <el-input v-model="queryParams.opticalAttenuation" placeholder="请输入光衰值（dB）" />
      </el-form-item>
      <el-form-item label="历史故障记录" prop="historyRemarks">
        <el-input v-model="queryParams.historyRemarks" placeholder="请输入历史故障记录" />
      </el-form-item>
      <el-form-item label="光缆段信息" prop="opticalCableOffRemarks">
        <el-input v-model="queryParams.opticalCableOffRemarks" placeholder="请输入光缆段信息" />
      </el-form-item>
      <el-form-item>
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>
    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="15" class="mb10">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="download" @click="handleExport" v-hasPermi="['odfports:export']">
          {{ $t('btn.export') }}
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table
      :data="dataList"
      v-loading="loading"
      ref="table"
      border
      header-cell-class-name="el-table-header-cell"
      highlight-current-row
      @sort-change="sortChange">
      <el-table-column prop="id" label="Id" align="center" v-if="columns.showColumn('id')" />
      <el-table-column prop="name" label="端口名称" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('name')" />
      <el-table-column prop="roomId" label="机房ID" align="center" v-if="columns.showColumn('roomId')" />
      <el-table-column prop="roomName" label="机房名称" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('roomName')" />
      <el-table-column prop="rackId" label="机架ID" align="center" v-if="columns.showColumn('rackId')" />
      <el-table-column prop="rackName" label="机架名称" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('rackName')" />
      <el-table-column prop="frameId" label="框ID" align="center" v-if="columns.showColumn('frameId')" />
      <el-table-column prop="frameName" label="机框名称" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('frameName')" />
      <el-table-column prop="deptId" label="部门ID" align="center" v-if="columns.showColumn('deptId')" />
      <el-table-column prop="deptName" label="DeptName" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('deptName')" />
      <el-table-column prop="rowNumber" label="行号（1-6）" align="center" v-if="columns.showColumn('rowNumber')" />
      <el-table-column prop="portNumber" label="端口号（1-12）" align="center" v-if="columns.showColumn('portNumber')" />
      <el-table-column prop="status" label="连接状态" align="center" v-if="columns.showColumn('status')">
        <template #default="scope">
          <dict-tag :options="options.odf_ports_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column prop="remarks" label="备注" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('remarks')" />

      <el-table-column
        prop="opticalAttenuation"
        label="光衰值（dB）"
        align="center"
        :show-overflow-tooltip="true"
        v-if="columns.showColumn('opticalAttenuation')" />
      <el-table-column
        prop="historyRemarks"
        label="历史故障记录"
        align="center"
        :show-overflow-tooltip="true"
        v-if="columns.showColumn('historyRemarks')" />
      <el-table-column
        prop="opticalCableOffRemarks"
        label="光缆段信息"
        align="center"
        :show-overflow-tooltip="true"
        v-if="columns.showColumn('opticalCableOffRemarks')" />
      <el-table-column prop="createdAt" label="创建时间" :show-overflow-tooltip="true" v-if="columns.showColumn('createdAt')" />
      <el-table-column prop="updatedAt" label="修改时间" :show-overflow-tooltip="true" v-if="columns.showColumn('updatedAt')" />
      <el-table-column label="操作" width="160">
        <template #default="scope">
          <el-button type="primary" size="small" icon="view" title="详情" @click="handlePreview(scope.row)"></el-button>
          <el-button
            type="success"
            size="small"
            icon="edit"
            title="编辑"
            v-hasPermi="['odfports:edit']"
            @click="handleUpdate(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <el-dialog :title="title" :lock-scroll="false" v-model="open">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :lg="12">
            <el-form-item label="端口名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入端口名称" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="机房ID" prop="roomId">
              <el-input v-model.number="form.roomId" placeholder="请输入机房ID" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="机房名称" prop="roomName">
              <el-input v-model="form.roomName" placeholder="请输入机房名称" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="机架ID" prop="rackId">
              <el-input v-model.number="form.rackId" placeholder="请输入机架ID" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="机架名称" prop="rackName">
              <el-input v-model="form.rackName" placeholder="请输入机架名称" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="框ID" prop="frameId">
              <el-input v-model.number="form.frameId" placeholder="请输入框ID" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="机框名称" prop="frameName">
              <el-input v-model="form.frameName" placeholder="请输入机框名称" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="部门ID" prop="deptId">
              <el-input v-model.number="form.deptId" placeholder="请输入部门ID" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="DeptName" prop="deptName">
              <el-input v-model="form.deptName" placeholder="请输入DeptName" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="行号（1-6）" prop="rowNumber">
              <el-input v-model.number="form.rowNumber" placeholder="请输入行号（1-6）" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="端口号（1-12）" prop="portNumber">
              <el-input v-model.number="form.portNumber" placeholder="请输入端口号（1-12）" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="连接状态，0正常，1断开" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio v-for="item in options.odf_ports_status" :key="item.dictValue" :value="parseInt(item.dictValue)">
                  {{ item.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="备注" prop="remarks">
              <el-input v-model="form.remarks" placeholder="请输入备注" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="光衰值（dB）" prop="opticalAttenuation">
              <el-input v-model="form.opticalAttenuation" placeholder="请输入光衰值（dB）" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="历史故障记录" prop="historyRemarks">
              <el-input v-model="form.historyRemarks" placeholder="请输入历史故障记录" />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="光缆段信息" prop="remarks">
              <el-input v-model="form.opticalCableOffRemarks" placeholder="光缆段信息" />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="创建时间" prop="createdAt">
              <el-date-picker v-model="form.createdAt" type="datetime" placeholder="选择日期时间" value-format="YYYY-MM-DD HH:mm:ss">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="修改时间" prop="updatedAt">
              <el-date-picker v-model="form.updatedAt" type="datetime" placeholder="选择日期时间" value-format="YYYY-MM-DD HH:mm:ss">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer v-if="opertype != 3">
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" :loading="state.submitLoading" @click="submitForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="odfports">
import { listOdfPortss, updateOdfPorts, getOdfPorts } from '@/api/business/odfports.js'
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sort: 'Id',
  sortType: 'desc',
  roomName: undefined,
  rackName: undefined,
  frameName: undefined,
  status: undefined,
  remarks: undefined,
  opticalAttenuation: undefined,
  historyRemarks: undefined
})
const columns = ref([
  { visible: true, align: 'center', type: '', prop: 'id', label: 'Id' },
  { visible: true, align: 'center', type: '', prop: 'name', label: '端口名称', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'roomId', label: '机房ID' },
  { visible: true, align: 'center', type: '', prop: 'roomName', label: '机房名称', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'rackId', label: '机架ID' },
  { visible: true, align: 'center', type: '', prop: 'rackName', label: '机架名称', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'frameId', label: '机框ID' },
  { visible: true, align: 'center', type: '', prop: 'frameName', label: '机框名称', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'deptId', label: '部门ID' },
  { visible: false, align: 'center', type: '', prop: 'deptName', label: '所属部门', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'rowNumber', label: '行号' },
  { visible: false, align: 'center', type: '', prop: 'portNumber', label: '端口号' },
  { visible: true, align: 'center', type: 'dict', prop: 'status', label: '连接状态', dictType: 'odf_ports_status' },
  { visible: true, align: 'center', type: '', prop: 'remarks', label: '备注', showOverflowTooltip: true },
  { visible: true, align: 'center', type: '', prop: 'opticalCableOffRemarks', label: '光缆段信息', showOverflowTooltip: true },
  { visible: true, align: 'center', type: '', prop: 'opticalAttenuation', label: '光衰值（dB）', showOverflowTooltip: true },
  { visible: true, align: 'center', type: '', prop: 'historyRemarks', label: '历史故障记录', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'createdAt', label: '创建时间', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'updatedAt', label: '修改时间', showOverflowTooltip: true }
  //{ visible: false, prop: 'actions', label: '操作', type: 'slot', width: '160' }
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])

var dictParams = ['odf_ports_status']

proxy.getDicts(dictParams).then((response) => {
  response.data.forEach((element) => {
    state.options[element.dictType] = element.list
  })
})

function getList() {
  loading.value = true
  listOdfPortss(queryParams).then((res) => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}

// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}
// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order
  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
  single: true,
  multiple: true,
  submitLoading: false,
  form: {},
  rules: {
    name: [{ required: true, message: '端口名称不能为空', trigger: 'blur' }],
    roomId: [{ required: true, message: '机房ID不能为空', trigger: 'blur', type: 'number' }],
    roomName: [{ required: true, message: '机房名称不能为空', trigger: 'blur' }],
    rackId: [{ required: true, message: '机架ID不能为空', trigger: 'blur', type: 'number' }],
    rackName: [{ required: true, message: '机架名称不能为空', trigger: 'blur' }],
    frameId: [{ required: true, message: '框ID不能为空', trigger: 'blur', type: 'number' }],
    frameName: [{ required: true, message: '机框名称不能为空', trigger: 'blur' }],
    deptId: [{ required: true, message: '部门ID不能为空', trigger: 'blur', type: 'number' }],
    rowNumber: [{ required: true, message: '行号（1-6）不能为空', trigger: 'blur', type: 'number' }],
    portNumber: [{ required: true, message: '端口号（1-12）不能为空', trigger: 'blur', type: 'number' }],
    status: [{ required: true, message: '连接状态，0正常，1断开不能为空', trigger: 'blur', type: 'number' }],
    createdAt: [{ required: true, message: '创建时间不能为空', trigger: 'blur' }],
    updatedAt: [{ required: true, message: '修改时间不能为空', trigger: 'blur' }]
  },
  options: {
    // 连接状态，0正常，1断开 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
    odf_ports_status: []
  }
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel() {
  open.value = false
  reset()
}

// 重置表单
function reset() {
  form.value = {
    id: null,
    name: null,
    roomId: null,
    roomName: null,
    rackId: null,
    rackName: null,
    frameId: null,
    frameName: null,
    deptId: null,
    deptName: null,
    rowNumber: null,
    portNumber: null,
    status: null,
    remarks: null,
    opticalAttenuation: null,
    historyRemarks: null,
    createdAt: null,
    updatedAt: null
  }
  proxy.resetForm('formRef')
}

/**
 * 查看
 * @param {*} row
 */
function handlePreview(row) {
  reset()
  const id = row.id
  getOdfPorts(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = '查看'
      opertype.value = 3
      form.value = {
        ...data
      }
    }
  })
}

// 修改按钮操作
function handleUpdate(row) {
  reset()
  state.submitLoading = false
  const id = row.id || ids.value
  getOdfPorts(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = '修改端口数据列表'
      opertype.value = 2

      form.value = {
        ...data
      }
    }
  })
}

// 添加&修改 表单提交
function submitForm() {
  proxy.$refs['formRef'].validate((valid) => {
    if (valid) {
      state.submitLoading = true

      if (form.value.id != undefined && opertype.value === 2) {
        updateOdfPorts(form.value).then((res) => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
      }
    }
  })
}
// 导出按钮操作
function handleExport() {
  proxy
    .$confirm('是否确认导出端口数据列表数据项?', '警告', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    .then(async () => {
      await proxy.downFile('/business/OdfPorts/exports', { ...queryParams })
    })
}
handleQuery()
</script>
