import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Address } from '@/components/Address';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import {
  AssetProjectsChargeEnum,
  AssetProjectsEntrustTypeEnum,
  AssetProjectsPropertyTypeEnum,
  AssetProjectsStatusEnum,
} 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.Asset.AssetProjects.Update>
      {...MyModalFormProps.props}
      title={`编辑${props.title}`}
      trigger={<MyButtons.Default title="编辑" type="primary" size="small" />}
      wrapperCol={{ span: 24 }}
      width="600px"
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue(props.item);
        }
      }}
      request={() =>
        Promise.resolve({
          ...props.item,
          casacader: [
            props.item?.province_id || '',
            props.item?.city_id || '',
            props.item?.area_id || '',
            props.item?.street_id || '',
          ],
        })
      }
      onFinish={async (values) =>
        Apis.Asset.AssetProjects.Update({ ...values, id: props.item?.id ?? 0 })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          key: 'name',
          title: '项目名称',
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'alias_name',
          title: '项目别名',
          colProps: { span: 12 },
        },
        Selects?.Companies({
          key: 'companies_id',
          title: '所属机构',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.number },
        }),
        Address.Cascader({
          key: 'casacader',
          title: '选择地址',
          colProps: { span: 16 },
          keys: ['province', 'city', 'area', 'street'],
          required: true,
        }),
        {
          key: 'address',
          title: '详细地址',
          colProps: { span: 8 },
          formItemProps: { ...rulesHelper.text },
        },

        MyFormItems.EnumSelect({
          key: 'property_type',
          title: '项目类型',
          colProps: { span: 8 },
          valueEnum: AssetProjectsPropertyTypeEnum,
          // required: true,
        }),
        MyFormItems.EnumSelect({
          key: 'status',
          title: '项目状态',
          colProps: { span: 8 },
          valueEnum: AssetProjectsStatusEnum,
          // required: true,
        }),
        MyFormItems.EnumSelect({
          key: 'entrust_type',
          title: '委托类型',
          colProps: { span: 8 },
          valueEnum: AssetProjectsEntrustTypeEnum,
          // required: true,
        }),
        MyFormItems.EnumRadio({
          key: 'charge',
          title: '收费方式',
          colProps: { span: 8 },
          valueEnum: AssetProjectsChargeEnum,
          // required: true,
        }),
        {
          key: 'takeover_date',
          title: '接管日期',
          valueType: 'date',
          fieldProps: {
            style: {
              width: '100%',
            },
          },
          colProps: { span: 8 },
        },
        {
          key: 'closure_date',
          title: '封园日期',
          valueType: 'date',
          fieldProps: {
            style: {
              width: '100%',
            },
          },
          colProps: { span: 8 },
        },
      ]}
    />
  );
}
