<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <Search :schema="ItemBasic.allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
      <!-- 列表头部 -->
    <TableHead :HeadButttondata="HeadButttondata" @buttonBaseClick="buttonBaseClick" />
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <Table :columns="ItemBasic.allSchemas.tableColumns" :data="tableObject.tableList" :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }" v-model:pageSize="tableObject.pageSize" v-model:currentPage="tableObject.currentPage">
      <template #action="{ row }">
        <el-button link type="primary" @click="openForm('update', row.id)" v-hasPermi="['wms:itembasic:update']">
          编辑
        </el-button>
        <el-button link type="primary" @click="openDetail(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>
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ItembasicForm ref="formRef" @success="getList" />
  <ImportForm ref="importFormRef" :url="ItembasicApi.importUrl" :importTemplateData="importTemplateData"
    @success="importSuccess" />
  <!-- 详情 -->
  <Detail ref="detailRef" />
</template>

<script setup lang="ts">
import download from '@/utils/download'
import * as ItembasicApi from '@/api/wms/itembasic'
import ItembasicForm from './ItembasicForm.vue'
import Detail from '@/components/Detail/src/Detail.vue'
import ItembasicDetail from './ItembasicDetail.vue'
import ImportForm from '@/components/ImportForm/src/ImportForm.vue'
import { ItemBasic } from '@/utils/disposition/tableColumns'
import * as defaultButtons from '@/utils/disposition/defaultButtons'
const router = useRouter() // 路由
// tableObject：表格的属性对象，可获得分页大小、条数等属性
// tableMethods：表格的操作对象，可进行获得分页、删除记录等操作
// 详细可见：https://doc.iocoder.cn/vue3/crud-schema/
const { tableObject, tableMethods } = useTable({
  getListApi: ItembasicApi.getItembasicPage // 分页接口
})

const importFormRef = ref()
// 列表头部按钮
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: ''
  // },
]

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

defineOptions({ name: 'Itembasic' })

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

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  code: null,
  name: null,
  desc1: null,
  desc2: null,
  status: null,
  uom: null,
  altUom: null,
  isStdPack: null,
  enableBuy: null,
  enableMake: null,
  enableOutsourcing: null,
  isRecycled: null,
  isPhantom: null,
  abcClass: null,
  type: null,
  category: null,
  itemGroup: null,
  color: null,
  configuration: null,
  project: null,
  eqLevel: null,
  validityDays: null,
  available: null,
  activeTime: [],
  expireTime: [],
  remark: null,
  createTime: [],
  creator: null
})
const exportLoading = ref(false) // 导出的加载中

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

  }
  // 其他按钮 
  else {

  }
}

onMounted(async () => {
  importTemplateData.templateUrl = await ItembasicApi.importTemplate()
})

const handleImport = () => {
  importFormRef.value.open()
}

// 导入附件弹窗所需的参数
const importTemplateData = reactive({
  templateUrl: '',
  templateTitle: '导入模版99.xls'
})

// 导入成功之后
const importSuccess = () => {
  getList()
}
/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}
const detailRef = ref()
const openDetail = (id?: number) => {
  detailRef.value.openDetail(id)
}
// const openDetail = (id?: number) => {
//   // detailRef.value.open(id)
//   router.push({ 
//     path: '/wms/itembasic-manage/itembasic-detail',
//     query:{
//       id
//     }
//  })
// }

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

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ItembasicApi.exportItembasic(queryParams)
    download.excel(data, '物品基本信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

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