import React from 'react';
import { connect } from 'dva';
import { DatePicker, Card } from 'antd';
import { DataTable } from '../../../components';
import { CascaderItem } from '../../components';
import { numberUtils } from '@utils';
import moment from 'moment';

const ItemPrice = ({ dispatch, itemPrice, loading }) => {
  const { list, total, filterParams } = itemPrice;
  const { MonthPicker } = DatePicker;

  const domain = 'itemPrice';
  const domainCN = '物料采购降本分析表';

  const tableProps = {
    rowKey: 'rowKey',
    sortBy: 'itemCode',
    scroll: { x: '200%' },
    domain,
    dispatch,
    loading,
    total,
    showAdd: false,
    enableSelection: false,
    dataSource: list,
    handleDownload: {
      requestUrl: `/srm/api/export/itemprice`,
      requestParam:  window.location.search,
    },
    filter: [{
      title: '选择月份',
      dataIndex: 'priceMonth',
      tag: (<MonthPicker placeholder="请选择月份" style={{ width: '100%' }} allowClear={false} />),
      options: {
        rules: [{
          required: true,
          message: '必填',
        }],
        initialValue: filterParams.priceMonth ? moment(filterParams.priceMonth, 'YYYYMM') : moment(),
      },
    }, {
      title: '公司',
      dataIndex: 'ouCodeName',
      options: {
        initialValue: filterParams.ouCodeName,
      },
    }, {
      title: '供应商',
      dataIndex: 'suppCodeName',
      options: {
        initialValue: filterParams.suppCodeName,
      },
    }, {
      title: '物料',
      dataIndex: 'itemCodeName',
      options: {
        initialValue: filterParams.itemCodeName,
      },
    }, {
      title: '物料类别',
      dataIndex: 'cccCode',
      tag: <CascaderItem placeholder="选择大类/中类/小类" changeOnSelect={true} />,
      options: {
        initialValue: filterParams.cccCode,
      },
    }],
    columns: [{
      title: '公司名称',
      dataIndex: 'ouName',
      width: '8%',
    }, {
      title: '物料名称',
      dataIndex: 'itemName',
      width: '8%',
    }, {
      title: '物料编码',
      dataIndex: 'itemCode',
      width: '5%',
    }, {
      title: '规格型号',
      dataIndex: 'spec',
      width: '5%',
    }, {
      title: '供应商编码',
      dataIndex: 'suppCode',
      width: '5%',
    }, {
      title: '供应商名称',
      dataIndex: 'suppName',
      width: '8%',
    }, {
      title: '大类',
      dataIndex: 'c1Name',
      width: '4%',
    }, {
      title: '中类',
      dataIndex: 'c2Name',
      width: '5%',
    }, {
      title: '小类',
      dataIndex: 'c3Name',
      width: '5%',
    }, {
      title: '主单位',
      dataIndex: 'uom',
      width: '3%',
    }, {
      title: '降本未税价格',
      dataIndex: 'guidePrice',
      width: '5%',
      render: (value) => numberUtils.round_half_up_format(value, 4),
    }, {
      title: '当月入库数量',
      dataIndex: 'qty',
      width: '4%',
    }, {
      title: '未税价格',
      dataIndex: 'netPrice',
      width: '5%',
      render: (value) => numberUtils.round_half_up_format(value, 4),
    }, {
      title: '税率',
      dataIndex: 'taxRate',
      width: '3%',
    }, {
      title: '含税价格',
      dataIndex: 'taxPrice',
      width: '5%',
      render: (value) => numberUtils.round_half_up_format(value, 4),
    }, {
      title: '现在未税价格-降本未税差异',
      dataIndex: 'diffPrice',
      width: '5%',
      render: (value) => numberUtils.round_half_up_format(value, 4),
    }, {
      title: '现在未税价格-降本未税差异百分率',
      dataIndex: 'diffPercent',
      width: '5%',
    }, {
      title: '降本未税差异金额',
      dataIndex: 'diffPriceQty',
      width: '5%',
      render: (value) => numberUtils.round_half_up_format(value, 4),
    }, {
      title: '现在未税金额-降本未税差异金额百分率',
      dataIndex: 'diffPercentQty',
      width: '6%',
    }
    ],
  };

  return (
    <Card>
      <DataTable {...tableProps} />
    </Card>
  );
};

export default connect(({ itemPrice, loading }) => ({ itemPrice, loading }))(ItemPrice);
