<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <BasicTable class="w-4/4 xl:w-5/5" @register="registerTable" :searchInfo="searchInfo">
      <template #toolbar>
        <PermissionBtn @btnEvent="onBtnClicked"></PermissionBtn>
      </template>
    </BasicTable>
    <DeptModal @register="registerModal" @success="handleSuccess" />
    <PosGroupModal @register="registerPosGroupModal" @success="handleSuccess"></PosGroupModal>
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { reactive, ref } from 'vue';

  import { BasicTable, useTable } from '@/components/Table';
  import { getNoticeList, deleteNotice,getNoticeDetail } from '@/api/demo/system';
  import { PageWrapper } from '@/components/Page';
  import { useModal } from '@/components/Modal';
  import { useMessage } from '@/hooks/web/useMessage';

  import { DeptTree } from './page';
  import { DeptModal } from './page';
  import { PosGroupModal } from './page';
  import { PermissionBtn } from '@/components/PermissionBtn/index';
  import { columns, searchFormSchema } from './dept.data';

  defineOptions({ name: 'DeptManagement' });
  const { createConfirm, createMessage } = useMessage();

  const [registerModal, { openModal: openDeptModal }] = useModal();
  const [registerPosGroupModal, { openModal: openPosGroupModal }] = useModal();
  const searchInfo = reactive<Recordable>({});

  const [registerTable, { reload, getSelectRows, clearSelectedRowKeys }] = useTable({
    title: '通知详情',
    api: getNoticeList,
    rowKey: 'Id',
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    rowSelection: {
      //多选框
      type: 'checkbox',
      // type: 'radio',
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    handleSearchInfoFn(info) {
      return info;
    },
  });


  async function handlePoGroup() {
    let rows = getSelectRows();
    if (!rows.length) {
      return createMessage.warn('请选择一个部门');
    }
    var record = rows[0];
    openPosGroupModal(true, {
      record,
      isUpdate: false,
    });
  }
  const childRef = ref<any>();

  function handlePreViewData(){

  }
  function handleCreate() {
    openDeptModal(true, {
      isUpdate: false,
    });
  }

  function handleEdit() {
    let rows = getSelectRows();
    if (rows.length != 1) {
      return createMessage.warn('请选择一条数据进行编辑');
    }
    const record = rows[0];
    
    // let res:any = getNoticeDetail({id:rows[0].Id});
    // const record = res.result;
    // console.log("record",res);
    openDeptModal(true, {
      record,
      isUpdate: true,
    });
  }

  async function handleDelete() {
    let rows = getSelectRows();

    if (rows.length == 0) {
      return createMessage.warn('请选择一条通知进行删除');
    }
    const query = [];
    rows.forEach((item,index)=>{
      query.push(item.Id);
    })
    
    createConfirm({
      iconType: 'info',
      title: '删除',
      content: '确定要删除当前通知吗',
      onOk: async () => {
        const data = await deleteNotice(query);
        if (data) {
          handleSuccess();
          return createMessage.success('删除成功');
        } else {
          return createMessage.error('删除失败');
        }
      },
    });
  }
  function onBtnClicked(domId) {
    switch (domId) {
      case 'btnAdd':
        handleCreate();
        break;
      case 'btnEdit':
        handleEdit();
        break;
      case 'btnDelete':
        handleDelete();
        break;
      case 'btnPoGroup':
        handlePoGroup();
        break;
      default:
        break;
    }
  }
  function handleSelect(orgId = '') {
    searchInfo.orgId = orgId;
    reload();
  }
  function handleSuccess() {
    clearSelectedRowKeys();
    // childRef.value.fetch();
    reload();
  }
</script>
