<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    :title="getTitle"
    :width="1000"
    wrapClassName="custom-modal-box"
    @ok="handleSubmit"
  >
    <BasicTable @register="registerTable" />
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref, watch } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicTable, useTable, FormSchema, BasicColumn } from '/@/components/Table';
  import { getPurchasePageList } from '/@/api/erp/purchase/order';
  import { getSalePageList } from '/@/api/erp/sale/order';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useI18n } from '/@/hooks/web/useI18n';

  const { notification } = useMessage();
  const { t } = useI18n();
  const emits = defineEmits(['success', 'register']);

  const props = defineProps({
    type: Number,
    id: String,
  });

  const searchFormSchema: FormSchema[] = [
    {
      field: 'keyword',
      label: '',
      component: 'Input',
      colProps: { span: 8 },
      componentProps: {
        placeholder: '请输入要查询的关键字',
      },
    },
  ];

  const saleColumns: BasicColumn[] = [
    {
      title: '销售单号',
      dataIndex: 'saleNumber',
    },
    {
      title: '销售订单名称',
      dataIndex: 'theme',
    },
    {
      title: '销售订单经手人',
      dataIndex: 'createUserName',
    },
    {
      title: '创建时间',
      dataIndex: 'createDate',
    },
  ];

  const purchaseColumns: BasicColumn[] = [
    {
      title: '采购单号',
      dataIndex: 'purchaseNumber',
    },
    {
      title: '采购订单名称',
      dataIndex: 'theme',
    },
    {
      title: '采购订单经手人',
      dataIndex: 'createUserName',
    },
    {
      title: '创建时间',
      dataIndex: 'createDate',
    },
  ];

  const getTitle = ref('');

  const [registerModal, { closeModal, setModalProps }] = useModalInner((data) => {
    setModalProps({ destroyOnClose: true });
    if (data.type === 0) {
      getTitle.value = '销售订单选择';
      setProps({
        api: getSalePageList,
        columns: saleColumns,
      });
    } else {
      getTitle.value = '采购订单选择';
      setProps({
        api: getPurchasePageList,
        columns: purchaseColumns,
      });
    }
  });
  const [registerTable, { getSelectRowKeys, getSelectRows, setProps }] = useTable({
    api: getPurchasePageList,
    rowKey: 'id',
    columns: saleColumns,
    formConfig: {
      rowProps: {
        gutter: 16,
      },
      schemas: searchFormSchema,
    },
    useSearchForm: true,
    striped: false,
    rowSelection: {
      type: 'radio',
    },
  });

  watch(
    () => props.id,
    async (val) => {
      if (val) {
        let res;
        if (props.type === 0) {
          res = await getSalePageList();
        } else {
          res = await getPurchasePageList();
        }
        const info = res?.list?.filter((x) => x.id === props.id);
        if (info.length) {
          emits('success', info[0]);
        }
      }
    },
  );
  const handleSubmit = async () => {
    if (!getSelectRowKeys()?.length) {
      notification.warning({
        message: t('提示'),
        description: '请选择数据！',
      });
      return;
    }
    closeModal();
    emits('success', getSelectRows()[0]);
  };
</script>
