<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <Search :schema="Itempackaging.allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
  </ContentWrap>

  <!-- 列表头部 -->
  <TableHead
    :HeadButttondata="HeadButttondata" 
    @buttonBaseClick="buttonBaseClick"
  />

  <!-- 列表 -->
  <ContentWrap>
    <Table
      :columns="Itempackaging.allSchemas.tableColumns"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      v-model:sort="tableObject.sort"
    >
      <template #action="{ row }">
        <el-button
          link
          type="primary"
          @click="openForm('update', row.id)"
          v-hasPermi="['wms:itembasic:update']"
        >
          编辑
        </el-button>
        <el-button
          link
          type="danger"
          @click="handleDelete(row.id)"
          v-hasPermi="['wms:itembasic:delete']"
        >
          删除
        </el-button>
      </template>
    </Table>
    <!-- <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="物品代码" align="center" prop="itemCode" width="150px" />
      <el-table-column label="计量单位" align="center" prop="uom" width="150px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.UOM" :value="scope.row.uom" />
        </template>
      </el-table-column>
      <el-table-column label="包装单位" align="center" prop="stdPackUnit" width="150px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.PACK_UNIT" :value="scope.row.stdPackUnit" />
        </template>
      </el-table-column>
      <el-table-column label="包装数量" align="center" prop="stdPackQty" width="150px" />
      <el-table-column label="替代包装单位" align="center" prop="altPackUnit1" width="150px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.PACK_UNIT" :value="scope.row.altPackUnit1" />
        </template>
      </el-table-column>
      <el-table-column label="替代包装量" align="center" prop="altPackQty1" width="150px" />
      <el-table-column label="替代包装单位" align="center" prop="altPackUnit2" width="150px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.PACK_UNIT" :value="scope.row.altPackUnit2" />
        </template>
      </el-table-column>
      <el-table-column label="替代包装量" align="center" prop="altPackQty2" width="150px" />
      <el-table-column label="替代包装单位" align="center" prop="altPackUnit3" width="150px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.PACK_UNIT" :value="scope.row.altPackUnit3" />
        </template>
      </el-table-column>
      <el-table-column label="替代包装量" align="center" prop="altPackQty3" width="150px" />
      <el-table-column label="替代包装单位" align="center" prop="altPackUnit4" width="150px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.PACK_UNIT" :value="scope.row.altPackUnit4" />
        </template>
      </el-table-column>
      <el-table-column label="替代包装量" align="center" prop="altPackQty4" width="150px" />
      <el-table-column label="是否可用" align="center" prop="available" width="150px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.TRUE_FALSE" :value="scope.row.available" />
        </template>
      </el-table-column>
      <el-table-column
        label="生效时间"
        align="center"
        prop="activeTime"
        :formatter="dateFormatter"
        width="150px"
      />
      <el-table-column
        label="失效时间"
        align="center"
        prop="expireTime"
        :formatter="dateFormatter"
        width="150px"
      />
      <el-table-column label="备注" align="center" prop="remark" width="150px" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="150px"
      />
      <el-table-column label="创建者ID" align="center" prop="creator" width="150px" />
      <el-table-column label="操作" align="center" width="150px"  fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['wms:itempackaging:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['wms:itempackaging:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table> -->
    <!-- 分页 -->
    <!-- <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    /> -->
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ItempackagingForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import download from '@/utils/download'
import * as ItempackagingApi from '@/api/wms/itempackaging'
import ItempackagingForm from './ItempackagingForm.vue'
import { Itempackaging } from '@/utils/disposition/tableColumns'
import * as defaultButtons from '@/utils/disposition/defaultButtons'
import TableHead from '@/components/TableHead/src/TableHead.vue' 

defineOptions({ name: 'Itempackaging' })

const { tableObject, tableMethods } = useTable({
  getListApi: ItempackagingApi.getItempackagingPage // 分页接口
})

// 获得表格的各种操作
const { getList, setSearchParams } = tableMethods

// 列表头部按钮
const HeadButttondata = [
  defaultButtons.defaultAddBtn(null), // 新增
  defaultButtons.defaultImportBtn(null), // 导入
  defaultButtons.defaultExportBtn(null), // 导出
  // defaultButtons.defaultFilterBtn(), // 筛选
  defaultButtons.defaultFreshBtn(null), // 刷新
  // {
  //   label: '自定义扩展按钮',
  //   name: 'zdy',
  //   hide: false,
  //   type: 'primary',
  //   icon: 'Select',
  //   color: ''
  // },
]
console.log(HeadButttondata);


// 头部按钮事件 
const buttonBaseClick = (val, item) => {
    // 新增
    if (val == 'add') {
      console.log(417, item)
    }
    // 导入
    else if (val == 'import') {

    }
    // 导出
    else if (val == 'export') {
      handleExport()
    }
    // 刷新
    else if (val == 'refresh') {
      getList()
    } 
    // 筛选
    else if (val == 'filtrate') {

    }
    // 其他按钮 
    else {

    }
  }

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



/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ItempackagingApi.deleteItempackaging(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const exportLoading = ref(false) // 导出的加载中
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ItempackagingApi.exportItempackaging(setSearchParams)
    download.excel(data, '物品包装信息 .xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

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