import {
  MyBetaModalFormProps,
  MyButtons,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
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.HouseCharge.HouseDoorCards.Update>
      {...MyModalFormProps.props}
      title={`编辑${props.title}`}
      trigger={<MyButtons.Edit />}
      wrapperCol={{ span: 24 }}
      width="600px"
      style={{ maxHeight: '680px', overflowY: 'auto', overflowX: 'hidden' }}
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue(props.item);
        }
      }}
      onFinish={async (values: any) =>
        Apis.HouseCharge.HouseDoorCards.Update({
          ...values,
          is_enable: values.is_enable ? 1 : 0,
          id: props.item?.id ?? 0,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '编辑成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        Selects?.AssetProjects({
          title: '选择项目',
          key: 'asset_projects_id',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
          fieldProps: {
            onChange: () => {
              // 切换计量单位时清空计费模式
              form.setFieldValue('asset_buildings_id', undefined);
              form.setFieldValue('asset_units_id', undefined);
              form.setFieldValue('asset_floors_id', undefined);
            },
          },
        }),
        {
          valueType: 'dependency',
          name: ['asset_projects_id', 'asset_buildings_id', 'asset_units_id'],
          columns: ({
            asset_projects_id,
            asset_buildings_id,
            asset_units_id,
          }) => {
            return [
              {
                valueType: 'group',
                columns: [
                  Selects?.AssetBuildings({
                    key: 'asset_buildings_id',
                    title: '楼栋',
                    params: {
                      asset_projects_id: asset_projects_id,
                    },
                    colProps: { span: 8 },
                    formItemProps: {
                      ...rulesHelper.text,
                    },
                    fieldProps: {
                      showSearch: true,
                      onChange: () => {
                        form.setFieldsValue({
                          asset_units_id: undefined,
                          asset_floors_id: undefined,
                        });
                      },
                    },
                  }),

                  Selects?.AssetUnits({
                    key: 'asset_units_id',
                    title: '单元',
                    params: {
                      asset_projects_id: asset_projects_id,
                      asset_buildings_id: asset_buildings_id,
                    },
                    colProps: { span: 8 },
                    formItemProps: {
                      ...rulesHelper.text,
                    },
                  }),
                  Selects?.AssetHouses({
                    title: '房屋',
                    key: 'asset_houses_id',
                    params: {
                      asset_projects_id: asset_projects_id,
                      asset_buildings_id: asset_buildings_id,
                      asset_units_id: asset_units_id,
                    },
                    formItemProps: {
                      ...rulesHelper.text,
                    },
                    colProps: { span: 8 },
                  }),
                ],
              },
            ];
          },
        },
        {
          key: 'card_number',
          title: '门禁卡编号',
          formItemProps: { ...rulesHelper.text },
          colProps: { span: 24 },
        },
        {
          key: 'name',
          title: '客户姓名',
          formItemProps: { ...rulesHelper.text },
          colProps: { span: 12 },
        },
        {
          title: '客户电话',
          key: 'phone',
          colProps: { span: 12 },
          fieldProps: {
            maxLength: 11,
          },
          formItemProps: { ...rulesHelper.text },
        },
        {
          title: '申请时间',
          key: 'process_date',
          colProps: { span: 12 },
          valueType: 'date',
          fieldProps: {
            style: {
              width: '100%',
            },
          },
          formItemProps: { ...rulesHelper.text },
        },
        {
          title: '人工成本费',
          key: 'labor_cost',
          colProps: { span: 12 },
          fieldProps: {
            prefix: '¥',
          },
          formItemProps: { ...rulesHelper.text },
        },
        {
          title: '是否启用',
          key: 'is_enable',
          valueType: 'switch',
          colProps: { span: 24 },
        },
      ]}
    />
  );
}
