import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import { EmergencyTeamMembersPositionEnum } 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.Emergency.EmergencyEventTeams.Update>
      {...MyModalFormProps.props}
      title={`编辑${props.title}`}
      trigger={<MyButtons.Edit />}
      wrapperCol={{ span: 24 }}
      width="800px"
      form={form}
      key={new Date().getTime()}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue({
            ...props.item,
            members: props.item?.emergency_team_members?.map(
              (item: { company_employees_id: number; position: string }) => ({
                company_employees_id: item.company_employees_id,
                position: item.position,
              }),
            ),
          });
        }
      }}
      onFinish={async (values) =>
        Apis.Emergency.EmergencyEventTeams.Update({
          ...values,
          id: props.item?.id ?? 0,
        })
          .then(() => {
            props.reload?.();
            message.success('编辑成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          title: '名称',
          key: 'name',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        Selects?.AssetProjects({
          title: '选择项目',
          key: 'asset_projects_id',
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.text },
        }),
        Selects?.Employees({
          key: 'company_employees_id',
          title: '组长',
          colProps: { span: 12 },
          required: true,
        }),
        {
          title: '是否启用',
          key: 'is_enabled',
          valueType: 'switch',
        },
        {
          valueType: 'formList',
          dataIndex: 'members',
          title: '团队成员',
          formItemProps: { ...rulesHelper.array },
          fieldProps: {
            copyIconProps: false,
            // deleteIconProps: false,
          },
          columns: [
            {
              valueType: 'group',
              colProps: { span: 24 },
              columns: [
                Selects?.Employees({
                  key: 'company_employees_id',
                  title: '成员',
                  colProps: { span: 12 },
                  required: true,
                }),
                MyFormItems.EnumSelect({
                  key: 'position',
                  title: '身份',
                  valueEnum: EmergencyTeamMembersPositionEnum,
                  colProps: { span: 12 },
                  formItemProps: { ...rulesHelper.text },
                }),
              ],
            },
          ],
        },
      ]}
    />
  );
}
