import React from 'react';
import { connect } from 'dva';
import { DatePicker, Input } from 'antd';
import { stringUtils, config } from '@utils';
import { DataTable, UdcSelect } from '@components';
import moment from 'moment';

const { api } = config;
const { excel } = api;
const { exportExcelURL } = excel;
const PayOrder = ({ dispatch, payOrder, loading }) => {
  const { list, total, supplier } = payOrder;
  const { RangePicker } = DatePicker;

  const domain = 'payOrder';
  const domainCN = '付款单';
  const monthFormat = 'YYYY-MM-DD';
  const formatter = (value) => {
    return (
      value ? `${parseFloat(value).toFixed(2)}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',') : ''
    );
  };

  const tableProps = {
    rowKey: 'id',
    limit: stringUtils.queryURL('limit'),
    offset: stringUtils.queryURL('offset'),
    sortBy: stringUtils.queryURL('sortBy') || 'docDate',
    sortDirection: stringUtils.queryURL('sortDirection') || 'DESC',
    scroll: { x: '130%', y: 400},
    domain,
    dispatch,
    loading,
    showAdd: false,
    showDelete: false,
    showEdit: false,
    total,
    dataSource: list,
    handleDownload: {
      requestUrl: `${exportExcelURL}/pyo`,
      requestParam: window.location.search,
    },
    filter: [{
      title: '付款单编号',
      dataIndex: 'docNo',
      options: {
        initialValue: supplier.docNo,
      },
    }, {
      title: '付款单状态',
      dataIndex: 'docStatus',
      tag: <UdcSelect domain="SRM" code="PAY_STATUS" />,
      options: {
        initialValue: supplier.docStatus,
      },
    }, {
      title: '供应商',
      dataIndex: 'suppCode',
      tag: <Input placeholder="请输入名称/编号" disabled={supplier.supplier} />,
      options: {
        initialValue: supplier.supplier ? supplier.suppName.trim() : supplier.suppCode,
      },
    }, {
      title: '付款方',
      dataIndex: 'ouCodeName',
      tag: <Input placeholder="请输入名称/编号" />,
      options: {
        initialValue: supplier.ouCodeName,
      },
    }, {
      title: '制单日期',
      dataIndex: 'docDate',
      tag: (<RangePicker style={{ width: '100%' }} />),
      options: {
        initialValue: supplier.docDateFrom ?
          [moment(supplier.docDateFrom, monthFormat), moment(supplier.docDateTo, monthFormat)] : '',
      },
    }, {
      title: '付款日期',
      dataIndex: 'payDate',
      tag: (<RangePicker style={{ width: '100%' }} />),
      options: {
        initialValue: supplier.payDateFrom ?
          [moment(supplier.payDateFrom, monthFormat), moment(supplier.payDateTo, monthFormat)] : '',
      },
    }],
    columns: [{
      title: '编号',
      dataIndex: 'docNo',
      width: '10%',
    }, {
      title: '状态',
      dataIndex: 'docStatusName',
      width: '8%',
    }, {
      title: '付款方',
      dataIndex: 'ouName',
      className: 'alignLeft',
      width: '20%',
    }, {
      title: '供应商名称',
      dataIndex: 'suppName',
      className: 'alignLeft',
      width: '20%',
    }, {
      title: '付款金额',
      dataIndex: 'payAmt',
      className: 'alignRight',
      width: '8%',
      render: formatter,
    }, {
      title: '制单日期',
      dataIndex: 'docDate',
      width: '8%',
    }, {
      title: '付款日期',
      dataIndex: 'payDate',
      width: '8%',
    }, {
      title: '备注',
      dataIndex: 'remark',
      width: '18%',
      className: 'alignLeft',
    }],
  };

  return (
    <div className="content-inner">
      <DataTable {...tableProps} />
    </div>
  );

};

export default connect(({ payOrder, loading }) => ({ payOrder, loading }))(PayOrder);
