<template>
  <div>
    <div class="flex justify-between mb-4">
      <div>
        <a-button type="primary" size="large" class="btn-add" @click="handleCreate">查房</a-button>
      </div>
      <div>
        <XTForm :form-data="formData" @change="callForm" />
      </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 === 'action'">
          <TableAction
            :actions="[
              {
                auth: 'archives:patrolWard:edit',
                icon: 'icon-xt-details_edit_default|iconfont',
                tooltip: '编辑',
                onClick: handleEdit.bind(null, record),
              },
              {
                auth: 'archives:patrolWard: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="callSuccess" />
  </div>
</template>
<script lang="ts" setup>
  import { reactive, ref } from 'vue';
  import { XTForm } from '/@/components/XTForm/index';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';

  // import { useModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import FormModal from './FormModal.vue';
  // import ViewDrawer from './viewDrawer.vue';
  import { columns } from './data';

  import {
    archivesPatrolWardQueryPage,
    archivesPatrolWardRemove,
  } from '/@/api/biz/archives/patrolWardApi';
  import { useModal } from '/@/components/Modal';
  import dayjs from 'dayjs';

  const props = defineProps({
    info: {
      type: Object,
      default: () => {},
    },
  });

  const { createConfirm, createMessage } = useMessage();
  const [registerModal, { openModal }] = useModal();
  const tableSort = ref([
    {
      field: 'create_time',
      direction: 'DESC',
    },
  ]) as any;

  // formdata
  const formData = [
    {
      name: 'patrolTime',
      label: '查房时间',
      componentType: 'RangePicker',
      placeholder: '请输入',
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD',
      defaultValue: [
        dayjs().subtract(3, 'month').format('YYYY-MM-DD'),
        dayjs().format('YYYY-MM-DD'),
      ],
    },
    {
      name: 'content',
      componentType: 'Input',
      placeholder: '请输入',
      prefix: 'icon-xt-search',
      width: 280,
    },
  ];

  const formValue = reactive({
    patrolTime: [
      dayjs().subtract(3, 'month').format('YYYY-MM-DD'),
      dayjs().add(1, 'day').format('YYYY-MM-DD'),
    ],
    content: '',
  });

  const [registerTable, { reload, getSelectRowKeys }] = useTable({
    api: archivesPatrolWardQueryPage,
    rowKey: 'id',
    columns,
    showIndexColumn: false,
    bordered: true,
    striped: false,
    actionColumn: {
      width: 200,
      title: '操作',
      dataIndex: 'action',
    },
    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 archivesPatrolWardRemove([record.id]);
      createMessage.success('删除成功！');
      await reload();
    } else {
      createConfirm({
        content: '你确定要删除?',
        iconType: 'warning',
        onOk: async () => {
          const keys = getSelectRowKeys();
          await archivesPatrolWardRemove(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) {
    console.log({ ...params, orders: tableSort.value, patientBasicId: props.info?.id });
    return {
      ...params,
      orders: tableSort.value,
      patientBasicId: props.info?.id,
      patrolTime: [
        formValue.patrolTime[0],
        dayjs(formValue.patrolTime[1]).add(1, 'day').format('YYYY-MM-DD'),
      ],
      content: formValue.content,
    };
  }

  // 弹窗回调事件
  async function callSuccess() {
    await reload();
  }

  // 查询回调
  async function callForm(data) {
    formValue.content = data.content || '';
    formValue.patrolTime = data.patrolTime || '';
    await reload();
  }
</script>
<style lang="less" scoped>
  ::v-deep(.ant-btn-link) {
    color: rgb(61 65 85 / 100%);
  }

  ::v-deep(.ant-input) {
    border-color: #c2ccd4;
    border-radius: 4px;
  }

  ::v-deep(.ant-input-affix-wrapper) {
    border-color: #c2ccd4;
    border-radius: 4px;
  }

  ::v-deep(.ant-picker) {
    border-color: #c2ccd4;
    border-radius: 4px;
  }

  ::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>
