import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import React, { useCallback, useEffect, useState } from "react";
import { locales } from "@pisell/utils";
import { Button, Form, Input, InputNumber, Select } from "antd";
import classNames from "classnames";
import "./FloorMapDataForm.less";
//#region src/components/pisellFloorMapLayout/components/FloorMapDataForm.tsx
/**
* FloorMapDataForm - 数据源落位的数据表单，调用 onDataSourceRecordSave
* 支持通过 columns 配置列标签与控件类型（string/number/select），与表格列一致
*/
const PREFIX = "pisell-floor-map-layout";
const I18N_DF = "pisell-floor-map-layout.data-form";
function renderFormControl(col, value) {
	var _col$type, _col$options;
	const type = (_col$type = col.type) !== null && _col$type !== void 0 ? _col$type : "string";
	if (type === "number") return /* @__PURE__ */ React.createElement(InputNumber, {
		style: { width: "100%" },
		min: 0
	});
	if (type === "select" && ((_col$options = col.options) === null || _col$options === void 0 ? void 0 : _col$options.length)) return /* @__PURE__ */ React.createElement(Select, {
		allowClear: false,
		options: col.options.map((o) => ({
			label: o.label,
			value: o.value
		})),
		style: { width: "100%" }
	});
	return /* @__PURE__ */ React.createElement(Input, null);
}
/** 简单通用表单：可编辑记录中的字段；支持 columns 配置标签与控件类型 */
function FloorMapDataForm(props) {
	const { dataSourceKey, recordId, record, dataSources, onSave, saving, columns, className } = props;
	const [form] = Form.useForm();
	const [changed, setChanged] = useState(false);
	const initialValues = React.useMemo(() => _objectSpread2({}, record), [record]);
	const handleFinish = useCallback((values) => {
		onSave(dataSourceKey, recordId, values);
		setChanged(false);
	}, [
		dataSourceKey,
		recordId,
		onSave
	]);
	const handleValuesChange = useCallback(() => setChanged(true), []);
	useEffect(() => {
		form.setFieldsValue(record);
	}, [
		recordId,
		record,
		form
	]);
	const fieldsToRender = columns ? columns.filter((col) => col.key !== "id") : Object.entries(record).filter(([k]) => k !== "id").map(([key]) => ({
		key,
		label: key,
		type: "string"
	}));
	if (fieldsToRender.length === 0) return /* @__PURE__ */ React.createElement("div", { className: classNames(`${PREFIX}-data-form`, className) }, /* @__PURE__ */ React.createElement("p", { className: `${PREFIX}-data-form-empty` }, locales.getText(`${I18N_DF}.empty`)));
	return /* @__PURE__ */ React.createElement("div", { className: classNames(`${PREFIX}-data-form`, className) }, /* @__PURE__ */ React.createElement(Form, {
		form,
		key: recordId,
		layout: "vertical",
		size: "small",
		initialValues,
		onFinish: handleFinish,
		onValuesChange: handleValuesChange
	}, /* @__PURE__ */ React.createElement(Form.Item, {
		name: "id",
		label: "ID",
		hidden: true
	}, /* @__PURE__ */ React.createElement(Input, { disabled: true })), fieldsToRender.map((col) => /* @__PURE__ */ React.createElement(Form.Item, {
		key: col.key,
		name: col.key,
		label: col.label
	}, renderFormControl(col, record[col.key]))), /* @__PURE__ */ React.createElement(Form.Item, null, /* @__PURE__ */ React.createElement(Button, {
		type: "primary",
		htmlType: "submit",
		loading: saving,
		disabled: !changed
	}, locales.getText(`${I18N_DF}.save-record`)))));
}
//#endregion
export { FloorMapDataForm as default };
