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 Update(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Company.CompanyEmployees.Update>
      {...MyModalFormProps.props}
      title={`编辑员工`}
      trigger={<MyButtons.Edit />}
      wrapperCol={{ span: 24 }}
      width="500px"
      key={new Date().getTime()}
      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: any) =>
        Apis.Company.CompanyEmployees.Update({
          ...values,
          id: props.item?.id ?? 0,
          type: props.item?.type,
          organizations_id:
            values?.organizations_id?.[values.organizations_id.length - 1] ||
            props.item?.organizations_id,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          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,
        }),
        {
          key: 'password',
          title: '密码',
          valueType: 'password',
          colProps: { span: 24 },
          fieldProps: {
            placeholder: '不修改密码请留空',
          },
        },
        Selects?.Positions({
          title: '岗位',
          key: 'positions_id',
          formItemProps: { ...rulesHelper.text },
          fieldProps: {
            showSearch: true,
          },
        }),
        SysSelects.SysRoles(),
        // {
        //   key: 'password',
        //   title: '密码',
        //   colProps: { span: 24 },
        //   valueType: 'password',
        // },
        {
          key: 'remark',
          title: '备注',
          colProps: { span: 24 },
          valueType: 'textarea',
        },
      ]}
    />
  );
}
