<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :title="modalTitle"
    :showOkBtn="false"
    :showCancelBtn="false"
    :footer="null"
    width="760px"
  >
    <div class="pt-3px pr-3px">
      <BasicTable @register="registerTable" />
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { computed, defineComponent, h, reactive } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { Time } from '/@/components/Time';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useLocaleStore } from '/@/store/modules/locale';
  import { getGiveCamLog } from '/@/api/device';

  export default defineComponent({
    components: {
      BasicModal,
      BasicTable,
    },
    setup() {
      const { t } = useI18n();
      const localeStore = useLocaleStore();
      const queryParams = reactive<Recordable>({
        id: undefined,
        cameraId: undefined,
        snCode: '',
      });

      const modalTitle = computed(() =>
        localeStore.getLocale === 'en' ? 'Distribution Log' : '分配日志',
      );

      const columns: BasicColumn[] = [
        {
          title: t('routes.retailer.jxs'),
          dataIndex: 'agentName',
          width: 220,
          customRender: ({ record }) => record.agentName || record.subAgentName || '-',
        },
        {
          title: t('routes.finance.updateTime'),
          dataIndex: 'updateTime',
          width: 220,
          customRender: ({ record }) => {
            const time = record.updateTime || record.createTime;
            return time
              ? h(Time, {
                  value: time,
                  mode: 'datetime',
                })
              : '-';
          },
        },
      ];

      const [registerTable, { reload }] = useTable({
        api: getGiveCamLog,
        columns,
        immediate: false,
        autoCreateKey: true,
        searchInfo: queryParams,
        showIndexColumn: true,
        indexColumnProps: {
          title: localeStore.getLocale === 'en' ? 'No.' : '\u5e8f\u5217',
          width: 70,
        },
        showTableSetting: false,
        canResize: false,
        bordered: true,
        fetchSetting: {
          pageField: 'pageNum',
          sizeField: 'pageSize',
          listField: 'list',
          totalField: 'total',
        },
      });

      const [register, { changeLoading }] = useModalInner(async (data) => {
        queryParams.id = data?.id;
        queryParams.cameraId = data?.id;
        queryParams.snCode = data?.snCode || '';

        changeLoading(true);
        try {
          await reload({ page: 1 });
        } finally {
          changeLoading(false);
        }
      });

      return {
        modalTitle,
        register,
        registerTable,
      };
    },
  });
</script>
