<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="租户号" prop="tenantId">
        <el-input
          v-model="queryParams.tenantId"
          placeholder="请输入租户号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="乐观锁" prop="revision">
        <el-input
          v-model="queryParams.revision"
          placeholder="请输入乐观锁"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="查获记录ID" prop="recordId">
        <el-input
          v-model="queryParams.recordId"
          placeholder="请输入查获记录ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物品编码" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入物品编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数量" prop="quantity">
        <el-input
          v-model="queryParams.quantity"
          placeholder="请输入数量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图片URL" prop="imageUrl">
        <el-input
          v-model="queryParams.imageUrl"
          placeholder="请输入图片URL"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检查部位编码1级" prop="checkPositionCodeOne">
        <el-input
          v-model="queryParams.checkPositionCodeOne"
          placeholder="请输入检查部位编码1级"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否故意隐匿(1-是,0-否)" prop="isActiveConcealment">
        <el-input
          v-model="queryParams.isActiveConcealment"
          placeholder="请输入是否故意隐匿(1-是,0-否)"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检查部位编码2级" prop="checkPositionCodeTwo">
        <el-input
          v-model="queryParams.checkPositionCodeTwo"
          placeholder="请输入检查部位编码2级"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="具体位置" prop="checkPositionSpecific">
        <el-input
          v-model="queryParams.checkPositionSpecific"
          placeholder="请输入具体位置"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物品名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入物品名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物品分类编码1级" prop="categoryCodeOne">
        <el-input
          v-model="queryParams.categoryCodeOne"
          placeholder="请输入物品分类编码1级"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物品分类名称1级" prop="categoryNameOne">
        <el-input
          v-model="queryParams.categoryNameOne"
          placeholder="请输入物品分类名称1级"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物品分类编码2级" prop="categoryCodeTwo">
        <el-input
          v-model="queryParams.categoryCodeTwo"
          placeholder="请输入物品分类编码2级"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物品分类名称2级" prop="categoryNameTwo">
        <el-input
          v-model="queryParams.categoryNameTwo"
          placeholder="请输入物品分类名称2级"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input
          v-model="queryParams.unit"
          placeholder="请输入单位"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检查部位名称1级" prop="checkPositionNameOne">
        <el-input
          v-model="queryParams.checkPositionNameOne"
          placeholder="请输入检查部位名称1级"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检查部位名称2级" prop="checkPositionNameTwo">
        <el-input
          v-model="queryParams.checkPositionNameTwo"
          placeholder="请输入检查部位名称2级"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位名称" prop="unitDesc">
        <el-input
          v-model="queryParams.unitDesc"
          placeholder="请输入单位名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理方式" prop="handlingMethod">
        <el-input
          v-model="queryParams.handlingMethod"
          placeholder="请输入处理方式"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理方式名称" prop="handlingMethodDesc">
        <el-input
          v-model="queryParams.handlingMethodDesc"
          placeholder="请输入处理方式名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图片ID" prop="imageId">
        <el-input
          v-model="queryParams.imageId"
          placeholder="请输入图片ID"
          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="['item:items:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['item:items:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['item:items:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['item:items:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="itemsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="租户号" align="center" prop="tenantId" />
      <el-table-column label="乐观锁" align="center" prop="revision" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="查获记录ID" align="center" prop="recordId" />
      <el-table-column label="物品编码" align="center" prop="itemCode" />
      <el-table-column label="数量" align="center" prop="quantity" />
      <el-table-column label="图片URL" align="center" prop="imageUrl" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="检查部位编码1级" align="center" prop="checkPositionCodeOne" />
      <el-table-column label="是否故意隐匿(1-是,0-否)" align="center" prop="isActiveConcealment" />
      <el-table-column label="检查部位编码2级" align="center" prop="checkPositionCodeTwo" />
      <el-table-column label="具体位置" align="center" prop="checkPositionSpecific" />
      <el-table-column label="物品名称" align="center" prop="itemName" />
      <el-table-column label="物品分类编码1级" align="center" prop="categoryCodeOne" />
      <el-table-column label="物品分类名称1级" align="center" prop="categoryNameOne" />
      <el-table-column label="物品分类编码2级" align="center" prop="categoryCodeTwo" />
      <el-table-column label="物品分类名称2级" align="center" prop="categoryNameTwo" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="检查部位名称1级" align="center" prop="checkPositionNameOne" />
      <el-table-column label="检查部位名称2级" align="center" prop="checkPositionNameTwo" />
      <el-table-column label="单位名称" align="center" prop="unitDesc" />
      <el-table-column label="处理方式" align="center" prop="handlingMethod" />
      <el-table-column label="处理方式名称" align="center" prop="handlingMethodDesc" />
      <el-table-column label="图片ID" align="center" prop="imageId" />
      <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="['item:items:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['item:items: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"
    />

    <!-- 添加或修改查获物品明细对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="itemsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="租户号" prop="tenantId">
          <el-input v-model="form.tenantId" placeholder="请输入租户号" />
        </el-form-item>
        <el-form-item label="乐观锁" prop="revision">
          <el-input v-model="form.revision" placeholder="请输入乐观锁" />
        </el-form-item>
        <el-form-item label="查获记录ID" prop="recordId">
          <el-input v-model="form.recordId" placeholder="请输入查获记录ID" />
        </el-form-item>
        <el-form-item label="物品编码" prop="itemCode">
          <el-input v-model="form.itemCode" placeholder="请输入物品编码" />
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="图片URL" prop="imageUrl">
          <el-input v-model="form.imageUrl" placeholder="请输入图片URL" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="检查部位编码1级" prop="checkPositionCodeOne">
          <el-input v-model="form.checkPositionCodeOne" placeholder="请输入检查部位编码1级" />
        </el-form-item>
        <el-form-item label="是否故意隐匿(1-是,0-否)" prop="isActiveConcealment">
          <el-input v-model="form.isActiveConcealment" placeholder="请输入是否故意隐匿(1-是,0-否)" />
        </el-form-item>
        <el-form-item label="检查部位编码2级" prop="checkPositionCodeTwo">
          <el-input v-model="form.checkPositionCodeTwo" placeholder="请输入检查部位编码2级" />
        </el-form-item>
        <el-form-item label="具体位置" prop="checkPositionSpecific">
          <el-input v-model="form.checkPositionSpecific" placeholder="请输入具体位置" />
        </el-form-item>
        <el-form-item label="物品名称" prop="itemName">
          <el-input v-model="form.itemName" placeholder="请输入物品名称" />
        </el-form-item>
        <el-form-item label="物品分类编码1级" prop="categoryCodeOne">
          <el-input v-model="form.categoryCodeOne" placeholder="请输入物品分类编码1级" />
        </el-form-item>
        <el-form-item label="物品分类名称1级" prop="categoryNameOne">
          <el-input v-model="form.categoryNameOne" placeholder="请输入物品分类名称1级" />
        </el-form-item>
        <el-form-item label="物品分类编码2级" prop="categoryCodeTwo">
          <el-input v-model="form.categoryCodeTwo" placeholder="请输入物品分类编码2级" />
        </el-form-item>
        <el-form-item label="物品分类名称2级" prop="categoryNameTwo">
          <el-input v-model="form.categoryNameTwo" placeholder="请输入物品分类名称2级" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="检查部位名称1级" prop="checkPositionNameOne">
          <el-input v-model="form.checkPositionNameOne" placeholder="请输入检查部位名称1级" />
        </el-form-item>
        <el-form-item label="检查部位名称2级" prop="checkPositionNameTwo">
          <el-input v-model="form.checkPositionNameTwo" placeholder="请输入检查部位名称2级" />
        </el-form-item>
        <el-form-item label="单位名称" prop="unitDesc">
          <el-input v-model="form.unitDesc" placeholder="请输入单位名称" />
        </el-form-item>
        <el-form-item label="处理方式" prop="handlingMethod">
          <el-input v-model="form.handlingMethod" placeholder="请输入处理方式" />
        </el-form-item>
        <el-form-item label="处理方式名称" prop="handlingMethodDesc">
          <el-input v-model="form.handlingMethodDesc" placeholder="请输入处理方式名称" />
        </el-form-item>
        <el-form-item label="图片ID" prop="imageId">
          <el-input v-model="form.imageId" placeholder="请输入图片ID" />
        </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="Items">
