| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564 |
- /* eslint-disable no-else-return */
- /* eslint-disable prefer-const */
- /* eslint-disable react/jsx-one-expression-per-line */
- /* eslint-disable no-underscore-dangle */
- /* eslint-disable no-shadow */
- import * as WebBrowser from "expo-web-browser";
- import * as React from "react";
- import { StyleSheet, View } from "react-native";
- import { useModel } from "flooks";
- import {
- Layout,
- Text,
- useTheme,
- Button,
- Input,
- Toggle,
- } from "@ui-kitten/components";
- import { useFocusEffect, useRoute } from "@react-navigation/native";
- import { useEventEmitter } from "@umijs/hooks";
- import ActionButton from "react-native-action-button";
- import ListComponent from "../../components/ListComponent";
- import NavHeaderBar from "../../components/NavHeaderBar";
- const styles = StyleSheet.create({
- lay: {
- backgroundColor: "#fff",
- flex: 1,
- },
- padBot: {
- paddingBottom: 100,
- },
- list: {
- paddingVertical: 10,
- paddingHorizontal: 15,
- backgroundColor: "transparent",
- flex: 0,
- },
- item: {
- flexDirection: "row",
- alignItems: "center",
- paddingVertical: 10,
- },
- input: {
- marginHorizontal: 5,
- minWidth: 49,
- // width: 49,
- },
- text: {
- flex: 1,
- },
- flexRow: {
- flexDirection: "row",
- alignItems: "center",
- },
- buttonlast: {
- marginLeft: 10,
- },
- button: {
- alignSelf: "flex-start",
- },
- money: {
- marginLeft: 10,
- },
- addNew2: {
- width: 80,
- alignSelf: "center",
- },
- separatorStyle: {
- height: 0,
- },
- });
- export default function GoodsSpecificationScreen() {
- const theme = useTheme();
- const { changeBackground } = useModel("barModel", true);
- // const { mid } = useModel("userModel");
- const { httpGet, httpPost } = useModel("httpModel", true);
- const { success, warnning } = useModel("loadingModel", true);
- const { showDialog } = useModel("dialogModel");
- const [allEditInfo, setEdit] = React.useState([]);
- const [goodsId, setGoodsId] = React.useState(0);
- const [startState, changeState] = React.useState(true);
- const [editList, setEditList] = React.useState([]);
- const [addNew, changeNew] = React.useState([]);
- const list$ = useEventEmitter();
- const {
- delText,
- editText,
- confirm,
- cancel,
- successText,
- removeTips,
- YMZEFF,
- MUKYDG,
- TMMNWL,
- IQYCDU,
- GVDYKL,
- BCXVJS,
- CUYTPO,
- MLPRPD,
- } = useModel("wordsModel");
- const { addClassGoods } = useModel("goodsModel");
- useFocusEffect(
- React.useCallback(() => {
- changeBackground(theme["color-primary-500"]);
- }, [])
- );
- const route = useRoute();
- function getList() {
- const { params } = route;
- const { goodsId } = params || {};
- setGoodsId(goodsId);
- return httpGet(
- "/goodsSpecification/byGoodsId",
- {
- goodsId: goodsId || 0,
- },
- true
- ).then(res => {
- changeState(false);
- let isNew = false;
- if (res.length === 0) {
- isNew = true;
- changeNew([]);
- } else {
- isNew = false;
- }
- let content = [];
- if (res.length > 0) {
- let parents = res.filter(item => {
- return !item.parent;
- });
- parents.forEach(item => {
- content.push(item);
- let _children = res.filter(_f => {
- return _f.parent === item.id;
- });
- content = content.concat(_children);
- });
- }
- if (isNew) {
- content.push({ name: "", multiple: false, addIndex: 0 });
- changeNew([{ name: "", multiple: false, addIndex: 0 }]);
- } else {
- addNew.forEach((item, index) => {
- if (item.addType === "1") {
- content.push({ name: "", multiple: false, addIndex: index });
- } else {
- let _index = content.findIndex(_i => {
- return _i.id === item.parent;
- });
- content.splice(_index + 1, 0, {
- name: "",
- amount: "",
- parent: item.parent,
- addIndex: index,
- });
- }
- });
- }
- content = content.map(item => {
- const _info = {
- ...item,
- amount: item.amount != null ? item.amount.toString() : "",
- };
- if (!_info.id || editList.indexOf(_info.id) !== -1) {
- _info.edit = true;
- } else {
- _info.edit = false;
- }
- return _info;
- });
- setEdit(content);
- return Promise.resolve({
- content,
- last: true,
- });
- });
- }
- const listByOrder = (list1, list2, type, extra) => {
- let list = [...list1];
- if (type === "add") {
- list2.forEach((item, index) => {
- if (item.addType === "1") {
- list.push({ name: "", multiple: false, addIndex: index });
- } else {
- let _index = list.findIndex(_i => {
- return _i.id === item.parent;
- });
- list.splice(_index + 1, 0, {
- name: "",
- amount: "",
- parent: item.parent,
- addIndex: index,
- });
- }
- });
- } else if (type === "save") {
- list = list.map(item => {
- if (
- (item.addIndex === extra && extra != null) ||
- item.id === list2[0].id
- ) {
- item = { ...list2[0] };
- }
- return item;
- });
- } else if (type === "edit") {
- list = list.map(item => {
- if (item.id === extra) {
- item = {
- ...item,
- edit: true,
- };
- }
- return item;
- });
- } else if (type === "cancel" && extra.id) {
- list = list.map(item => {
- if (item.id === extra.id) {
- item.edit = false;
- }
- return item;
- });
- } else if (type === "del" || (!extra.id && type === "cancel")) {
- list = list.filter(item => {
- return (
- (extra.id && item.id !== extra.id) ||
- (!extra.id && item.addIndex !== extra.addIndex)
- );
- });
- }
- setEdit(list);
- return list;
- };
- const editInfo = id => {
- const _editList = [...editList];
- _editList.push(id);
- setEditList(_editList);
- list$.emit({
- type: "edit",
- list: [],
- changeEvent: listByOrder,
- extra: id,
- });
- };
- const delInfo = info => {
- showDialog({
- bodyText: removeTips,
- status: "danger",
- cancelable: true,
- confirmCallback: () => {
- if (!info.parent) {
- let _allEditInfo = [...allEditInfo];
- let _children = _allEditInfo.filter(item => {
- return item.parent === info.id;
- });
- if (_children.length > 0) {
- showDialog({
- bodyText: YMZEFF,
- status: "danger",
- });
- return;
- }
- }
- httpPost(`/goodsSpecification/del/${info.id}`)
- .then(() => {
- success(successText);
- // changeState(true);
- list$.emit({
- type: "del",
- list: [],
- changeEvent: listByOrder,
- extra: info,
- });
- })
- .catch(e => {
- warnning(e.error);
- });
- },
- });
- };
- // 取消
- const cancelInfo = info => {
- if (info.id) {
- let _editList = [...editList];
- _editList = _editList.filter(item => {
- return item !== info.id;
- });
- setEditList(_editList);
- list$.emit({
- type: "cancel",
- list: [],
- changeEvent: listByOrder,
- extra: info,
- });
- } else {
- let _addNew = [...addNew];
- _addNew.splice(info.addIndex, 1);
- changeNew(_addNew);
- list$.emit({
- type: "cancel",
- list: [],
- changeEvent: listByOrder,
- extra: info,
- });
- }
- // changeState(true);
- };
- // 保存
- const saveInfo = info => {
- // const _editInfo = { ...allEditInfo };
- // let name = "";
- // let amount = "";
- // if (_editInfo[info.id || "new"]) {
- // name = _editInfo[info.id || "new"].name;
- // amount = _editInfo[info.id || "new"].amount;
- // }
- const data = {
- ...info,
- goodsId,
- };
- delete data.addIndex;
- addClassGoods(data).then(res => {
- if (info.id) {
- let _editList = [...editList];
- _editList = _editList.filter(item => {
- return item !== info.id;
- });
- setEditList(_editList);
- list$.emit({
- type: "save",
- list: [res],
- changeEvent: listByOrder,
- });
- } else {
- let _addNew = [...addNew];
- _addNew.splice(info.addIndex, 1);
- changeNew(_addNew);
- list$.emit({
- type: "save",
- list: [res],
- extra: info.addIndex,
- changeEvent: listByOrder,
- });
- }
- });
- };
- //
- const addFullReduction = info => {
- let list = [...addNew];
- let hasType1 = list.find(item => {
- return item.addType === "1";
- });
- if (info && info.parent) {
- let hasParent = list.find(item => {
- return item.parent === info.parent;
- });
- if (!hasParent) {
- list.push(info);
- changeNew(list);
- list$.emit({
- type: "add",
- list: [
- {
- ...info,
- },
- ],
- changeEvent: listByOrder,
- });
- }
- } else if (!info && !hasType1) {
- list.push({
- addType: "1",
- });
- changeNew(list);
- list$.emit({
- type: "add",
- list: [
- {
- addType: "1",
- },
- ],
- changeEvent: listByOrder,
- });
- }
- };
- const saveItem = (info, index) => {
- let editNowInfo = { ...info };
- return (
- <View Key={index}>
- <Layout style={styles.item}>
- <Layout style={[styles.text, styles.flexRow]}>
- {editNowInfo.parent ? (
- <Text category="c1">
- {MUKYDG}
- {editNowInfo.name}
- </Text>
- ) : (
- <Text category="s1">
- {TMMNWL}
- {editNowInfo.name}
- </Text>
- )}
- {!!editNowInfo.parent && (
- <Layout style={styles.money}>
- <Text category="c1">¥{editNowInfo.amount}</Text>
- </Layout>
- )}
- {!editNowInfo.parent && (
- <Layout style={styles.money}>
- <Text category="s1">
- {editNowInfo.multiple ? { IQYCDU } : { GVDYKL }}
- </Text>
- </Layout>
- )}
- </Layout>
- <Button
- size="small"
- appearance="outline"
- onPress={() => editInfo(editNowInfo.id)}
- >
- {editText}
- </Button>
- <Button
- size="small"
- status="danger"
- style={styles.buttonlast}
- onPress={() => delInfo(editNowInfo, index)}
- >
- {delText}
- </Button>
- </Layout>
- {!editNowInfo.parent && (
- <Button
- style={styles.addNew2}
- onPress={() =>
- addFullReduction({ addType: "2", parent: editNowInfo.id })
- }
- >
- {MUKYDG}
- </Button>
- )}
- </View>
- );
- };
- const editItem = (info, index) => {
- let _allEditInfo = [...allEditInfo];
- let editNowInfo = { ..._allEditInfo[index] };
- return (
- <Layout style={styles.item}>
- <Layout style={[styles.text, styles.flexRow]}>
- <Text>{editNowInfo.parent ? { BCXVJS } : { TMMNWL }}</Text>
- <Input
- size="small"
- defaultValue={editNowInfo.name}
- style={styles.input}
- key={`${index}_0`}
- onChangeText={text => {
- editNowInfo.name = text;
- _allEditInfo.splice(index, 1, editNowInfo);
- setEdit(_allEditInfo);
- }}
- />
- {!!editNowInfo.parent && (
- <>
- <Text>价钱</Text>
- <Input
- size="small"
- defaultValue={info.amount}
- style={styles.input}
- key={`${index}_2`}
- keyboardType="numeric"
- onChangeText={text => {
- editNowInfo.amount = text;
- _allEditInfo.splice(index, 1, editNowInfo);
- setEdit(_allEditInfo);
- }}
- />
- </>
- )}
- {!editNowInfo.parent && (
- <>
- <Text>{MLPRPD}</Text>
- <Toggle
- key="Toggle"
- checked={editNowInfo.multiple}
- onChange={checked => {
- editNowInfo.multiple = checked;
- _allEditInfo.splice(index, 1, editNowInfo);
- setEdit(_allEditInfo);
- }}
- />
- </>
- )}
- </Layout>
- <Button
- disabled={
- !editNowInfo.name ||
- (!!editNowInfo.parent && editNowInfo.amount === "")
- }
- size="small"
- onPress={() => saveInfo(editNowInfo)}
- >
- {confirm}
- </Button>
- <Button
- size="small"
- appearance="outline"
- style={styles.buttonlast}
- onPress={() => cancelInfo(editNowInfo)}
- >
- {cancel}
- </Button>
- </Layout>
- );
- };
- const renderItem = ({ item, index }) => {
- if (!item.id || item.edit) {
- return editItem(item, index);
- }
- return saveItem(item);
- };
- return (
- <>
- <NavHeaderBar title={CUYTPO} />
- <Layout style={[styles.lay]}>
- <ListComponent
- getInfo={getList}
- renderItem={renderItem}
- style={styles.list}
- separatorStyle={styles.separatorStyle}
- showEmpty
- extraData={{ allEditInfo }}
- startState={startState}
- list$={list$}
- />
- <ActionButton
- buttonColor={theme["color-primary-500"]}
- onPress={addFullReduction}
- position="left"
- />
- </Layout>
- </>
- );
- }
|