<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="title" width="80%" :showCancelBtn="false" :showOkBtn="false">
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button type="primary" @click="handleAdd">添加</a-button>
      </template>
    </BasicTable>
  </BasicModal>
</template>

<script lang="ts" setup>
  import { ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { ActionItem, BasicColumn, BasicTable, TableAction } from '/@/components/Table';
  import { saveOrUpdate, addList } from '../api';
  import { columns, addSearchFormSchema } from '../data';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getJd } from '/@/api/common/api';
  // Emits声明
  const emit = defineEmits(['register', 'success']);
  const isUpdate = ref(true);

  const { createMessage } = useMessage();
  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {});
  //注册table数据
  const { tableContext, onExportXls } = useListPage({
    tableProps: {
      title: '省级审核数据',
      api: addList,
      columns,
      canResize: false,
      clickToRowSelect: true,
      //定义rowSelection的类型，默认是checkbox多选，可以设置成radio单选
      rowKey: 'bsm',
      rowSelection: { type: 'radio' },
      formConfig: {
        labelWidth: 120,
        schemas: addSearchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: false,
      },
      showIndexColumn: true,
      showActionColumn: false,
    },
  });

  const [registerTable, { reload }, { rowSelection, selectedRows, selectedRowKeys }] = tableContext;
  //设置标题
  const title = computed(() => (!unref(isUpdate) ? '新增' : '新增'));
  const jd = ref('');
  const getJdData = ()=>{
    getJd({}).then((res) => {
      console.log(res);
      jd.value = `${res.nd}0${res.jd}`
    });
  }
  getJdData();
  
  //添加按钮事件
  const handleAdd = () => {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    saveOrUpdate({jd:jd.value, dkdm: selectedRows.value[0].dkdm}).then(() => {
      createMessage.success('操作成功!');
      //关闭弹窗
      closeModal();
      //刷新列表
      emit('success');
    });
  };
</script>

<style lang="less" scoped></style>
