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,
          password: '#LY0123',
          companies_id: values?.companies_id || props?.item?.id,
          organizations_id:
            values?.organizations_id?.[values.organizations_id.length - 1],
        })
          .then(() => {
            props.reload?.();
            message.success('添加成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        ...(props?.item?.id
          ? []
          : [
              Selects?.Companies({
                key: 'companies_id',
                title: '公司',
                colProps: { span: 24 },
                required: true,
                fieldProps: {
                  onChange: () => {
                    form.setFieldsValue({
                      organizations_id: undefined,
                      positions_id: undefined,
                    });
                  },
                },
              }),
            ]),
        {
          valueType: 'dependency',
          name: ['companies_id'],
          columns: ({ companies_id }) => {
            return [
              Selects?.OrganizationsTree({
                title: '选择组织',
                key: 'organizations_id',
                params: {
                  companies_id: companies_id || props?.item?.id || 0,
                },
                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,
        }),
        {
          title: '初始登录密码',
          key: 'password',
          // formItemProps: { ...rulesHelper.text },
          tooltip: '初始登录密码：#LY0123',
          colProps: { span: 24 },
          fieldProps: {
            disabled: true,
            defaultValue: '#LY0123',
          },
        },
        {
          valueType: 'dependency',
          name: ['companies_id'],
          columns: ({ companies_id }) => {
            return [
              Selects?.Positions({
                title: '岗位',
                params: {
                  companies_id: companies_id || props?.item?.id || 0,
                },
                key: 'positions_id',
                formItemProps: { ...rulesHelper.text },
              }),
            ];
          },
        },
        {
          title: '是否管理员',
          key: 'is_company_admin',
          valueType: 'switch',
          colProps: { span: 6 },
        },
        {
          key: 'remark',
          title: '备注',
          colProps: { span: 24 },
          valueType: 'textarea',
        },
      ]}
    />
  );
}
