FullReduction.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 scrollPage from "../decorator/scrollPage";
  12. import { useModel } from "flooks";
  13. import {
  14. Layout,
  15. Tab,
  16. TabView,
  17. Text,
  18. useTheme,
  19. Button,
  20. Card,
  21. List,
  22. Input,
  23. Icon,
  24. } from "@ui-kitten/components";
  25. import EmptyComponent from "../components/EmptyComponent";
  26. import FormInput from "../components/FormInput";
  27. import { useFocusEffect } from "@react-navigation/native";
  28. import ScrollPage from "../components/ScrollPage";
  29. import NavHeaderBar from "../components/NavHeaderBar";
  30. import GoodsCard from "../components/GoodsCard";
  31. import ActionButton from "react-native-action-button";
  32. export default function FullReduction({ navigation, route }) {
  33. const theme = useTheme();
  34. const { changeBackground } = useModel("barModel", true);
  35. const { mid } = useModel("userModel");
  36. const { httpGet, httpPost } = useModel("httpModel", true);
  37. const { success, warnning } = useModel("loadingModel", true);
  38. const { showDialog } = useModel("dialogModel");
  39. const {
  40. userTitle21,
  41. fullReduction2,
  42. fullReduction1,
  43. delText,
  44. editText,
  45. confirm,
  46. cancel,
  47. complete,
  48. successText,
  49. removeTips,
  50. } = useModel("wordsModel");
  51. const [fullReductions, changeFllReduction] = React.useState([
  52. { fullAmount: "", minusAmount: "" },
  53. ]);
  54. useFocusEffect(
  55. React.useCallback(() => {
  56. changeBackground(theme["color-primary-500"]);
  57. httpGet("/fullReduction/my").then(res => {
  58. if (res.length > 0) {
  59. changeFllReduction(
  60. res.map(item => {
  61. return { ...item, edit: false };
  62. })
  63. );
  64. }
  65. });
  66. }, [])
  67. );
  68. const renderItem = ({ item, index }) => {
  69. if (!item.id || item.edit) {
  70. return editItem(item, index);
  71. } else {
  72. return saveItem(item);
  73. }
  74. };
  75. const saveItem = (info, index) => (
  76. <Layout style={styles.item}>
  77. <Text style={styles.text}>
  78. {fullReduction1}
  79. {info.fullAmount}
  80. {fullReduction2}
  81. {info.minusAmount}
  82. </Text>
  83. <Button
  84. size='small'
  85. appearance='outline'
  86. onPress={() => editInfo(info, index)}
  87. >
  88. {editText}
  89. </Button>
  90. <Button
  91. size='small'
  92. status='danger'
  93. style={styles.buttonlast}
  94. onPress={() => delInfo(info, index)}
  95. >
  96. {delText}
  97. </Button>
  98. </Layout>
  99. );
  100. const editItem = (info, index) => (
  101. <Layout style={styles.item}>
  102. <Layout style={[styles.text, styles.flexRow]}>
  103. <Text>{fullReduction1}</Text>
  104. <Input
  105. size='small'
  106. defaultValue={info.fullAmount.toString()}
  107. style={styles.input}
  108. key={index + 0}
  109. keyboardType='numeric'
  110. onChangeText={value => changeText(value, index, "full")}
  111. />
  112. <Text>{fullReduction2}</Text>
  113. <Input
  114. size='small'
  115. defaultValue={info.minusAmount.toString()}
  116. style={styles.input}
  117. key={index + 1}
  118. keyboardType='numeric'
  119. onChangeText={value => changeText(value, index, "minus")}
  120. />
  121. </Layout>
  122. <Button
  123. size='small'
  124. disabled={!info.minusAmount || !info.fullAmount}
  125. onPress={() => saveInfo(info, index)}
  126. >
  127. {confirm}
  128. </Button>
  129. {!!info.id && (
  130. <Button
  131. size='small'
  132. appearance='outline'
  133. style={styles.buttonlast}
  134. onPress={() => cancelInfo(info, index)}
  135. >
  136. {cancel}
  137. </Button>
  138. )}
  139. </Layout>
  140. );
  141. const editInfo = (info, index) => {
  142. let _fullReductions = [...fullReductions];
  143. info.edit = true;
  144. _fullReductions[index] = info;
  145. changeFllReduction(_fullReductions);
  146. };
  147. const delInfo = (info, index) => {
  148. showDialog({
  149. bodyText: removeTips,
  150. status: "danger",
  151. cancelable: true,
  152. confirmCallback: () => {
  153. httpPost("/fullReduction/del/" + info.id)
  154. .then(res => {
  155. success(successText);
  156. let _fullReductions = [...fullReductions];
  157. _fullReductions.splice(index, 1);
  158. changeFllReduction(_fullReductions);
  159. })
  160. .catch(e => {
  161. warnning(e.error);
  162. });
  163. },
  164. });
  165. };
  166. const cancelInfo = (info, index) => {
  167. let _fullReductions = [...fullReductions];
  168. if (info.id) {
  169. info.edit = false;
  170. _fullReductions[index] = info;
  171. } else {
  172. _fullReductions.pop();
  173. }
  174. changeFllReduction(_fullReductions);
  175. };
  176. const saveInfo = (info, index) => {
  177. httpPost(
  178. "/fullReduction/save",
  179. {
  180. ...info,
  181. merchantId: mid,
  182. },
  183. {
  184. body: "json",
  185. }
  186. )
  187. .then(res => {
  188. success(successText);
  189. let _fullReductions = [...fullReductions];
  190. _fullReductions.splice(index, 1, {
  191. ...res,
  192. edit: false,
  193. });
  194. changeFllReduction(_fullReductions);
  195. })
  196. .catch(e => {
  197. warnning(e.error);
  198. });
  199. };
  200. function changeText(value, index, type) {
  201. let _fullReductions = [...fullReductions];
  202. let info = _fullReductions[index];
  203. if (type == "full") {
  204. info.fullAmount = value;
  205. } else {
  206. info.minusAmount = value;
  207. }
  208. _fullReductions.splice(index, 1, info);
  209. changeFllReduction(_fullReductions);
  210. }
  211. const addFullReduction = () => {
  212. console.log("aaa");
  213. let _fullReductions = [...fullReductions];
  214. let last = _fullReductions[_fullReductions.length - 1];
  215. if (last.id) {
  216. _fullReductions.push({
  217. fullAmount: "",
  218. minusAmount: "",
  219. });
  220. changeFllReduction(_fullReductions);
  221. }
  222. };
  223. return (
  224. <>
  225. <NavHeaderBar title={userTitle21} />
  226. <Layout style={styles.all}>
  227. <Layout style={[styles.all]}>
  228. <List
  229. style={styles.list}
  230. data={fullReductions}
  231. renderItem={renderItem}
  232. ListEmptyComponent={EmptyComponent}
  233. keyboardShouldPersistTaps='handled'
  234. />
  235. </Layout>
  236. <ActionButton
  237. buttonColor={theme["color-primary-500"]}
  238. onPress={addFullReduction}
  239. position='left'
  240. />
  241. </Layout>
  242. </>
  243. );
  244. }
  245. const PulsIcon = props => (
  246. <Icon
  247. {...props}
  248. style={[props.style, { width: 33, height: 33 }]}
  249. name='plus-circle'
  250. />
  251. );
  252. const styles = StyleSheet.create({
  253. all: {
  254. backgroundColor: "#fff",
  255. flex: 1,
  256. },
  257. lay: {
  258. backgroundColor: "#fff",
  259. },
  260. padBot: {
  261. paddingBottom: 100,
  262. },
  263. list: {
  264. paddingVertical: 10,
  265. paddingHorizontal: 15,
  266. backgroundColor: "transparent",
  267. flex: 1,
  268. },
  269. item: {
  270. flexDirection: "row",
  271. alignItems: "center",
  272. paddingVertical: 10,
  273. },
  274. input: {
  275. marginHorizontal: 5,
  276. minWidth: 49,
  277. },
  278. text: {
  279. flex: 1,
  280. },
  281. flexRow: {
  282. flexDirection: "row",
  283. alignItems: "center",
  284. },
  285. buttonlast: {
  286. marginLeft: 10,
  287. },
  288. button: {
  289. alignSelf: "flex-start",
  290. },
  291. });