<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection" @fetch-success="onFetchSuccess">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="handle('baseInfo')" v-auth="'zjgl-zj-jbxx'">查看基本信息</a-button>
        <a-button type="primary" @click="handle('personInfo')" v-auth="'zjgl-zj-jbxx'">查看笔录信息</a-button>
      </template>
    </BasicTable>
    <BaseInfoModel @register="registerModal" />
    <PutDownInfoModel @register="registerPutDownModal" />
  </div>
</template>

<script lang="ts" name="dkgl-investigate-Filling" setup>
  import { BasicTable } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import BaseInfoModel from './model/BaseInfoModel.vue';
  import PutDownInfoModel from './model/PutDownInfoModel.vue';
  import { columns, searchFormSchema } from './data';
  import { useModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { list } from './api';

  const [registerModal, { openModal }] = useModal();
  const [registerPutDownModal, { openModal: openPutDownModal }] = useModal();

  //注册table数据
  const { tableContext } = useListPage({
    tableProps: {
      title: '地块信息',
      api: list,
      columns,
      canResize: false,
      //定义rowSelection的类型，默认是checkbox多选，可以设置成radio单选
      rowKey: 'id',
      clickToRowSelect: true,
      showActionColumn: false,
      showIndexColumn: true,
      rowSelection: { type: 'radio' },
      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: false,
      },
      actionColumn: {
        width: 120,
        fixed: 'right',
      },
    },
  });

  const [registerTable, { reload }, { rowSelection, selectedRows, selectedRowKeys }] = tableContext;
  const { createMessage } = useMessage();
  function handle(type) {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    if (type === 'baseInfo') {
      openModal(true, {
        record: selectedRows.value[0],
        isUpdate: true,
        showFooter: false,
      });
    }
    if (type === 'personInfo') {
      openPutDownModal(true, {
        record: selectedRows.value[0],
        isUpdate: true,
        showFooter: false,
      });
    }
  }
  /**
   * 接口请求成功后回调
   */
  function onFetchSuccess(result) {
    result.items.forEach((item, index) => {
      item.id = index + 1;
    });
  }
</script>
<style scoped></style>
