<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    title="更换记录"
    :width="800"
    @visible-change="handleVisibleChange"
  >
    <BasicTable @register="registerSubtable"></BasicTable>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, nextTick, onMounted, reactive, computed, h, ref } from 'vue';
  import { BasicTable, useTable, BasicColumn, TableAction } from '/@/components/Table';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm } from '/@/components/Form/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { authorizeCamerasnUpdateLog } from '/@/api/authorizeModeling';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { GetCameraDetailApi, UnbindCameraApi } from '/@/api/account';
  import { otherInfoStore } from '/@/store/modules/other';
  import { DetailsApi, IncrementDelayApi } from '/@/api/account';
  import { usePermissionStore } from '/@/store/modules/permission';
  import { useModal } from '/@/components/Modal';

  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicForm, BasicTable, TableAction },
    props: {
      userData: { type: Object },
    },
    emits: ['update'],
    setup(props, { emit }) {
      const otherInfo = otherInfoStore();
      const overviewInfo = computed(() => otherInfo.getOverviewInfo);
      const { createMessage, createConfirm } = useMessage();
      const [registerList, { openModal }] = useModal();
      const permissionStore = usePermissionStore();
      const { getCheckPerm } = permissionStore;
      console.log('overviewInfo', overviewInfo);
      // const overviewInfo = getOverviewInfo() || {}
      const id = ref('');
      const fileFlow = reactive({
        file: null,
        id: '',
        type: 'down', //down-下载，equity-权益
      });
      const columns: BasicColumn[] = [
        {
          title: '原SN码',
          dataIndex: 'oldSnCode',
          width: 120,
        },
        {
          title: '新SN码',
          dataIndex: 'newSnCode',
          width: 150,
        },
        {
          title: '更换Key',
          dataIndex: 'updateAuthorizeKey',
          width: 150,
        },
        {
          title: '操作时间',
          dataIndex: 'updateTime',
          width: 150,
        },
      ];
      const [registerSubtable, { reload }] = useTable({
        api: () => {
          return authorizeCamerasnUpdateLog(id.value);
        },
        columns: columns,
        immediate: false,
        rowKey: 'id',
        fetchSetting: {
          pageField: 'pageNum',
          sizeField: 'pageSize',
          listField: 'data',
          totalField: 'total',
        },
        pagination: false,
        canResize: true,
        bordered: true,
      });

      onMounted(() => {});
      let addListFunc = () => {};
      const [register, { closeModal }] = useModalInner((data) => {
        data && onDataReceive(data);
      });
      function onDataReceive(data) {
        console.log('onDataReceive', data);
        fileFlow.id = data.id;
        id.value = data.id;
        reload();
      }
      async function handleDelete(record: Recordable) {
        createConfirm({
          iconType: 'warning',
          title: () => h('span', '温馨提示'),
          content: () => h('span', `您确定解绑该相机！`),
          onOk: async () => {
            await await UnbindCameraApi({ cameraId: record.id });
            createMessage.success(t('common.optSuccess'));
            reload();
          },
        });
      }
      const handleSubmit = async () => {
        closeModal();
        emit('update');
      };
      function handleVisibleChange(v) {
        v && props.userData && nextTick(() => onDataReceive(props.userData));
      }
      return {
        register,
        registerList,
        fileFlow,
        handleVisibleChange,
        handleSubmit,
        addListFunc,
        registerSubtable,
        reload,
        handleDelete,
        t,
      };
    },
  });
</script>
