GoodsSpecificationScreen.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. import * as WebBrowser from "expo-web-browser";
  2. import * as React from "react";
  3. import {
  4. Image,
  5. Platform,
  6. StyleSheet,
  7. TouchableOpacity,
  8. View,
  9. ImageBackground,
  10. } from "react-native";
  11. import { useModel } from "flooks";
  12. import {
  13. Layout,
  14. Tab,
  15. TabView,
  16. Text,
  17. useTheme,
  18. Button,
  19. Card,
  20. Input,
  21. Icon,
  22. } from "@ui-kitten/components";
  23. import EmptyComponent from "../../components/EmptyComponent";
  24. import FormInput from "../../components/FormInput";
  25. import { useFocusEffect, useRoute } from "@react-navigation/native";
  26. import ListComponent from "../../components/ListComponent";
  27. import NavHeaderBar from "../../components/NavHeaderBar";
  28. import GoodsCard from "../../components/GoodsCard";
  29. import ActionButton from "react-native-action-button";
  30. export default function GoodsSpecificationScreen({ navigation }) {
  31. const theme = useTheme();
  32. const { changeBackground } = useModel("barModel", true);
  33. const { mid } = useModel("userModel");
  34. const { httpGet, httpPost } = useModel("httpModel", true);
  35. const { success, warnning } = useModel("loadingModel", true);
  36. const { showDialog } = useModel("dialogModel");
  37. const [allEditInfo, setEdit] = React.useState({});
  38. const [goodsId, setGoodsId] = React.useState(0);
  39. const [startState, changeState] = React.useState(true);
  40. const [editList, setEditList] = React.useState([]);
  41. const [addNew, changeNew] = React.useState(false);
  42. const [isNew, changeIsNew] = React.useState(false);
  43. const {
  44. userTitle21,
  45. fullReduction2,
  46. fullReduction1,
  47. delText,
  48. editText,
  49. confirm,
  50. cancel,
  51. complete,
  52. successText,
  53. removeTips,
  54. } = useModel("wordsModel");
  55. const { addClassGoods } = useModel("goodsModel");
  56. useFocusEffect(
  57. React.useCallback(() => {
  58. changeBackground(theme["color-primary-500"]);
  59. }, [])
  60. );
  61. const route = useRoute();
  62. function getList() {
  63. let { params } = route;
  64. let { goodsId } = params || {};
  65. setGoodsId(goodsId);
  66. return httpGet(
  67. "/goodsSpecification/byGoodsId",
  68. {
  69. goodsId: goodsId || 0,
  70. },
  71. true
  72. ).then(res => {
  73. changeState(false);
  74. if (res.length === 0) {
  75. changeIsNew(true);
  76. } else {
  77. changeIsNew(false);
  78. }
  79. let content = res || [];
  80. if (isNew || addNew) {
  81. content.push({ name: "", amount: "" });
  82. }
  83. return Promise.resolve({
  84. content: content.map(item => {
  85. let _info = { ...item, amount: item.amount.toString() };
  86. if (!_info.id || editList.indexOf(_info.id) != -1) {
  87. _info.edit = true;
  88. } else {
  89. _info.edit = false;
  90. }
  91. return _info;
  92. }),
  93. last: true,
  94. });
  95. });
  96. }
  97. const renderItem = ({ item, index }) => {
  98. if (!item.id || item.edit) {
  99. return editItem(item, index);
  100. } else {
  101. return saveItem(item);
  102. }
  103. };
  104. const saveItem = (info, index) => (
  105. <Layout style={styles.item}>
  106. <Layout style={[styles.text, styles.flexRow]}>
  107. <Text category='c1'>添加{info.name}</Text>
  108. <Layout style={styles.money}>
  109. <Text category='c1'>¥{info.amount}</Text>
  110. </Layout>
  111. </Layout>
  112. <Button
  113. size='small'
  114. appearance='outline'
  115. onPress={() => editInfo(info.id)}
  116. >
  117. {editText}
  118. </Button>
  119. <Button
  120. size='small'
  121. status='danger'
  122. style={styles.buttonlast}
  123. onPress={() => delInfo(info, index)}
  124. >
  125. {delText}
  126. </Button>
  127. </Layout>
  128. );
  129. const editItem = (info, index) => (
  130. <Layout style={styles.item}>
  131. <Layout style={[styles.text, styles.flexRow]}>
  132. <Text>规格</Text>
  133. <Input
  134. size='small'
  135. defaultValue={info.name}
  136. style={styles.input}
  137. key={index + "_0"}
  138. keyboardType='numeric'
  139. onChangeText={text => {
  140. let _editInfo = { ...allEditInfo } || {};
  141. if (!_editInfo[info.id || "new"]) {
  142. _editInfo[info.id || "new"] = {};
  143. }
  144. _editInfo[info.id || "new"]["name"] = text;
  145. setEdit(_editInfo);
  146. }}
  147. />
  148. <Text>价钱</Text>
  149. <Input
  150. size='small'
  151. defaultValue={info.amount}
  152. style={styles.input}
  153. key={index + "_1"}
  154. keyboardType='numeric'
  155. onChangeText={text => {
  156. let _editInfo = { ...allEditInfo } || {};
  157. if (!_editInfo[info.id || "new"]) {
  158. _editInfo[info.id || "new"] = {};
  159. }
  160. _editInfo[info.id || "new"]["amount"] = text;
  161. setEdit(_editInfo);
  162. }}
  163. />
  164. </Layout>
  165. <Button size='small' onPress={() => saveInfo(info)}>
  166. {confirm}
  167. </Button>
  168. <Button
  169. size='small'
  170. appearance='outline'
  171. style={styles.buttonlast}
  172. onPress={() => cancelInfo(info)}
  173. >
  174. {cancel}
  175. </Button>
  176. </Layout>
  177. );
  178. function setName(index, text) {}
  179. function setAmount(index, text) {}
  180. const editInfo = id => {
  181. let _editList = [...editList];
  182. _editList.push(id);
  183. setEditList(_editList);
  184. changeState(true);
  185. };
  186. const delInfo = (info, index) => {
  187. showDialog({
  188. bodyText: removeTips,
  189. status: "danger",
  190. cancelable: true,
  191. confirmCallback: () => {
  192. httpPost("/goodsSpecification/del/" + info.id)
  193. .then(res => {
  194. success(successText);
  195. changeState(true);
  196. })
  197. .catch(e => {
  198. warnning(e.error);
  199. });
  200. },
  201. });
  202. };
  203. //取消
  204. const cancelInfo = info => {
  205. let _editInfo = { ...allEditInfo };
  206. _editInfo[info.id || "new"] = {};
  207. setEdit(_editInfo);
  208. if (info.id) {
  209. let _editList = [...editList];
  210. _editList = _editList.filter(item => {
  211. return item != res.id;
  212. });
  213. setEditList(_editList);
  214. } else if (!isNew) {
  215. changeNew(false);
  216. }
  217. changeState(true);
  218. };
  219. //保存
  220. const saveInfo = info => {
  221. let _editInfo = { ...allEditInfo };
  222. let name = "";
  223. let amount = "";
  224. if (_editInfo[info.id || "new"]) {
  225. name = _editInfo[info.id || "new"]["name"];
  226. amount = _editInfo[info.id || "new"]["amount"];
  227. }
  228. let data = {
  229. ...info,
  230. name: name,
  231. amount: amount,
  232. goodsId: goodsId,
  233. };
  234. if (!info.id && !isNew) {
  235. changeNew(false);
  236. }
  237. addClassGoods(data).then(res => {
  238. _editInfo["new"] = {};
  239. _editInfo[res.id] = {};
  240. setEdit(_editInfo);
  241. let _editList = [...editList];
  242. _editList = _editList.filter(item => {
  243. return item != res.id;
  244. });
  245. setEditList(_editList);
  246. changeState(true);
  247. });
  248. };
  249. //
  250. const addFullReduction = () => {
  251. changeNew(true);
  252. changeState(true);
  253. };
  254. return (
  255. <>
  256. <NavHeaderBar title='商品规格编辑' />
  257. <Layout style={[styles.lay]}>
  258. <ListComponent
  259. getInfo={getList}
  260. renderItem={renderItem}
  261. style={styles.list}
  262. separatorStyle={styles.separatorStyle}
  263. showEmpty={true}
  264. extraData={{ allEditInfo }}
  265. startState={startState}
  266. />
  267. <ActionButton
  268. buttonColor={theme["color-primary-500"]}
  269. onPress={addFullReduction}
  270. position='left'
  271. />
  272. </Layout>
  273. </>
  274. );
  275. }
  276. const PulsIcon = props => (
  277. <Icon
  278. {...props}
  279. style={[props.style, { width: 33, height: 33 }]}
  280. name='plus-circle'
  281. />
  282. );
  283. const styles = StyleSheet.create({
  284. lay: {
  285. backgroundColor: "#fff",
  286. flex: 1,
  287. },
  288. padBot: {
  289. paddingBottom: 100,
  290. },
  291. list: {
  292. paddingVertical: 10,
  293. paddingHorizontal: 15,
  294. backgroundColor: "transparent",
  295. flex: 0,
  296. },
  297. item: {
  298. flexDirection: "row",
  299. alignItems: "center",
  300. paddingVertical: 10,
  301. },
  302. input: {
  303. marginHorizontal: 5,
  304. minWidth: 49,
  305. },
  306. text: {
  307. flex: 1,
  308. },
  309. flexRow: {
  310. flexDirection: "row",
  311. alignItems: "center",
  312. },
  313. buttonlast: {
  314. marginLeft: 10,
  315. },
  316. button: {
  317. alignSelf: "flex-start",
  318. },
  319. money: {
  320. marginLeft: 10,
  321. },
  322. });