<template>
  <div class="waiting-restock">
    <div class="restock-filter">
      <ElForm
        :inline="true"
        :model="filterForm"
        size="default"
        class="search-form"
      >
        <ElFormItem label="库存SKU">
          <ElInput
            v-model="filterForm.stockSku"
            placeholder="库存SKU"
            clearable
            style="width: 150px"
          />
        </ElFormItem>
        <ElFormItem label="款号">
          <ElInput
            v-model="filterForm.styleNo"
            placeholder="款号"
            clearable
            style="width: 150px"
          />
        </ElFormItem>
        <ElFormItem>
          <ElButton type="primary" @click="handleSearch">查询</ElButton>
          <ElButton @click="handleReset">重置</ElButton>
        </ElFormItem>
      </ElForm>
    </div>

    <div v-loading="loading" class="restock-table">
      <TableView :paginated-data="data" :columns="columns" serial-numberable>
        <template #operation="{ row }">
          <ElButton
            type="primary"
            link
            size="small"
            @click="handleRestockCheck(row)"
          >
            补货校验
          </ElButton>
        </template>
      </TableView>
    </div>

    <ElPagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[50, 100, 200, 300]"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="margin: 10px auto 0"
      @size-change="onPageSizeChange"
      @current-change="onCurrentPageChange"
    />
  </div>
</template>

<script setup lang="tsx">
import { reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { getRestockListApi, restockCheckApi } from '@/api/factoryOrderNew'
import type { RestockData } from '@/types/api/factoryOrderNew'
import TableView from '@/components/TableView.vue'
import usePageList from '@/utils/hooks/usePageList'

const filterForm = reactive({
  stockSku: '',
  styleNo: '',
})

const {
  loading,
  currentPage,
  pageSize,
  total,
  data,
  onCurrentPageChange,
  onPageSizeChange,
  refresh,
} = usePageList<RestockData>({
  initPageSize: 50,
  query: async (current, size) => {
    const res = await getRestockListApi(
      {
        stockSku: filterForm.stockSku || undefined,
        styleNo: filterForm.styleNo || undefined,
      },
      current,
      size,
    )
    return res.data
  },
})

const columns = [
  {
    prop: 'warehouseName',
    label: '仓库名称',
    width: 160,
  },
  {
    label: 'SKU图片',
    width: 100,
    align: 'center',
    render: (row: RestockData) => {
      return (
        <el-image
          src={row.skuImage ?? ''}
          style={{ width: '50px', height: '50px' }}
          previewSrcList={[row.skuImage ?? '']}
          previewTeleported
        />
      )
    },
  },
  {
    prop: 'productName',
    label: '商品名称',
    minWidth: 140,
    showOverflowTooltip: true,
  },
  { prop: 'supplierProductNo', label: '款号', width: 130 },
  {
    key: 'thirdSkuCode',
    prop: 'thirdSkuCode',
    label: '库存SKU',
    width: 160,
    align: 'center',
  },
  {
    prop: 'stockOutNum',
    label: '缺货数量',
    width: 90,
    align: 'right'
  },
  {
    label: '可用数量',
    prop: 'usableInventory',
    width: 90,
    align: 'right',
  },
  {
    key: 'stockQuantity',
    prop: 'stockQuantity',
    label: '库存数量',
    width: 90,
    align: 'right',
  },
  {
    prop: 'occupiedQuantity',
    label: '占用数量',
    width: 90,
    align: 'right',
  },
  {
    key: 'operation',
    label: '操作',
    width: 120,
    align: 'center',
    fixed: 'right',
    slot: 'operation',
  },
]

const handleSearch = () => {
  refresh()
}

const handleReset = () => {
  filterForm.stockSku = ''
  filterForm.styleNo = ''
  refresh()
}

const handleRestockCheck = async (row: RestockData) => {
  try {
    await restockCheckApi(row.id)
    ElMessage.success('补货校验成功')
    refresh()
  } catch (e) {
    const err = e as Error & { message?: string }
    ElMessage.error(err?.message || '补货校验失败')
  }
}

defineExpose({ refresh })
</script>

<style scoped lang="scss">
.waiting-restock {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
}

.restock-filter {
  flex-shrink: 0;
  padding-bottom: 10px;
}

.restock-table {
  flex: 1;
  overflow: auto;
}
.search-form {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0;
  min-width: 0;

  :deep(.el-form-item) {
    margin-right: 10px;
    margin-bottom: 10px;
  }

  :deep(.el-form-item__content) {
    min-width: 0;
  }
}
</style>
