<template>
  <BasicDrawer v-bind="$attrs" :isDetail="true" @register="channelDrawer" title="通道列表" destroyOnClose>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined" />
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <c-button>
            批量操作
            <Icon icon="ant-design:down-outlined" />
          </c-button>
        </a-dropdown>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
      <template #fileSlot="{ text }">
        <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
        <c-button
          v-else
          :ghost="true"
          type="primary"
          preIcon="ant-design:download-outlined"
          size="small"
          @click="downloadFile(text)"
          >下载</c-button
        >
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <JgbDeviceDrawer @register="registerDrawer" @success="reload" :showFooter="showFooter" />
    <JgbDeviceModal @register="registerModal" @success="handleSuccess" />
    <PlayDrawer @register="playDrawer" @success="reload" />
    <PlaybackDrawer @register="playbackDrawer" @success="reload" />
  </BasicDrawer>
</template>

<script lang="ts" name="device-jgbDevice" setup>
  import { ref } from 'vue';
  import {
    BasicTable,
    TableAction,
    BasicDrawer,
    useDrawer,
    useDrawerInner,
    useModal,
    useListPage,
    downloadFile
  } from '@jdlinker/ui';
  import { useUserStore, usePermission } from '@jdlinker/func';
  import JgbDeviceDrawer from './components/JgbDeviceDrawer.vue';
  import JgbDeviceModal from './components/JgbDeviceModal.vue';
  import PlayDrawer from './components/PlayDrawer.vue';
  import PlaybackDrawer from './components/PlaybackDrawer.vue';
  import { columns, searchFormSchema } from './JgbDevice_data';
  import { list, deleteOne, batchDelete, getImportUrl, getExportUrl } from './JgbDevice_api';
  const userStore = useUserStore();
  const { hasPermission } = usePermission();
  //注册model
  const [registerModal, { openModal }] = useModal();
  //注册Drawer
  const [registerDrawer, { openDrawer }] = useDrawer();
  //注册播放Drawer
  const [playDrawer, { openDrawer: openPlayDrawer }] = useDrawer();
  //注册回放Drawer
  const [playbackDrawer, { openDrawer: openPlaybackDrawer }] = useDrawer();
  const showFooter = ref(true);
  const deviceId = ref('');
  const [channelDrawer] = useDrawerInner(async (data) => {
    console.log(data.deviceId);
    deviceId.value = data.deviceId;
    // queryList(deviceId.value);
  });
  //列表数据集合
  const channelList = ref([]);
  //注册table数据
  const { tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: 'jgb_device',
      api: list,
      // dataSource:channelList,
      columns,
      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: true,
        fieldMapToNumber: [],
        fieldMapToTime: []
      },
      fetchSetting: {
        // 传给后台的当前页字段
        pageField: 'page',
        // 传给后台的每页显示多少条的字段
        sizeField: 'count',
        // 接口返回表格数据的字段
        listField: 'list',
        // 接口返回表格总数的字段
        totalField: 'total'
      },
      beforeFetch(params) {
        params.deviceId = deviceId.value;
      },
      actionColumn: {
        width: 220,
        fixed: 'right'
      }
    },
    exportConfig: {
      name: 'jgb_device',
      url: getExportUrl
    },
    importConfig: {
      url: getImportUrl,
      success: handleSuccess
    }
  });

  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

  /**
   * 查询集合数据
   */
  function queryList(deviceId) {
    list({ deviceId: deviceId, page: 1, count: 15 }).then((res) => {
      channelList.value = res.list;
    });
  }

  /**
   * 新增事件
   */
  function handleAdd() {
    openDrawer(true, { isUpdate: 0, showFooter: true });
  }
  /**
   * 编辑事件
   */
  function handleEdit(record: any) {
    if (userStore.getSysOpenModal) {
      openModal(true, { record, isUpdate: true, showFooter: true });
    } else {
      openDrawer(true, { record, isUpdate: 1, showFooter: true });
    }
  }
  /**
   * 详情
   */
  function handleDetail(record: any) {
    if (userStore.getSysOpenModal) {
      openModal(true, { record, isUpdate: 2 });
    } else {
      openDrawer(true, { record, isUpdate: 2 });
    }
  }
  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, handleSuccess);
  }
  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
  }
  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }
  /**
   * 播放
   */
  function handlePlay(record: any) {
    openPlayDrawer(true, { record });
  }
  /**
   * 回放
   */
  function handlePlayback(record: any) {
    openPlaybackDrawer(true, { record });
  }
  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        icon: 'ant-design:video-camera-outlined',
        tooltip: '播放',
        onClick: handlePlay.bind(null, record)
      },
      {
        icon: 'ant-design:video-camera-add-outlined',
        tooltip: '回放',
        onClick: handlePlayback.bind(null, record),
        ifShow: () => hasPermission('videoCenter:channel:playback')
      }
      // {
      //   icon: 'clarity:info-standard-line',
      //   tooltip: '详情',
      //   onClick: handleDetail.bind(null, record),
      // },
      // {
      //   icon: 'clarity:note-edit-line',
      //   tooltip: '编辑',
      //   onClick: handleEdit.bind(null, record),
      // },
      // {
      //   icon: 'ant-design:delete-outlined',
      //   color: 'error',
      //   tooltip: '删除',
      //   popConfirm: {
      //     title: '是否确认删除',
      //     placement: 'left',
      //     confirm: handleDelete.bind(null, record),
      //   },
      // },
    ];
  }
  /**
   * 下拉操作栏
   */
  function getDropDownAction(record) {
    return [
      {
        label: '详情',
        onClick: handleDetail.bind(null, record)
      },
      {
        label: '删除',
        popConfirm: {
          title: '确定删除吗?',
          confirm: handleDelete.bind(null, record)
        }
      }
    ];
  }
</script>
<style scoped></style>
