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 PriceAnalyse = ({ dispatch, priceAnalyse, loading }) => {
  const { list, total, filterParams } = priceAnalyse;
  const { MonthPicker } = DatePicker;

  const domain = 'priceAnalyse';
  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/priceanalyse`,
      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: '6%',
    }, {
      title: '供应商编码',
      dataIndex: 'suppCode',
      width: '4%',
    }, {
      title: '供应商名称',
      dataIndex: 'suppName',
      width: '6%',
    }, {
      title: '中类描述',
      dataIndex: 'c2Name',
      width: '4%',
    }, {
      title: '物料编码',
      dataIndex: 'itemCode',
      width: '4%',
    }, {
      title: '物料名称',
      dataIndex: 'itemName',
      width: '8%',
    }, {
      title: '物料品牌',
      dataIndex: 'brand',
      width: '4%',
    }, {
      title: '规格型号',
      dataIndex: 'spec',
      width: '4%',
    }, {
      title: '主单位',
      dataIndex: 'uom',
      width: '3%',
    }, {
      title: '含税单价',
      dataIndex: 'taxPrice',
      width: '4%',
      render: (value) => numberUtils.round_half_up_format(value, 4),
    }, {
      title: '未税单价',
      dataIndex: 'netPrice',
      width: '4%',
      render: (value) => numberUtils.round_half_up_format(value, 4),
    }, {
      title: '税率',
      dataIndex: 'taxRate',
      width: '3%',
    }, {
      title: '开始日期',
      dataIndex: 'validFromDate',
      width: '4%',
    }, {
      title: '结束日期',
      dataIndex: 'validToDate',
      width: '4%',
    }, {
      title: '上月入库数量',
      dataIndex: 'preMonthQty',
      width: '4%',
    }, {
      title: '上月入库金额',
      dataIndex: 'preMonthAmt',
      width: '4%',
      render: (value) => numberUtils.round_half_up_format(value, 4),
    }, {
      title: '上月含税均价',
      dataIndex: 'preMonthAvgAmt',
      width: '4%',
      render: (value) => numberUtils.round_half_up_format(value, 4),
    }, {
      title: '去年同期入库数量',
      dataIndex: 'preYearQty',
      width: '4%',
    }, {
      title: '去年同期入库金额',
      dataIndex: 'preYearAmt',
      width: '4%',
      render: (value) => numberUtils.round_half_up_format(value, 4),
    }, {
      title: '去年同期含税均价',
      dataIndex: 'preYearAvgAmt',
      width: '4%',
      render: (value) => numberUtils.round_half_up_format(value, 4),
    }, {
      title: '含税单价环比',
      dataIndex: 'chainRate',
      width: '4%',
    }, {
      title: '含税单价同比',
      dataIndex: 'yearRate',
      width: '4%',
    }, {
      title: '使用品牌',
      dataIndex: 'innerBrandDesc',
      width: '6%',
    }
    ],
  };

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

export default connect(({ priceAnalyse, loading }) => ({ priceAnalyse, loading }))(PriceAnalyse);
