import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Address } from '@/components/Address';
import { SysSelects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import {
  AssetProjectsChargeEnum,
  AssetProjectsEntrustTypeEnum,
  AssetProjectsPropertyTypeEnum,
  AssetProjectsStatusEnum,
} from '@/gen/Enums';
import { BetaSchemaForm } from '@ant-design/pro-components';
import { message } from 'antd';

export default function Update(props: MyBetaModalFormProps) {
  return (
    <BetaSchemaForm<ApiTypes.Asset.AssetProjects.Update>
      {...MyModalFormProps.props}
      title={`编辑${props.title}`}
      trigger={
        <MyButtons.Default
          title="修改"
          color="primary"
          variant="filled"
          size="small"
        />
      }
      wrapperCol={{ span: 24 }}
      width="800px"
      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: 6 },
        },
        SysSelects?.Companies({
          key: 'companies_id',
          title: '所属机构',
          colProps: { span: 6 },
          formItemProps: { ...rulesHelper.number },
        }),
        // {
        //   key: 'code',
        //   title: '项目编码',
        //   colProps: { span: 8 },
        //   formItemProps: { ...rulesHelper.text },
        // },
        Address.Cascader({
          key: 'casacader',
          title: '选择地址',
          colProps: { span: 12 },
          keys: ['province', 'city', 'area', 'street'],
          required: true,
        }),
        {
          key: 'address',
          title: '详细地址',
          colProps: { span: 6 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'longitude',
          title: '经度',
          colProps: { span: 3 },
        },
        {
          key: 'latitude',
          title: '纬度',
          colProps: { span: 3 },
        },
        MyFormItems.EnumSelect({
          key: 'property_type',
          title: '项目类型',
          colProps: { span: 6 },
          valueEnum: AssetProjectsPropertyTypeEnum,
          // required: true,
        }),
        MyFormItems.EnumSelect({
          key: 'status',
          title: '项目状态',
          colProps: { span: 6 },
          valueEnum: AssetProjectsStatusEnum,
          // required: true,
        }),
        MyFormItems.EnumSelect({
          key: 'entrust_type',
          title: '委托类型',
          colProps: { span: 6 },
          valueEnum: AssetProjectsEntrustTypeEnum,
          // required: true,
        }),
        MyFormItems.EnumRadio({
          key: 'charge',
          title: '收费方式',
          colProps: { span: 6 },
          valueEnum: AssetProjectsChargeEnum,
          // required: true,
        }),
        {
          key: 'takeover_date',
          title: '接管日期',
          valueType: 'date',
          fieldProps: {
            style: {
              width: '100%',
            },
          },
          colProps: { span: 6 },
        },
        {
          key: 'closure_date',
          title: '封园日期',
          valueType: 'date',
          fieldProps: {
            style: {
              width: '100%',
            },
          },
          colProps: { span: 6 },
        },
      ]}
    />
  );
}
