<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <c-button
          v-show="hasPermission('system:menu:add')"
          preIcon="ant-design:plus-outlined"
          type="primary"
          @click="handleCreate"
          >新增菜单</c-button
        >
        <c-button type="primary" @click="expandAll" preIcon="ic:round-expand">展开全部</c-button>
        <c-button type="primary" @click="collapseAll" preIcon="ic:round-compress">折叠全部</c-button>
        <RadioGroup v-model:value="indexType" @change="handleModeChange" class="radiogroup">
          <!--<a-radio :value="1">WEB</a-radio>-->
          <!--<a-radio :value="2">APP</a-radio>-->
          <!--<a-radio :value="3">小程序</a-radio>-->
          <template v-for="item in dictOptions" :key="`${item.value}`">
            <Radio :value="item.value">
              {{ item.label }}
            </Radio>
          </template>
        </RadioGroup>

        <Dropdown v-if="checkedKeys.length > 0">
          <template #overlay>
            <Menu>
              <MenuItem v-if="hasPermission('system:menu:deleteBatch')" key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined" />
                删除
              </MenuItem>
            </Menu>
          </template>
          <c-button
            >批量操作
            <Icon icon="ant-design:down-outlined" />
          </c-button>
        </Dropdown>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>
    <MenuDrawer @register="registerDrawer" @success="handleSuccess" :showFooter="showFooter" :indexType="indexType" />
    <DataRuleList @register="registerDrawer1" />
  </div>
</template>
<script lang="ts" name="system-menu" setup>
  import { ref } from 'vue';
  import { BasicTable, TableAction, useListPage, Button as CButton, useDrawer } from '@jdlinker/ui';
  import MenuDrawer from './MenuDrawer.vue';
  import DataRuleList from './DataRuleList.vue';
  import { columns, searchFormSchema } from './menu.data';
  import { list, deleteMenu, batchDeleteMenu } from './menu.api';
  import { initDictOptions, usePermission } from '@jdlinker/func';
  import { omit } from 'lodash-es';
  import { RadioGroup, Radio, Menu, Dropdown, MenuItem } from 'ant-design-vue';

  const { hasPermission } = usePermission();
  const checkedKeys = ref<any>([]);
  const showFooter = ref(true);
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerDrawer1, { openDrawer: openDataRule }] = useDrawer();
  // 列表页面公共参数、方法
  const { tableContext } = useListPage({
    tableProps: {
      title: '菜单列表',
      api: list,
      columns: columns,
      pagination: false,
      isTreeTable: true,
      useSearchForm: true,
      showTableSetting: true,
      bordered: true,
      showIndexColumn: false,
      tableSetting: { fullScreen: true },
      formConfig: {
        schemas: searchFormSchema
      },
      beforeFetch(params) {
        initDictData();
        params.clientType = indexType.value;
      }
    }
  });
  //注册table数据
  const [registerTable, { reload, expandAll, collapseAll }] = tableContext;

  /**
   * 选择列配置
   */
  const rowSelection = {
    type: 'checkbox',
    columnWidth: 30,
    selectedRowKeys: checkedKeys,
    onChange: onSelectChange
  };

  const indexType = ref('1');
  const dictOptions = ref<any>([]);

  /**
   * 选择事件
   */
  function onSelectChange(selectedRowKeys: any[]) {
    checkedKeys.value = selectedRowKeys;
  }

  /**
   * 新增
   */
  function handleCreate() {
    showFooter.value = true;
    openDrawer(true, {
      record: { clientType: indexType.value },
      isUpdate: false
    });
  }

  /**
   * 编辑
   */
  function handleEdit(record) {
    showFooter.value = true;
    openDrawer(true, {
      record: { clientType: indexType.value, ...record },
      isUpdate: true
    });
  }
  /**
   * 详情
   */
  // function handleDetail(record) {
  //   showFooter.value = false;
  //   openDrawer(true, {
  //     record,
  //     isUpdate: true
  //   });
  // }
  /**
   * 添加下级
   */
  function handleAddSub(record) {
    openDrawer(true, {
      record: { parentId: record.id, menuType: 1, clientType: indexType.value },
      isUpdate: false
    });
  }
  /**
   * 数据权限弹窗
   */
  function handleDataRule(record) {
    openDataRule(true, { id: record.id });
  }

  /**
   * 删除
   */
  async function handleDelete(record) {
    await deleteMenu({ id: record.id }, reload);
  }
  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDeleteMenu({ ids: checkedKeys.value }, reload);
  }
  /**
   * 成功回调
   */
  function handleSuccess() {
    reload();
  }

  // function onFetchSuccess() {
  //   // 演示默认展开所有表项
  //   nextTick(expandAll);
  // }

  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        icon: 'clarity:note-edit-line',
        tooltip: '编辑',
        onClick: handleEdit.bind(null, record),
        ifShow: () => hasPermission('system:menu:edit')
      }
    ];
  }

  /**
   * 下拉操作栏
   */
  function getDropDownAction(record) {
    return [
      // {
      //   label: '详情',
      //   onClick: handleDetail.bind(null, record),
      // },
      {
        // icon: 'ant-design:apartment-outlined',
        // tooltip: '添加下级',
        label: '添加下级',
        onClick: handleAddSub.bind(null, record),
        ifShow: () => hasPermission('system:menu:addChild')
      },
      {
        label: '数据规则',
        onClick: handleDataRule.bind(null, record),
        ifShow: () => hasPermission('system:menu:rule')
      },
      {
        label: '删除',
        color: 'error',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record)
        },
        ifShow: () => hasPermission('system:menu:delete')
      }
    ];
  }

  function handleModeChange() {
    //const mode = e.target.value;
    reload();
  }

  async function initDictData() {
    // let { dictCode, stringToNumber } = props;
    //根据字典Code, 初始化字典数组
    const dictData = await initDictOptions('client_type');
    dictOptions.value = dictData.reduce((prev, next) => {
      if (next) {
        const value = next['value'];
        prev.push({
          label: next['text'] || next['label'],
          value: value,
          ...omit(next, ['text', 'value'])
        });
      }
      return prev;
    }, []);
  }
</script>
<style lang="less" scoped>
  .radiogroup {
    margin: 5px 0 0 30px;
  }
</style>
