/*
"use client";
import React, { useCallback, useEffect, useState } from "react";
import {
  Box,
  Button,
  Card,
  CardContent,
  CardActions,
  Stack,
  Typography,
  Chip,
  CircularProgress,
  Grid,
} from "@mui/material";
import ArrowBackIcon from "@mui/icons-material/ArrowBack";
import { useTranslation } from "react-i18next";
import { fetchAllJoPickOrders, AllJoPickOrderResponse } from "@/app/api/jo/actions";
import JobPickExecution from "./newJobPickExecution";
interface Props {
  onSwitchToRecordTab?: () => void;
}
const JoPickOrderList: React.FC<Props> = ({ onSwitchToRecordTab }) =>{
  const { t } = useTranslation(["common", "jo"]);
  const [loading, setLoading] = useState(false);
  const [pickOrders, setPickOrders] = useState<AllJoPickOrderResponse[]>([]);
  const [selectedPickOrderId, setSelectedPickOrderId] = useState<number | undefined>(undefined);
  const [selectedJobOrderId, setSelectedJobOrderId] = useState<number | undefined>(undefined);
  type PickOrderFilter = "all" | "drink" | "Powder_Mixture" | "other";
  const [filter, setFilter] = useState<PickOrderFilter>("all");
  type FloorFilter = "ALL" | "2F" | "3F" | "4F" | "NO_LOT";
  const [floorFilter, setFloorFilter] = useState<FloorFilter>("ALL");
  const fetchPickOrders = useCallback(async () => {
    setLoading(true);
    try {
      const typeParam = filter === "all" ? undefined : filter;
      const floorParam = floorFilter === "ALL" ? undefined : floorFilter;
      const data = await fetchAllJoPickOrders(typeParam, floorParam);
      setPickOrders(Array.isArray(data) ? data : []);
    } catch (e) {
      console.error(e);
      setPickOrders([]);
    } finally {
      setLoading(false);
    }
  }, [filter, floorFilter]);

  useEffect(() => {
    fetchPickOrders();
  }, [fetchPickOrders, filter, floorFilter]);
  const handleBackToList = useCallback(() => {
    setSelectedPickOrderId(undefined);
    setSelectedJobOrderId(undefined);
    fetchPickOrders();
  }, [fetchPickOrders]);
  // If a pick order is selected, show JobPickExecution detail view
  if (selectedPickOrderId !== undefined) {
    return (
      <Box>
        <Box sx={{ mb: 2 }}>
          <Button
            variant="outlined"
            onClick={handleBackToList}
            startIcon={<ArrowBackIcon />}
          >
            {t("Back to List")}
          </Button>
        </Box>
        <JobPickExecution 
          filterArgs={{ pickOrderId: selectedPickOrderId, jobOrderId: selectedJobOrderId }} 
          //onSwitchToRecordTab={onSwitchToRecordTab}
          onBackToList={handleBackToList} // 传递新的回调
        />
      </Box>
    );
  }

  return (
    <Box>
      {loading ? (
        <Box sx={{ display: "flex", justifyContent: "center", p: 3 }}>
          <CircularProgress />
        </Box>
      ) : (
        
        <Box>
          <Box sx={{ display: 'flex', gap: 1, alignItems: 'center', flexWrap: 'wrap', mb: 2 }}>
          <Button
            variant={filter === 'all' ? 'contained' : 'outlined'}
            size="small"
            onClick={() => setFilter('all')}
          >
            {t("All")}
          </Button>
          <Button
            variant={filter === 'drink' ? 'contained' : 'outlined'}
            size="small"
            onClick={() => setFilter('drink')}
          >
            {t("Drink")}
          </Button>
          <Button
            variant={filter === 'Powder_Mixture' ? 'contained' : 'outlined'}
            size="small"
            onClick={() => setFilter('Powder_Mixture')}
          >
            {t("Powder Mixture")}
          </Button>
          <Button
            variant={filter === 'other' ? 'contained' : 'outlined'}
            size="small"
            onClick={() => setFilter('other')}
          >
            {t("Other")}
          </Button>
        </Box>

        <Box sx={{ display: 'flex', gap: 1, alignItems: 'center', flexWrap: 'wrap', mb: 2 }}>
          <Button
            variant={floorFilter === "ALL" ? "contained" : "outlined"}
            size="small"
            onClick={() => setFloorFilter("ALL")}
          >
            {t("Select All")}
          </Button>
          <Button
            variant={floorFilter === "2F" ? "contained" : "outlined"}
            size="small"
            onClick={() => setFloorFilter("2F")}
          >
            2F
          </Button>
          <Button
            variant={floorFilter === "3F" ? "contained" : "outlined"}
            size="small"
            onClick={() => setFloorFilter("3F")}
          >
            3F
          </Button>
          <Button
            variant={floorFilter === "4F" ? "contained" : "outlined"}
            size="small"
            onClick={() => setFloorFilter("4F")}
          >
            4F
          </Button>
          <Button
            variant={floorFilter === "NO_LOT" ? "contained" : "outlined"}
            size="small"
            onClick={() => setFloorFilter("NO_LOT")}
          >
            {t("No Lot")}
          </Button>
        </Box>
          <Typography variant="body2" color="text.secondary" sx={{ mb: 2 }}>
            {t("Total pick orders")}: {pickOrders.length}
          </Typography>

          <Grid container spacing={2}>
            {pickOrders.map((pickOrder) => {
              const status = String(pickOrder.jobOrderStatus || "");
              const statusLower = status.toLowerCase();
              const statusColor =
                statusLower === "completed"
                  ? "success"
                  : statusLower === "pending" || statusLower === "processing"
                  ? "primary"
                  : "default";

              const finishedCount = pickOrder.finishedPickOLineCount ?? 0;

              return (
                
                <Grid key={pickOrder.id} item xs={12} sm={6} md={4}>
                  <Card
                    sx={{
                      minHeight: 180,
                      maxHeight: 280,
                      display: "flex",
                      flexDirection: "column",
                    }}
                  >
                    <CardContent
                      sx={{
                        pb: 1,
                        flexGrow: 1,
                        overflow: "auto",
                      }}
                    >
                      <Stack direction="row" justifyContent="space-between" alignItems="center">
                        <Box sx={{ minWidth: 0 }}>
                          <Typography variant="subtitle1">
                            {t("Job Order")}: {pickOrder.jobOrderCode || "-"}
                          </Typography>
                        </Box>
                        <Chip size="small" label={t(status)} color={statusColor as any} />
                      </Stack>
                      <Typography variant="body2" color="text.secondary">
                        {t("Lot No")}: {pickOrder.lotNo || "-"}
                      </Typography>
                      <Typography variant="body2" color="text.secondary">
                        {t("Pick Order")}: {pickOrder.pickOrderCode || "-"}
                      </Typography>
                      <Typography variant="body2" color="text.secondary">
                        {t("Item Name")}: {pickOrder.itemName}
                      </Typography>
                      <Typography variant="body2" color="text.secondary">
                        {t("Required Qty")}: {pickOrder.reqQty} ({pickOrder.uomName})
                      </Typography>
                      {floorFilter === "ALL" ? (
                        <>
                          {pickOrder.floorPickCounts?.map(({ floor, finishedCount, totalCount }) => (
                            <Typography
                              key={floor}
                              variant="body2"
                              color="text.secondary"
                              component="span"
                              sx={{ mr: 1 }}
                            >
                              {floor}: {finishedCount}/{totalCount}
                            </Typography>
                          ))}
                          {!!pickOrder.noLotPickCount && (
                            <Typography
                              key="NO_LOT"
                              variant="body2"
                              color="text.secondary"
                              component="span"
                              sx={{ mr: 1 }}
                            >
                              {t("No Lot")}: {pickOrder.noLotPickCount.finishedCount}/{pickOrder.noLotPickCount.totalCount}
                            </Typography>
                          )}
                        </>
                      ) : floorFilter === "NO_LOT" ? (
                        !!pickOrder.noLotPickCount && (
                          <Typography
                            key="NO_LOT"
                            variant="body2"
                            color="text.secondary"
                            component="span"
                            sx={{ mr: 1 }}
                          >
                            {t("No Lot")}: {pickOrder.noLotPickCount.finishedCount}/{pickOrder.noLotPickCount.totalCount}
                          </Typography>
                        )
                      ) : (
                        pickOrder.floorPickCounts
                          ?.filter((c) => c.floor === floorFilter)
                          .map(({ floor, finishedCount, totalCount }) => (
                            <Typography
                              key={floor}
                              variant="body2"
                              color="text.secondary"
                              component="span"
                              sx={{ mr: 1 }}
                            >
                              {floor}: {finishedCount}/{totalCount}
                            </Typography>
                          ))
                      )}
                      {typeof pickOrder.suggestedFailCount === "number" && pickOrder.suggestedFailCount > 0 && (
                        <Typography variant="body2" color="error" sx={{ mt: 0.5 }}>
                          {t("Suggested Fail")}: {pickOrder.suggestedFailCount}
                        </Typography>
                      )}
                      {statusLower !== "pending" && finishedCount > 0 && (
                        <Box sx={{ mt: 1 }}>
                          <Typography variant="body2" fontWeight={600}>
                            {t("Finished lines")}: {finishedCount}
                          </Typography>
                        </Box>
                      )}
                    </CardContent>

                    <CardActions sx={{ pt: 0.5 }}>
                      <Button
                        variant="contained"
                        size="small"
                        onClick={() => {
                          setSelectedPickOrderId(pickOrder.pickOrderId ?? undefined);
                          setSelectedJobOrderId(pickOrder.jobOrderId ?? undefined);
                        }}
                      >
                        {t("View Details")}
                      </Button>
                      <Box sx={{ flex: 1 }} />
                    </CardActions>
                  </Card>
                </Grid>
              );
            })}
          </Grid>
        </Box>
      )}
    </Box>
  );
};

export default JoPickOrderList;
*/