import React from 'react';
import { Input, InputNumber } from 'antd';
import { dataUtils, numberUtils } from '../../../utils';
import { EditableDataTable, FormEL, UploadEL } from '../../../components';
import styles from './styles.less';

const compareDetailCfg = (record, mode, dispatch, loading, ) => {
    const { TextArea } = Input;
    const domain = 'compareDetail';

    // 使用DataTable为了分页，但是不支持动态表单，所以手动控制
    const editable = mode === 'create' || mode === 'edit';

    const handleChange = (rowKey, key, index, newValue) => {
        dispatch({ type: `${domain}/updateCell`, payload: { rowKey, key, index, value: newValue } });
    };

    const handleUpdateNumberCell = (rowKey, key, index) => (value) => {
      handleChange(rowKey, key, index, value.target.value ? parseFloat(value.target.value.replace(',', '')) : null);
    };

    //基本信息：询价单信息/比价单信息
    const basicInfo = [
        {
            title: '供货区域',
            dataIndex: 'suppArea',
        }, {
            title: '价格生效期间',
            dataIndex: 'priceInterval',
        }, {
            title: '报价截止时间',
            dataIndex: 'quoteDeadline',
        }, {
            title: '议价截止日时间',
            dataIndex: 'bargainDeadline',
        }
    ]

    //整体比价结果列表
    const compareList = [
        {
            title: '供应商名称',
            dataIndex: 'suppName',
            width: 120,
            fixed: 'left',
            className: 'alignLeft',
        }, {
            title: '物料名称',
            dataIndex: 'itemName',
            width: 120,
            fixed: 'left',
            className: 'alignLeft',
        }, {
            title: '含税议价',
            dataIndex: 'bargainWithTax',
            width: 100,
            fixed: 'left',
            className: 'alignRight',
            formatter: numberUtils.round_half_up_format,
            precision: 4,
        }, {
            title: '物料编号',
            dataIndex: 'itemCode',
            width: 100,
            className: 'alignLeft',
        }, {
            title: '物料规格',
            dataIndex: 'itemSpec',
            width: 200,
            className: 'alignLeft',
        }, {
            title: '单位',
            dataIndex: 'itemUomDesc',
            width: 80,
            className: 'alignLeft',
        }, {
            title: '询价单编号',
            dataIndex: 'peDocNo',
            width: 130,
            className: 'noWrap',
        }, {
            title: '当期预估用量',
            dataIndex: 'estimatedNum',
            width: 100,
            className: 'alignRight',
            formatter: numberUtils.round_half_up_format,
            precision: 2,
        }, {
            title: '单次起订量',
            dataIndex: 'minNum',
            width: 100,
            className: 'alignRight',
            formatter: numberUtils.round_half_up_format,
            precision: 2,
        }, {
            title: '含税报价',
            dataIndex: 'quoteWithTax',
            width: 100,
            className: 'alignRight',
            formatter: numberUtils.round_half_up_format,
            precision: 4,
        }, {
            title: '议报价差异',
            dataIndex: 'gapPrice',
            width: 100,
            className: 'alignRight',
            render: (value, row, index) => {
              return (
                <span className={row.gapPrice > 0 ? styles.warningNum : ''}>{value}</span>
              )
            }
        }, {
            title: '未税基准价',
            dataIndex: 'guidePrice',
            width: 100,
            className: 'alignRight',
            formatter: numberUtils.round_half_up_format,
            precision: 4,
        }, {
            title: '未税降本差异金额',
            dataIndex: 'gapAmt',
            width: 100,
            className: 'alignRight',
            formatter: numberUtils.round_half_up_format,
            precision: 2,
        }, {
            title: '税率(%)',
            dataIndex: 'taxRate',
            width: 100,
            className: 'alignRight',
        }, {
            title: '未税单价',
            dataIndex: 'netPrice',
            width: 100,
            className: 'alignRight',
            formatter: numberUtils.round_half_up_format,
            precision: 4,
        }, {
            title: '含税金额',
            dataIndex: 'amt',
            width: 100,
            className: 'alignRight',
            formatter: numberUtils.round_half_up_format,
            precision: 2,
        }, {
            title: '未税金额',
            dataIndex: 'netAmt',
            width: 100,
            className: 'alignRight',
            formatter: numberUtils.round_half_up_format,
            precision: 2,
        }, {
            title: '税额',
            dataIndex: 'taxAmt',
            width: 100,
            className: 'alignRight',
            formatter: numberUtils.round_half_up_format,
            precision: 2,
        }, {
            title: '最高可供应量',
            dataIndex: 'maxProvideNum',
            width: 100,
            className: 'alignRight',
            formatter: numberUtils.round_half_up_format,
            precision: 2,
        }, {
            title: '当期价格',
            dataIndex: 'lastPrice',
            width: 100,
            className: 'alignRight',
            formatter: numberUtils.round_half_up_format,
            precision: 4,
        }, {
            title: '外部参考价',
            dataIndex: 'itemOuterPrice',
            width: 100,
            className: 'alignRight',
            formatter: numberUtils.round_half_up_format,
            precision: 4,
        }, {
            title: '上月绩效',
            dataIndex: 'scoreGrade',
            width: 100,
            className: 'noWrap',
        }, {
            title: '订货或生产周期(天)',
            dataIndex: 'orderPeriod',
            width: 100,
            className: 'alignRight',
        }, {
            title: '物流配送时间(天)',
            dataIndex: 'deliveryPeriod',
            width: 100,
            className: 'alignRight',
        }, {
            title: '备注',
            dataIndex: 'remark',
            width: 100,
            className: 'alignLeft',
        }, {
            title: '报价附件',
            dataIndex: 'quoteAttch',
            className: 'alignLeft noWrap',
            render: (value, row, index) => {
                return (
                    <UploadEL
                        size={'small'}
                        api={`/api/source-mgmt/qo/iqo/sfs/token?clsPath=${row.attachRepo}&dataKey=${row.pqmId}`}
                        dataKey={row.pqmId}
                        listTypes={'text'}
                        disabled={true}
                    />
                )
            },
        }, {
            title: '报价时间',
            dataIndex: 'quoteTime',
            width: 150,
            className: 'noWrap',
            render: (value, row, index) => {
                return (<span>{dataUtils.dtm(value)}</span>);
            },
            // fixed: 'right',
        }, {
            title: '分配比例(%)',
            dataIndex: 'percent',
            width: 120,
            required: true,
            fixed: 'right',
            className: 'alignRight',
            render: (value, row, index) => {
                const numProp = {
                    min: 0,
                    max: 100,
                    precision: 2,
                    step: 1,
                    style: { width: '100%' },
                };
                return (
                  <InputNumber
                    disabled={!editable}
                    className={row.invalidate ? styles.invalidate : ''}
                    {...numProp}
                    defaultValue={value}
                    onBlur={handleUpdateNumberCell(row.pqdDocId, 'percent', index)}
                    // onChange={(input) => {
                    //     handleChange('percent', index, input);
                    // }}
                  />
                );
            },
        }, {
            title: '分配数量',
            dataIndex: 'allottedQuantity',
            width: 80,
            fixed: 'right',
            className: 'noWrap',
            formatter: numberUtils.round_half_up_format,
            precision: 2,
        }, {
            title: '分配金额',
            dataIndex: 'allottedAmt',
            width: 80,
            fixed: 'right',
            className: 'noWrap',
            formatter: numberUtils.round_half_up_format,
            precision: 2,
        }
    ]

    //替代物料参考列表
    const substituteList = [
        {
            title: '标准物编号',
            dataIndex: 'originItemCode',
            width: 100,
            className: 'noWrap',
        }, {
            title: '标准料名称',
            dataIndex: 'originItemName',
            width: 120,
            className: 'alignLeft',
        }, {
            title: '替代料编号',
            dataIndex: 'subItemCode',
            width: 100,
            className: 'noWrap',
        }, {
            title: '替代料名称',
            dataIndex: 'subItemName',
            width: 120,
            className: 'noWrap',
        }, {
            title: '供应商名称',
            dataIndex: 'suppName',
            width: 150,
            className: 'alignLeft',
        }, {
            title: '供应区域',
            dataIndex: 'suppArea',
            width: 100,
            className: 'noWrap',
        }, {
            title: '含税单价',
            dataIndex: 'priceWithTax',
            width: 100,
            className: 'alignRight',
            formatter: numberUtils.round_half_up_format,
            precision: 4,
        }, {
            title: '价格期间',
            dataIndex: 'priceInterval',
            width: 100,
            className: 'alignRight',
        }, {
            title: '来源区分',
            dataIndex: 'srcDocClsDesc',
            width: 100,
            className: 'alignRight',
        }, {
            title: '来源单号',
            dataIndex: 'srcDocNo',
            width: 100,
            className: 'alignRight',
        }
    ]

    return {
        basicInfo,
        compareList,
        substituteList
    };
};

export default compareDetailCfg;
