import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
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}`}
      wrapperCol={{ span: 24 }}
      width="500px"
      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,
          organizations_id:
            values?.organizations_id?.[values.organizations_id.length - 1],
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        ...(props?.item?.id
          ? []
          : [
              Selects?.Companies({
                key: 'companies_id',
                title: '公司',
                colProps: { span: 24 },
                required: true,
              }),
            ]),
        Selects?.OrganizationsTree({
          title: '选择组织',
          key: 'organizations_id',
          params: { companies_id: props?.item?.companies_id },
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        }),
        {
          key: 'name',
          title: '姓名',
          colProps: { span: 8 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'phone',
          title: '手机号',
          valueType: 'number',
          fieldProps: {
            maxLength: 11,
          },
          colProps: { span: 10 },
          formItemProps: { ...rulesHelper.phone },
        },
        MyFormItems.EnumRadio({
          key: 'sex',
          title: '性别',
          colProps: { span: 6 },
          valueEnum: SexEnum,
          required: true,
        }),
        Selects?.Positions({
          title: '岗位',
          params: { companies_id: props?.item?.companies_id },
          key: 'positions_id',
          formItemProps: { ...rulesHelper.text },
        }),
        // {
        //   key: 'password',
        //   title: '密码',
        //   colProps: { span: 24 },
        //   valueType: 'password',
        // },
        {
          key: 'remark',
          title: '备注',
          colProps: { span: 24 },
          valueType: 'textarea',
        },
      ]}
    />
  );
}
