<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    title="相机详情"
    :width="800"
    @visible-change="handleVisibleChange"
    @ok="handleSubmit"
  >
    <div class="pt-3px pr-3px">
      <div class="table_list">
        <BasicTable @register="registerSubtable" >
          <template #action="{ record }">
            <TableAction
              :actions="[
                 {
                   label: '解绑',
                   onClick: handleDelete.bind(null, record, 'edit'),
                 },
              ]"
            />
          </template>
        </BasicTable>
      </div>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, nextTick, onMounted, reactive, computed,h } from 'vue';
  import { BasicTable, useTable, BasicColumn,TableAction } from '/@/components/Table';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm } from '/@/components/Form/index';
  import { Time } from '/@/components/Time';
  import { useMessage } from '/@/hooks/web/useMessage';
  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';

  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicForm, BasicTable,TableAction },
    props: {
      userData: { type: Object },
    },
    emits: ['update', 'register'],
    setup(props, { emit }) {
      const otherInfo = otherInfoStore();
      const overviewInfo = computed(() => otherInfo.getOverviewInfo);
      const { createMessage, createConfirm } = useMessage();
      const permissionStore = usePermissionStore();
      const { getCheckPerm } = permissionStore;
      console.log('overviewInfo',overviewInfo)
      // const overviewInfo = getOverviewInfo() || {}
      const fileFlow = reactive({
        file:null,
        id:'',
        type:'down',//down-下载，equity-权益
      })
      const columns: BasicColumn[] = [
        {
          title: 'SN码',
          dataIndex: 'snCode',
          width: 120,
        },
        {
          title: '激活时间',
          dataIndex: 'activatedTime',
          width: 150,
          customRender: ({ record }) => {
            return record.activatedTime
              ? h(Time, {
                  value: record.activatedTime,
                  mode: 'datetime',
                })
              : '-';
          },
        },
        {
          title: '出库时间',
          dataIndex: 'outTime',
          width: 150,
          customRender: ({ record }) => {
            return record.outTime
              ? h(Time, {
                  value: record.outTime,
                  mode: 'datetime',
                })
              : '-';
          },
        },
        {
          title: t('common.operating'),
          dataIndex: 'action',
          ifShow:getCheckPerm('account-equityCameraUnbind'),
          slots: { customRender: 'action' },
          fixed: 'right',
          width: 80,
        },
      ];
      const [
        registerSubtable,
        {
          reload,
        },
      ] = useTable({
        title: `查看相机列表`,
        rowKey: 'phone',
        searchInfo:{
          userId:overviewInfo.value?.id
        },
        api: GetCameraDetailApi,
        columns: columns,
        rowKey: 'id',
        fetchSetting: {
          pageField: 'pageNum',
          sizeField: 'pageSize',
          listField: 'list',
          totalField: 'total',
        },
        showIndexColumn: 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
      }
      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,
        fileFlow,
        handleVisibleChange,
        handleSubmit,
        addListFunc,
        registerSubtable,
        reload,
        handleDelete,
        t,
      };
    },
  });
</script>
