<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="天线编码" prop="antennaCode">
        <el-input
          v-model="queryParams.antennaCode"
          placeholder="请输入天线编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="读写器ID" prop="readerId">
        <el-input
          v-model="queryParams.readerId"
          placeholder="请输入读写器ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库ID" prop="warehouseId">
        <el-input
          v-model="queryParams.warehouseId"
          placeholder="请输入仓库ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库区ID" prop="areaId">
        <el-input
          v-model="queryParams.areaId"
          placeholder="请输入库区ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货架ID" prop="locationId">
        <el-input
          v-model="queryParams.locationId"
          placeholder="请输入货架ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="安装位置" prop="installationLocation">
        <el-input
          v-model="queryParams.installationLocation"
          placeholder="请输入安装位置"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="X坐标" prop="xCoordinate">
        <el-input
          v-model="queryParams.xCoordinate"
          placeholder="请输入X坐标"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Y坐标" prop="yCoordinate">
        <el-input
          v-model="queryParams.yCoordinate"
          placeholder="请输入Y坐标"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Z坐标" prop="zCoordinate">
        <el-input
          v-model="queryParams.zCoordinate"
          placeholder="请输入Z坐标"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="方向角度" prop="directionAngle">
        <el-input
          v-model="queryParams.directionAngle"
          placeholder="请输入方向角度"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="覆盖半径" prop="coverageRadius">
        <el-input
          v-model="queryParams.coverageRadius"
          placeholder="请输入覆盖半径"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="功率级别" prop="powerLevel">
        <el-input
          v-model="queryParams.powerLevel"
          placeholder="请输入功率级别"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="频率" prop="frequency">
        <el-input
          v-model="queryParams.frequency"
          placeholder="请输入频率"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最后检测时间" prop="lastCheckTime">
        <el-date-picker clearable
          v-model="queryParams.lastCheckTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择最后检测时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="读取次数" prop="readCount">
        <el-input
          v-model="queryParams.readCount"
          placeholder="请输入读取次数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="读取成功率" prop="readSuccessRate">
        <el-input
          v-model="queryParams.readSuccessRate"
          placeholder="请输入读取成功率"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="平均读取时间" prop="avgReadTime">
        <el-input
          v-model="queryParams.avgReadTime"
          placeholder="请输入平均读取时间"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['ware:wmsRfidAntenna:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ware:wmsRfidAntenna:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ware:wmsRfidAntenna:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['ware:wmsRfidAntenna:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wmsRfidAntennaList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="天线ID" align="center" prop="antennaId" />
      <el-table-column label="天线编码" align="center" prop="antennaCode" />
      <el-table-column label="读写器ID" align="center" prop="readerId" />
      <el-table-column label="仓库ID" align="center" prop="warehouseId" />
      <el-table-column label="库区ID" align="center" prop="areaId" />
      <el-table-column label="货架ID" align="center" prop="locationId" />
      <el-table-column label="安装位置" align="center" prop="installationLocation" />
      <el-table-column label="X坐标" align="center" prop="xCoordinate" />
      <el-table-column label="Y坐标" align="center" prop="yCoordinate" />
      <el-table-column label="Z坐标" align="center" prop="zCoordinate" />
      <el-table-column label="方向角度" align="center" prop="directionAngle" />
      <el-table-column label="覆盖半径" align="center" prop="coverageRadius" />
      <el-table-column label="天线状态" align="center" prop="status" />
      <el-table-column label="功率级别" align="center" prop="powerLevel" />
      <el-table-column label="频率" align="center" prop="frequency" />
      <el-table-column label="最后检测时间" align="center" prop="lastCheckTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.lastCheckTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="读取次数" align="center" prop="readCount" />
      <el-table-column label="读取成功率" align="center" prop="readSuccessRate" />
      <el-table-column label="平均读取时间" align="center" prop="avgReadTime" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['ware:wmsRfidAntenna:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['ware:wmsRfidAntenna:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改RFID天线对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="wmsRfidAntennaRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="天线编码" prop="antennaCode">
          <el-input v-model="form.antennaCode" placeholder="请输入天线编码" />
        </el-form-item>
        <el-form-item label="读写器ID" prop="readerId">
          <el-input v-model="form.readerId" placeholder="请输入读写器ID" />
        </el-form-item>
        <el-form-item label="仓库ID" prop="warehouseId">
          <el-input v-model="form.warehouseId" placeholder="请输入仓库ID" />
        </el-form-item>
        <el-form-item label="库区ID" prop="areaId">
          <el-input v-model="form.areaId" placeholder="请输入库区ID" />
        </el-form-item>
        <el-form-item label="货架ID" prop="locationId">
          <el-input v-model="form.locationId" placeholder="请输入货架ID" />
        </el-form-item>
        <el-form-item label="安装位置" prop="installationLocation">
          <el-input v-model="form.installationLocation" placeholder="请输入安装位置" />
        </el-form-item>
        <el-form-item label="X坐标" prop="xCoordinate">
          <el-input v-model="form.xCoordinate" placeholder="请输入X坐标" />
        </el-form-item>
        <el-form-item label="Y坐标" prop="yCoordinate">
          <el-input v-model="form.yCoordinate" placeholder="请输入Y坐标" />
        </el-form-item>
        <el-form-item label="Z坐标" prop="zCoordinate">
          <el-input v-model="form.zCoordinate" placeholder="请输入Z坐标" />
        </el-form-item>
        <el-form-item label="方向角度" prop="directionAngle">
          <el-input v-model="form.directionAngle" placeholder="请输入方向角度" />
        </el-form-item>
        <el-form-item label="覆盖半径" prop="coverageRadius">
          <el-input v-model="form.coverageRadius" placeholder="请输入覆盖半径" />
        </el-form-item>
        <el-form-item label="功率级别" prop="powerLevel">
          <el-input v-model="form.powerLevel" placeholder="请输入功率级别" />
        </el-form-item>
        <el-form-item label="频率" prop="frequency">
          <el-input v-model="form.frequency" placeholder="请输入频率" />
        </el-form-item>
        <el-form-item label="最后检测时间" prop="lastCheckTime">
          <el-date-picker clearable
            v-model="form.lastCheckTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择最后检测时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="读取次数" prop="readCount">
          <el-input v-model="form.readCount" placeholder="请输入读取次数" />
        </el-form-item>
        <el-form-item label="读取成功率" prop="readSuccessRate">
          <el-input v-model="form.readSuccessRate" placeholder="请输入读取成功率" />
        </el-form-item>
        <el-form-item label="平均读取时间" prop="avgReadTime">
          <el-input v-model="form.avgReadTime" placeholder="请输入平均读取时间" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="WmsRfidAntenna">
import { listWmsRfidAntenna, getWmsRfidAntenna, delWmsRfidAntenna, addWmsRfidAntenna, updateWmsRfidAntenna } from "@/api/ware/wmsRfidAntenna"

const { proxy } = getCurrentInstance()

const wmsRfidAntennaList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    antennaCode: null,
    readerId: null,
    warehouseId: null,
    areaId: null,
    locationId: null,
    installationLocation: null,
    xCoordinate: null,
    yCoordinate: null,
    zCoordinate: null,
    directionAngle: null,
    coverageRadius: null,
    status: null,
    powerLevel: null,
    frequency: null,
    lastCheckTime: null,
    readCount: null,
    readSuccessRate: null,
    avgReadTime: null,
  },
  rules: {
    antennaCode: [
      { required: true, message: "天线编码不能为空", trigger: "blur" }
    ],
    readerId: [
      { required: true, message: "读写器ID不能为空", trigger: "blur" }
    ],
    warehouseId: [
      { required: true, message: "仓库ID不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询RFID天线列表 */
function getList() {
  loading.value = true
  listWmsRfidAntenna(queryParams.value).then(response => {
    wmsRfidAntennaList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    antennaId: null,
    antennaCode: null,
    readerId: null,
    warehouseId: null,
    areaId: null,
    locationId: null,
    installationLocation: null,
    xCoordinate: null,
    yCoordinate: null,
    zCoordinate: null,
    directionAngle: null,
    coverageRadius: null,
    status: null,
    powerLevel: null,
    frequency: null,
    lastCheckTime: null,
    readCount: null,
    readSuccessRate: null,
    avgReadTime: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  }
  proxy.resetForm("wmsRfidAntennaRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.antennaId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加RFID天线"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _antennaId = row.antennaId || ids.value
  getWmsRfidAntenna(_antennaId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改RFID天线"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["wmsRfidAntennaRef"].validate(valid => {
    if (valid) {
      if (form.value.antennaId != null) {
        updateWmsRfidAntenna(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addWmsRfidAntenna(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _antennaIds = row.antennaId || ids.value
  proxy.$modal.confirm('是否确认删除RFID天线编号为"' + _antennaIds + '"的数据项？').then(function() {
    return delWmsRfidAntenna(_antennaIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('ware/wmsRfidAntenna/export', {
    ...queryParams.value
  }, `wmsRfidAntenna_${new Date().getTime()}.xlsx`)
}

getList()
</script>
