import { useEffect, useState } from 'react' import { Table, Form, Modal, Button, Radio, Input, Flex, Card, Select, Typography, InputNumber, Checkbox, DatePicker, Space } from 'antd' import { useTranslation } from 'react-i18next' import dayjs from 'dayjs' import { CloseOutlined, StarTwoTone, PlusOutlined } from '@ant-design/icons'; import { useDatePresets } from '@/hooks/useDatePresets' import useProductsStore from '@/stores/Products/Index' const { RangePicker } = DatePicker const PriceInput = (props) => { const { id, value = {}, onChange } = props const [numberStart, setNumberStart] = useState(0) const [numberEnd, setNumberEnd] = useState(0) const [audultPrice, setAudultPrice] = useState(0) const [childrenPrice, setChildrenPrice] = useState(0) const triggerChange = (changedValue) => { onChange?.({ numberStart, numberEnd, audultPrice, childrenPrice, ...value, ...changedValue, }) } const onNumberStartChange = (e) => { const newNumber = parseInt(e.target.value || '0', 10) if (Number.isNaN(numberStart)) { return } if (!('numberStart' in value)) { setNumberStart(newNumber); } triggerChange({ numberStart: newNumber, }) } const onNumberEndChange = (e) => { const newNumber = parseInt(e.target.value || '0', 10) if (Number.isNaN(numberEnd)) { return } if (!('numberEnd' in value)) { setNumberEnd(newNumber) } triggerChange({ numberEnd: newNumber, }); }; const onAudultPriceChange = (e) => { const newNumber = parseInt(e.target.value || '0', 10) if (Number.isNaN(audultPrice)) { return } if (!('audultPrice' in value)) { setAudultPrice(newNumber) } triggerChange({ audultPrice: newNumber, }) } const onChildrenPriceChange = (e) => { const newNumber = parseInt(e.target.value || '0', 10) if (Number.isNaN(childrenPrice)) { return } if (!('childrenPrice' in value)) { setChildrenPrice(newNumber) } triggerChange({ childrenPrice: newNumber, }) }; return ( ) } const batchSetupInitialValues = { 'defList': [ // 旺季 { 'useDate': [ dayjs().add(1, 'year').startOf('y'), dayjs().add(1, 'year').endOf('y') ], 'unitName': '每人', 'currency': 'CNY', 'weekend': [ '5', '6' ], 'priceList': [ { 'priceInput': { 'numberStart': 1, 'numberEnd': 2, 'audultPrice': 0, 'childrenPrice': 0 } }, { 'priceInput': { 'numberStart': 3, 'numberEnd': 4, 'audultPrice': 0, 'childrenPrice': 0 } }, { 'priceInput': { 'numberStart': 5, 'numberEnd': 6, 'audultPrice': 0, 'childrenPrice': 0 } }, { 'priceInput': { 'numberStart': 7, 'numberEnd': 9, 'audultPrice': 0, 'childrenPrice': 0 } } ] }, // 淡季 { 'useDate': [ dayjs().add(1, 'year').subtract(2, 'M').startOf('M'), dayjs().add(1, 'year').endOf('M') ], 'unitName': '每人', 'currency': 'CNY', 'weekend': [ '5', '6' ], 'priceList': [ { 'priceInput': { 'numberStart': 1, 'numberEnd': 2, 'audultPrice': 0, 'childrenPrice': 0 } }, { 'priceInput': { 'numberStart': 3, 'numberEnd': 4, 'audultPrice': 0, 'childrenPrice': 0 } }, { 'priceInput': { 'numberStart': 5, 'numberEnd': 6, 'audultPrice': 0, 'childrenPrice': 0 } }, { 'priceInput': { 'numberStart': 7, 'numberEnd': 9, 'audultPrice': 0, 'childrenPrice': 0 } } ] } ] } const defaultPriceValue = { 'priceInput': { 'numberStart': 1, 'numberEnd': 2, 'audultPrice': 0, 'childrenPrice': 0 } } const defaultDefinitionValue = { 'useDate': [ dayjs().add(1, 'year').subtract(2, 'M').startOf('M'), dayjs().add(1, 'year').endOf('M') ], 'unitName': '每人', 'currency': 'CNY', 'weekend': [ '5', '6' ], 'priceList': [ defaultPriceValue ] } const ProductInfoQuotation = ({ editable, ...props }) => { const { t } = useTranslation() const [isQuotationModalOpen, setQuotationModalOpen] = useState(false) const [isBatchSetupModalOpen, setBatchSetupModalOpen] = useState(false) const [quotationForm] = Form.useForm() const [batchSetupForm] = Form.useForm() const datePresets = useDatePresets() const [newEmptyQuotation, appendQuotationList, quotationList] = useProductsStore((state) => [state.newEmptyQuotation, state.appendQuotationList, state.quotationList]) useEffect(() => { console.info('quotationList: ', quotationList) }, [quotationList]) const onQuotationSeleted = async (quotation) => { // 把 start, end 转换为 RangePicker 数组格式 quotation.use_dates = [dayjs(quotation.use_dates_start), dayjs(quotation.use_dates_end)] quotationForm.setFieldsValue(quotation) setQuotationModalOpen(true) } const onNewQuotation = () => { const emptyQuotation = newEmptyQuotation() quotationForm.setFieldsValue(emptyQuotation) setQuotationModalOpen(true) } const onQuotationFinish = (values) => { console.info(values) setQuotationModalOpen(false) // saveOrUpdateQuotation(values) // .then(() => { // setQuotationModalOpen(false) // }) // .catch(ex => { // notification.error({ // message: 'Notification', // description: ex.message, // placement: 'top', // duration: 4, // }) // }) } const onQuotationFailed = (error) => { console.log('Failed:', error) // form.resetFields() } const onBatchSetupFinish = () => { // console.info(batchSetupForm.getFieldsValue()) let previewList = [] const defList = batchSetupForm.getFieldsValue().defList defList.forEach(definition => { const previewPrice = definition?.priceList.map(price => { return { id: 0, adult_cost: price.priceInput.audultPrice, child_cost: price.priceInput.childrenPrice, group_size_min: price.priceInput.numberStart, group_size_max: price.priceInput.numberEnd, currency: definition.currency, unit: definition.unitName, // 保持和 API 返回格式一致,日期要转换为字符串 use_dates_start: definition.useDate[0].format('YYYY-MM-DD'), use_dates_end: definition.useDate[1].format('YYYY-MM-DD'), weekdays: definition.weekend.join(','), } }) previewList.push(...previewPrice) }) appendQuotationList(previewList) setBatchSetupModalOpen(false) } const quotationColumns = [ { title: t('products:adultPrice'), dataIndex: 'adult_cost', width: '4rem' }, { title: t('products:childrenPrice'), dataIndex: 'child_cost', width: '4rem' }, { title: t('products:currency'), dataIndex: 'currency', width: '4rem' }, { title: t('products:Types'), dataIndex: 'unit_name', width: '4rem', render: (text) => (text === '0' ? '每人' : text === '1' ? '每团' : text), }, { title: t('products:number'), dataIndex: 'group_size', width: '4rem', render: (_, record) => `${record.group_size_min}-${record.group_size_max}`, }, { title: t('products:validityPeriod'), dataIndex: 'use_dates', width: '6rem', render: (_, record) => `${record.use_dates_start}-${record.use_dates_end}`, }, { title: t('products:Weekdays'), dataIndex: 'weekdays', width: '4rem' }, { title: t('products:operation'), dataIndex: 'operation', width: '3%', render: (_, quotation) => { return ( ) }, }, ] return ( <>

{t('products:EditComponents.Quotation')}

{ // editable && } { // editable && } onBatchSetupFinish()} onCancel={() => setBatchSetupModalOpen(false)} destroyOnClose forceRender >
{(fields, { add, remove }) => ( {fields.map((field, index) => ( : { remove(field.name) }} />} > {(priceFieldList, priceOptList) => ( {priceFieldList.map((priceField, index) => ( {index == 0 ? : priceOptList.remove(priceField.name)} />} ))} )} ))} )} {() => (
{JSON.stringify(batchSetupForm.getFieldsValue(), null, 2)}
)}
setQuotationModalOpen(false)} destroyOnClose forceRender modalRender={(dom) => (
{dom} )} > RMB USD 每人 每团
) } export default ProductInfoQuotation