<template>
  <q-splitter :model-value="60" class="w-full h-full">
    <template #before>
      <div class="pr-1">
        <w-grid
          ref="treeGridRef"
          :title="$t('system.department.grid.title')"
          :config-button="true"
          dense-body
          selection="multiple"
          :checkbox-selection="false"
          :tree="true"
          :tree-icon="
            (row) => {
              return { name: 'folder', color: 'amber' };
            }
          "
          :data-url="Environment.apiContextPath('/api/system/department')"
          :pageable="false"
          :toolbar-configure="{ noIcon: false }"
          :toolbar-actions="[
            'refresh',
            'separator',
            [
              { extend: 'addTop', label: $t('system.department.grid.toolbar.addTop') },
              { extend: 'addChild', label: $t('system.department.grid.toolbar.addChild') },
            ],
            'expand',
            'edit',
            'remove',
            'separator',
            'view',
            'separator',
            'export',
          ]"
          :columns="[
            { width: '100%', name: 'name', label: $t('name') },
            { width: 120, name: 'code', label: $t('code') },
            { width: 100, name: 'lastModifier', label: $t('lastModifier') },
            { width: 110, name: 'lastModifyDate', label: $t('lastModifyDate'), format: Formater.dateOnly() },
            { width: 80, name: 'enable', label: $t('status'), sortable: false, format: Formater.enableTag() },
          ]"
          :editor="{
            dialog: {
              width: '600px',
              height: '300px',
            },
            form: {
              colsNum: 1,
              fields: [
                { name: 'code', label: $t('code'), type: 'text', required: true },
                { name: 'name', label: $t('name'), type: 'text', required: true },
                { name: 'description', label: $t('description'), type: 'textarea', rows: 1 },
                { name: 'enable', label: $t('enable'), type: 'checkbox', defaultValue: true },
              ],
            },
          }"
          :viewer="{
            panel: {
              columnNum: 1,
              fields: [
                { name: 'id', label: $t('id') },
                { name: 'code', label: $t('code') },
                { name: 'name', label: $t('name') },
                { name: 'description', label: $t('description') },
                { name: 'enable', label: $t('enable'), format: Formater.none() },
                { name: 'dataComeFrom', label: $t('dataComeFrom') },
                { name: 'creator', label: $t('creator') },
                { name: 'createDate', label: $t('createDate') },
                { name: 'lastModifier', label: $t('lastModifier') },
                { name: 'lastModifyDate', label: $t('lastModifyDate'), format: Formater.none() },
                { name: 'corporationCode', label: $t('corporationCode') },
              ],
            },
          }"
          @row-click="
            (evt, row, index) => {
              currentSelectedDepartmentId = row.id;
              userGridRef?.refresh();
            }
          "
          @before-request-data="
            () => {
              currentSelectedDepartmentId = '';
              userGridRef?.refresh();
            }
          "
        ></w-grid>
      </div>
    </template>
    <template #after>
      <div class="pl-1">
        <q-tabs v-model="selectedTabRef" inline-label align="left" :breakpoint="0">
          <q-tab name="user" icon="bi-diagram-3" :label="$t('user')" />
        </q-tabs>

        <q-tab-panels v-model="selectedTabRef" animated swipeable keep-alive>
          <q-tab-panel name="user" class="px-0">
            <SelectUserGrid
              ref="userGridRef"
              :fetch-data-url="Environment.apiContextPath('/api/system/user/queryUsersByDepartment')"
              :fetch-other-data-url="Environment.apiContextPath('/api/system/user/queryOtherUsersByDepartment')"
              foreign-key="departmentId"
              :foreign-value="currentSelectedDepartmentId"
              @select-in="selectIn"
              @select-out="selectOut"
              @select-all-in="selectAllIn"
              @select-all-out="selectAllOut"
            >
            </SelectUserGrid>
          </q-tab-panel>
        </q-tab-panels>
      </div>
    </template>
  </q-splitter>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { Environment, axios, EnumTools, Formater } from 'platform-core';
import SelectUserGrid from '../shared/SelectUserGrid.vue';

const treeGridRef = ref();
const userGridRef = ref();

const selectedTabRef = ref('user');
const currentSelectedDepartmentId = ref('');

const selectIn = (ids: string[], gridComponent, dialogComponent) => {
  axios
    .post(Environment.apiContextPath('/api/system/department/addUsers'), {
      one: treeGridRef.value.getSelectedRows()[0].id,
      many: ids,
    })
    .then(() => {
      gridComponent?.refresh();
      dialogComponent?.close();
    });
};

const selectOut = (ids, gridComponent) => {
  axios
    .post(Environment.apiContextPath('/api/system/department/removeUsers'), {
      one: treeGridRef.value.getSelectedRows()[0].id,
      many: ids,
    })
    .then(() => {
      gridComponent?.refresh();
    });
};

const selectAllIn = (gridComponent) => {
  axios
    .post(Environment.apiContextPath('/api/system/department/addAllUsers'), {
      one: treeGridRef.value.getSelectedRows()[0].id,
      many: [],
    })
    .then(() => {
      gridComponent?.refresh();
    });
};

const selectAllOut = (gridComponent) => {
  axios
    .post(Environment.apiContextPath('/api/system/department/removeAllUsers'), {
      one: treeGridRef.value.getSelectedRows()[0].id,
      many: [],
    })
    .then(() => {
      gridComponent?.refresh();
    });
};

const update = (ids, gridComponent) => {
  axios
    .post(Environment.apiContextPath('/api/system/department/updateMenus'), {
      one: treeGridRef.value.getSelectedRows()[0].id,
      many: ids,
    })
    .then(() => {
      gridComponent.refresh();
    });
};
</script>
