<script lang="ts" setup>
  import { PageWrapper } from '/@/components/Page';
  import { useDrawer } from '/@/components/Drawer';
  import type { DrawerParams } from './config.data';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import {
    columnSchema,
    formSchema,
    streamingMediaTypeMapping,
    streamingMediaSSLMapping,
  } from './config.data';
  import SteramingDrawer from './SteramingDrawer.vue';
  import { getStreamingMediaList, deleteStreamingMediaRecord } from '/@/api/camera/cameraManager';
  import { StreamingMediaModel } from '/@/api/camera/model/cameraModel';
  import { ref } from 'vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { Popconfirm } from 'ant-design-vue';
  import { Authority } from '/@/components/Authority';

  const enabledBatchDelete = ref(true);
  const [register, { reload, getSelectRowKeys }] = useTable({
    title: '流媒体列表',
    api: getStreamingMediaList,
    columns: columnSchema,
    showIndexColumn: false,
    clickToRowSelect: false,
    formConfig: {
      labelWidth: 120,
      schemas: formSchema,
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    rowKey: 'id',
    actionColumn: {
      width: 100,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },

    rowSelection: {
      type: 'checkbox',
      onChange(rowKeys: string[]) {
        enabledBatchDelete.value = rowKeys.length <= 0;
      },
    },
  });

  const [registerDrawer, { openDrawer }] = useDrawer();

  const handleCreateStreamingMedia = () => {
    openDrawer<DrawerParams>(true, {
      createFlag: true,
    });
  };

  const handleUpdateStreamingMedia = (record: StreamingMediaModel) => {
    openDrawer<DrawerParams>(true, {
      createFlag: false,
      record,
    });
  };

  const handleDeleteRecord = async (record?: StreamingMediaModel) => {
    let ids = record ? [record.id] : getSelectRowKeys();
    try {
      await deleteStreamingMediaRecord({ ids });
      enabledBatchDelete.value = true;
      const { createMessage } = useMessage();
      createMessage.success('删除成功');
    } catch (e) {
    } finally {
      reload();
    }
  };

  const handleDrawerSubmitSuccess = () => {
    openDrawer(false);
    reload();
  };
</script>

<template>
  <PageWrapper dense contentFullHeight>
    <BasicTable @register="register">
      <template #type="{ record }">
        <span>{{ streamingMediaTypeMapping[record.type] }}</span>
      </template>
      <template #ssl="{ record }">
        <span>{{ streamingMediaSSLMapping[record.ssl] }}</span>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '编辑',
              auth: 'api:yt:video:platform:update',
              icon: 'clarity:note-edit-line',
              onClick: handleUpdateStreamingMedia.bind(null, record),
            },
            {
              label: '删除',
              auth: 'api:yt:video:platform:delete',
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDeleteRecord.bind(null, record),
              },
            },
          ]"
        />
      </template>
      <template #toolbar>
        <Authority value="api:yt:video:platform:post">
          <a-button type="primary" @click="handleCreateStreamingMedia">新增流媒体</a-button>
        </Authority>
        <Authority value="api:yt:video:platform:delete">
          <Popconfirm
            title="您确定要批量删除数据"
            ok-text="确定"
            cancel-text="取消"
            @confirm="handleDeleteRecord()"
          >
            <a-button color="error" type="primary" :disabled="enabledBatchDelete">
              批量删除
            </a-button>
          </Popconfirm>
        </Authority>
      </template>
    </BasicTable>
    <SteramingDrawer @register="registerDrawer" @success="handleDrawerSubmitSuccess" />
  </PageWrapper>
</template>

<style></style>
