<template>
  <div class="info-box">
    <div class="top-box">
      <div>
        <a-button @click="emit('returnPage')">返回</a-button>
        <span class="text-base font-bold pl-5">回款记录</span>
      </div>
      <a-button type="primary" @click="handleCreate">新增回款记录</a-button>
    </div>
    <a-divider />
    <a-card :bordered="false">
      <div class="title-name">{{ baseInfo?.name }}</div>
      <div class="title-info">
        <span>计划回款金额：{{ baseInfo?.waitAmount }}</span>
        <span>计划回款日期：{{ baseInfo?.receivedDate }}</span>
        <span>最迟回款日期：{{ baseInfo?.finallyDate }}</span>
        <span>合同标题：{{ baseInfo?.title }}</span>
        <span>合同负责人：{{ baseInfo?.principalNames }}</span>
      </div>
    </a-card>
    <BasicTable @register="registerTable">
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex == 'action'">
          <a-button type="link" class="actionTxt" @click="handleEdit(record)">编辑</a-button>
          <a-button
            type="link"
            class="actionTxt"
            @click="handleView(record)"
            :disabled="!record.filePath"
          >
            附件查看
          </a-button>
          <a-button type="link" class="actionTxt" @click="handleDelete(record)">删除</a-button>
        </template>
      </template>
    </BasicTable>
  </div>
  <CollectionDetailModal @register="registerModal" @success="reloadTable()" />
  <AttachmentModal @register="registerAttachmentModal" />
</template>
<script lang="ts" setup>
  import { onMounted, ref, createVNode } from 'vue';
  import { BasicTable, useTable, BasicColumn } from '/@/components/Table';
  import { getDetailInfo, deleteCollectionDetail } from '/@/api/erp/customer/collection';
  import { useModal } from '/@/components/Modal';
  import CollectionDetailModal from './CollectionDetailModal.vue';
  import AttachmentModal from './AttachmentModal.vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { Modal } from 'ant-design-vue';
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue';

  const columns: BasicColumn[] = [
    {
      dataIndex: 'amountCollect',
      title: '回款金额',
      align: 'center',
    },

    {
      dataIndex: 'remark',
      title: '备注',
      align: 'center',
    },
    {
      dataIndex: 'date',
      title: '回款时间',
      align: 'center',
    },
    {
      dataIndex: 'createDate',
      title: '创建时间',
      align: 'center',
    },
    {
      dataIndex: 'createUserName',
      title: '创建人',
      align: 'center',
    },
  ];

  const { t } = useI18n();

  const props = defineProps({
    id: String,
  });
  const emit = defineEmits(['returnPage']);
  const dataSource = ref<any>([]);
  const baseInfo = ref();
  const { notification } = useMessage();

  const [registerModal, { openModal }] = useModal();
  const [registerAttachmentModal, { openModal: openAttachmentModal }] = useModal();

  onMounted(() => {
    getInfo();
  });

  const [registerTable, { reload }] = useTable({
    dataSource,
    rowKey: 'id',
    columns,
    bordered: true,
    pagination: false,
    actionColumn: {
      width: 150,
      title: t('操作'),
      dataIndex: 'action',
      slots: { customRender: 'action' },
    },
  });

  const getInfo = async () => {
    const res = await getDetailInfo(props.id!);
    baseInfo.value = res;
    dataSource.value = res?.caseErpCustomerGatherDetailVoList;
  };

  const reloadTable = () => {
    getInfo();
    reload();
  };

  const handleCreate = () => {
    openModal(true, {
      gatherId: props.id,
    });
  };

  const handleEdit = (record) => {
    openModal(true, {
      gatherId: props.id,
      id: record.id,
      isUpdate: true,
    });
  };

  const handleView = (record) => {
    openAttachmentModal(true, {
      filePath: record.filePath,
    });
  };

  const handleDelete = (record) => {
    Modal.confirm({
      title: t('提示信息'),
      icon: createVNode(ExclamationCircleOutlined),
      content: t('是否确认删除？'),
      okText: t('确认'),
      cancelText: t('取消'),
      async onOk() {
        await deleteCollectionDetail(record.id);
        notification.success({
          message: t('提示'),
          description: t('删除成功'),
        });
        reloadTable();
      },
      onCancel() {},
    });
  };
</script>
<style lang="less" scoped>
  .info-box {
    background-color: #fff;
    width: 100%;
    height: 100%;
    padding: 16px;
    margin-right: 8px;

    .top-box {
      display: flex;
      justify-content: space-between;
    }

    .title-name {
      font-size: 18px;
      color: #444;
      font-weight: 700;
      margin-bottom: 25px;
    }

    .title-info span {
      margin-right: 40px;
    }

    .actionTxt {
      padding: 4px 2px;

      &:last-child {
        color: #f56c6c;
      }
    }
  }

  :deep(.ant-divider) {
    height: 1px;
    background-color: #dcdfe6;
  }

  :deep(.ant-card-body) {
    background-color: #f8f8f8;
    margin-bottom: 20px;
  }

  :deep(.vben-basic-table) {
    height: calc(100% - 200px);
  }
</style>
