index.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <script>
  2. import useColumns from "./columns";
  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. dict: {
  19. type: Object,
  20. require: true,
  21. },
  22. },
  23. components: {
  24. ElSuperForm: () => import("@/components/super-form/index.vue"),
  25. ElSuperTable: () => import("@/components/super-table/index.vue"),
  26. ElPopoverSelectV2: () => import("@/components/popover-select-v2/index.vue"),
  27. },
  28. data() {
  29. const {
  30. TabColumns,
  31. TableColumns,
  32. TabColumns: [
  33. {
  34. item: { key: tabName },
  35. },
  36. ],
  37. } = useColumns();
  38. const rules = this.$init.rules(TableColumns);
  39. const params = this.$init.params([...TabColumns, ...TableColumns]);
  40. return {
  41. title: "编 辑",
  42. width: "100%",
  43. visible: false,
  44. loading: false,
  45. rules: rules,
  46. params: params,
  47. tabName: tabName,
  48. TabColumns: TabColumns,
  49. TableColumns: TableColumns,
  50. };
  51. },
  52. computed: {
  53. disabled: {
  54. get() {
  55. const { selectData } = this;
  56. if (selectData.length !== 1) {
  57. return true;
  58. }
  59. },
  60. set() {},
  61. },
  62. },
  63. watch: {},
  64. methods: {
  65. //
  66. async changeMaterialName(prop) {
  67. const { row } = prop;
  68. const { rateCode } = row;
  69. try {
  70. // try
  71. this.loading = true;
  72. const { code, rows } = await REFER({
  73. search: rateCode,
  74. type: "TAX_RATE_PARAM",
  75. });
  76. if (code === 200) {
  77. const [{ ntaxrate }] = rows;
  78. row.tax = ntaxrate === "0E-8" ? "0.00000000" : ntaxrate;
  79. }
  80. } catch (err) {
  81. // catch
  82. console.error(err);
  83. } finally {
  84. // finally
  85. this.loading = false;
  86. }
  87. },
  88. //
  89. async fetchItem(prop) {
  90. try {
  91. // try
  92. this.loading = true;
  93. const { code, data } = await ITEM(prop);
  94. if (code === 200) {
  95. this.params = data;
  96. return true;
  97. } else {
  98. return false;
  99. }
  100. } catch (err) {
  101. // catch
  102. console.error(err);
  103. } finally {
  104. // finally
  105. this.loading = false;
  106. }
  107. },
  108. //
  109. async open() {
  110. const { selectData } = this.$props;
  111. const [{ id }] = selectData;
  112. this.visible = await this.fetchItem(id);
  113. },
  114. //
  115. async hide() {
  116. const {
  117. TabColumns,
  118. TableColumns,
  119. TabColumns: [
  120. {
  121. item: { key: tabName },
  122. },
  123. ],
  124. } = useColumns();
  125. this.visible = false;
  126. this.tabName = tabName;
  127. this.params = this.$init.params([...TabColumns, ...TableColumns]);
  128. },
  129. //
  130. async useRowAdd(prop) {
  131. const { TableColumns } = this.TabColumns.find(
  132. ({ item: { key } }) => key === prop
  133. );
  134. this.params[prop].push(this.$init.params(TableColumns));
  135. },
  136. //
  137. async useRowRemove(prop, scope) {
  138. const { REMOVE } = TABLE;
  139. const {
  140. $index,
  141. row: { id, contractId },
  142. } = scope;
  143. if (id) {
  144. try {
  145. // try
  146. this.loading = true;
  147. const { code } = REMOVE(id, prop);
  148. if (code === 200) {
  149. this.fetchItem(contractId);
  150. }
  151. } catch (err) {
  152. // catch
  153. console.error(err);
  154. } finally {
  155. // finally
  156. this.loading = false;
  157. }
  158. } else {
  159. this.params[prop].splice($index, 1);
  160. }
  161. },
  162. //
  163. async useRowSubmit(prop, scope) {
  164. const {
  165. row,
  166. row: { contractId },
  167. } = scope;
  168. const { id } = this.params;
  169. const { ADD, EDIT } = TABLE;
  170. try {
  171. // try
  172. this.loading = true;
  173. if (contractId) {
  174. await EDIT(row, prop);
  175. } else {
  176. await ADD({ ...row, contractId: id }, prop);
  177. }
  178. } catch (err) {
  179. // catch
  180. console.error(err);
  181. } finally {
  182. // finally
  183. this.fetchItem(id);
  184. this.loading = false;
  185. }
  186. },
  187. //
  188. async useSubmit(prop) {
  189. this.$refs[prop].$refs[prop].validate(async (valid) => {
  190. if (valid) {
  191. try {
  192. // try
  193. this.loading = true;
  194. const {
  195. params,
  196. params: { status },
  197. } = this;
  198. const TASK = status === "2" ? EDIT : ALTERATION;
  199. const { msg, code } = await TASK(params);
  200. if (code === 200) {
  201. this.hide();
  202. this.$emit("success");
  203. this.$notify.success(msg);
  204. }
  205. } catch (err) {
  206. // catch
  207. console.error(err);
  208. } finally {
  209. // finally
  210. this.loading = false;
  211. }
  212. } else {
  213. return false;
  214. }
  215. });
  216. },
  217. },
  218. created() {},
  219. mounted() {},
  220. destroyed() {},
  221. };
  222. </script>
  223. <template>
  224. <el-button
  225. v-bind="$attrs"
  226. v-on="$listeners"
  227. :disabled="disabled"
  228. @click="open"
  229. >
  230. {{ title }}
  231. <el-drawer
  232. :size="width"
  233. :title="title"
  234. :visible.sync="visible"
  235. append-to-body
  236. :show-close="false"
  237. destroy-on-close
  238. @close="hide"
  239. >
  240. <div slot="title"
  241. style="
  242. display: flex;
  243. justify-content: space-between;
  244. align-items: center;
  245. ">
  246. <h3>{{title}}</h3>
  247. <div style="text-align: right">
  248. <el-button
  249. type="primary"
  250. :size="$attrs.size"
  251. :loading="loading"
  252. @click="useSubmit('superForm')"
  253. >确 认</el-button
  254. >
  255. <el-button :size="$attrs.size" :loading="loading" @click="hide"
  256. >取 消</el-button
  257. >
  258. </div>
  259. </div>
  260. <el-super-form
  261. v-model="params"
  262. :dict="dict"
  263. :rules="rules"
  264. :size="$attrs.size"
  265. :columns="TableColumns"
  266. ref="superForm"
  267. label-width="auto"
  268. label-position="right"
  269. style="padding: 20px"
  270. ></el-super-form>
  271. <el-tabs v-model="tabName" style="margin: 0 20px">
  272. <el-tab-pane
  273. v-for="{ item, TableColumns: columns } in TabColumns"
  274. :key="item.key"
  275. :label="item.title"
  276. :name="item.key"
  277. lazy
  278. >
  279. <el-super-table
  280. v-model="params[item.key]"
  281. :dict="dict"
  282. :ref="tabName"
  283. :columns="columns"
  284. :size="$attrs.size"
  285. :iconOperation="false"
  286. >
  287. <template slot="materialName" slot-scope="scope">
  288. <component
  289. v-bind="scope.attr"
  290. v-model="scope.row[scope.item.key]"
  291. :size="$attrs.size"
  292. :source.sync="scope.row"
  293. @change="changeMaterialName(scope)"
  294. >
  295. </component>
  296. </template>
  297. <el-table-column fixed="right" label="操作" width="100">
  298. <template slot="header" slot-scope="scope">
  299. <el-button
  300. type="text"
  301. :size="$attrs.size"
  302. @click="useRowAdd(tabName)"
  303. >增行
  304. </el-button>
  305. </template>
  306. <template slot-scope="scope">
  307. <el-button
  308. type="text"
  309. :size="$attrs.size"
  310. @click.native.prevent="useRowSubmit(tabName, scope)"
  311. >更新
  312. </el-button>
  313. <el-button
  314. type="text"
  315. :size="$attrs.size"
  316. @click.native.prevent="useRowRemove(tabName, scope)"
  317. >删行
  318. </el-button>
  319. </template>
  320. </el-table-column>
  321. </el-super-table>
  322. </el-tab-pane>
  323. </el-tabs>
  324. </el-drawer>
  325. </el-button>
  326. </template>