ViewDrawerComplication.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <template>
  2. <BasicDrawer
  3. v-bind="$attrs"
  4. destroyOnClose
  5. @register="registerDrawer"
  6. :title="getTitle"
  7. :width="width"
  8. @ok="handleSubmit"
  9. :showFooter="true"
  10. :show-ok-btn="false"
  11. cancel-text="关闭"
  12. >
  13. <div class="!px-4 !pt-4">
  14. <div class="mb-4" v-auth="'archives:accessComplication:add'">
  15. <a-button type="primary" shape="round" @click="handleAdd">
  16. <template #icon>
  17. <PlusOutlined />
  18. </template>
  19. 添加
  20. </a-button>
  21. </div>
  22. <div>
  23. <BasicTable @register="registerTable" @edit-change="onEditChange">
  24. <template #bodyCell="{ column, record }">
  25. <template v-if="column.key === 'action'">
  26. <TableAction :actions="createActions(record, column)" />
  27. </template>
  28. </template>
  29. </BasicTable>
  30. </div>
  31. </div>
  32. </BasicDrawer>
  33. </template>
  34. <script lang="ts" setup>
  35. import { ref, reactive, unref, nextTick } from 'vue';
  36. import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  37. import {
  38. BasicTable,
  39. useTable,
  40. TableAction,
  41. EditRecordRow,
  42. ActionItem,
  43. BasicColumn,
  44. } from '/@/components/Table';
  45. import { useMessage } from '/@/hooks/web/useMessage';
  46. import { columnsComplication } from './data';
  47. import {
  48. archivesAccessComplicationAdd,
  49. archivesAccessComplicationEdit,
  50. archivesAccessComplicationQueryList,
  51. archivesAccessComplicationRemove,
  52. } from '/@/api/biz/archives/accessComplicationApi';
  53. import { listDictModel } from '/@/api/common';
  54. import { PlusOutlined } from '@ant-design/icons-vue';
  55. import { formatDictLabel, formatDictValue } from '/@/utils';
  56. import dayjs from 'dayjs';
  57. const emit = defineEmits(['success', 'register']);
  58. const getTitle = ref(``);
  59. const width = '60%';
  60. const isUpdate = ref(false);
  61. const vascularAccessId = ref();
  62. const bizDictOptions = reactive({
  63. accessType: [],
  64. vaComp: [],
  65. });
  66. // 表格数据
  67. const tableData = ref();
  68. const { createMessage } = useMessage();
  69. const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async data => {
  70. setDrawerProps({ confirmLoading: false });
  71. getTitle.value = `通路并发症 ( ${data.record.name} | ${data.record.gender} | ${data.record.age} )`;
  72. isUpdate.value = !!data?.isUpdate;
  73. bizDictOptions.accessType = await listDictModel({ dictCode: 'va_type' });
  74. bizDictOptions.vaComp = await listDictModel({ dictCode: 'va_comp' });
  75. vascularAccessId.value = data.accessId;
  76. await getData();
  77. });
  78. const [registerTable, { setTableData, getDataSource }] = useTable({
  79. dataSource: tableData.value,
  80. columns: columnsComplication,
  81. autoCreateKey: true,
  82. showIndexColumn: false,
  83. bordered: true,
  84. striped: false,
  85. actionColumn: {
  86. width: 100,
  87. title: '操作',
  88. dataIndex: 'action',
  89. },
  90. });
  91. function createActions(record: EditRecordRow, column: BasicColumn): ActionItem[] {
  92. if (!record.editable) {
  93. return [
  94. {
  95. auth: 'archives:accessComplication:add',
  96. icon: 'icon-xt-details_edit_default|iconfont',
  97. tooltip: '编辑',
  98. onClick: handleEdit.bind(null, record),
  99. },
  100. {
  101. auth: 'archives:accessComplication:remove',
  102. icon: 'icon-xt-details_delete_default|iconfont',
  103. tooltip: '删除',
  104. popConfirm: {
  105. title: '是否取消删除',
  106. placement: 'left',
  107. confirm: handleDel.bind(null, record, column),
  108. },
  109. },
  110. ];
  111. }
  112. return [
  113. {
  114. label: '保存',
  115. onClick: handleSave.bind(null, record, column),
  116. },
  117. {
  118. label: '取消',
  119. popConfirm: {
  120. title: '是否取消编辑',
  121. placement: 'left',
  122. confirm: handleCancel.bind(null, record, column),
  123. },
  124. },
  125. ];
  126. }
  127. // 获取数据
  128. async function getData() {
  129. tableData.value = await archivesAccessComplicationQueryList(vascularAccessId.value);
  130. tableData.value = tableData.value.map(ele => {
  131. ele.name = formatDictValue(bizDictOptions.vaComp, ele.name);
  132. return ele;
  133. });
  134. setTableData(tableData.value);
  135. await nextTick();
  136. }
  137. // 提交按钮事件
  138. async function handleSubmit() {
  139. try {
  140. setDrawerProps({ confirmLoading: true });
  141. emit('success', {
  142. isUpdate: unref(isUpdate),
  143. });
  144. closeDrawer();
  145. } finally {
  146. setDrawerProps({ confirmLoading: false });
  147. }
  148. }
  149. async function handleAdd() {
  150. const isAdd = getDataSource().filter(ele => {
  151. return ele.editable;
  152. });
  153. if (isAdd.length) {
  154. createMessage.error('请编辑完成后添加!');
  155. return;
  156. }
  157. tableData.value.unshift({
  158. occurredTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
  159. name: '',
  160. remark: '',
  161. });
  162. await nextTick();
  163. setTableData(tableData.value);
  164. console.log('getDataSource()', getDataSource());
  165. const record = getDataSource()[0];
  166. record.editable = true;
  167. handleEdit(record);
  168. }
  169. async function handleEdit(record) {
  170. record.onEdit?.(true);
  171. }
  172. async function handleDel(record) {
  173. const data = getDataSource();
  174. const index = data.findIndex(item => item.key === record.key);
  175. const res = await archivesAccessComplicationRemove([record?.id]);
  176. if (res) {
  177. tableData.value.splice(index, 1);
  178. setTableData(tableData.value);
  179. } else {
  180. createMessage.error('删除失败');
  181. }
  182. }
  183. async function handleSave(record: EditRecordRow) {
  184. console.log('🚀 ~ file: ViewDrawerComplication.vue:166 ~ handleSave ~ record:', record);
  185. record.onEdit?.(false, true);
  186. const data = {
  187. id: record?.id || '',
  188. name: record?.name || '',
  189. occurredTime: record?.occurredTime || '',
  190. remark: record?.remark || '',
  191. vascularAccessId: vascularAccessId.value,
  192. };
  193. if (record?.id) {
  194. data.name = formatDictLabel(bizDictOptions.vaComp, record?.name) || record?.name;
  195. }
  196. data.id
  197. ? await archivesAccessComplicationEdit(data)
  198. : await archivesAccessComplicationAdd(data);
  199. createMessage.success(data.id ? '编辑成功' : '新增成功!');
  200. await getData();
  201. }
  202. async function handleCancel(record: EditRecordRow) {
  203. console.log('🚀 ~ file: ViewDrawerComplication.vue:179 ~ handleCancel ~ record:', record);
  204. if (!record.id) {
  205. tableData.value.shift();
  206. }
  207. await nextTick();
  208. setTableData(tableData.value);
  209. record.onEdit?.(false);
  210. }
  211. function onEditChange({ column, value, record }) {
  212. // 本例
  213. // if (column.dataIndex === 'id') {
  214. // record.editValueRefs.name4.value = `${value}`;
  215. // }
  216. console.log(column, value, record);
  217. }
  218. </script>
  219. <style lang="less" scoped>
  220. ::v-deep(.ant-btn-link) {
  221. color: rgb(61 65 85 / 100%);
  222. }
  223. </style>