<template>
  <div class="m-4">
    <div class="mb-2">
      <XTTitle title="排床备忘录" :right-data="titleData" @click="callTitleClick" />
    </div>
    <BasicTable @register="registerTable">
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'createTime'">
          {{ record.createTime ? dayjs(record.createTime).format('YYYY-MM-DD') : '' }}
        </template>
        <template v-if="column.key === 'appointmentDate'">
          {{ record.appointmentDate ? dayjs(record.appointmentDate).format('YYYY-MM-DD') : '' }}
        </template>
        <template v-if="column.key === 'appointmentSailingsName'">
          <span v-for="(item, index) in record.appointmentSailingsName" :key="item">
            {{ item }}
            {{ index == record.appointmentSailingsName.length - 1 ? '' : '、' }}
          </span>
        </template>
        <template v-if="column.key === 'patientInfo'">
          {{ record.patientName }}/
          {{ formatDictValue(bizDictOptions.gender, record.patientGender) }}/
          {{ record.age }}
        </template>
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                auth: 'bed:scheduledMemo:remove',
                icon: 'icon-xt-details_delete_default|iconfont',
                tooltip: '删除',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <FormModal @register="registerModal" @success="callSuccess" />
  </div>
</template>
<script lang="ts" setup>
  import { onMounted, reactive, ref } from 'vue';

  import { BasicTable, useTable, TableAction } from '/@/components/TableCard';
  import { XTTitle } from '/@/components/XTTitle/index';
  // import { useModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import FormModal from './FormModal.vue';
  import { columns } from './data';

  import {
    bedScheduledMemoQueryPage,
    bedScheduledMemoRemove,
  } from '/@/api/biz/bed/scheduledMemoApi';
  import { listDictModelBatch } from '/@/api/common';
  import { formatDictValue } from '/@/utils';
  import { useModal } from '@/components/Modal';
  import dayjs from 'dayjs';

  const { createConfirm, createMessage } = useMessage();
  const [registerModal, { openModal }] = useModal();

  const tableSort = ref([
    {
      field: 'create_time',
      direction: 'DESC',
    },
  ]) as any;
  const bizDictOptions = reactive<any>({});
  const bizDictData = ref([{ key: 'gender', dictCode: 'pb_sex' }]);
  onMounted(async () => {
    const res = await listDictModelBatch(bizDictData.value.map(ele => ele.dictCode));
    for (const i in res) {
      const filter = bizDictData.value.filter(ele => ele.dictCode == i)[0];
      bizDictOptions[filter.key] = res[i];
    }
  });
  const [registerTable, { reload, getSelectRowKeys }] = useTable({
    api: bedScheduledMemoQueryPage,
    rowKey: 'id',
    columns,
    useSearchForm: false,
    showIndexColumn: false,
    actionColumn: {
      width: 80,
      title: '操作',
      dataIndex: 'action',
    },
    beforeFetch: handleBeforeFetch,
  });

  // 标题数据
  const titleData = [
    {
      type: 'add',
      btnIcon: 'icon-xt-add_default',
      btnText: '新增备忘录',
    },
  ];

  // 新增按钮事件
  function callTitleClick(data) {
    if (data.type == 'add') {
      openModal(true, {
        isUpdate: false,
        record: data,
      });
    }
  }

  // 删除按钮事件
  async function handleDelete(record: Recordable) {
    if (record) {
      await bedScheduledMemoRemove([record.id]);
      createMessage.success('删除成功！');
      await reload();
    } else {
      createConfirm({
        content: '你确定要删除?',
        iconType: 'warning',
        onOk: async () => {
          const keys = getSelectRowKeys();
          await bedScheduledMemoRemove(keys);
          createMessage.success('删除成功！');
          await reload();
        },
      });
    }
  }

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

  // 弹窗回调事件
  async function callSuccess({ 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%);
  }
</style>
