<template>
  <div>
    <BasicDrawer
      v-bind="$attrs"
      destroyOnClose
      @register="registerDrawer"
      :title="getTitle"
      :width="width"
    >
      <Description @register="registerDesc" :data="descData" />
      <div>
        <a-button type="primary" size="large" class="btn-add" @click="handleCreate">新增</a-button>
      </div>

      <BasicTable @register="registerTable">
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'action'">
            <TableAction
              :actions="[
                {
                  auth: 'archives:patrolWard:edit',
                  icon: 'icon-edit|iconfont',
                  // label: '编辑',
                  onClick: handleEdit.bind(null, record),
                },
                {
                  auth: 'visit:handoverShifts:edit',
                  icon: 'icon-xt-details_delete_default|iconfont',
                  tooltip: '删除',
                  popConfirm: {
                    title: '是否确认删除',
                    placement: 'left',
                    confirm: handleDelete.bind(null, record),
                  },
                },
              ]"
            />
          </template>
        </template>
      </BasicTable>
      <FormModal @register="registerModal" @success="handleSuccess" />
    </BasicDrawer>
  </div>
</template>
<script lang="ts" setup>
  import { onBeforeMount, ref, nextTick } from 'vue'; // onBeforeMount,
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { Description, useDescription } from '/@/components/Description';
  import { viewSchema, columns } from './data';

  import { checkDetail, checkRemove, checkQueryPage } from '/@/api/biz/visit/checkApi';
  import { listDictModel } from '/@/api/common';
  import { useMessage } from '/@/hooks/web/useMessage';
  import FormModal from './formModal.vue';
  import { BasicTable, useTable, TableAction } from '/@/components/TableCard';
  import { formatDictValue } from '/@/utils';
  import { useModal } from '/@/components/Modal';

  const descData = ref({});
  const lists = ref([]);
  const getTitle = '查看查房记录';

  const width = '45%';
  const [registerModal, { openModal }] = useModal();
  const { createMessage } = useMessage();
  // const siftData = ref([]);
  // const activeKey = ref();
  const patientBasicId = ref();

  const typeOptions = ref();
  onBeforeMount(async () => {
    typeOptions.value = await listDictModel({ dictCode: 'sys_login_log_type' });
  });
  // 新增按钮事件

  function handleCreate(data) {
    openModal(true, {
      isUpdate: false,
      record: data,
    });
  }
  const [registerDrawer] = useDrawerInner(async data => {
    console.log('::::::::::', data.record);
    const resData = await checkDetail(data.record.id);
    patientBasicId.value = resData.patientBasicId;
    descData.value = {
      ...resData,
      type: formatDictValue(typeOptions.value, resData.type),
    };
    const params = {
      pageNum: 1,
      pageSize: 999,
      patientBasicId: resData.patientBasicId,
    };
    const list = await checkQueryPage(params);
    console.log(list);
    if (list && list.data) {
      lists.value = list.data;
    }
    await nextTick();
    await setTableData(lists.value);
  });
  const [registerTable, { reload, setTableData }] = useTable({
    dataSource: lists.value,
    rowKey: 'id',
    columns,
    showIndexColumn: false,
    bordered: true,
    actionColumn: {
      width: 200,
      title: '操作',
      dataIndex: 'action',
    },
    afterFetch: handleAfterFetch,
  });

  const [registerDesc] = useDescription({
    schema: viewSchema,
    column: 1,
    size: 'middle',
    labelStyle: {
      width: '120px',
    },
  });

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

  // 删除按钮事件
  // async function handleDelete(record: Recordable) {
  //   if (record) {
  //     await archivesPatrolWardRemove([record.id]);
  //     createMessage.success('删除成功！');
  //     clearSelectedRowKeys();
  //     await reload();
  //   } else {
  //     createConfirm({
  //       content: '你确定要删除?',
  //       iconType: 'warning',
  //       onOk: async () => {
  //         const keys = getSelectRowKeys();
  //         await archivesPatrolWardRemove(keys);
  //         createMessage.success('删除成功！');
  //         await reload();
  //       },
  //     });
  //   }
  // }
  function handleAfterFetch(data) {
    // data.forEach(item => {
    //   item.status = 2;
    // });
    return data;
  }
  // 删除按钮事件
  async function handleDelete(record: Recordable) {
    console.log('🚀 ~ file: index.vue:141 ~ handleDelete ~ record', record);
    await checkRemove([record.id]);
    createMessage.success('删除成功！');
    await reload();
  }

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

<style lang="less" scoped>
  .btn-add {
    margin: 2%;
    float: right;
    width: 100px;
    border-radius: 4px;
  }
</style>
