Ben Lin
2024-06-18 9dfa701454d6a94690bad39dbb0e38f2a0b31489
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<template>
  <div class="m-4 mr-0 overflow-hidden bg-white" >
    <TigerTree
      :title="t('仓库')"
      toolbar
      search
      add
      :clickRowToExpand="false"
      :treeData="treeData"
      :renderIcon="createIcon"
      :fieldNames="{ key: 'id', title: 'houseName' }"
      :beforeRightClick="getRightMenuList"
      @select="handleSelect"
      @addHouse="addHouse"
    />
    <!-- <button :onClick="aa" style="
    position: absolute;
    left: 11px;
    top: 15px;
    font-size: 22px;
    
    background-color: transparent;">+</button> -->
    <WareHouseModal @register="registerWareHouse" @success="handleSuccess" />
    <RegionModal @register="registerRegion" @success="handleSuccess" />
    <ShelfModal @register="registerShelf" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, onMounted, ref } from 'vue';
  import { useModal } from '/@/components/Modal';
  import { TigerTree, TreeItem, ContextMenuItem } from '/@/components/TigerTree';
  import WareHouseModal from './WareHouseModal.vue';
  import RegionModal from './RegionModal.vue';
  import ShelfModal from './ShelfModal.vue';
  import { getTreeList, DeleteWareHouse, getWHList } from '/@/api/tigerapi/wms/house';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useI18n } from '/@/hooks/web/useI18n';
  const { t } = useI18n('WMS.WareHouse');
 
  export default defineComponent({
    name: 'HouseTree',
    components: { TigerTree, RegionModal, WareHouseModal, ShelfModal },
 
    emits: ['select', 'success','delete'],
    setup(_, { emit }) {
      const treeData = ref<TreeItem[]>();
      const [registerWareHouse, { openModal: openWareHouseModal }] = useModal();
      const [registerRegion, { openModal: openRegionModal }] = useModal();
      const [registerShelf, { openModal: openShelfModal }] = useModal();
      const { createMessage } = useMessage();
      function addHouse() {
        openWareHouseModal(true, {
          data: 'content',
          info: 'Info',
        });
      }
      async function fetch() {
        treeData.value = (await getTreeList()) as unknown as TreeItem[];
        // treeData.value.forEach(warehouse => {
        //   if(warehouse.children?.length>0){
        //     warehouse.children.forEach(regions => {
        //       if(regions.children?.length>0){
        //         regions.children.forEach(shelf => {
        //           createIcon(shelf.houseType)
        //         });
        //       }else{ 
        //         createIcon(regions.houseType);
        //       }
        //     });
        //   }else{
        //     createIcon(warehouse.houseType);
        //   }
 
        // });
        console.log(treeData.value);
      }
 
      function handleSelect(keys: any[]) {
        console.log(treeData.value);
 
        emit('select', keys[0]);
      }
      onMounted(() => {
        fetch();
      });
 
      function handleSuccess() {
        fetch();
      }
 
      async function getRightMenuList(node: any): Promise<ContextMenuItem[]> {
        var name = node.houseType === 'Warehouse' ? t('新增储区') : t('新增货架');
 
        if (node.houseType === 'Shelf') {
          return [
            {
              label: t('删除'),
              handler: () => {
                console.log('点击了删除', node);
                const apiAction = DeleteWareHouse(node);
                apiAction.then((action) => {
                  if (action.IsSuccessed) {
                    emit('delete', 0);
                    createMessage.info(t('删除成功'));
                    fetch();
                  } else {
                    createMessage.error(node.Code + ':' + t('删除失败'));
                  }
                });
              },
              icon: 'bx:bxs-folder-open',
            },
          ];
        } else {
          return [
            {
              label: name,
              handler: async function () {
                console.log('点击了新增', node);
                if (node.houseType === 'Warehouse') {
                  var org = await GetORG_CODE(node.eventKey);
                  openRegionModal(true, {
                    data: org,
                    info: node.id.slice(1),
                  });
                } else {
                  org = await GetORG_CODE(node.eventKey);
                  openShelfModal(true, {
                    data: org,
                    info: node.id.slice(1),
                  });
                }
              },
              icon: 'bi:plus',
            },
            {
              label: t('删除'),
              handler: () => {
                console.log('点击了删除', node);
                const apiAction = DeleteWareHouse(node);
                apiAction.then((action) => {
                  if (action.IsSuccessed) {
                    console.log(action);
                    emit('delete', 0);
                    createMessage.info(t('删除成功'));
                    fetch();
                  } else {
                    createMessage.error(node.Code + ':' + t('删除失败'));
                  }
                });
              },
              icon: 'bx:bxs-folder-open',
            },
          ];
        }
      }
      async function GetORG_CODE(params: string) {
        var org = await getWHList(params);
        return org[0].AUTH_ORG;
      }
      function createIcon({ houseType }) {
        if (houseType === 'Warehouse') {
          return 'warehouse|svg';
        }
        if (houseType === 'Region') {
          return 'Region|svg';
        }
        if (houseType === 'Shelf') {
          return 'Shelf|svg';
        }
        return '';
      }
      return {
        t,
        treeData,
        handleSelect,
        getRightMenuList,
        addHouse,
        registerWareHouse,
        registerRegion,
        registerShelf,
        handleSuccess,
        GetORG_CODE,
        createIcon,
      };
    },
  });
</script>