index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <script>
  2. import useColumns from "./column";
  3. import { initParams, initRules } from "@/utils/init.js";
  4. import { REFER } from "@/components/popover-select/api";
  5. import {
  6. EDIT,
  7. ITEM,
  8. TABLE,
  9. ALTERATION,
  10. } from "@/api/business/purchase/contract";
  11. export default {
  12. name: "EditDrawer",
  13. components: {
  14. ElFileUpload: () => import("@/components/FileUpload/index.vue"),
  15. ElPopoverSelectV2: () => import("@/components/popover-select-v2/index.vue"),
  16. ElComputedInputV2: () => import("@/components/computed-input-v2/index.vue"),
  17. },
  18. data() {
  19. const {
  20. TabColumns,
  21. FormColumns,
  22. TabColumns: [
  23. {
  24. item: { key: tabName },
  25. },
  26. ],
  27. } = useColumns();
  28. const params = initParams([...TabColumns, ...FormColumns]);
  29. const rules = initRules(FormColumns);
  30. return {
  31. title: "更 新",
  32. size: "mini",
  33. width: "100%",
  34. visible: false,
  35. loading: false,
  36. rules: rules,
  37. params: params,
  38. tabName: tabName,
  39. TabColumns,
  40. FormColumns,
  41. };
  42. },
  43. computed: {
  44. root: {
  45. get() {
  46. return this.$parent.$parent;
  47. },
  48. },
  49. $dicts: {
  50. get() {
  51. return this.$parent.$parent.$dicts;
  52. },
  53. },
  54. },
  55. watch: {},
  56. methods: {
  57. async fetchRefer(row, prop) {
  58. const { rateCode } = row;
  59. const { source, referName } = prop;
  60. if (referName === "MATERIAL_PARAM") {
  61. try {
  62. // try
  63. this.loading = true;
  64. const { code, rows } = await REFER({
  65. search: rateCode,
  66. type: "TAX_RATE_PARAM",
  67. });
  68. if (code === 200) {
  69. const [{ ntaxrate }] = rows;
  70. source.tax = ntaxrate === "0E-8" ? "0.00000000" : ntaxrate;
  71. }
  72. } catch (err) {
  73. // catch
  74. console.error(err);
  75. } finally {
  76. // finally
  77. this.loading = false;
  78. }
  79. }
  80. },
  81. async fetchItem(prop) {
  82. try {
  83. // try
  84. this.loading = true;
  85. const { code, data } = await ITEM(prop);
  86. if (code === 200) {
  87. this.params = data;
  88. return true;
  89. } else {
  90. return false;
  91. }
  92. } catch (err) {
  93. // catch
  94. console.error(err);
  95. } finally {
  96. // finally
  97. this.loading = false;
  98. }
  99. },
  100. //
  101. async open(prop) {
  102. this.visible = await this.fetchItem(prop);
  103. },
  104. //
  105. async hide() {
  106. const {
  107. TabColumns,
  108. FormColumns,
  109. TabColumns: [
  110. {
  111. item: { key: tabName },
  112. },
  113. ],
  114. } = useColumns();
  115. this.visible = false;
  116. this.tabName = tabName;
  117. this.params = initParams([...TabColumns, ...FormColumns]);
  118. },
  119. //
  120. async useRowAdd(prop) {
  121. const { TableColumns } = this.TabColumns.find(
  122. ({ item: { key } }) => key === prop
  123. );
  124. this.params[prop].push(initParams(TableColumns));
  125. console.log(this.params[prop]);
  126. },
  127. //
  128. async useRowRemove(prop, scope) {
  129. const { REMOVE } = TABLE;
  130. const {
  131. $index,
  132. row: { id, contractId },
  133. } = scope;
  134. if (id) {
  135. try {
  136. // try
  137. this.loading = true;
  138. const { code } = REMOVE(id, prop);
  139. if (code === 200) {
  140. this.fetchItem(contractId);
  141. }
  142. } catch (err) {
  143. // catch
  144. console.error(err);
  145. } finally {
  146. // finally
  147. this.loading = false;
  148. }
  149. } else {
  150. this.params[prop].splice($index, 1);
  151. }
  152. },
  153. //
  154. async useRowSubmit(prop, scope) {
  155. const {
  156. row,
  157. row: { contractId },
  158. } = scope;
  159. const { id } = this.params;
  160. const { ADD, EDIT } = TABLE;
  161. try {
  162. // try
  163. this.loading = true;
  164. if (contractId) {
  165. await EDIT(row, prop);
  166. } else {
  167. await ADD({ ...row, contractId: id }, prop);
  168. }
  169. } catch (err) {
  170. // catch
  171. console.error(err);
  172. } finally {
  173. // finally
  174. this.fetchItem(id);
  175. this.loading = false;
  176. }
  177. },
  178. //
  179. async useSubmit(prop) {
  180. this.$refs[prop].validate(async (valid) => {
  181. if (valid) {
  182. try {
  183. // try
  184. this.loading = true;
  185. const {
  186. params,
  187. params: { status },
  188. } = this;
  189. const TASK = status === "2" ? EDIT : ALTERATION;
  190. const { msg, code } = await TASK(params);
  191. if (code === 200) {
  192. const { id } = this.params;
  193. const { fetchItem } = this.root.$refs.SeeModel;
  194. await fetchItem(id);
  195. this.hide();
  196. this.$emit("success");
  197. this.$notify.success(msg);
  198. }
  199. } catch (err) {
  200. // catch
  201. console.error(err);
  202. } finally {
  203. // finally
  204. this.loading = false;
  205. }
  206. } else {
  207. return false;
  208. }
  209. });
  210. },
  211. //
  212. async useAlteration(prop) {
  213. await this.root
  214. .useAlteration(prop)
  215. .then(() => {
  216. const [{ id }] = prop;
  217. const { fetchItem } = this.root.$refs.SeeModel;
  218. this.hide();
  219. fetchItem(id);
  220. })
  221. .catch(() => {});
  222. },
  223. },
  224. created() {},
  225. mounted() {},
  226. destroyed() {},
  227. };
  228. </script>
  229. <template>
  230. <el-drawer
  231. :size="width"
  232. :title="title"
  233. :show-close="false"
  234. :visible.sync="visible"
  235. >
  236. <template slot="title">
  237. <span>{{ title }}</span>
  238. <span>
  239. <el-tooltip
  240. effect="dark"
  241. :content="params.status === '2' ? '变 更' : '更 新'"
  242. placement="bottom-end"
  243. >
  244. <el-button
  245. :size="size"
  246. circle
  247. icon="el-icon-check"
  248. @click="useSubmit('ruleForm')"
  249. >
  250. </el-button>
  251. </el-tooltip>
  252. <el-button
  253. :size="size"
  254. circle
  255. type="danger"
  256. icon="el-icon-close"
  257. @click="hide"
  258. ></el-button>
  259. </span>
  260. </template>
  261. <el-form
  262. ref="ruleForm"
  263. v-loading="loading"
  264. :size="size"
  265. :rules="rules"
  266. :model="params"
  267. label-width="auto"
  268. label-position="right"
  269. style="padding: 10px"
  270. >
  271. <el-row :gutter="20" style="display: flex; flex-wrap: wrap">
  272. <el-col
  273. v-for="{ item, attr } in FormColumns"
  274. :key="item.key"
  275. :span="item.span || 6"
  276. >
  277. <el-form-item :prop="item.key" :label="item.title">
  278. <component
  279. v-if="attr.is === 'el-input'"
  280. v-bind="attr"
  281. v-model="params[item.key]"
  282. style="width: 100%"
  283. ></component>
  284. <component
  285. v-if="attr.is === 'el-popover-select-v2'"
  286. v-bind="attr"
  287. v-model="params[item.key]"
  288. :source.sync="params"
  289. style="width: 100%"
  290. >
  291. </component>
  292. <component
  293. v-if="attr.is === 'el-input-number'"
  294. v-bind="attr"
  295. v-model="params[item.key]"
  296. style="width: 100%"
  297. ></component>
  298. <component
  299. v-if="attr.is === 'el-select'"
  300. v-bind="attr"
  301. v-model="params[item.key]"
  302. style="width: 100%"
  303. >
  304. <el-option
  305. v-for="dict in $dicts[attr.dictName]"
  306. :key="dict.value"
  307. :label="dict.label"
  308. :value="dict.value"
  309. >
  310. </el-option>
  311. </component>
  312. <component
  313. v-if="attr.is === 'el-date-picker'"
  314. v-bind="attr"
  315. v-model="params[item.key]"
  316. style="width: 100%"
  317. >
  318. </component>
  319. <component
  320. v-if="attr.is === 'el-file-upload'"
  321. v-bind="attr"
  322. v-model="params[item.key]"
  323. ></component>
  324. </el-form-item>
  325. </el-col>
  326. <el-col :span="24">
  327. <el-form-item label-width="0">
  328. <el-tabs v-model="tabName">
  329. <el-tab-pane
  330. v-for="{ item, attr, TableColumns } in TabColumns"
  331. :key="item.key"
  332. :label="item.title"
  333. :name="item.key"
  334. lazy
  335. >
  336. <el-table :size="size" :data="params[item.key]">
  337. <el-table-column label="序号">
  338. <template slot-scope="scope">
  339. {{ scope.$index + 1 }}
  340. </template>
  341. </el-table-column>
  342. <el-table-column
  343. v-for="{ item: cItem, attr: cAttr } in TableColumns"
  344. :key="cItem.key"
  345. :prop="cItem.key"
  346. :label="cItem.title"
  347. :width="cItem.width || 300"
  348. show-overflow-tooltip
  349. >
  350. <template slot-scope="scope">
  351. <component
  352. v-if="cAttr.is === 'el-input'"
  353. v-bind="cAttr"
  354. v-model="scope.row[cItem.key]"
  355. style="width: 100%"
  356. ></component>
  357. <component
  358. v-else-if="cAttr.is === 'el-popover-select-v2'"
  359. v-bind="cAttr"
  360. v-model="scope.row[cItem.key]"
  361. :source.sync="scope.row"
  362. style="width: 100%"
  363. >
  364. </component>
  365. <component
  366. v-else-if="cAttr.is === 'el-input-number'"
  367. v-bind="cAttr"
  368. v-model="scope.row[cItem.key]"
  369. style="width: 100%"
  370. ></component>
  371. <component
  372. v-else-if="cAttr.is === 'el-select'"
  373. v-bind="cAttr"
  374. v-model="scope.row[cItem.key]"
  375. style="width: 100%"
  376. >
  377. <el-option
  378. v-for="dict in $dicts[cAttr.dictName]"
  379. :key="dict.value"
  380. :label="dict.label"
  381. :value="dict.value"
  382. >
  383. </el-option>
  384. </component>
  385. <component
  386. v-else-if="cAttr.is === 'el-date-picker'"
  387. v-bind="cAttr"
  388. v-model="scope.row[cItem.key]"
  389. style="width: 100%"
  390. >
  391. </component>
  392. <component
  393. v-else-if="cAttr.is === 'el-computed-input-v2'"
  394. v-bind="cAttr"
  395. v-model="scope.row[cItem.key]"
  396. :source.sync="scope.row"
  397. ></component>
  398. <span v-else> {{ scope.row[cItem.key] }}</span>
  399. </template>
  400. </el-table-column>
  401. <el-table-column fixed="right" label="操作" width="100">
  402. <template slot="header" slot-scope="scope">
  403. <el-button
  404. circle
  405. icon="el-icon-plus"
  406. :size="size"
  407. @click="useRowAdd(tabName)"
  408. >
  409. </el-button>
  410. </template>
  411. <template slot-scope="scope">
  412. <el-button
  413. circle
  414. icon="el-icon-check"
  415. :size="size"
  416. @click.native.prevent="useRowSubmit(tabName, scope)"
  417. >
  418. </el-button>
  419. <el-button
  420. circle
  421. icon="el-icon-minus"
  422. :size="size"
  423. @click.native.prevent="useRowRemove(tabName, scope)"
  424. >
  425. </el-button>
  426. </template>
  427. </el-table-column>
  428. </el-table>
  429. </el-tab-pane>
  430. </el-tabs>
  431. </el-form-item>
  432. </el-col>
  433. </el-row>
  434. </el-form>
  435. </el-drawer>
  436. </template>