import { useShallow } from 'zustand/react/shallow';
import { useOrgStore } from '../store';
import { useNavigation, useParams } from 'react-router';
import { useEffect } from 'react';
import { Button, Input, Modal, Select, Space, Tooltip } from 'antd';
import { message } from '@/modules/message';
import { DeleteOutlined, EditOutlined, LeftOutlined, PlusOutlined } from '@ant-design/icons';
import { Form } from 'antd';
import { useNewNavigate } from '@/modules';
import { isObjectNull } from '@/utils/is-null';
import copy from 'copy-to-clipboard';

const FormModal = () => {
  const [form] = Form.useForm();
  const userStore = useOrgStore(
    useShallow((state) => {
      return {
        showEdit: state.showUserEdit,
        setShowEdit: state.setShowUserEdit,
        formData: state.userFormData,
        setFormData: state.setUserFormData,
        addUser: state.addUser,
      };
    }),
  );
  useEffect(() => {
    const open = userStore.showEdit;
    if (open) {
      const isNull = isObjectNull(userStore.formData);
      if (isNull) {
        form.setFieldsValue({});
      } else form.setFieldsValue(userStore.formData);
    }
  }, [userStore.showEdit, userStore.formData]);
  const onFinish = async (values: any) => {
    //
    console.log(values);
    const username = values.username;
    const role = values.role;
    if (!username) {
      message.error('username is required');
      return;
    }
    userStore.addUser({ username: username, role });
  };
  const onClose = () => {
    userStore.setShowEdit(false);
    userStore.setFormData({});
  };
  const isEdit = userStore.formData.id;
  return (
    <Modal
      title={isEdit ? 'Edit' : 'Add'}
      open={userStore.showEdit}
      onClose={() => userStore.setShowEdit(false)}
      destroyOnClose
      footer={false}
      width={800}
      onCancel={onClose}>
      <Form
        form={form}
        onFinish={onFinish}
        labelCol={{
          span: 4,
        }}
        wrapperCol={{
          span: 20,
        }}>
        <Form.Item name='id' hidden>
          <Input />
        </Form.Item>
        <Form.Item name='username' label='username'>
          <Input disabled={isEdit} />
        </Form.Item>
        <Form.Item name='role' label='role'>
          <Select
            options={[
              {
                label: 'admin',
                value: 'admin',
              },
              {
                label: 'member',
                value: 'member',
              },
            ]}></Select>
        </Form.Item>
        <Form.Item label=' ' colon={false}>
          <Button type='primary' htmlType='submit'>
            Submit
          </Button>
          <Button className='ml-2' htmlType='reset' onClick={onClose}>
            Cancel
          </Button>
        </Form.Item>
      </Form>
    </Modal>
  );
};

export const UserList = () => {
  const param = useParams();
  const navicate = useNewNavigate();
  const [modal, contextHolder] = Modal.useModal();

  const orgStore = useOrgStore(
    useShallow((state) => {
      return {
        users: state.users,
        org: state.org,
        setOrgId: state.setOrgId,
        getOrg: state.getOrg,
        setShowUserEdit: state.setShowUserEdit,
        setUserFormData: state.setUserFormData,
        setShowEdit: state.setShowEdit,
        removeUser: state.removeUser,
        addUser: state.addUser,
      };
    }),
  );
  useEffect(() => {
    if (param.id) {
      orgStore.setOrgId(param.id);
      orgStore.getOrg();
    }
    return () => {
      orgStore.setUserFormData({});
    };
  }, []);
  return (
    <div className='w-full h-full bg-gray-200 flex'>
      <div className='p-2 bg-white mr-2'>
        <Button
          icon={<PlusOutlined />}
          onClick={() => {
            orgStore.setUserFormData({});
            orgStore.setShowUserEdit(true);
          }}></Button>
      </div>
      <div className='p-4 pt-12  grow relative'>
        <div className='absolute top-2'>
          <Tooltip title='返回'>
            <Button
              onClick={() => {
                navicate(-1);
              }}
              icon={<LeftOutlined />}></Button>
          </Tooltip>
        </div>
        <div className='p-4  bg-white  rounded-lg border border-gray-200 shadow-md h-full'>
          <div className='flex gap-4'>
            {orgStore.users.map((item) => {
              const isOwner = item.role === 'owner';
              return (
                <div key={item.id} className='card w-[300px] border-t border-gray-200 justify-between p-2 border-b'>
                  <div
                    className='card-title capitalize truncate cursor-pointer'
                    onClick={() => {
                      copy(item.username);
                    }}>
                    username: {item.username}
                  </div>
                  <div className='flex gap-2 capitalize'>{item.role || '-'}</div>
                  <div className='mt-2'>
                    <Space.Compact>
                      {/* <Tooltip title='Edit'>
                        <Button
                          icon={<EditOutlined />}
                          disabled={isOwner}
                          onClick={() => {
                            orgStore.setShowEdit(true);
                            orgStore.setUserFormData(item);
                          }}></Button>
                      </Tooltip> */}
                      <Tooltip title='delete'>
                        <Button
                          icon={<DeleteOutlined />}
                          disabled={isOwner}
                          onClick={() => {
                            // o-NDO62XGeyEQoz_Sytz-1UUB7kw
                            modal.confirm({
                              title: 'Delete',
                              content: 'Are you sure?',
                              onOk: () => {
                                orgStore.removeUser(item.id);
                              },
                            });
                          }}></Button>
                      </Tooltip>
                    </Space.Compact>
                  </div>
                </div>
              );
            })}
          </div>
        </div>
      </div>

      <FormModal />
      {contextHolder}
    </div>
  );
};
