<template>
    <Table
        ref="orderTable"
        :tableParams="tableParams"
        :btnRole="[]"
        :right-btn="[]"
        isEmail
        @selectionChange="handleSelectionChange"
        isCurrentChange
        @currentChange="emailContent"
    >
        <template #left>
            <el-button @click='CompletelyDel()'>删除</el-button>
        </template>
    </Table>
</template>

<script setup lang="ts">
    import Table from '@/components/Table/index.vue'
    import { tableTypes } from '@/components/Table/types'
    import { listEmails,trashEmails,completelyDelDraft } from '@/api/emails';
    import { EmailsVO, EmailsQuery } from '@/api/emails/types';
    import SvgIcon from '@/components/SvgIcon/index.vue'
    const router = useRouter();

    const emits = defineEmits(['refresh', 'emailContent', 'delChange', 'exportMails', 'readChange'])
    const emailContent = (val: any) => {
        if(val) {
            emits('emailContent', val)
        }
    }

    const tableParams = reactive<tableTypes<EmailsQuery, EmailsVO>>({
        tableColumn: [
            {
                show: true,
                label: '收件人',
                prop: 'recipientName',
                render: ({column}: { column: EmailsVO }) => {
                        return [
                            h('div', {}, column.recipientName || column.recipient),
                        ]
                    }
             },
            {
                show: true,
                label: '主题',
                prop: 'subject',
                sortable: false,
                render: ({column}: { column: EmailsVO }) => {
                    let annex = null
                    if(column.attachmentNum) {
                        annex = h(SvgIcon, { 'iconClass': 'email-annex', style: { 'margin-right': '2px', width: '16px', height: '16px' } })
                    }
                    return [
                        h('div', { style: { display: 'flex', 'align-items': 'center' } }, [
                            annex,
                            h('span', column.subject)
                        ])
                    ]
                }
            },
            { show: true, label: '日期', prop: 'createTime', sortable: false },
            { show: true, label: '大小', prop: 'emailSizeName', sortable: false},
            {
                show: true, label: '操作', width: 150, prop: 'operation', disabledMove: true, render: ({ column }: { column: EmailsVO }) => {
                    return [
                        h(ElButton, {
                            link: true,
                            type: 'primary',
                            onClick:async($event)=>{
                                $event.stopPropagation()
                                const { href } = router.resolve({
                                    path: '/email/send',
                                    query: {
                                        id: column.id,
                                        type:'edit'
                                    }
                                })
                                window.open(href, '_blank')
                            }
                        }, '重发'),
                    ]
                }
            },
        ],
        queryParams: {
            boxCode:'DRAFT',
            blurry: ''
        },
        listFn: listEmails
    })

    const { queryParams } = toRefs(tableParams)

    const ids = ref([])
    const single:any = ref(0)
    /** 多选框选中数据 */
    const handleSelectionChange = (selection: EmailsVO[]) => {
        ids.value = selection.map(item => item.id);
        single.value = selection.length;
    }

    const CompletelyDel = () =>{
        if(single.value == 0){
            ElMessage.error('请选择需要删除的邮件')
        }else{
            ElMessageBox.confirm(
                '是否确认删除当前草稿？',
                '确认提示',
                { type: 'warning', confirmButtonText: '提交' }
            ).then(() => {
                completelyDelDraft(ids.value).then(res => {
                    if(res.success) {
                        ElMessage.success('草稿已删除成功')
                        handleQuery()
                    }
                }).finally(() => {
                })

            }).catch(() => {})
        }
    }

    /** 搜索操作 */
    const orderTable = ref()
    const handleQuery = () => {
        orderTable.value.getData()
    }

    defineExpose({
        handleQuery,
        queryParams
    })
</script>
