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 Update(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Company.CompanyEmployees.Update>
      {...MyModalFormProps.props}
      title={`编辑员工`}
      trigger={<MyButtons.Edit />}
      wrapperCol={{ span: 24 }}
      width="500px"
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue({
            ...props.item,
            roles_id: props.item?.roles?.map((item: any) => item.value),
          });
        }
      }}
      onFinish={async (values) =>
        Apis.Company.CompanyEmployees.Update({
          ...values,
          id: props.item?.id ?? 0,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        Selects?.Organizations({
          title: '所属组织',
          params: { companies_id: props?.item?.companies_id },
          key: 'organizations_id',
          formItemProps: { ...rulesHelper.text },
        }),
        {
          key: 'name',
          title: '姓名',
          colProps: { span: 6 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'phone',
          title: '手机号',
          valueType: 'number',
          fieldProps: {
            maxLength: 11,
          },
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.text },
        },
        MyFormItems.EnumRadio({
          key: 'sex',
          title: '性别',
          colProps: { span: 6 },
          valueEnum: SexEnum,
          required: true,
        }),

        {
          key: 'password',
          title: '密码',
          colProps: { span: 24 },
          valueType: 'password',
        },
        {
          key: 'remark',
          title: '备注',
          colProps: { span: 24 },
          valueType: 'textarea',
        },
      ]}
    />
  );
}
