import { apiConfig } from '@/config/api.config';
import { useRef, useState } from 'react';
import { useManageTransferTypeContext } from '../hooks/useManageTransferTypeContext';
import { Alert, KeenIcon, useDataGrid } from '@/components';
import { useCallApi } from '@/hooks';
import {
  Dialog,
  DialogBody,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle
} from '@/components/ui/dialog';
import { Input } from '@/components/ui/input';
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue
} from '@/components/ui/select';
import { Button } from '@/components/ui/button';

interface CreateTransferTypeParams {
  transfer_type_name: string;
  description: string;
  from_account: string;
  to_account: string;
  minimal_amount: number;
  maximum_amount: number;
  otp_threshold: number;
  maximum_transaction_perDay: number;
}

const API_URL = apiConfig.service_dashboard;

const AddDialog = () => {
  const parentRef = useRef<any | null>(null);
  const { showAddDialog, handleAddDialog, accounts } = useManageTransferTypeContext();
  const { reload } = useDataGrid();
  const { PostData, PutData } = useCallApi();
  const [alert, setAlert] = useState({
    show: false,
    message: ''
  });

  const initialState = {
    transfer_type_name: '',
    description: '',
    from_account: '',
    to_account: '',
    minimal_amount: 0,
    maximum_amount: 0,
    otp_threshold: 0,
    maximum_transaction_perDay: 0
  };

  const [formField, setFormField] = useState(initialState);
  const resetForm = () => {
    setFormField(initialState);
  };
  const [isSubmitting, setIsSubmitting] = useState(false);

  const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
    e.preventDefault();
    // setIsSubmitting(true);
    const payload = {
      transfer_type_name: formField.transfer_type_name,
      description: formField.description,
      from_account: formField.from_account,
      to_account: formField.to_account,
      minimal_amount: formField.minimal_amount,
      maximum_amount: formField.maximum_amount,
      otp_threshold: formField.otp_threshold,
      maximum_transaction_perDay: formField.maximum_transaction_perDay
    };
    console.log(payload);
  };

  return (
    <Dialog open={showAddDialog} onOpenChange={(open) => handleAddDialog(open)}>
      <DialogContent className="container-fixed max-w-[768px] flex flex-col p-5 overflow-hidden [&>button]:hidden">
        <DialogHeader className="p-5 border-0">
          <div className="flex items-center justify-between flex-wrap grow">
            <div className="flex flex-col justify-center">
              <h1 className="text-xl font-semibold leading-none text-gray-900">
                Create Transfer Type
              </h1>
              <div className="flex items-center gap-2 text-sm font-normal text-gray-700"></div>
            </div>
            <div
              className="cursor-pointer hover:opacity-100 opacity-50"
              onClick={() => {
                handleAddDialog(false);
                resetForm();
              }}
            >
              <KeenIcon icon="cross" className="text-1.5xl" />
            </div>
          </div>
        </DialogHeader>
        <DialogBody className="scrollable-y px-0 pb-0" ref={parentRef}>
          <div className="flex flex-col px-0">
            {alert.show && (
              <Alert variant="danger" className="mb-3">
                <h3>{alert.message}</h3>
              </Alert>
            )}
            <form action="" onSubmit={handleSubmit}>
              <div className="card-body grid gap-5 p-0">
                <div className="w-full">
                  <div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
                    <label className="form-label flex items-center gap-1 max-w-56">
                      Transfer Type Name
                    </label>
                    <Input
                      className="input"
                      type="text"
                      autoComplete="off"
                      value={formField.transfer_type_name}
                      onChange={({ target }) =>
                        setFormField((prev) => ({ ...prev, transfer_type_name: target.value }))
                      }
                    />
                  </div>
                </div>

                <div className="w-full">
                  <div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
                    <label className="form-label flex items-center gap-1 max-w-56">
                      Description
                    </label>
                    <Input
                      className="input"
                      type="text"
                      autoComplete="off"
                      value={formField.description}
                      onChange={({ target }) =>
                        setFormField((prev) => ({ ...prev, description: target.value }))
                      }
                    />
                  </div>
                </div>

                <div className="w-full">
                  <div className="flex items-center flex-wrap gap-2.5">
                    <label className="form-label max-w-56">From Account</label>

                    <div className="grow">
                      <Select
                        value={formField.from_account}
                        onValueChange={(target) =>
                          setFormField((prev) => ({ ...prev, from_account: target }))
                        }
                      >
                        <SelectTrigger>
                          <SelectValue placeholder="Select" />
                        </SelectTrigger>
                        <SelectContent>
                          {accounts.map((account, idx) => (
                            <SelectItem value={account.name} key={account.id}>
                              {account.name}
                            </SelectItem>
                          ))}
                        </SelectContent>
                      </Select>
                    </div>
                  </div>
                </div>

                <div className="w-full">
                  <div className="flex items-center flex-wrap gap-2.5">
                    <label className="form-label max-w-56">To Account</label>

                    <div className="grow">
                      <Select
                        value={formField.to_account}
                        onValueChange={(target) =>
                          setFormField((prev) => ({ ...prev, to_account: target }))
                        }
                      >
                        <SelectTrigger>
                          <SelectValue placeholder="Select" />
                        </SelectTrigger>
                        <SelectContent>
                          {accounts.map((account, idx) => (
                            <SelectItem value={account.name} key={account.id}>
                              {account.name}
                            </SelectItem>
                          ))}
                        </SelectContent>
                      </Select>
                    </div>
                  </div>
                </div>

                <div className="w-full">
                  <div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
                    <label className="form-label flex items-center gap-1 max-w-56">
                      Minimal Amount
                    </label>
                    <Input
                      className="input"
                      type="number"
                      autoComplete="off"
                      value={formField.minimal_amount}
                      onChange={({ target }) =>
                        setFormField((prev) => ({
                          ...prev,
                          minimal_amount: Number(target.value)
                        }))
                      }
                    />
                  </div>
                </div>

                <div className="w-full">
                  <div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
                    <label className="form-label flex items-center gap-1 max-w-56">
                      Maximum Amount
                    </label>
                    <Input
                      className="input"
                      type="number"
                      autoComplete="off"
                      value={formField.maximum_amount}
                      onChange={({ target }) =>
                        setFormField((prev) => ({
                          ...prev,
                          maximum_amount: Number(target.value)
                        }))
                      }
                    />
                  </div>
                </div>

                <div className="w-full">
                  <div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
                    <label className="form-label flex items-center gap-1 max-w-56">
                      OTP Threshold
                    </label>
                    <Input
                      className="input"
                      type="number"
                      autoComplete="off"
                      value={formField.otp_threshold}
                      onChange={({ target }) =>
                        setFormField((prev) => ({
                          ...prev,
                          otp_threshold: Number(target.value)
                        }))
                      }
                    />
                  </div>
                </div>

                <div className="w-full">
                  <div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
                    <label className="form-label flex items-center gap-1 max-w-56">
                      Maximum Transaction / day
                    </label>
                    <Input
                      className="input"
                      type="number"
                      autoComplete="off"
                      value={formField.maximum_transaction_perDay}
                      onChange={({ target }) =>
                        setFormField((prev) => ({
                          ...prev,
                          maximum_transaction_perDay: Number(target.value)
                        }))
                      }
                    />
                  </div>
                </div>

                <div className="flex justify-end pt-2.5 gap-5">
                  <Button variant={'outline'} type="reset">
                    Reset
                  </Button>
                  <Button variant={'default'} type="submit">
                    Save Changes
                  </Button>
                </div>
              </div>
            </form>
          </div>
        </DialogBody>
      </DialogContent>
    </Dialog>
  );
};

export default AddDialog;
