import {
  Box,
  Button,
  FormControlLabel,
  Radio,
  RadioGroup,
  TextField,
  FormHelperText,
  FormControl,
  FormLabel,
  Typography,
  Stack,
  Divider,
} from "@mui/material";
import { useFormik } from "formik";
import * as Yup from "yup";
import { SPACING } from "../../../../data/spacing";
import { Grid } from "../../../../components/grid/Grid";
import { format } from "date-fns-jalali";

export const SlaughterNoneRecieptModal = ({ handleSubmit, item }) => {
  const validationSchema = Yup.object({
    state: Yup.string().required("لطفا یک گزینه را انتخاب کنید"),
    message: Yup.string().required("پیام الزامی است"),
  });

  const formik = useFormik({
    initialValues: {
      state: "accepted",
      message: "",
    },
    validationSchema,
    onSubmit: (values) => {
      handleSubmit(values);
    },
  });

  return (
    <Grid container gap={SPACING.SMALL} direction="column">
      <Box
        component="form"
        onSubmit={formik.handleSubmit}
        sx={{ p: 2, minWidth: 300 }}
      >
        <Grid container spacing={2} sx={{ mb: 3 }}>
          <Grid item xs={6}>
            <Stack spacing={1}>
              <Stack direction="row" spacing={2}>
                <Typography variant="body2">کدبار:</Typography>
                <Typography>{item?.barCode || "-"}</Typography>
              </Stack>

              <Stack direction="row" spacing={1}>
                <Typography variant="body2">تاریخ کشتار:</Typography>
                <Typography>
                  {item?.poultryRequest.sendDate
                    ? format(
                        new Date(item?.poultryRequest.sendDate),
                        "yyyy/MM/dd"
                      )
                    : "-"}
                </Typography>
              </Stack>

              <Stack spacing={1}>
                <Stack direction="row" spacing={2}>
                  <Typography variant="body2">مرغدار:</Typography>
                  <Typography>{`${item.poultryRequest?.poultry?.unitName}`}</Typography>
                </Stack>
              </Stack>
            </Stack>
          </Grid>

          <Grid item xs={6}>
            <Stack direction="row" spacing={2}>
              <Typography variant="body2">خریدار:</Typography>
              <Typography>{`${item.killhouseUser?.name} (${item.killhouseUser?.killHouseOperator?.user?.mobile})`}</Typography>
            </Stack>
            <Stack direction="row" spacing={2}>
              <Typography variant="body2"> کد سفارش:</Typography>
              <Typography>{item?.poultryRequest.orderCode}</Typography>
            </Stack>

            <Stack direction="row" spacing={2}>
              <Typography variant="body2">تعداد اولیه:</Typography>
              <Typography>{item.quantity?.toLocaleString()} (قطعه)</Typography>
            </Stack>
            <Stack direction="row" spacing={2}>
              <Typography variant="body2">وزن :</Typography>
              <Typography>
                {item?.weightInfo?.weight?.toLocaleString()} (کیلوگرم)
              </Typography>
            </Stack>
          </Grid>
        </Grid>

        <Divider sx={{ mt: 1, mb: 2 }} />

        <FormControl
          component="fieldset"
          error={formik.touched.state && Boolean(formik.errors.state)}
          fullWidth
          sx={{ mb: 2 }}
        >
          <FormLabel component="legend">وضعیت</FormLabel>
          <RadioGroup
            row
            name="state"
            value={formik.values.state}
            onChange={formik.handleChange}
            onBlur={formik.handleBlur}
          >
            <FormControlLabel
              value="accepted"
              control={<Radio />}
              label="تایید"
            />
            <FormControlLabel value="rejected" control={<Radio />} label="رد" />
          </RadioGroup>
          {formik.touched.state && formik.errors.state && (
            <FormHelperText>{formik.errors.state}</FormHelperText>
          )}
        </FormControl>

        <TextField
          name="message"
          label="پیام (اجباری)"
          multiline
          rows={4}
          value={formik.values.message}
          onChange={formik.handleChange}
          onBlur={formik.handleBlur}
          error={formik.touched.message && Boolean(formik.errors.message)}
          helperText={formik.touched.message && formik.errors.message}
          variant="outlined"
          fullWidth
          sx={{ mb: 2 }}
        />

        <Button
          fullWidth
          variant="contained"
          color="primary"
          type="submit"
          disabled={!formik.isValid || formik.isSubmitting}
        >
          ثبت
        </Button>
      </Box>
    </Grid>
  );
};
