'use client'
import { useCallback, useState } from 'react'
import { RiCloseLine } from '@remixicon/react'
import s from './index.module.css'
import cn from '@/utils/classnames'
import Modal from '@/app/components/base/modal'
import Button from '@/app/components/base/button'
import { addKnowledge, editKnowledge } from '@/service/common'
import 'react-multi-email/dist/style.css'
import Input from '@/app/components/base/input'
import { SimpleSelect } from '@/app/components/base/select'

const InviteModal = ({
  transfer,
  onCancel,
  onSend,
}: any) => {
  const [serviceType, setServiceType] = useState<any>(transfer.row?.serviceType || '')
  const [serviceName, setServiceName] = useState<string>(transfer.row?.serviceName || '')
  const [url, setUrl] = useState<string>(transfer.row?.url || '')
  const [method, setMethod] = useState<string>(transfer.row?.method || '')
  const options = [
    { name: '智能问答', value: 1 },
    { name: '智能搜索', value: 2 },
    { name: '智能推荐', value: 3 },
  ]
  const handleSave = useCallback(async () => {
    try {
      let res: any = () => {}
      if (transfer.mode === 'add') {
        res = await addKnowledge({
          url: '/workspaces/123123',
          body: { serviceType, serviceName, url, method },
        })
      }
      else {
        res = await editKnowledge({
          url: '/workspaces/123123',
          body: { id: transfer.id, serviceType, serviceName, url, method },
        })
      }
      const { result }: any = res
      console.log(result)
      if (result === 'success') {
        onCancel()
        onSend()
      }
    }
    catch (e) { }
  }, [name, onCancel, onSend, transfer])

  return (
    <div className={cn(s.wrap)}>
      <Modal overflowVisible isShow onClose={() => { }} className={cn(s.modal)}>
        <div className='mb-2 flex justify-between'>
          <div className='text-xl font-semibold text-text-primary'>{transfer.mode === 'add' ? '添加' : '编辑'}类型</div>
          <RiCloseLine className='h-4 w-4 cursor-pointer text-text-tertiary' onClick={onCancel} />
        </div>
        <div>
          <div className={cn('flex flex-wrap items-center justify-between')}>
            <div className='shrink-0 py-2 text-sm font-medium leading-[20px] text-text-primary'>
              服务类型
            </div>
            <div className='w-full'>
              <SimpleSelect
                defaultValue={serviceType}
                onSelect={(i) => { setServiceType(i.value) }}
                items={options}
                allowSearch={false}
              />
            </div>
          </div>
          <div className={cn('flex flex-wrap items-center justify-between')}>
            <div className='shrink-0 py-2 text-sm font-medium leading-[20px] text-text-primary'>
              系统名称
            </div>
            <Input
              value={serviceName}
              onChange={e => setServiceName(e.target.value)}
              className='h-9'
              placeholder='请输入系统名称'
            />
          </div>
          <div className={cn('flex flex-wrap items-center justify-between')}>
            <div className='shrink-0 py-2 text-sm font-medium leading-[20px] text-text-primary'>
              URL
            </div>
            <Input
              value={url}
              onChange={e => setUrl(e.target.value)}
              className='h-9'
              placeholder='请输入URL'
            />
          </div>
          <div className={cn('flex flex-wrap items-center justify-between')}>
            <div className='shrink-0 py-2 text-sm font-medium leading-[20px] text-text-primary'>
              请求方式
            </div>
            <Input
              value={method}
              onChange={e => setMethod(e.target.value)}
              className='h-9'
              placeholder='请输入请求方式'
            />
          </div>
          <Button
            tabIndex={0}
            className='mt-4 w-full'
            onClick={handleSave}
            disabled={!serviceType.length || !serviceName.length || !url.length || !method.length}
            variant='primary'
          >
            保存
          </Button>
        </div>
      </Modal>
    </div>
  )
}

export default InviteModal
