<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.warehouseSku"
            placeholder="库存SKU"
            clearable
            style="width: 150px"
          />
        </ElFormItem>
        <ElFormItem label="款号">
          <ElInput
            v-model="filterForm.productNo"
            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({
  warehouseSku: '',
  productNo: '',
})

const {
  loading,
  currentPage,
  pageSize,
  total,
  data,
  onCurrentPageChange,
  onPageSizeChange,
  refresh,
} = usePageList<RestockData>({
  initPageSize: 50,
  query: async (current, size) => {
    const res = await getRestockListApi(
      {
        warehouseSku: filterForm.warehouseSku || undefined,
        productNo: filterForm.productNo || 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.image ?? ''}
          style={{ width: '50px', height: '50px' }}
          previewSrcList={[row.image ?? '']}
          previewTeleported
        />
      )
    },
  },
  {
    prop: 'skuName',
    label: '商品名称',
    minWidth: 140,
    showOverflowTooltip: true,
  },
  { prop: 'productNo', label: '款号', width: 130, align: 'center' },
  {
    prop: 'warehouseSku',
    label: '库存SKU',
    width: 200,
    align: 'center',
  },
  {
    prop: 'usableInventory',
    label: '缺货数量',
    width: 90,
    align: 'right',
    render: (row: RestockData) => {
      const v = row.usableInventory ?? 0
      return (
        <span style="color: #f56c6c; font-weight: bold">{Math.abs(v)}</span>
      )
    },
  },
  {
    label: '可用数量',
    prop: 'usableInventory',
    width: 90,
    align: 'right',
  },
  {
    prop: 'inventory',
    label: '库存数量',
    width: 90,
    align: 'right',
  },
  {
    prop: 'occupyInventory',
    label: '占用数量',
    width: 90,
    align: 'right',
  },
  {
    key: 'operation',
    label: '操作',
    width: 120,
    align: 'center',
    fixed: 'right',
    slot: 'operation',
  },
]

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

const handleReset = () => {
  filterForm.warehouseSku = ''
  filterForm.productNo = ''
  refresh()
}

const handleRestockCheck = async (row: RestockData) => {
  try {
    await ElMessageBox.confirm('确定补货校验吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    })
  } catch {
    return
  }
  try {
    const res = await restockCheckApi(
      row.id,
      row.warehouseSku ?? '',
      row.warehouseId,
    )
    if (res.code !== 200) return
    ElMessage.success('补货校验成功')
    refresh()
  } catch (e) {
    console.error(e)
  }
}

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>
