<template>
  <BasicTable
    ref="tableRef"
    @register="registerTable"
    rowKey="id"
    @selectionChange="selectionChange"
    :rowSelection="{ type: 'radio' }"
    @rowClick="rowClick"
    :pagination="{
      pageSize: 10,
      defaultPageSize: 10,
      showSizeChanger: false,
    }"
  >
    <template #toolbar>
      <div class="tool-btn-wrap">
        <a-button type="primary" @click="handleAdd"> 添加 </a-button>
      </div>
    </template>
    <template #form-custom> custom-slot </template>
  </BasicTable>
  <Popup @register="addRegister" @saveData="saveData" />
</template>
<script lang="ts">
  import { defineComponent, ref, unref } from 'vue';
  import Popup from './popup.vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useModal } from '/@/components/Modal';
  import { getFormConfig, columns } from './data';
  import { getPersonList, addPerson } from '/@/api/sys/member';
  import moment from 'moment';
  import { BasicTable, useTable, EditRecordRow, TableActionType } from '/@/components/Table';

  export default defineComponent({
    name: 'Person',
    components: { BasicTable, Popup },
    emits: ['select'],
    setup(_, { emit }) {
      const { createMessage } = useMessage();
      const { success /*, error*/ } = createMessage;
      const tableRef = ref<Nullable<TableActionType>>(null);
      const [registerTable] = useTable({
        columns: columns,
        // clickToRowSelect: false, // 点击行不勾选
        api: getPersonList,
        useSearchForm: true,
        showTableSetting: true,
        tableSetting: {
          redo: false,
          size: false,
        },
        beforeFetch: beforeFetch,
        formConfig: getFormConfig(),
        maxHeight: 190,
        showIndexColumn: false,
        bordered: true,
      });
      const [addRegister, { openModal: openPopup }] = useModal();
      function getTableAction() {
        // 获取组件
        const tableAction = unref(tableRef);
        if (!tableAction) {
          throw new Error('tableAction is null');
        }
        return tableAction;
      }

      function beforeFetch(params) {
        if (!params.field && !params.order) {
          params.field = 'id';
          params.order = 'descend';
        }
        for (let k in params) {
          if (k !== 'page' && k !== 'pageSize' && k !== 'field' && k !== 'order') {
            if (params[k] === '') {
              delete params[k];
            } else {
              if (!params.filter) {
                params.filter = {};
              }
              if (params.birthday) {
                params.birthday = moment(params.birthday).format('YYYY-MM-DD');
              } else {
                delete params.birthday;
              }
              params.filter[k] = params[k];
              delete params[k];
            }
          }
        }
        params.filter = JSON.stringify(params.filter);
        params.offset = params.page;
        params.limit = params.pageSize;
        delete params.page;
        delete params.pageSize;
      }

      function handleAdd() {
        // 开启弹窗
        openPopup(true, { family: [] });
      }
      function selectionChange(evt) {
        emit('select', evt.rows[0]);
      }

      function rowClick(record: EditRecordRow) {
        emit('select', record);
      }

      async function saveData(params: any) {
        const data = params.data;
        const closeModel = params.closeModal;
        await addPerson(data).then(() => {
          getTableAction().reload();
          closeModel();
          success('创建成功!');
        });
      }

      return {
        tableRef,
        registerTable,
        handleAdd,
        selectionChange,
        rowClick,
        getTableAction,
        addRegister,
        saveData,
      };
    },
  });
</script>
<style scoped>
  .ant-calendar-picker {
    width: 100%;
  }

  /* @media (max-width: 639px) {
    .sys-container .vben-basic-table-header__toolbar > * {
      padding: 6px !important;
      margin-right: 3px;
      font-size: 12px !important;
    }

    .sys-container .vben-basic-table .ant-table-wrapper {
      padding: 3px;
    }
  } */
  .vben-basic-table-header__toolbar {
    justify-content: space-between;
  }

  .tool-btn-wrap {
    flex: 1;
  }

  .tool-btn-wrap button {
    margin-right: 5px;
  }
</style>
