import {
  MyBetaModalFormProps,
  MyButtons,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Apis } from '@/gen/Apis';
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.Renovation.RenovationInspectionRules.Update>
      {...MyModalFormProps.props}
      title={`编辑${props.title}`}
      trigger={<MyButtons.Edit />}
      wrapperCol={{ span: 24 }}
      key={new Date().getTime()}
      width="500px"
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue(props.item);
        }
      }}
      onFinish={async (values) =>
        Apis.Renovation.RenovationInspectionRules.Update({
          ...values,
          id: props.item?.id ?? 0,
        })
          .then(() => {
            props.reload?.();
            message.success('编辑成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          key: 'name',
          title: '名称',
          formItemProps: { ...rulesHelper.text },
          colProps: { span: 24 },
        },
        {
          valueType: 'formList',
          dataIndex: 'rule',
          title: '巡逻时段(添加多行，代表一天巡逻多次)',
          formItemProps: { ...rulesHelper.array },
          fieldProps: {
            copyIconProps: false,
            // deleteIconProps: false,
          },
          columns: [
            {
              valueType: 'group',
              colProps: { span: 24 },
              columns: [
                {
                  title: '开始时间',
                  key: 'start_time',
                  valueType: 'time',
                  colProps: { span: 12 },
                  fieldProps: {
                    format: 'HH:mm',
                    style: {
                      width: '100%',
                    },
                  },
                  formItemProps: { ...rulesHelper.text },
                },
                {
                  title: '结束时间',
                  key: 'end_time',
                  valueType: 'time',
                  colProps: { span: 12 },
                  fieldProps: {
                    format: 'HH:mm',
                    style: {
                      width: '100%',
                    },
                  },
                  formItemProps: { ...rulesHelper.text },
                },
              ],
            },
          ],
        },
        {
          title: '巡逻说明',
          key: 'description',
          valueType: 'textarea',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
      ]}
    />
  );
}
