import {ModalForm, ProForm,ProFormRadio} from '@ant-design/pro-components';
import {ProFormText} from '@ant-design/pro-components';
import {ProFormSwitch} from '@ant-design/pro-components';
import {ProFormSelect, ProFormDigit} from '@ant-design/pro-components';
import {FormattedMessage, useIntl} from '@umijs/max';
import React, {useEffect, useState} from 'react';
import {App, Button, Form, message, Modal} from 'antd';
import {putDepartmentUpdateDepartment} from "@/services/system/Department";
export type FormValueType = {
  target?: string;
  template?: string;
  type?: string;
  time?: string;
  frequency?: string;
} & Partial<API.Department>;

export type UpdateFormProps = {
  updateModalOpen: boolean;
  handleModal: () => void;
  values: Partial<API.Department>;
  reload: any;
};
const UpdateForm: React.FC<UpdateFormProps> = (props) => {

  const intl = useIntl();
  const [form] = Form.useForm<API.Department>();
  const [parentId, setParentId] = useState<number>(0);
  const [parentSelectOptions, setParentSelectOptions] = useState<any[]>([{label: '根节点', value: 0}]);


  return (
    <ModalForm<API.Department>
      title={intl.formatMessage({
        id: 'common.modal.table.create.title',
        defaultMessage: '$$$',
      })}
      open={props.updateModalOpen}
      form={form}
      autoFocusFirstInput
      modalProps={{
        destroyOnClose: true,
        onCancel: () => props.handleModal(),
      }}
      submitTimeout={2000}
      onFinish={async (values) => {
        console.log(values)
        putDepartmentUpdateDepartment(values).then(() => {
          message.success(intl.formatMessage({id: 'common.success', defaultMessage: '$$$'}))
          props.reload()
        }).catch(() => {
          message.error(intl.formatMessage({id: 'common.error', defaultMessage: '$$$'}))
        })
        props.handleModal();
        return true;
      }}
    >

      <ProForm.Group>
        <ProFormText width="md" name="id" label="id" disabled={true} initialValue={props.values.id}/>
        <ProFormSelect width="md" name="parentId"
                       disabled={true}
                       initialValue={parentId}
                       request={async () => parentSelectOptions}
                       label={<FormattedMessage id="system.department.table.list.parentId" defaultMessage="$$$"/>}
                       placeholder={`${intl.formatMessage({id: 'common.please_input', defaultMessage: '$$$'})}${intl.formatMessage({id: 'system.department.table.list.parentId', defaultMessage: '$$$'})}`}
        />
      </ProForm.Group>

      <ProForm.Group>
        <ProFormText width="md" name="name"
                     label={<FormattedMessage id="system.department.table.list.name" defaultMessage="$$$"/>}
                     placeholder={`${intl.formatMessage({id: 'common.please_input', defaultMessage: '$$$'})}${intl.formatMessage({id: 'system.department.table.list.name', defaultMessage: '$$$'})}`}
                     required={true}
                     initialValue={props.values.name}
                     rules={[
                       {
                         required: true,
                         message: (
                           <FormattedMessage
                             id="system.department.table.rule.required.name"
                             defaultMessage="name is required"
                           />
                         ),
                       },
                     ]}
        />
        <ProFormText width="md" name="code"
                     initialValue={props.values.code}
                     label={<FormattedMessage id="system.department.table.list.code" defaultMessage="$$$"/>}
                     placeholder={`${intl.formatMessage({id: 'common.please_input', defaultMessage: '$$$'})}${intl.formatMessage({id: 'system.department.table.list.code', defaultMessage: '$$$'})}`}
        />
      </ProForm.Group>
      <ProForm.Group>

        <ProFormDigit width="md" fieldProps={{precision: 0}} name="sort"
                      initialValue={props.values.sort}
                      label={<FormattedMessage id="system.department.table.list.sort" defaultMessage="$$$"/>}
                      placeholder={`${intl.formatMessage({id: 'common.please_input', defaultMessage: '$$$'})}${intl.formatMessage({id: 'system.department.table.list.sort', defaultMessage: '$$$'})}`}
        />
      </ProForm.Group>
      <ProForm.Group>
        <ProFormText width="md" name="phone"
                     initialValue={props.values.phone}
                     label={<FormattedMessage id="system.department.table.list.phone" defaultMessage="$$$"/>}
                     placeholder={`${intl.formatMessage({id: 'common.please_input', defaultMessage: '$$$'})}${intl.formatMessage({id: 'system.department.table.list.phone', defaultMessage: '$$$'})}`}
                     required={false}/>
        <ProFormText width="md" name="email"
                     initialValue={props.values.email}
                     label={<FormattedMessage id="system.department.table.list.email" defaultMessage="$$$"/>}
                     placeholder={`${intl.formatMessage({id: 'common.please_input', defaultMessage: '$$$'})}${intl.formatMessage({id: 'system.department.table.list.email', defaultMessage: '$$$'})}`}
                     required={false}/>
      </ProForm.Group>
      <ProForm.Group>
        <ProFormText width="md" name="remark"
                     initialValue={props.values.remark}
                     label={<FormattedMessage id="system.department.table.list.remark" defaultMessage="$$$"/>}
                     placeholder={`${intl.formatMessage({id: 'common.please_input', defaultMessage: '$$$'})}${intl.formatMessage({id: 'system.department.table.list.remark', defaultMessage: '$$$'})}`}
                     required={false}/>
        <ProFormSwitch width="md" name="status" initialValue={props.values.status}
                       label={<FormattedMessage id="system.department.table.list.status" defaultMessage="$$$"/>}
                       required={false}>

        </ProFormSwitch>
      </ProForm.Group>


    </ModalForm>)
}
export default UpdateForm;