import { listItems, getItems, delItems, addItems, updateItems } from "@/api/item/items"

const { proxy } = getCurrentInstance()

const itemsList = 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,
    tenantId: null,
    revision: null,
    recordId: null,
    itemCode: null,
    quantity: null,
    imageUrl: null,
    checkPositionCodeOne: null,
    isActiveConcealment: null,
    checkPositionCodeTwo: null,
    checkPositionSpecific: null,
    itemName: null,
    categoryCodeOne: null,
    categoryNameOne: null,
    categoryCodeTwo: null,
    categoryNameTwo: null,
    unit: null,
    checkPositionNameOne: null,
    checkPositionNameTwo: null,
    unitDesc: null,
    handlingMethod: null,
    handlingMethodDesc: null,
    imageId: null
  },
  rules: {
    recordId: [
      { required: true, message: "查获记录ID不能为空", trigger: "blur" }
    ],
    itemCode: [
      { required: true, message: "物品编码不能为空", trigger: "blur" }
    ],
    quantity: [
      { required: true, message: "数量不能为空", trigger: "blur" }
    ],
    checkPositionCodeOne: [
      { required: true, message: "检查部位编码1级不能为空", trigger: "blur" }
    ],
    isActiveConcealment: [
      { required: true, message: "是否故意隐匿(1-是,0-否)不能为空", trigger: "blur" }
    ],
    checkPositionCodeTwo: [
      { required: true, message: "检查部位编码2级不能为空", trigger: "blur" }
    ],
    checkPositionSpecific: [
      { required: true, message: "具体位置不能为空", trigger: "blur" }
    ],
    itemName: [
      { required: true, message: "物品名称不能为空", trigger: "blur" }
    ],
    categoryCodeOne: [
      { required: true, message: "物品分类编码1级不能为空", trigger: "blur" }
    ],
    categoryNameOne: [
      { required: true, message: "物品分类名称1级不能为空", trigger: "blur" }
    ],
    categoryCodeTwo: [
      { required: true, message: "物品分类编码2级不能为空", trigger: "blur" }
    ],
    categoryNameTwo: [
      { required: true, message: "物品分类名称2级不能为空", trigger: "blur" }
    ],
    unit: [
      { required: true, message: "单位不能为空", trigger: "blur" }
    ],
    checkPositionNameOne: [
      { required: true, message: "检查部位名称1级不能为空", trigger: "blur" }
    ],
    checkPositionNameTwo: [
      { required: true, message: "检查部位名称2级不能为空", trigger: "blur" }
    ],
    unitDesc: [
      { required: true, message: "单位名称不能为空", trigger: "blur" }
    ],
    handlingMethod: [
      { required: true, message: "处理方式不能为空", trigger: "blur" }
    ],
    handlingMethodDesc: [
      { required: true, message: "处理方式名称不能为空", trigger: "blur" }
    ],
  }
})

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

/** 查询查获物品明细列表 */
function getList() {
  loading.value = true
  listItems(queryParams.value).then(response => {
    itemsList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

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

// 表单重置
function reset() {
  form.value = {
    tenantId: null,
    revision: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    id: null,
    recordId: null,
    itemCode: null,
    quantity: null,
    imageUrl: null,
    remark: null,
    checkPositionCodeOne: null,
    isActiveConcealment: null,
    checkPositionCodeTwo: null,
    checkPositionSpecific: null,
    itemName: null,
    categoryCodeOne: null,
    categoryNameOne: null,
    categoryCodeTwo: null,
    categoryNameTwo: null,
    unit: null,
    checkPositionNameOne: null,
    checkPositionNameTwo: null,
    unitDesc: null,
    handlingMethod: null,
    handlingMethodDesc: null,
    imageId: null
  }
  proxy.resetForm("itemsRef")
}

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

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

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

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加查获物品明细"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getItems(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改查获物品明细"
  })
}

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

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除数据项？').then(function() {
    return delItems(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

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

getList()
</script>
