const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_objectWithoutProperties = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
const require_useEngineContext = require("../../../hooks/useEngineContext.js");
const require_index = require("../../../utils/index.js");
const require_index$1 = require("./PhoneField/index.js");
const require_index$2 = require("./GenderField/index.js");
const require_serve = require("../serve.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let ahooks = require("ahooks");
require("./index.less");
//#region src/plus/clinetSearch/Add/index.tsx
const _excluded = ["phone"];
const AddClient = (0, react.forwardRef)((props, ref) => {
	var _context$appHelper;
	const context = require_useEngineContext.default();
	require_index.request.setRequest((_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.request);
	const { onChange, phone, containerProps } = props;
	const [form] = antd.Form.useForm();
	const useIsChecked = antd.Form.useWatch("is_checked", form);
	(0, react.useEffect)(() => {
		form.setFieldsValue({ phone: {
			code: "+61",
			phone
		} });
	}, [phone]);
	(0, react.useImperativeHandle)(ref, () => ({ reset: () => {
		form.resetFields();
	} }));
	const _addClient = (0, ahooks.useRequest)(require_serve.addCustomer, {
		manual: true,
		onSuccess: (res, param) => {
			var _param$;
			onChange(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, (_param$ = param[0]) === null || _param$ === void 0 ? void 0 : _param$.customer), {}, { id: res.customer_id }), useIsChecked);
		}
	});
	const onFinish = (values) => {
		var _values$phone;
		const { phone } = values, rest = require_objectWithoutProperties._objectWithoutProperties(values, _excluded);
		if (!values.first_name && !values.last_name && !values.email && !(values === null || values === void 0 || (_values$phone = values.phone) === null || _values$phone === void 0 ? void 0 : _values$phone.phone)) {
			antd.message.info(_pisell_utils.locales.getText("pisell2.text.add-client.confirm"));
			return;
		}
		_addClient.run({
			customer: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({
				accepts_marketing: 0,
				register_channel: "pos",
				tax_exempt: 1
			}, rest), {}, {
				first_name: rest.first_name || "",
				last_name: rest.last_name || "",
				note: rest.note || "",
				email: rest.email || "",
				country_calling_code: phone.code || "",
				phone: phone.phone || ""
			}),
			address: {
				first_name: "",
				last_name: "",
				company: "",
				address1: "",
				address2: "",
				city: "",
				country_code: "AU",
				province_code: "ACT",
				phone: "",
				country: "澳大利亚",
				province: "Australian Capital Territory",
				country_calling_code: "+61"
			},
			tags: []
		});
	};
	const renderContent = () => {
		return /* @__PURE__ */ react.default.createElement(antd.Form, {
			form,
			onFinish,
			layout: "vertical",
			initialValues: {
				is_checked: 0,
				phone: {
					code: "+61",
					phone: ""
				}
			},
			size: "large"
		}, /* @__PURE__ */ react.default.createElement(antd.Row, { gutter: [16, 0] }, /* @__PURE__ */ react.default.createElement(antd.Col, { span: 12 }, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
			label: _pisell_utils.locales.getText("pisell2.text.firstname"),
			name: "first_name"
		}, /* @__PURE__ */ react.default.createElement(antd.Input, {
			placeholder: _pisell_utils.locales.getText("pisell2.text.firstname"),
			size: "large"
		}))), /* @__PURE__ */ react.default.createElement(antd.Col, { span: 12 }, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
			label: _pisell_utils.locales.getText("pisell2.text.lastname"),
			name: "last_name"
		}, /* @__PURE__ */ react.default.createElement(antd.Input, {
			placeholder: _pisell_utils.locales.getText("pisell2.text.lastname"),
			size: "large"
		}))), /* @__PURE__ */ react.default.createElement(antd.Col, { span: 12 }, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
			label: _pisell_utils.locales.getText("pisell2.text.gender"),
			name: "gender"
		}, /* @__PURE__ */ react.default.createElement(require_index$2.default, null))), /* @__PURE__ */ react.default.createElement(antd.Col, { span: 12 }, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
			label: _pisell_utils.locales.getText("pisell2.text.birthday"),
			name: "birthday"
		}, /* @__PURE__ */ react.default.createElement(antd.DatePicker, {
			style: { width: "100%" },
			size: "large",
			format: "DD/MM/YYYY",
			inputReadOnly: true,
			placeholder: _pisell_utils.locales.getText("pisell2.text.birthday"),
			placement: "bottomLeft"
		}))), /* @__PURE__ */ react.default.createElement(antd.Col, { span: 12 }, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
			label: _pisell_utils.locales.getText("pisell2.text.phone"),
			name: "phone"
		}, /* @__PURE__ */ react.default.createElement(require_index$1.default, null))), /* @__PURE__ */ react.default.createElement(antd.Col, { span: 12 }, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
			label: _pisell_utils.locales.getText("pisell2.text.email"),
			name: "email",
			rules: [{
				pattern: /^([a-zA-Z0-9_\-\.+])+@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,63})+$/,
				message: _pisell_utils.locales.getText("pisell2.text.email-error-message")
			}]
		}, /* @__PURE__ */ react.default.createElement(antd.Input, {
			placeholder: _pisell_utils.locales.getText("pisell2.text.email"),
			size: "large"
		}))), /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
			hidden: true,
			name: "is_checked"
		}, /* @__PURE__ */ react.default.createElement(antd.Input, null))));
	};
	const renderFooter = () => {
		return /* @__PURE__ */ react.default.createElement("div", { className: "select-customer-right-panel-add-client__footer" }, /* @__PURE__ */ react.default.createElement(antd.Button, {
			size: "large",
			style: { flex: 1 },
			onClick: () => {
				form.setFieldsValue({ is_checked: 0 });
				form.submit();
			},
			loading: _addClient.loading && !useIsChecked
		}, _pisell_utils.locales.getText("pisell2.text.save")), /* @__PURE__ */ react.default.createElement(antd.Button, {
			type: "primary",
			size: "large",
			style: { flex: 1 },
			onClick: () => {
				form.setFieldsValue({ is_checked: 1 });
				form.submit();
			},
			loading: _addClient.loading && useIsChecked
		}, _pisell_utils.locales.getText("pisell2.text.save-and-select")));
	};
	return /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellContainer, {
		renderType: "modal",
		props: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, containerProps), {}, {
			bodyStyle: { padding: "24px 24px 0" },
			footer: renderFooter(),
			destroyOnClose: true
		})
	}, renderContent());
});
//#endregion
exports.default = AddClient;
