index.vue 12 KB

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