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.Default title="编辑" type="primary" size="small" />}
      layout="horizontal"
      labelCol={{ span: 5 }}
      wrapperCol={{ span: 19 }}
      labelAlign="left"
      width="500px"
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue({
            ...props.item,
            roles_id: props.item?.company_roles?.map((item: any) => item.value),
            employee_roles_id: props.item?.employee_roles?.map(
              (item: any) => item.value,
            ),
            positions_id: props.item?.positions_id ?? '',
          });
        }
      }}
      onFinish={async (values: any) =>
        Apis.Company.CompanyEmployees.Update({
          ...values,
          id: props.item?.id ?? 0,
          type: props.item?.type,
          organizations_id: props.item?.organizations_id ?? '',
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          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',
          type: 'password',
          // formItemProps: { ...rulesHelper.text },
          tooltip: '必须超6位数，包含数字、大小字母、#-_@&*特殊字符',
          colProps: { span: 24 },
          fieldProps: {
            placeholder: '必须超6位数，包含数字、大小字母、#-_@&*特殊字符',
          },
        },
        MyFormItems.EnumRadio({
          key: 'sex',
          title: '性别',
          colProps: { span: 24 },
          valueEnum: SexEnum,
          required: true,
        }),
        // MyFormItems.EnumRadio({
        //   key: 'type',
        //   title: '来源',
        //   colProps: { span: 24 },
        //   valueEnum: CompanyEmployeesTypeEnum,
        //   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,
          fieldProps: {
            labelRender: (res: any) => {
              if (res?.label) {
                return res?.label;
              } else {
                return props.item?.employee_roles?.map(
                  (item: any) => item.label,
                );
              }
            },
          },
        }),
        SysSelects.SysRoles({
          title: '后台角色',
          required: false,
          fieldProps: {
            labelRender: (res: any) => {
              if (res?.label) {
                return res?.label;
              } else {
                return props.item?.company_roles?.map(
                  (item: any) => item.label,
                );
              }
            },
          },
        }),
        {
          key: 'remark',
          title: '备注',
          colProps: { span: 24 },
          valueType: 'textarea',
        },
      ]}
    />
  );
}
