<template>
  <PageWrapper>
    <a-card class="proCard tabsCard">
      <a-tabs v-model:activeKey="activeName" @change="handleClick">
        <a-tab-pane :key="item.value" :tab="item.configName" v-for="(item, index) in messageTypeList">
          <template #tab>
            <a-badge :count="item.number?item.number:''" class="item">
             <span class="t1">{{item.name}}</span>
            </a-badge>
          </template>
        </a-tab-pane>
      </a-tabs>
      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        :row-key="(row) => row.id"
        ref="tableRef"
        :row-selection="{onChange: onSelectionChange}"
      >
        <template #tableTitle>
          <a-space>
            <a-button type="primary" @click="handleSetRead()">
              批量确认
            </a-button>
            <a-button type="danger" @click="handleDelete()" :disabled="!selectionData.length">
              <template #icon>
              <DeleteOutlined />
            </template>
                删除
              </a-button>
          </a-space>
        </template>
        <template #bodyCell="{ column, record }">
      <template v-if="column.key === 'action'">
        <a-space>
          <a-button type="primary" @click="handleSetRead(record.id)">
            <template #icon><CheckOutlined /></template>
            确认
          </a-button>
          <a-button type="primary" @click="handleInfo(record.id)">
            <template #icon><EyeOutlined /></template>
            详情
          </a-button>
          <a-button type="primary" danger @click="handleDelete(record.id)">
            <template #icon><DeleteOutlined /></template>
            删除
          </a-button>
        </a-space>
        </template>
      </template>
      </BasicTable>
    </a-card>
    <editDialog
        v-if="editVisible"
        :messageId="messageId"
        v-model:visible="editVisible"
        @success="reloadTable('noRefresh')"
    >
    </editDialog>
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { reactive, ref, h,nextTick,defineAsyncComponent,onMounted } from 'vue';
  import {EyeOutlined,DeleteOutlined,CheckOutlined} from '@ant-design/icons-vue';
  import { getMessageProfile,messageDelete,messageBatchDelete,setRead } from '@/api/dashboard/message';
  import { columns } from './columns';
  import { Modal,message } from 'ant-design-vue';
  const editDialog = defineAsyncComponent(() =>
    import('./edit.vue')
)
const messageId =ref(0)
const docHeight = ref(500)
const activeName = ref(1)
const messageTypeList = ref([
  {
    name:'系统通知',
    value:1
  },
  {
    name:'用户私信',
    value:2
  },
  {
    name:'代办事项',
    value:3
  },
])
const editVisible=ref(false)
  const selectionData = ref([])
  const tableRef = ref();
  const formParams = reactive({
    type:1
  });

  const loadDataTable = async (res: any) => {
    const result = await getMessageProfile({ ...formParams, ...res });
    let item = messageTypeList.value.find((item,index)=>item.value==activeName.value)
    item.number = result.number
    return result;
  };

  function reloadTable(noRefresh='') {
    tableRef.value.reload(noRefresh?{}:{pageNo:1});
  }
const handleClick = (e)=>{
  activeName.value = e
  formParams.type = e
  reloadTable()
}

const handleInfo = async (id) => {
  messageId.value=id
  await nextTick();
  editVisible.value=true
};

async function handleDelete(id) {
    Modal.confirm({
      title: '提示',
      content: "确定要删除？",
      onOk: async () => {
        id? await messageDelete(id):await messageBatchDelete(selectionData.value);
        message.success("删除成功");
        reloadTable()
      }
    });
}
async function handleSetRead(id) {
    Modal.confirm({
      title: '提示',
      content: "确定标记为已读？",
      onOk: async () => {
        id? await setRead(id):await setRead(selectionData.value);
        message.success("标记成功");
        reloadTable()
      }
    });
}
function onSelectionChange(value){
    selectionData.value = value
  }
  onMounted(() => {
  docHeight.value =document.body.clientHeight - 160
})
</script>

<style lang="less" scoped>
:deep(.ant-badge-count){
    right: -13px;
  }
</style>
