import React, { useRef, useState } from 'react';
import '@/assets/xsy_style.less';
import { Button, Card, message, Spin } from 'antd';
import ProTable, { ActionType, ProColumns } from '@ant-design/pro-table';
import { getPage, getProjectById } from './service';
import { getURLInformation } from '@/utils/CommonUtils';

const Sing: React.FC<{}> = () => {
  //表格控制
  const actionRef = useRef<ActionType>();
  //reviewTurnId获取
  const reviewTurnId = getURLInformation('turnId') == undefined ? '' : getURLInformation('turnId');
  //loading
  const [loading, setLoading] = useState<boolean>(false)

  const toViewOffer = async (record: any) => {
    if(record.decryptStatus == "1") {
      message.info("应答文件未解密，无法查看报价详情，请先到【应答文件查看】中进行解密")
    } else {
      setLoading(true)
      await getProjectById(record?.projectId).then((res) => {
        if (res?.code == 200 && res?.success == true) {
          const data = res?.data;
          const url = `${REACT_APP_XUNJIA_REDIRECT}/inquiryoffer/ztbViewOffer.do?inquiry_no=${data?.ebpProjectNumber}&offer_no=${record?.offerOrderNo}`
          window.open(url);
        }
      }).finally(() => {
        setLoading(false)
      });
    }
  };
  //columns列
  const columns: ProColumns<any>[] = [
    {
      title: '序号',
      width: 100,
      render: (value: any, record: any, index: any) => index + 1,
    },
    {
      title: '供应商名称',
      dataIndex: 'companyName',
      key: 'companyName',
    },
    {
      title: '报价总金额净价（元）',
      dataIndex: 'offerTotalPrice',
      key: 'offerTotalPrice',
      render: (value: any) => (value == null || value == -999 ? '-' : value),
    },
    {
      title: '报价总金额含税价（元）',
      dataIndex: 'offerTotalTaxPrice',
      key: 'offerTotalTaxPrice',
      render: (value: any) => (value == null || value == -999 ? '-' : value),
    },
    {
      title: '操作',
      width: 200,
      render: (value: any, record: any, index: any) => {
        return (
          <Button type="text" onClick={() => toViewOffer(record)}>
            查看报价详情
          </Button>
        );
      },
    },
  ];
  return (
    <Card bordered={false} bodyStyle={{ padding: '24px 64px 24px 0px' }}>
      <Spin spinning={loading} delay={300}>
      <ProTable
        columns={columns}
        actionRef={actionRef}
        search={false}
        options={false}
        size="small"
        pagination={false}
        request={async () =>
          await getPage(reviewTurnId).then((res) => {
            // 表单搜索项会从 params 传入，传递给后端接口。
            return {
              data: res?.data,
              success: res?.success,
            };
          })
        }
        rowKey="id"
        dateFormatter="string"
      />
      </Spin>
    </Card>
  );
};
export default Sing;
