const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let antd = require("antd");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
require("./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.default.createElement(antd.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.default.createElement(antd.Select, {
		allowClear: false,
		options: col.options.map((o) => ({
			label: o.label,
			value: o.value
		})),
		style: { width: "100%" }
	});
	return /* @__PURE__ */ react.default.createElement(antd.Input, null);
}
/** 简单通用表单：可编辑记录中的字段；支持 columns 配置标签与控件类型 */
function FloorMapDataForm(props) {
	const { dataSourceKey, recordId, record, dataSources, onSave, saving, columns, className } = props;
	const [form] = antd.Form.useForm();
	const [changed, setChanged] = (0, react.useState)(false);
	const initialValues = react.default.useMemo(() => require_objectSpread2._objectSpread2({}, record), [record]);
	const handleFinish = (0, react.useCallback)((values) => {
		onSave(dataSourceKey, recordId, values);
		setChanged(false);
	}, [
		dataSourceKey,
		recordId,
		onSave
	]);
	const handleValuesChange = (0, react.useCallback)(() => setChanged(true), []);
	(0, react.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.default.createElement("div", { className: (0, classnames.default)(`${PREFIX}-data-form`, className) }, /* @__PURE__ */ react.default.createElement("p", { className: `${PREFIX}-data-form-empty` }, _pisell_utils.locales.getText(`${I18N_DF}.empty`)));
	return /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)(`${PREFIX}-data-form`, className) }, /* @__PURE__ */ react.default.createElement(antd.Form, {
		form,
		key: recordId,
		layout: "vertical",
		size: "small",
		initialValues,
		onFinish: handleFinish,
		onValuesChange: handleValuesChange
	}, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: "id",
		label: "ID",
		hidden: true
	}, /* @__PURE__ */ react.default.createElement(antd.Input, { disabled: true })), fieldsToRender.map((col) => /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		key: col.key,
		name: col.key,
		label: col.label
	}, renderFormControl(col, record[col.key]))), /* @__PURE__ */ react.default.createElement(antd.Form.Item, null, /* @__PURE__ */ react.default.createElement(antd.Button, {
		type: "primary",
		htmlType: "submit",
		loading: saving,
		disabled: !changed
	}, _pisell_utils.locales.getText(`${I18N_DF}.save-record`)))));
}
//#endregion
exports.default = FloorMapDataForm;
