<template>
  <div>
    <div class="flex">
      <div>
        <a-button type="primary" size="large" class="btn-add" @click="handleCreate">
          添加转归
        </a-button>
      </div>
    </div>
    <BasicTable @register="registerTable">
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'updatorName'">
          <img :src="record.updateAvatar" class="table-avatar" />
          {{ record.updatorName }}
        </template>
        <template v-if="column.key === 'type'">
          {{ formatDictValue(bizDictOptions.type, record.type) }}
        </template>
        <template v-if="column.key === 'returnTime'">
          {{ dayjs(record.returnTime).format('YYYY-MM-DD') }}
        </template>
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                auth: 'archives:patientReturn:edit',
                icon: 'icon-xt-details_edit_default|iconfont',
                tooltip: '编辑',
                onClick: handleEdit.bind(null, record),
              },
              {
                auth: 'archives:patientReturn:remove',
                icon: 'icon-xt-details_delete_default|iconfont',
                tooltip: '删除',
                popConfirm: {
                  title: '是否取消删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record, column),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <FormModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup>
  import { reactive, ref } from 'vue';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';

  import { useMessage } from '/@/hooks/web/useMessage';
  import FormModal from './FormModal.vue';
  import { columns } from './data';

  import {
    archivesPatientReturnQueryPage,
    archivesPatientReturnRemove,
  } from '/@/api/biz/archives/patientReturnApi';
  import { listDictModel } from '/@/api/common';
  import { formatDictValue } from '/@/utils';
  import { useModal } from '/@/components/Modal';
  import { onBeforeMount } from 'vue';
  import dayjs from 'dayjs';

  const props = defineProps({
    info: {
      type: Object,
      default: () => {},
    } as any,
  });
  const bizDictOptions = reactive({
    type: [],
  });
  onBeforeMount(async () => {
    bizDictOptions.type = await listDictModel({ dictCode: 'pb_return' });
  });
  const { createConfirm, createMessage } = useMessage();
  // const [registerModal, { openModal }] = useModal();
  const [registerModal, { openModal }] = useModal();

  const tableSort = ref([
    {
      field: 'create_time',
      direction: 'ASC',
    },
  ]) as any;

  const [registerTable, { reload, getSelectRowKeys }] = useTable({
    api: archivesPatientReturnQueryPage,
    rowKey: 'id',
    columns,
    showIndexColumn: false,
    useSearchForm: false,
    bordered: false,
    striped: false,
    actionColumn: {
      width: 200,
      title: '操作',
      dataIndex: 'action',
    },
    pagination: false,
    beforeFetch: handleBeforeFetch,
    sortFn: handleSortFn,
  });
  // 新增按钮事件
  function handleCreate() {
    openModal(true, {
      isUpdate: false,
      record: { patientBasicId: props.info.id },
    });
  }

  // 编辑按钮事件
  function handleEdit(record: Recordable) {
    openModal(true, {
      record: { patientBasicId: props.info.id, ...record },
      isUpdate: true,
    });
  }

  // 删除按钮事件
  async function handleDelete(record: Recordable) {
    if (record) {
      await archivesPatientReturnRemove([record.id]);
      createMessage.success('删除成功！');
      await reload();
    } else {
      createConfirm({
        content: '你确定要删除?',
        iconType: 'warning',
        onOk: async () => {
          const keys = getSelectRowKeys();
          await archivesPatientReturnRemove(keys);
          createMessage.success('删除成功！');
          await reload();
        },
      });
    }
  }
  // 表格点击字段排序
  function handleSortFn(sortInfo) {
    if (sortInfo?.order && sortInfo?.columnKey) {
      // 默认单列排序
      tableSort.value = [
        {
          field: sortInfo.columnKey,
          direction: sortInfo.order.replace(/(\w+)(end)/g, '$1').toUpperCase(),
        },
      ];
    }
  }

  // 表格请求之前，对参数进行处理, 添加默认 排序
  function handleBeforeFetch(params) {
    return { ...params, orders: tableSort.value, patientBasicId: props.info?.id };
  }

  // 弹窗回调事件
  async function handleSuccess({ isUpdate, values }) {
    console.log(isUpdate);
    console.log(values);
    await reload();
  }
</script>

<style lang="less" scoped>
  ::v-deep(.ant-btn-link) {
    color: rgb(61 65 85 / 100%);
  }

  ::v-deep(.ant-table-tbody > tr > td) {
    border-right: 0 !important;
    border-left: 0 !important;
    border-bottom: 1px solid #f0f0f0;
  }

  ::v-deep(.ant-table-wrapper table) {
    border: 0;
  }

  ::v-deep(.ant-table-cell-fix-right-first::after, .ant-table-cell-fix-right-last::after) {
    content: '';
    top: auto;
    width: 0;
  }

  .btn-add {
    width: 120px;
    border-radius: 4px;
  }
</style>
