import SearchForm from '@/components/SearchForm'
import useAccountStore, { fetchRoleList, fetchTravelAgencyByName, genRandomPassword } from '@/stores/Account'
import useFormStore from '@/stores/Form'
import { isEmpty, debounce } from '@/utils/commons'
import { ExclamationCircleFilled } from '@ant-design/icons'
import { App, Button, Col, Form, Input, Modal, Row, Select, Space, Table, Typography, Switch } from 'antd'
import dayjs from 'dayjs'
import { useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'

const { Title } = Typography

function Management() {
  const { t } = useTranslation()

  const accountListColumns = [
    {
      title: t('account:username'),
      dataIndex: 'username',
      render: accountRender
    },
    {
      title: t('account:realname'),
      dataIndex: 'realname',
    },
    {
      title: t('account:email'),
      dataIndex: 'email',
    },
    {
      title: t('account:travelAgency'),
      dataIndex: 'travelAgencyName',
    },
    {
      title: t('account:roleName'),
      dataIndex: 'role'
    },
    {
      title: t('account:lastLogin'),
      dataIndex: 'lastLogin',
      render: (text) => (isEmpty(text) ? '' : dayjs(text).format('YYYY-MM-DD HH:mm:ss'))
    },
    {
      title: t('account:action'),
      dataIndex: 'account:action',
      render: actionRender
    },
  ]

  function accountRender(text, account) {
    return (
      <Button type='link' onClick={() => onAccountSeleted(account)}>{text}</Button>
    )
  }

  function actionRender(_, account) {
    return (
      <Space key='actionRenderSpace' size='middle'>
        <Switch checkedChildren={t('account:action.enable')} unCheckedChildren={t('account:action.disable')} checked={account.disabled==0} onChange={(checked) => {
          showDisableConfirm(account, checked)
        }} />
        <Button type='link' key='resetPassword' onClick={() => showResetPasswordConfirm(account)}>{t('account:action.resetPassword')}</Button>
      </Space>
    )
  }

  const [isAccountModalOpen, setAccountModalOpen] = useState(false)
  const [dataLoading, setDataLoading] = useState(false)
  const [roleAllList, setRoleAllList] = useState([])
  const [travelAgencyList, setTravelAgencyList] = useState([])
  const [currentTravelAgency, setCurrentTravelAgency] = useState(null)

  const [accountForm] = Form.useForm()
  const [searchAccountByCriteria, accountList, toggleAccountStatus, saveOrUpdateAccount, resetAccountPassword, newEmptyAccount] =
    useAccountStore((state) =>
      [state.searchAccountByCriteria, state.accountList, state.toggleAccountStatus, state.saveOrUpdateAccount, state.resetAccountPassword, state.newEmptyAccount])

  const formValues = useFormStore(state => state.formValues)
  const { notification, modal } = App.useApp()

  useEffect(() => {
    fetchRoleList()
      .then((roleList) => {
        const roleListMap = roleList.map(r => {
          return {
            value: r.role_id,
            label: r.role_name,
            disabled: r.role_id === 1
          }
        })
        roleListMap.unshift({ value: 0, label: '未设置', disabled: true });
        setRoleAllList(roleListMap)
      })
  }, [])

  const handelAccountSearch = () => {
    setDataLoading(true)
    searchAccountByCriteria(formValues)
      .catch(ex => {
        notification.error({
          message: 'Notification',
          description: ex.message,
          placement: 'top',
          duration: 4,
        })
      })
      .finally(() => {
        setDataLoading(false)
      })
  }

  const onAccountSeleted = async (account) => {
    setTravelAgencyList([{
      label: account.travelAgencyName,
      value: account.travelAgencyId
    }])
    accountForm.setFieldsValue(account)
    setCurrentTravelAgency(account.travelAgencyId)
    setAccountModalOpen(true)
  }

  const onNewAccount = () => {
    const emptyAccount = newEmptyAccount()
    accountForm.setFieldsValue(emptyAccount)
    setAccountModalOpen(true)
  }

  const onAccountFinish = (values) => {
    saveOrUpdateAccount(values)
      .then(() => {
        setAccountModalOpen(false)
        handelAccountSearch()
      })
      .catch(ex => {
        notification.error({
          message: 'Notification',
          description: ex.message,
          placement: 'top',
          duration: 4,
        })
      })
  }

  const onAccountFailed = (error) => {
    console.log('Failed:', error)
    // form.resetFields()
  }

  const handleTravelAgencySearch = (newValue) => {
    setDataLoading(true)
    fetchTravelAgencyByName(newValue)
      .then(result => {
        setTravelAgencyList(result.map(r => {
          return {
            label: r.travel_agency_name,
            value: r.travel_agency_id
          }
        }))
      })
      .finally(() => {
        setDataLoading(false)
      })
  }

  const handleTravelAgencyChange = (newValue) => {
    setCurrentTravelAgency(newValue)
  }

  const showDisableConfirm = (account, status) => {

    const confirmTitle = status ? t('account:action.enable.title') : t('account:action.disable.title')

    modal.confirm({
      title: confirmTitle,
      icon: <ExclamationCircleFilled />,
      content: t('account:username') + ': ' + account.username + ', ' + t('account:realname') + ': ' + account.realname,
      onOk() {
        toggleAccountStatus(account.userId, status)
          .then(() => {
            handelAccountSearch()
          })
          .catch(ex => {
            notification.error({
              message: 'Notification',
              description: ex.message,
              placement: 'top',
              duration: 4,
            })
          })
      },
      onCancel() {
      },
    })
  }

  const showResetPasswordConfirm = (account) => {
    const confirmTitle = t('account:action.resetPassword.tile')
    const randomPassword = genRandomPassword()
    modal.confirm({
      title: confirmTitle,
      icon: <ExclamationCircleFilled />,
      content: t('account:username') + ': ' + account.username + ', ' + t('account:realname') + ': ' + account.realname,
      onOk() {
        resetAccountPassword(account.userId, randomPassword)
          .then(() => {
            notification.info({
              message: `请复制新密码给 [${account.realname}]`,
              description: '新密码：' + randomPassword,
              placement: 'top',
              duration: 60,
            })
          })
      },
      onCancel() {
      },
    })
  }

  return (
    <>
      <Modal
        centered
        okButtonProps={{
          autoFocus: true,
          htmlType: 'submit',
        }}
        title={t('account:detail')}
        open={isAccountModalOpen} onCancel={() => setAccountModalOpen(false)}
        destroyOnClose
        forceRender
        modalRender={(dom) => (
          <Form
            name='AccountForm'
            form={accountForm}
            layout='vertical'
            size='large'
            className='max-w-2xl'
            onFinish={onAccountFinish}
            onFinishFailed={onAccountFailed}
            autoComplete='off'
          >
            {dom}
          </Form>
        )}
      >
        <Form.Item name='accountId' className='hidden' ><Input /></Form.Item>
        <Form.Item name='userId' className='hidden' ><Input /></Form.Item>
        <Form.Item name='lmi2_sn' className='hidden' ><Input /></Form.Item>
        <Form.Item
          label={t('account:username')}
          name='username'
          rules={[
            {
              required: true,
              message: t('account:Validation.username'),
            },
          ]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          label={t('account:realname')}
          name='realname'
          rules={[
            {
              required: true,
              message: t('account:Validation.realname'),
            },
          ]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          label={t('account:email')}
          name='email'
          rules={[
            {
              required: true,
              message: t('account:Validation.email'),
            },
          ]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          label={t('account:travelAgency')}
          name='travelAgencyId'
          rules={[
            {
              required: true,
              message: t('account:Validation.travelAgency'),
            },
          ]}
        >
          <Select
            options={travelAgencyList}
            value={currentTravelAgency}
            onChange={handleTravelAgencyChange}
            loading={dataLoading}
            showSearch
            filterOption={false}
            onSearch={debounce(handleTravelAgencySearch, 800)}
            notFoundContent={null}
          >
          </Select>
        </Form.Item>
        <Form.Item
          label={t('account:roleName')}
          name='roleId'
          rules={[
            {
              required: true,
              message: t('account:Validation.role'),
            },
          ]}
        >
          <Select
            options={roleAllList}
            filterOption={false}
            notFoundContent={null}
          >
          </Select>
        </Form.Item>
      </Modal>
      <Space direction='vertical' className='w-full'>
        <Title level={3}>{t('account:accountList')}</Title>
        <SearchForm
          fieldsConfig={{
            shows: ['username', 'agency'],
            fieldProps: {
              username: { label: t('account:username') + '/' + t('account:realname') },
              agency: { label: t('account:travelAgency') },
            },
            sort: { username: 1, agency: 2},
          }}
          onSubmit={() => {
            handelAccountSearch()
          }}
        />
        <Row>
          <Col span={24}>
            <Space>
              <Button onClick={() => onNewAccount()}>{t('account:newAccount')}</Button>
            </Space>
          </Col>
        </Row>
        <Row>
          <Col span={24}>
            <Table
              bordered
              loading={dataLoading}
              rowKey='username'
              pagination={{
                showQuickJumper: true,
                showLessItems: true,
                showSizeChanger: true,
                showTotal: (total) => { return t('Total') + `：${total}` }
              }}
              columns={accountListColumns} dataSource={accountList}
            />
          </Col>
        </Row>
      </Space>
    </>
  )
}

export default Management
