<template>
  <div>
    <PageWrapper dense contentFullHeight contentClass="flex">
      <OrganizationIdTree @select="handleSelect" ref="organizationIdTreeRef" />
      <BasicTable
        style="flex: auto"
        :clickToRowSelect="false"
        @register="registerTable"
        :searchInfo="searchInfo"
        class="w-3/4 xl:w-4/5"
      >
        <template #toolbar>
          <a-button type="primary" @click="handleSwitchMode">分屏模式</a-button>
          <Authority :value="CameraPermission.CREATE">
            <a-button type="primary" @click="handleCreateOrEdit(null)"> 新增视频 </a-button>
          </Authority>
          <Authority :value="CameraPermission.DELETE">
            <Popconfirm
              title="您确定要批量删除数据"
              ok-text="确定"
              cancel-text="取消"
              @confirm="handleDeleteOrBatchDelete(null)"
            >
              <a-button type="primary" color="error" :disabled="hasBatchDelete">
                批量删除
              </a-button>
            </Popconfirm>
          </Authority>
        </template>
        <template #img="{ record }">
          <TableImg
            :size="30"
            :showBadge="false"
            :simpleShow="true"
            :imgList="
              typeof record.avatar !== 'undefined' && record.avatar !== '' && record.avatar != null
                ? [record.avatar]
                : null
            "
          />
        </template>
        <template #accessMode="{ record }">
          <Tag :color="record.accessMode === AccessMode.ManuallyEnter ? 'cyan' : 'blue'">{{
            record.accessMode === AccessMode.ManuallyEnter ? '手动输入' : '流媒体获取'
          }}</Tag>
        </template>
        <template #action="{ record }">
          <TableAction
            :actions="[
              {
                label: '预览',
                icon: 'clarity:note-edit-line',
                auth: CameraPermission.PREVIEW,
                onClick: handleViewVideo.bind(null, record),
              },
              {
                label: '编辑',
                auth: CameraPermission.UPDATE,
                icon: 'clarity:note-edit-line',
                onClick: handleCreateOrEdit.bind(null, record),
              },
            ]"
            :drop-down-actions="[
              {
                label: '删除',
                auth: CameraPermission.DELETE,
                icon: 'ant-design:delete-outlined',
                color: 'error',
                popConfirm: {
                  title: '是否确认删除',
                  confirm: handleDeleteOrBatchDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </BasicTable>
    </PageWrapper>
    <CameraDrawer @register="registerDrawer" @success="handleSuccess" />
    <VideoPreviewModal @register="registerModal" />
  </div>
</template>

<script lang="ts">
  import { defineComponent, reactive, nextTick } from 'vue';
  import { BasicTable, useTable, TableAction, TableImg } from '/@/components/Table';
  import { PageWrapper } from '/@/components/Page';
  import { useDrawer } from '/@/components/Drawer';
  import CameraDrawer from './CameraDrawer.vue';
  import { useResetOrganizationTree, OrganizationIdTree } from '/@/views/common/organizationIdTree';
  import { cameraPage, deleteCameraManage } from '/@/api/camera/cameraManager';
  import { searchFormSchema, columns, AccessMode, PageMode, CameraPermission } from './config.data';
  import VideoPreviewModal from './DialogPreviewVideo.vue';
  import { useModal } from '/@/components/Modal';
  import { Authority } from '/@/components/Authority';
  import { useBatchDelete } from '/@/hooks/web/useBatchDelete';
  import { Popconfirm } from 'ant-design-vue';
  import { Tag } from 'ant-design-vue';

  export default defineComponent({
    components: {
      PageWrapper,
      OrganizationIdTree,
      BasicTable,
      TableAction,
      CameraDrawer,
      VideoPreviewModal,
      TableImg,
      Authority,
      Popconfirm,
      Tag,
    },
    emits: ['switchMode'],
    setup(_, { emit }) {
      const searchInfo = reactive<Recordable>({});
      const { organizationIdTreeRef, resetFn } = useResetOrganizationTree(searchInfo);
      const [registerModal, { openModal }] = useModal();
      // 表格hooks
      const [registerTable, { reload, setProps }] = useTable({
        title: '视频列表',
        api: cameraPage,
        columns,
        showIndexColumn: false,
        clickToRowSelect: false,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
          resetFunc: resetFn,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        rowKey: 'id',
        actionColumn: {
          width: 200,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
      });
      // 弹框
      const [registerDrawer, { openDrawer }] = useDrawer();

      // 刷新
      const handleSuccess = () => {
        reload();
      };
      const { hasBatchDelete, handleDeleteOrBatchDelete, selectionOptions } = useBatchDelete(
        deleteCameraManage,
        handleSuccess,
        setProps
      );

      nextTick(() => {
        setProps(selectionOptions);
      });
      // 新增或编辑
      const handleCreateOrEdit = (record: Recordable | null) => {
        if (record) {
          openDrawer(true, {
            isUpdate: true,
            record,
          });
        } else {
          openDrawer(true, {
            isUpdate: false,
          });
        }
      };

      // 树形选择器
      const handleSelect = (organizationId: string) => {
        searchInfo.organizationId = organizationId;
        handleSuccess();
      };
      const handleViewVideo = (record) => {
        openModal(true, {
          isUpdate: true,
          record,
        });
      };

      const handleSwitchMode = () => {
        emit('switchMode', PageMode.SPLIT_SCREEN_MODE);
      };

      return {
        searchInfo,
        hasBatchDelete,
        handleCreateOrEdit,
        handleDeleteOrBatchDelete,
        handleSelect,
        handleSuccess,
        registerTable,
        registerDrawer,
        organizationIdTreeRef,
        handleViewVideo,
        registerModal,
        AccessMode,
        handleSwitchMode,
        CameraPermission,
      };
    },
  });
</script>
