<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :title="fileFlow.name + '查询结果'"
    okText="我知道了"
    @visible-change="handleVisibleChange"
    @cancel="resetFields"
    :confirmLoading="loading"
    min-height="50"
    @ok="handleSubmit"
  >
    <div class="pt-2px pr-3px">
      <div class="text">查询到 <span style="color: #0960bd">{{fileFlow.name}}</span> 无以下场景权限，请联系管理员开通</div>
      <a-tabs v-model:activeKey="tableType" @change="changeTable">
        <a-tab-pane :key="0" tab="查看权限" :disabled="loading" />
        <a-tab-pane :key="1" tab="编辑权限" :disabled="loading" />
      </a-tabs>
      <BasicTable @register="registerTable">
        <template #href="{ record }">
          <a
            v-if="record.caseTitle"
            target="_blank"
            :title="record.caseTitle"
            :href="`/code/index.html?caseId=${record.fusionId}#/show`"
            >{{ record.caseTitle }}</a
          >
          <span v-else>-</span>
        </template>
      </BasicTable>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, nextTick, onMounted, reactive, ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicTable, useTable } from '/@/components/Table';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { InvoiceRegister } from '/@/api/order';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { uploadApi } from '/@/api/product/index';
  import { ResultEnum } from '/@/enums/httpEnum';
  import { caseListApi } from '/@/api/operate';
  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicTable },
    props: {
      userData: { type: Object },
    },
    emits: ['update', 'register'],
    setup(props, { emit }) {
      const fileFlow = reactive({
        file: null,
        name: '',
        type: 2, //2-普通发票，3-专用发票
      });
      const tableType = ref<number>(0); //0查看 、1编辑
      const columns = [
        {
          title: '标题',
          dataIndex: 'sceneName',
          ellipsis: true,
          slots: { customRender: 'href' },
          resizable: true,
          minWidth: 150,
          width: 300,
        },
        {
          title: '姓名',
          dataIndex: 'userName',
          ellipsis: true,
          width: 100,
        }]
      const [registerTable, { reload }] = useTable({
        api: caseListApi,
        title: ``,
        // titleHelpMessage: ['已启用expandRowByClick', '已启用stopButtonPropagation'],
        columns: columns,
        searchInfo: { isShare: tableType },
        useSearchForm: false,
        showIndexColumn: false,
        showTableSetting: true,
        beforeFetch: (T) => {
          loading.value = true;
          return T;
        },
        afterFetch: (T) => {
          loading.value = false;
          return T;
        },
        rowKey: 'caseId',
        fetchSetting: {
          pageField: 'pageNum',
          sizeField: 'pageSize',
          listField: 'list',
          totalField: 'total',
        },               
        canResize: true,
      });
      const okText = ref('确定');
      const loading = ref(false);
      const { createMessage } = useMessage();

      onMounted(() => {});
      let addListFunc = () => {};
      const [register, { closeModal }] = useModalInner((data) => {
        data && onDataReceive(data);
      });
      function onDataReceive(data) {
        resetFields();
        setFieldsValue(data);
      }
      const handleSubmit = async () => {
        loading.value = true;
        try {
          const params = await validate();
          const apiData = params;
          console.log('res', apiData, params);
          await InvoiceRegister(apiData);
          closeModal();
          resetFields();
          createMessage.success(t('common.optSuccess'));
          emit('update');
          loading.value = false;
        } catch (error) {
          loading.value = false;
          console.log('not passing', error);
        }
      };
      function handleVisibleChange(v) {
        v && props.userData && nextTick(() => onDataReceive(props.userData));
      }
      return {
        register,
        registerTable,
        fileFlow,
        handleVisibleChange,
        handleSubmit,
        addListFunc,
        reload,
        okText,
        loading,
        t,
      };
    },
  });
</script>
