import { apiConfig } from '@/config/api.config';
import { useRef, useState } from 'react';
import { useManageAccessTypeContext } from '../hooks/useManageAccessTypeContext';
import { Alert, KeenIcon, useDataGrid } from '@/components';
import { useCallApi } from '@/hooks';
import { Dialog, DialogBody, DialogContent, DialogHeader } from '@/components/ui/dialog';
import { Input } from '@/components/ui/input';
import { Button } from '@/components/ui/button';

const API_URL = apiConfig.service_dashboard;

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

  const initialState = {
    name: '',
    description: '',
    internal_name: ''
  };

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

  const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
    e.preventDefault();
    // setIsSubmitting(true);
    console.log(formField);
  };

  const handleReset = () => {
    setFormField(initialState);
  };

  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 Access 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">Name</label>
                    <Input
                      className="input"
                      type="text"
                      autoComplete="off"
                      value={formField.name}
                      onChange={({ target }) =>
                        setFormField((prev) => ({ ...prev, 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-baseline flex-wrap lg:flex-nowrap gap-2.5">
                    <label className="form-label flex items-center gap-1 max-w-56">
                      Internal Name
                    </label>
                    <Input
                      className="input"
                      type="text"
                      autoComplete="off"
                      value={formField.internal_name}
                      onChange={({ target }) =>
                        setFormField((prev) => ({ ...prev, internal_name: target.value }))
                      }
                    />
                  </div>
                </div>

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

export default AddDialog;
