<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="title" @ok="handleSubmit" width="70%">
    <BasicTable @register="registerTable" :rowSelection="rowSelection"> </BasicTable>
  </BasicModal>
</template>

<script setup lang="ts">
  import { ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { searchsjFormSchema } from '../GZjcyb.data';
  import { BasicTable } from '/@/components/Table';
  import { columns } from '../GZjcyb.data';
  import { getZjlist } from '../GZjcyb.api';

  //注册table数据
  const { tableContext } = useListPage({
    tableProps: {
      title: '抽取',
      api: getZjlist,
      searchInfo: {},
      columns: columns,
      canResize: false,
      immediate: false,
      clickToRowSelect: true,
      rowKey: 'id',
      beforeFetch(params) {
        // params = { ...params, ...getData.value };
        params.noZjIds = getData.value.noZjIds;
        params.zyfxCodes = getData.value.zyfxCodes;
        // params.cqdkId = getData.value.cqdkId;
        let res = [...columns];

        res.splice(7, 1);
        setColumns(res);
      },
      formConfig: {
        labelWidth: 120,
        schemas: searchsjFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: true,
        autoAdvancedCol: 10, //超过几行显示折叠默认是3
      },
      actionColumn: {
        width: 120,
      },
    },
  });
  const emit = defineEmits(['register', 'success']);
  const isUpdate = ref(true);
  const getData = ref<any>({});
  const [registerTable, { reload, setColumns }, { rowSelection, selectedRows }] = tableContext;

  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    setModalProps({ confirmLoading: false, showCancelBtn: data?.showFooter, showOkBtn: data?.showFooter });
    isUpdate.value = !!data?.isUpdate;
    console.log(data.record, 'data.record');
    getData.value = data.record;
    // 刷新列表
    reload();
  });
  //设置标题
  const title = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));
  //提交事件
  async function handleSubmit() {
    try {
      setModalProps({ confirmLoading: true });
      emit('success', selectedRows);
      //关闭弹窗
      closeModal();
      selectedRows.value = [];
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
