<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="操作编号" prop="operationNo">
        <el-input
          v-model="queryParams.operationNo"
          placeholder="请输入操作编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业务单据ID" prop="businessId">
        <el-input
          v-model="queryParams.businessId"
          placeholder="请输入业务单据ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业务单号" prop="businessNo">
        <el-input
          v-model="queryParams.businessNo"
          placeholder="请输入业务单号"
          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="操作位置" prop="operationLocation">
        <el-input
          v-model="queryParams.operationLocation"
          placeholder="请输入操作位置"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备ID" prop="deviceId">
        <el-input
          v-model="queryParams.deviceId"
          placeholder="请输入设备ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="天线ID" prop="antennaId">
        <el-input
          v-model="queryParams.antennaId"
          placeholder="请输入天线ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作人ID" prop="operatorId">
        <el-input
          v-model="queryParams.operatorId"
          placeholder="请输入操作人ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="操作时长" prop="operationDuration">
        <el-input
          v-model="queryParams.operationDuration"
          placeholder="请输入操作时长"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总标签数" prop="totalCards">
        <el-input
          v-model="queryParams.totalCards"
          placeholder="请输入总标签数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="成功标签数" prop="successCards">
        <el-input
          v-model="queryParams.successCards"
          placeholder="请输入成功标签数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="失败标签数" prop="failedCards">
        <el-input
          v-model="queryParams.failedCards"
          placeholder="请输入失败标签数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="成功率" prop="successRate">
        <el-input
          v-model="queryParams.successRate"
          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:wmsRfidBatchOperation:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ware:wmsRfidBatchOperation:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ware:wmsRfidBatchOperation:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['ware:wmsRfidBatchOperation:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wmsRfidBatchOperationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="操作ID" align="center" prop="operationId" />
      <el-table-column label="操作编号" align="center" prop="operationNo" />
      <el-table-column label="操作类型" align="center" prop="operationType" />
      <el-table-column label="业务单据ID" align="center" prop="businessId" />
      <el-table-column label="业务单号" align="center" prop="businessNo" />
      <el-table-column label="仓库ID" align="center" prop="warehouseId" />
      <el-table-column label="库区ID" align="center" prop="areaId" />
      <el-table-column label="操作位置" align="center" prop="operationLocation" />
      <el-table-column label="设备ID" align="center" prop="deviceId" />
      <el-table-column label="天线ID" align="center" prop="antennaId" />
      <el-table-column label="操作人ID" align="center" prop="operatorId" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作时长" align="center" prop="operationDuration" />
      <el-table-column label="总标签数" align="center" prop="totalCards" />
      <el-table-column label="成功标签数" align="center" prop="successCards" />
      <el-table-column label="失败标签数" align="center" prop="failedCards" />
      <el-table-column label="成功率" align="center" prop="successRate" />
      <el-table-column label="操作数据" align="center" prop="operationData" />
      <el-table-column label="结果数据" align="center" prop="resultData" />
      <el-table-column label="操作状态" align="center" prop="operationStatus" />
      <el-table-column label="错误信息" align="center" prop="errorMessage" />
      <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:wmsRfidBatchOperation:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['ware:wmsRfidBatchOperation: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="wmsRfidBatchOperationRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="操作编号" prop="operationNo">
          <el-input v-model="form.operationNo" placeholder="请输入操作编号" />
        </el-form-item>
        <el-form-item label="业务单据ID" prop="businessId">
          <el-input v-model="form.businessId" placeholder="请输入业务单据ID" />
        </el-form-item>
        <el-form-item label="业务单号" prop="businessNo">
          <el-input v-model="form.businessNo" placeholder="请输入业务单号" />
        </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="操作位置" prop="operationLocation">
          <el-input v-model="form.operationLocation" placeholder="请输入操作位置" />
        </el-form-item>
        <el-form-item label="设备ID" prop="deviceId">
          <el-input v-model="form.deviceId" placeholder="请输入设备ID" />
        </el-form-item>
        <el-form-item label="天线ID" prop="antennaId">
          <el-input v-model="form.antennaId" placeholder="请输入天线ID" />
        </el-form-item>
        <el-form-item label="操作人ID" prop="operatorId">
          <el-input v-model="form.operatorId" placeholder="请输入操作人ID" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="操作时长" prop="operationDuration">
          <el-input v-model="form.operationDuration" placeholder="请输入操作时长" />
        </el-form-item>
        <el-form-item label="总标签数" prop="totalCards">
          <el-input v-model="form.totalCards" placeholder="请输入总标签数" />
        </el-form-item>
        <el-form-item label="成功标签数" prop="successCards">
          <el-input v-model="form.successCards" placeholder="请输入成功标签数" />
        </el-form-item>
        <el-form-item label="失败标签数" prop="failedCards">
          <el-input v-model="form.failedCards" placeholder="请输入失败标签数" />
        </el-form-item>
        <el-form-item label="成功率" prop="successRate">
          <el-input v-model="form.successRate" placeholder="请输入成功率" />
        </el-form-item>
        <el-form-item label="错误信息" prop="errorMessage">
          <el-input v-model="form.errorMessage" type="textarea" 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="WmsRfidBatchOperation">
import { listWmsRfidBatchOperation, getWmsRfidBatchOperation, delWmsRfidBatchOperation, addWmsRfidBatchOperation, updateWmsRfidBatchOperation } from "@/api/ware/wmsRfidBatchOperation"

const { proxy } = getCurrentInstance()

const wmsRfidBatchOperationList = 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,
    operationNo: null,
    operationType: null,
    businessId: null,
    businessNo: null,
    warehouseId: null,
    areaId: null,
    operationLocation: null,
    deviceId: null,
    antennaId: null,
    operatorId: null,
    startTime: null,
    endTime: null,
    operationDuration: null,
    totalCards: null,
    successCards: null,
    failedCards: null,
    successRate: null,
    operationData: null,
    resultData: null,
    operationStatus: null,
    errorMessage: null,
  },
  rules: {
    operationNo: [
      { required: true, message: "操作编号不能为空", trigger: "blur" }
    ],
    operationType: [
      { required: true, message: "操作类型不能为空", trigger: "change" }
    ],
    warehouseId: [
      { required: true, message: "仓库ID不能为空", trigger: "blur" }
    ],
    operatorId: [
      { required: true, message: "操作人ID不能为空", trigger: "blur" }
    ],
  }
})

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

/** 查询RFID批量操作列表 */
function getList() {
  loading.value = true
  listWmsRfidBatchOperation(queryParams.value).then(response => {
    wmsRfidBatchOperationList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

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

// 表单重置
function reset() {
  form.value = {
    operationId: null,
    operationNo: null,
    operationType: null,
    businessId: null,
    businessNo: null,
    warehouseId: null,
    areaId: null,
    operationLocation: null,
    deviceId: null,
    antennaId: null,
    operatorId: null,
    startTime: null,
    endTime: null,
    operationDuration: null,
    totalCards: null,
    successCards: null,
    failedCards: null,
    successRate: null,
    operationData: null,
    resultData: null,
    operationStatus: null,
    errorMessage: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  }
  proxy.resetForm("wmsRfidBatchOperationRef")
}

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

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

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

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

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _operationId = row.operationId || ids.value
  getWmsRfidBatchOperation(_operationId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改RFID批量操作"
  })
}

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

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

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

getList()
</script>
