Ben Lin
2024-06-25 f73947395184fd635df3d74c1c4b2701d0c708c1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit">
    <BasicForm @register="registerForm">
      <template #org="{ model, field }">
        <BasicTree
          v-model:value="model[field]"
          :treeData="treeDataOrg"
          :renderIcon="createIconOrg"
          :fieldNames="{ title: 'deptName', key: 'id' }"
          @gethalfCheckedKeys="gethalfCheckedKeysOrg"
          :expanded-keys="expandedKeysOrg"
          checkable
          toolbar
          title="分配组织"
        />
      </template>
    </BasicForm>
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { accountFormSchema } from './account.data';
  import { getTreeList } from '/@/api/tigerapi/dept';
  import { BasicTree, TreeItem } from '/@/components/Tree';
  import { SaveUser } from '/@/api/tigerapi/account';
 
  type CheckKeys = KeyType[] | { checked: string[] | number[]; halfChecked: string[] | number[] };
  type _expandAll = KeyType[] | undefined;
  const emits = defineEmits(['success', 'register']);
 
  const isUpdate = ref(true);
  const rowId = ref('');
  const treeDataOrg = ref<TreeItem[]>([]);
  const expandedKeysOrg = ref<_expandAll>([]);
  const halfCheckedKeysOrg = ref<CheckKeys>([]);
 
  const gethalfCheckedKeysOrg = (obj) => {
    halfCheckedKeysOrg.value = obj;
    console.log(obj);
  };
 
  const [registerForm, { setFieldsValue, updateSchema, resetFields, validate }] = useForm({
    labelWidth: 100,
    schemas: accountFormSchema,
    showActionButtonGroup: false,
    actionColOptions: {
      span: 24,
    },
  });
 
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    resetFields();
    setModalProps({ confirmLoading: false });
    isUpdate.value = !!data?.isUpdate;
 
    if (unref(isUpdate)) {
      rowId.value = data.record.id;
      setFieldsValue({
        ...data.record,
      });
    }
    if (unref(treeDataOrg).length === 0) {
      treeDataOrg.value = (await getTreeList()) as any as TreeItem[];
    }
    // const treeData = await getTreeList();
    // updateSchema([
    //   {
    //     field: 'USER_PWD',
    //     show: !unref(isUpdate),
    //   },
    //   {
    //     field: 'ORG_CODE',
    //     componentProps: { treeDataOrg },
    //   },
    // ]);
  });
 
  const getTitle = computed(() => (!unref(isUpdate) ? '新增账号' : '编辑账号'));
 
  async function handleSubmit() {
    try {
      const values = await validate();
      console.log('提交4', values.STATUS_CODE == true ? 'ENABLE' : 'DISABLEDS');
 
      // return
 
      console.log('提交', values.STATUS_CODE);
      setModalProps({ confirmLoading: true });
      // TODO custom api
      //保存用户
      const apiAction = SaveUser(values, unref(isUpdate));
      console.log('保存', values);
      // values.STATUS_CODE = values.STATUS_CODE == true?'ENABLE':'DISABLEDS'
      apiAction.then((action) => {
        if (action.IsSuccessed) {
          closeModal();
          emits('success', {
            isUpdate: unref(isUpdate),
            values: { ...values, id: rowId.value },
          });
        }
      });
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>