<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="规则名称" prop="ruleName">
        <el-input
          v-model="queryParams.ruleName"
          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="materialCategoryId">
        <el-input
          v-model="queryParams.materialCategoryId"
          placeholder="请输入物资分类ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物资ID" prop="materialId">
        <el-input
          v-model="queryParams.materialId"
          placeholder="请输入物资ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="阈值" prop="thresholdValue">
        <el-input
          v-model="queryParams.thresholdValue"
          placeholder="请输入阈值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="比较运算符" prop="comparisonOperator">
        <el-input
          v-model="queryParams.comparisonOperator"
          placeholder="请输入比较运算符"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预警级别" prop="alertLevel">
        <el-input
          v-model="queryParams.alertLevel"
          placeholder="请输入预警级别"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否启用" prop="isEnabled">
        <el-input
          v-model="queryParams.isEnabled"
          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:wmsAlertRule:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ware:wmsAlertRule:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ware:wmsAlertRule:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['ware:wmsAlertRule:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wmsAlertRuleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="规则ID" align="center" prop="ruleId" />
      <el-table-column label="规则名称" align="center" prop="ruleName" />
      <el-table-column label="预警类型" align="center" prop="alertType" />
      <el-table-column label="仓库ID" align="center" prop="warehouseId" />
      <el-table-column label="物资分类ID" align="center" prop="materialCategoryId" />
      <el-table-column label="物资ID" align="center" prop="materialId" />
      <el-table-column label="条件类型" align="center" prop="conditionType" />
      <el-table-column label="阈值" align="center" prop="thresholdValue" />
      <el-table-column label="比较运算符" align="center" prop="comparisonOperator" />
      <el-table-column label="预警级别" align="center" prop="alertLevel" />
      <el-table-column label="通知方式" align="center" prop="notificationType" />
      <el-table-column label="通知接收人" align="center" prop="notificationReceivers" />
      <el-table-column label="是否启用" align="center" prop="isEnabled" />
      <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:wmsAlertRule:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['ware:wmsAlertRule: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="wmsAlertRuleRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="规则名称" prop="ruleName">
          <el-input v-model="form.ruleName" 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="materialCategoryId">
          <el-input v-model="form.materialCategoryId" placeholder="请输入物资分类ID" />
        </el-form-item>
        <el-form-item label="物资ID" prop="materialId">
          <el-input v-model="form.materialId" placeholder="请输入物资ID" />
        </el-form-item>
        <el-form-item label="阈值" prop="thresholdValue">
          <el-input v-model="form.thresholdValue" placeholder="请输入阈值" />
        </el-form-item>
        <el-form-item label="比较运算符" prop="comparisonOperator">
          <el-input v-model="form.comparisonOperator" placeholder="请输入比较运算符" />
        </el-form-item>
        <el-form-item label="预警级别" prop="alertLevel">
          <el-input v-model="form.alertLevel" placeholder="请输入预警级别" />
        </el-form-item>
        <el-form-item label="通知接收人" prop="notificationReceivers">
          <el-input v-model="form.notificationReceivers" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否启用" prop="isEnabled">
          <el-input v-model="form.isEnabled" 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="WmsAlertRule">
import { listWmsAlertRule, getWmsAlertRule, delWmsAlertRule, addWmsAlertRule, updateWmsAlertRule } from "@/api/ware/wmsAlertRule"

const { proxy } = getCurrentInstance()

const wmsAlertRuleList = 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,
    ruleName: null,
    alertType: null,
    warehouseId: null,
    materialCategoryId: null,
    materialId: null,
    conditionType: null,
    thresholdValue: null,
    comparisonOperator: null,
    alertLevel: null,
    notificationType: null,
    notificationReceivers: null,
    isEnabled: null,
  },
  rules: {
    ruleName: [
      { required: true, message: "规则名称不能为空", trigger: "blur" }
    ],
    alertType: [
      { required: true, message: "预警类型不能为空", trigger: "change" }
    ],
  }
})

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

/** 查询预警规则列表 */
function getList() {
  loading.value = true
  listWmsAlertRule(queryParams.value).then(response => {
    wmsAlertRuleList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

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

// 表单重置
function reset() {
  form.value = {
    ruleId: null,
    ruleName: null,
    alertType: null,
    warehouseId: null,
    materialCategoryId: null,
    materialId: null,
    conditionType: null,
    thresholdValue: null,
    comparisonOperator: null,
    alertLevel: null,
    notificationType: null,
    notificationReceivers: null,
    isEnabled: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  }
  proxy.resetForm("wmsAlertRuleRef")
}

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

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

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

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加预警规则"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _ruleId = row.ruleId || ids.value
  getWmsAlertRule(_ruleId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改预警规则"
  })
}

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

/** 删除按钮操作 */
function handleDelete(row) {
  const _ruleIds = row.ruleId || ids.value
  proxy.$modal.confirm('是否确认删除预警规则编号为"' + _ruleIds + '"的数据项？').then(function() {
    return delWmsAlertRule(_ruleIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

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

getList()
</script>
