<!-- 批次策略 -->
<template>
  <!-- 搜索工作栏 -->
  <ContentWrap>
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="策略名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入策略名称" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="优先级" prop="priority">
        <el-input v-model.number="queryParams.priority" type="number" oninput ="value=value.replace(/[^\d]/g,'')" placeholder="请输入优先级" clearable @keyup.enter="handleQuery" class="!w-240px" />
      </el-form-item>
      <el-form-item>
        <el-button type="info" plain @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button type="info" plain @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <!-- 列表头部 -->
  <TableHead :HeadButttondata="HeadButttondata" @buttonBaseClick="buttonBaseClick" />
  <!-- 列表 -->
  <ContentWrap>
    <el-table v-clientTable v-loading="loading" :data="list" :default-sort="{ prop: 'priority', order: 'null' }">
      <el-table-column align="center" label="策略代码" prop="strategyCode" />
      <el-table-column align="center" label="策略名称" prop="name" show-overflow-tooltip />
      <el-table-column align="center" label="策略描述" prop="description" />
      <el-table-column align="center" label="优先级" prop="priority" sortable/>
      <el-table-column align="center" label="状态" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" fixed="right" width="240">
        <template #default="scope">
          <ButtonBase :Butttondata="butttondata(scope.row)" @button-base-click="buttonTableClick($event, scope.row)" />
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNo" :total="total"
      @pagination="getList" />
  </ContentWrap>
  <!-- 表单弹窗：添加/修改 -->
  <AddForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { DICT_TYPE } from '@/utils/dict'
import * as defaultButtons from '@/utils/disposition/defaultButtons'
import * as RuleApi from '@/api/wms/rule'
import AddForm from './AddForm.vue'

defineOptions({ name: 'SupplieDeliveryStrategy' })
const queryFormRef = ref() // 搜索的表单

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 字典表格数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  strategyCode: 'S010',
})

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await RuleApi.getRulePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}
// 显示弹窗
const formRef = ref()
const openForm = (type: string, strategyCode: string, id: number) => {
  formRef.value.open(type, strategyCode, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await RuleApi.deleteRule(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    buttonBaseClick('refresh',null)
  } catch { }
}
// 列表头部按钮
const HeadButttondata = [
  defaultButtons.defaultAddBtn(null), // 新增
]



// 头部按钮事件 
const buttonBaseClick = (val, item) => {
  if (val == 'add') { // 新增
    openForm('create', queryParams.strategyCode)
  } else if (val == 'import') { // 导入
    handleImport()
  } else if (val == 'export') { // 导出
    handleExport()
  } else if (val == 'refresh') { // 刷新
    getList()
  } else if (val == 'filtrate') { // 筛选
  } else { // 其他按钮 
    console.log('其他按钮', item)
  }
}

// 根据状态返回该按钮是否显示
const isShowMainButton = (row,val) => {
  if (val == row.priority) {
    return true
  } else {
    return false
  }
}

// 列表-操作按钮
const butttondata = (row) => {
  return [
     defaultButtons.mainListEditBtn(null), // 编辑
    defaultButtons.mainListDeleteBtn({hide:isShowMainButton(row,0)}), // 删除
  ]
}

// 列表-操作按钮事件 
const buttonTableClick = (val, row) => {
  if (val == 'edit') { // 编辑
    openForm('update', queryParams.strategyCode, row.id)
  } else if (val == 'delete') { // 删除
    handleDelete(row.id)
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})

</script>
