import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { HouseOccupantsCardTypeEnum } from '@/gen/Enums';
import { BetaSchemaForm } from '@ant-design/pro-components';
import { Form, message } from 'antd';

export default function Index(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Renovation.RenovationWorkers.Update>
      {...MyModalFormProps.props}
      title="编辑工人"
      width="700px"
      wrapperCol={{ span: 24 }}
      labelAlign="left"
      trigger={<MyButtons.Edit />}
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue(props.item);
        }
      }}
      onFinish={async (values) =>
        Apis.Renovation.RenovationWorkers.Update({
          ...values,
          worker_phone: values.worker_phone?.toString(),
          id: props?.item?.id ?? 0,
        })
          .then(() => {
            props.reload?.();
            message.success('更新成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          title: '姓名',
          colProps: { span: 12 },
          key: 'worker_name',
          formItemProps: { ...rulesHelper.text },
        },
        {
          title: '手机号',
          colProps: { span: 12 },
          valueType: 'digit',
          key: 'worker_phone',
          fieldProps: { maxLength: 11, style: { width: '100%' } },
          formItemProps: { ...rulesHelper.text },
        },
        MyFormItems.EnumSelect({
          key: 'card_type',
          title: '证件类型',
          valueEnum: HouseOccupantsCardTypeEnum,
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.text },
        }),
        {
          title: '证件号',
          colProps: { span: 12 },
          key: 'id_card',
          formItemProps: { ...rulesHelper.text },
        },
        MyFormItems.UploadImages({
          key: 'card_front',
          title: '证件正面',
          max: 1,
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.array },
        }),
        MyFormItems.UploadImages({
          key: 'card_back',
          title: '证件反面',
          max: 1,
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.array },
        }),
        {
          title: '证件有效期-开始',
          colProps: { span: 12 },
          valueType: 'date',
          key: 'valid_from',
          fieldProps: { style: { width: '100%' } },
          formItemProps: { ...rulesHelper.text },
        },
        {
          title: '证件有效期-结束',
          colProps: { span: 12 },
          valueType: 'date',
          key: 'valid_to',
          fieldProps: { style: { width: '100%' } },
          formItemProps: { ...rulesHelper.text },
        },
        MyFormItems.UploadImages({
          key: 'worker_photo',
          title: '工人照片',
          max: 1,
          formItemProps: { ...rulesHelper.array },
          colProps: { span: 24 },
        }),
      ]}
    />
  );
}
