const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_index = require("../../components/customSelect/index.js");
const require_index$1 = require("../../components/collapsibleList/index.js");
const require_useComponents = require("../../../../hooks/useComponents.js");
const require_RenderValueById = require("./RenderValueById.js");
const require_date = require("../../../../utils/date.js");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _pisell_icon_es_FileDownload02 = require("@pisell/icon/es/FileDownload02");
_pisell_icon_es_FileDownload02 = require_runtime.__toESM(_pisell_icon_es_FileDownload02);
require("./index.less");
//#region src/components/booking/forms/single/index.tsx
const queryValueById = [
	"checkbox",
	"radioButton",
	"select",
	"form_to_form"
];
/**
* @title: 表单选择器
* @description:
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-01-30 17:06
*/
const Single = ({ value, onChange, form, mode, optionRender, loading, customSelectProps, onAdd, addButtonProps, modalOpen, watch, drawerProps }) => {
	var _form$form4, _form$form5;
	const [HumanBodySelector] = require_useComponents.default({ components: ["HumanBodySelector"] });
	/**
	* @title: 渲染表单内容
	* @description:
	* @param {any} fieldData
	* @param {any} field
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-01-30 17:06
	*/
	const renderFieldValue = (fieldData, field) => {
		const val = fieldData[field.key];
		if (field.filed_type === "attachment") {
			if (Array.isArray(val)) return val.map((item) => {
				if (item.type.indexOf("image") !== -1) return /* @__PURE__ */ react.default.createElement("img", {
					className: "attachment-item",
					src: _pisell_utils.image.ali(item.url, 200),
					key: _pisell_utils.image.ali(item.url, 200)
				});
				return /* @__PURE__ */ react.default.createElement("a", {
					href: item.url,
					key: item.url,
					style: {
						display: "block",
						marginTop: 8
					},
					onClick: (e) => {
						e.stopPropagation();
					}
				}, /* @__PURE__ */ react.default.createElement(_pisell_icon_es_FileDownload02.default, { style: { fontSize: 40 } }));
			});
		}
		if (field.filed_type === "customer_cover") return /* @__PURE__ */ react.default.createElement(antd.Avatar, { src: val });
		if (field.filed_type === "signature") return /* @__PURE__ */ react.default.createElement("img", {
			className: "attachment-item",
			src: _pisell_utils.image.ali(val, 200),
			key: _pisell_utils.image.ali(val, 200)
		});
		if (field.filed_type === "customer_phone") {
			if (val) return `${val.country_calling_code && `(+${val.country_calling_code})`} ${val.phone || ""}`;
		}
		if (field.filed_type === "body_hotspot_image") return HumanBodySelector ? /* @__PURE__ */ react.default.createElement(HumanBodySelector, {
			value: val,
			disable: true
		}) : val;
		if (field.filed_type === "agreement") return val === 1 ? _pisell_utils.locales.getText("pisell2.booking.info.text.form-agreement-yes") : _pisell_utils.locales.getText("pisell2.booking.info.text.form-agreement-no");
		if (queryValueById.includes(field === null || field === void 0 ? void 0 : field.filed_type)) return /* @__PURE__ */ react.default.createElement(require_RenderValueById.default, {
			field,
			value: val
		});
		if ((field === null || field === void 0 ? void 0 : field.filed_type) === "media") return null;
		return val || "-";
	};
	/**
	* @title:渲染表单数据
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-01-30 17:07
	*/
	const renderItem = (0, react.useCallback)((fieldData) => {
		let _fields = form.field || [];
		_fields = _fields.filter((item) => item.status !== "hide");
		return /* @__PURE__ */ react.default.createElement(require_index$1.default, {
			list: _fields,
			renderItem: (field) => {
				return /* @__PURE__ */ react.default.createElement("div", {
					className: "pet-form-item",
					key: field.key
				}, /* @__PURE__ */ react.default.createElement("div", { className: "pet-form-item-label" }, field.field_column.title), /* @__PURE__ */ react.default.createElement("div", { className: "pet-form-item-value" }, renderFieldValue(fieldData, field)));
			}
		});
	}, [form]);
	console.log("fff", form);
	const ignoreValidity = (0, react.useMemo)(() => {
		var _form$form;
		return !(form === null || form === void 0 || (_form$form = form.form) === null || _form$form === void 0 || (_form$form = _form$form.setting) === null || _form$form === void 0 ? void 0 : _form$form.validity) || form.form.setting.validity.type === "no_expiry";
	}, [form === null || form === void 0 ? void 0 : form.form]);
	const getExpireStatus = (expiredAt) => {
		if (ignoreValidity || !expiredAt) return null;
		const now = (0, dayjs.default)();
		const expireDate = (0, dayjs.default)(expiredAt).endOf("day");
		const diffDays = expireDate.diff(now, "day");
		const formattedDate = require_date.formatDate(expireDate, "YYYY-MM-DD", "DD/MM/YYYY");
		if (now.isAfter(expireDate)) return /* @__PURE__ */ react.default.createElement("div", { style: {
			color: "#999999",
			fontSize: "14px",
			lineHeight: "20px"
		} }, /* @__PURE__ */ react.default.createElement(antd.Tag, {
			color: "red",
			style: {
				borderRadius: 16,
				marginRight: 8
			}
		}, _pisell_utils.locales.getText("pisell2.text.status.expired")), _pisell_utils.locales.getText("pisell2.text.validity.expired")(formattedDate));
		if (diffDays < 7) return /* @__PURE__ */ react.default.createElement("div", { style: {
			color: "#FFA940",
			fontSize: "14px",
			lineHeight: "20px"
		} }, _pisell_utils.locales.getText("pisell2.text.validity.expiring_soon")(formattedDate));
		return /* @__PURE__ */ react.default.createElement("div", { style: {
			color: "#999999",
			fontSize: "14px",
			lineHeight: "20px"
		} }, _pisell_utils.locales.getText("pisell2.text.validity.valid_through")(formattedDate));
	};
	/**
	* @title: 渲染字段
	* @description:
	* @param {any} item
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-01-30 17:08
	*/
	const _optionRender = (item) => {
		if (optionRender) return optionRender(item);
		return /* @__PURE__ */ react.default.createElement("div", { style: { flex: 1 } }, /* @__PURE__ */ react.default.createElement("div", { style: {
			color: "#1B1B1B",
			fontSize: 16,
			fontWeight: 600,
			lineHeight: "22px"
		} }, item["main_field"] || item[form.option_key] || "-"), /* @__PURE__ */ react.default.createElement("div", null, getExpireStatus(item.expired_at)));
	};
	/**
	* @title: 添加按钮
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-01-30 17:09
	*/
	const _addButtonProps = (0, react.useMemo)(() => {
		var _form$form2, _form$form3;
		let obj = require_objectSpread2._objectSpread2({
			title: `${_pisell_utils.locales.getText("pisell2.text.add-a")} ${((_form$form2 = form.form) === null || _form$form2 === void 0 ? void 0 : _form$form2.title) || "-"}`,
			subTitle: `${_pisell_utils.locales.getText("pisell2.text.add-another")} ${((_form$form3 = form.form) === null || _form$form3 === void 0 ? void 0 : _form$form3.title) || "-"}`
		}, addButtonProps);
		if (["form", "table"].includes(form.type)) obj.onClick = onAdd;
		return obj;
	}, [
		form,
		addButtonProps,
		value,
		watch
	]);
	/**
	* @title: 统一 form 为单选  table为多选
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-01-30 17:09
	*/
	const _mode = (0, react.useMemo)(() => {
		if (["form"].includes(mode)) return "single";
		if (["table"].includes(mode)) return "multiple";
		return mode;
	}, [mode, form]);
	return /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__booking-pet-cards" }, /* @__PURE__ */ react.default.createElement(require_index.default, require_objectSpread2._objectSpread2({
		loading,
		value,
		options: form.options || [],
		onValueChange: (e) => {
			onChange(e);
		},
		modalOpen,
		mode: _mode,
		previewItemClassName: "pisell-lowcode__booking-pet-card",
		previewItemRender: (d) => {
			return renderItem(d);
		},
		name: ((_form$form4 = form.form) === null || _form$form4 === void 0 ? void 0 : _form$form4.title) || "-",
		addButtonProps: _addButtonProps,
		drawerProps: require_objectSpread2._objectSpread2({
			title: `${_pisell_utils.locales.getText("pisell2.text.select")} ${((_form$form5 = form.form) === null || _form$form5 === void 0 ? void 0 : _form$form5.title) || "-"}`,
			destroyOnClose: true
		}, drawerProps || {}),
		optionRender: _optionRender,
		rowKey: "form_record_id"
	}, customSelectProps || {})));
};
//#endregion
exports.default = Single;
