diff --git a/src/components/combining/index.jsx b/src/components/combining/index.jsx new file mode 100644 index 00000000..749cb9d1 --- /dev/null +++ b/src/components/combining/index.jsx @@ -0,0 +1,393 @@ +import FormItem from "@/components/form-item/main" +import Form from "@/components/form/main" +import Grid from "@/components/gird/main.js" +import Ipt from "@/components/input/main" +import KnockGold from "@/components/knockGold" +import { knockGoldFun } from "@/tools/index" +import { useSetState } from "ahooks" +import { isSameDay } from "date-fns" +import { map } from "lodash-es" +import moment from "moment" +import React, { forwardRef, useEffect, useImperativeHandle, useRef } from "react" +import { Button, Card, DateRangePicker, Input, Notify, Select, Drawer } from "zent" +import { codeInfoRules, rulesInfoRules, tableColumn } from "./static" + +function initArray(targetNum) { + return Array.from({ length: targetNum }, (_, index) => index) +} + +const Combining = forwardRef((props, ref) => { + const { propsData } = props + const codeInfoEl = useRef(null) + const codeRuleEl = useRef(null) + const table_el = useRef(null) + const knockGoldRef = useRef(null) + + const [state, setState] = useSetState({ + rankOptions: [], + rank: [], + tableData: [], + visible: false, + info: { + describe: "", + code_name: "", + issued: "", + date_time: "" + } + }) + + useEffect(() => { + if (propsData && propsData !== "") { + setState({ id: propsData.id, status: propsData.status }) + editFun() + } else { + // 如果是新增,给计划时间 + let deDateTime = sessionStorage.getItem("knockGold_effectDate") + if (deDateTime) { + } + } + }, []) + + useImperativeHandle(ref, () => ({ + submit, + getModel + })) + + /* 提交 */ + function submit() { + let validator = false + if (codeInfoEl.current.validator()) { + validator = true + } else { + validator = false + return validator + } + + if (codeRuleEl.current.validator()) { + validator = true + } else { + validator = false + return validator + } + + if (validator) { + if (state.rank.length > 0) { + let arr = state.tableData.filter((item) => item.checked) + // 第二步:最终的校验 + // 所选商品库存和总发行数校验 + let sum = 0 + arr.map((item) => { + sum += parseInt(item.quantity) + }) + // if (sum < Number(info.issued)) { + // Notify.error("所选商品库存总数小于发放量") + // return false + // } + } else { + Notify.error(`请选择商品范围`) + return false + } + } + + return validator + } + + /* 商品提交 */ + function productSubmit() { + if (knockGoldRef.current.submit()) { + } + } + + /* 获取数据 */ + function getModel() { + let param = { + id: state.id, + status: state.status, + product: { + legal: [], + reduce: [], + cash: [] + } + } + let arr = state.tableData.filter((item) => item.checked) + param.product.legal = arr.filter((item) => item.type === 1) // 商品 + let param_lj = arr.filter((item) => item.type === 2) // 立减金 + const redPacketsData = arr.filter((item) => item.type === 3) // 红包 + + param.product.reduce = param_lj.map((item) => { + return { + ...item.only + } + }) + param.product.cash = redPacketsData.map((item) => { + return { + ...item.only + } + }) + + return param + } + + function editFun() { + // 立减金 + let lj_arr = propsData.product.reduce.map((item) => { + let table_obj = knockGoldFun(item) + return table_obj + }) + + let arr = map(lj_arr, (res) => { + let obj = {} + obj.key = res.product_id + obj.text = res.product_name + return obj + }) + + setState({ + rankOptions: arr, + rank: arr, + tableData: arr + }) + } + + /* 选择商品 */ + function onRankChange(e) { + let new_table_data = state.tableData.map((item) => { + item.checked = + e.findIndex((checks) => { + return checks.key == item.product_id + }) > -1 + return item + }) + setState({ tableData: new_table_data, rank: e }) + } + + /* 新增 */ + function addProduct() { + sessionStorage.setItem( + "knockGold_effectDate", + JSON.stringify({ + // begin_time: info.date_time[0], + // end_time: info.date_time[1] + }) + ) + setState({ visible: true }) + } + + /* 编辑 */ + function productEdit(rowData, index) { + let obj = rowData.only + setState({ productData: obj }) + } + + /* 表格选择 */ + function selectionFun(e) { + let arr = [] + map(e, (res) => { + let obj = {} + obj.key = res.product_id + obj.text = res.product_name + arr.push(obj) + return obj + }) + setState({ rank: arr }) + } + + function onDisabledTime(date, type) { + let step1 = JSON.parse(sessionStorage.getItem("knockGold_effectDate")) + const min = new Date(step1.begin_time) + const hour = min.getHours() + const minute = min.getMinutes() + const second = min.getSeconds() + const isSame = isSameDay(date, min) + + return isSame + ? { + disabledHours: () => initArray(hour), + disabledMinutes: (hourValue) => (hourValue === hour ? initArray(minute) : []), + disabledSeconds: (hourValue, minuteValue) => + hourValue === hour && minuteValue === minute ? initArray(second) : [] + } + : {} + } + + function onDisabledRange(date, type) { + let step1 = JSON.parse(sessionStorage.getItem("knockGold_effectDate")) + let isdisabled = false + let str = moment(date).format("YYYY-MM-DD HH:mm:ss") + const isDay = moment(step1.end_time).format("HH:mm:ss") + if (type == "start") { + if (isDay < "23:59:59") { + isdisabled = + moment(str).isBefore(step1.begin_time) || + moment(str).subtract(1, "days").isAfter(step1.end_time) + } else { + isdisabled = moment(str).isBefore(step1.begin_time) || moment(str).isAfter(step1.end_time) + } + } + + if (type == "end") { + isdisabled = + moment(str).add(1, "days").isBefore(step1.begin_time) || moment(str).isAfter(step1.end_time) + } + return isdisabled + } + + return ( +
+ +
+
+ + {}} + onClearItem={(e) => {}} + value={state.info.code_name} + placeholder={"请输入兑换码名称"} + labelWidth={"0px"} + maxLength={10} + height={"36px"} + width={"520px"} + alignment={"left"} + /> + + + {}} + onClearItem={(e) => {}} + unit="条" + countShow={false} + value={state.info.issued} + placeholder={"请输入发放总量"} + labelWidth={"0px"} + maxLength={12} + height={"36px"} + width={"520px"} + alignment={"left"} + /> + + + {}} + /> + +
+
+
+ + +
+
+ + {}} + disabledTime={onDisabledTime} + disabledDate={onDisabledRange} + /> + + +
+