index.vue 12 KB

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