<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate">
          {{ t('wechat.wx.addWx') }}
        </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:logout-outlined',
                color: 'error',
                popConfirm: {
                  title: '是否确认退出账号?',
                  placement: 'left',
                  confirm: handleLogout.bind(null, record),
                },
              },
              {
                icon: 'ant-design:retweet-outlined',
                popConfirm: {
                  title: '刷新联系人？',
                  placement: 'left',
                  confirm: handleRefreshContact.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <WxDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { createVNode, defineComponent, ref } from 'vue'
  import { Modal } from 'ant-design-vue'
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue/lib/icons'
  import { BasicTable, useTable, TableAction } from '@/components/Table'
  import { Button } from '@/components/Button'

  import { useDrawer } from '@/components/Drawer'
  import WxDrawer from './WxDrawer.vue'
  import { useI18n } from 'vue-i18n'

  import { columns, searchFormSchema } from './wx.data'
  import { getWxList, deleteWx } from '@/api/wechat/wx'
  import { getFriendsAndGroupsApi, logoutApi } from "@/api/wechat/wxhook";

  export default defineComponent({
    name: 'WxManagement',
    components: { BasicTable, WxDrawer, TableAction, Button },
    setup() {
      const { t } = useI18n()
      const selectedIds = ref<number[] | string[]>()
      const showDeleteButton = ref<boolean>(false)

      const [registerDrawer, { openDrawer }] = useDrawer()
      const [registerTable, { reload }] = useTable({
        title: t('wechat.wx.wxList'),
        api: getWxList,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        clickToRowSelect: false,
        actionColumn: {
          width: 30,
          title: t('common.action'),
          dataIndex: 'action',
          fixed: undefined,
        },
        rowKey: 'id',
      })

      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
        })
      }

      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          isUpdate: true,
        })
      }

      async function handleLogout(record: Recordable) {
        // 退出账号
        const result = await logoutApi({ id: record.id })
        if (result.code === 0) {
          await reload()
        }
      }

      async function handleRefreshContact(record: Recordable) {
        // 刷新联系人
        const result = await getFriendsAndGroupsApi({ id: record.id })
        if (result.code === 0) {
          await reload()
        }
      }

      async function handleDelete(record: Recordable) {
        const result = await deleteWx({ ids: [record.id] })
        if (result.code === 0) {
          await reload()
        }
      }

      async function handleBatchDelete() {
        Modal.confirm({
          title: t('common.deleteConfirm'),
          icon: createVNode(ExclamationCircleOutlined),
          async onOk() {
            const result = await deleteWx({ ids: selectedIds.value as number[] })
            if (result.code === 0) {
              showDeleteButton.value = false
              await reload()
            }
          },
          onCancel() {
            console.log('Cancel')
          },
        })
      }

      async function handleSuccess() {
        await reload()
      }

      return {
        t,
        registerTable,
        registerDrawer,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        handleBatchDelete,
        handleLogout,
        handleRefreshContact,
        showDeleteButton,
      }
    },
  })
</script>
