import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
import { SysSelects } from '@/components/SysSelects';
import { Apis } from '@/gen/Apis';
import { SexEnum } from '@/gen/Enums';
import { BetaSchemaForm } from '@ant-design/pro-components';
import { Form, message } from 'antd';

export default function Create(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Company.CompanyEmployees.Store>
      {...MyModalFormProps.props}
      title={`添加${props?.title}`}
      layout="horizontal"
      labelCol={{ span: 5 }}
      wrapperCol={{ span: 19 }}
      labelAlign="left"
      width="500px"
      key={new Date().getTime()}
      trigger={<MyButtons.Create title={props?.title} />}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      onFinish={async (values: any) =>
        Apis.Company.CompanyEmployees.Store({
          ...values,
          companies_id: values?.companies_id || props?.item?.id,
          // type: CompanyEmployeesTypeEnum.External.value,
          password: 'ly#123',
          organizations_id:
            values?.organizations_id?.[values.organizations_id.length - 1],
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        Selects?.OrganizationsTree({
          title: '选择组织',
          key: 'organizations_id',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        }),

        {
          key: 'name',
          title: '姓名',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'phone',
          title: '手机号',
          valueType: 'number',
          fieldProps: {
            maxLength: 11,
          },
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.phone },
        },
        {
          title: '初始登录密码',
          key: 'password',
          // formItemProps: { ...rulesHelper.text },
          tooltip: '初始登录密码：#LY0123',
          colProps: { span: 24 },
          fieldProps: {
            disabled: true,
            defaultValue: '#LY0123',
          },
        },
        MyFormItems.EnumRadio({
          key: 'sex',
          title: '性别',
          colProps: { span: 24 },
          valueEnum: SexEnum,
          required: true,
        }),
        Selects?.Positions({
          title: '岗位',
          key: 'positions_id',
          formItemProps: { ...rulesHelper.text },
          params: {
            name: props.item?.position?.name ?? '',
          },
          fieldProps: {
            showSearch: true,
            placeholder: '请输入关键字搜索',
          },
        }),
        SysSelects.SysEmployeeRoles({
          title: '员工角色',
          required: false,
        }),
        SysSelects.SysRoles({
          title: '后台角色',
          required: false,
        }),
        {
          key: 'remark',
          title: '备注',
          colProps: { span: 24 },
          valueType: 'textarea',
        },
      ]}
    />
  );
}
