<template>
  <BasicModal
    class="resultListTab"
    v-bind="$attrs"
    @register="register"
    :showCancelBtn="false"
    :title="fileFlow.name + '查询结果'"
    okText="我知道了"
    @visible-change="handleVisibleChange"
    :confirmLoading="loading"
    width="700px"
    min-height="50"
    @ok="handleSubmit"
  >
    <div class="pt-2px pr-3px">
      <div class="text"
        >查询到 {{ fileFlow.name }}
        <!-- <span style="color: #0960bd">{{ fileFlow.name }}</span> -->
        无以下场景权限，请联系管理员开通</div
      >
      <BasicTable class="zdyform" @register="registerTable">
        <template #tableTitle>
          <a-tabs v-model:activeKey="authType" @change="changeTable">
          <Tabs.TabPane :key="0" tab="场景查看权限" :disabled="loading" />
          <Tabs.TabPane :key="1" tab="场景编辑权限" :disabled="loading" />
          <Tabs.TabPane :key="2" tab="多元融合查看权限" :disabled="loading" />
          <Tabs.TabPane :key="3" tab="多元融合编辑权限" :disabled="loading" />
        </a-tabs>
          <!-- <a-tabs class="resultListTab" v-model:activeKey="authType" @change="changeTable">
            <Tabs.TabPane :key="0" tab="查看权限" />
            <Tabs.TabPane :key="1" tab="编辑权限" />
          </a-tabs> -->
        </template>
        <template #href="{ record }">
          <a v-if="record.title" target="_blank" :title="record.title" :href="record.webSite">{{
            record.title
          }}</a>
          <span v-else>-</span>
        </template>
        <template #href1="{ record }">
          <a v-if="record.title" target="_blank" :title="record.title" :href="`/code/index.html?caseId=${record.id}#/show`">{{
            record.title
          }}</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 { Tabs } from 'ant-design-vue';
  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';
  import { getNotAuthScene } from '/@/api/operate/case';
  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicTable, [Tabs.name]: Tabs },
    props: {
      userData: { type: Object },
    },
    emits: ['update', 'register'],
    setup(props, { emit }) {
      const fileFlow = reactive({
        file: null,
        name: '',
        list: [],
        type: 2, //2-普通发票，3-专用发票
      });
      const caseId = ref<number>(0); //0查看 、1编辑
      const ryId = ref<string>(''); //0查看 、1编辑
      const authType = ref<number>(0); //0查看 、1编辑
      const sourceType = ref<string>('scene'); //0查看 、1编辑
      const columns = [
        {
          title: '场景标题',
          dataIndex: 'sceneName',
          ellipsis: true,
          slots: { customRender: 'href' },
          resizable: true,
          minWidth: 150,
          width: 250,
        },
        {
          title: '姓名',
          dataIndex: 'ryNickName',
          ellipsis: true,
        },
      ];
      const columns1 = [
        {
          title: '标题',
          dataIndex: 'sceneName',
          ellipsis: true,
          slots: { customRender: 'href1' },
          resizable: true,
          minWidth: 150,
          width: 250,
        },
        {
          title: '姓名',
          dataIndex: 'ryNickName',
          ellipsis: true,
        },
      ];
      const [registerTable, { reload, setTableData, setColumns }] = useTable({
        title: ``,
        // titleHelpMessage: ['已启用expandRowByClick', '已启用stopButtonPropagation'],
        columns: columns,
        searchInfo: { authType: authType.value == 2?0:authType.value == 3?1:authType, caseId: caseId, ryId: ryId, sourceType },
        useSearchForm: false,
        showIndexColumn: false,
        showTableSetting: true,
        pagination: false,
        //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) {
        caseId.value = data.caseId || data.id;
        fileFlow.name = data.ryNo;
        ryId.value = data.ryId;
        getList()
      }
      const handleSubmit = async () => {
        loading.value = true;
        try {
          // const params = await validate();
          // const apiData = params;
          // console.log('res', apiData, params);
          // await InvoiceRegister(apiData);
          closeModal();
          authType.value = 0
          changeTable()
          // createMessage.success(t('common.optSuccess'));
          emit('update');
          loading.value = false;
        } catch (error) {
          loading.value = false;
          console.log('not passing', error);
        }
      };
      const getList = async () => {
        fileFlow.list = await getNotAuthScene({caseId: caseId.value, ryId: ryId.value})
          console.log('not passing', fileFlow.list, authType.value);
        let { list } = fileFlow.list[authType.value];
          console.log('not passing', list);
        setTableData(list)
      }
      function changeTable() {
        console.log('changeTable', authType);
        if (authType.value == 1 || authType.value == 0) {
          sourceType.value = 'scene';
        } else {
          sourceType.value = 'fusion';
        }
        setColumns(sourceType.value == 'scene'?columns:columns1);
        let { list } = fileFlow.list[authType.value];
        console.log('setTableData', list);
        setTableData(list || [])
        // reload();
      }
      function handleVisibleChange(v) {
        v && props.userData && nextTick(() => onDataReceive(props.userData));
      }
      return {
        register,
        registerTable,
        fileFlow,
        handleVisibleChange,
        handleSubmit,
        addListFunc,
        reload,
        okText,
        authType,
        loading,
        changeTable,
        t,
      };
    },
  });
</script>

<style lang="less">
  .resultListTab {
    .ant-tabs-nav {
      margin: 0;
    }
  }
  .zdyform {
    .vben-basic-form {
      margin: 0;
    }
  }
</style>
