import React from 'react';
import { connect } from 'dva';
import { dataUtils, enums, stringUtils } from '@utils';
import { DatePicker, Input, message, Select, Tooltip } from 'antd';
import { DataTable, UdcSelect } from '../../../components';
const { RangePicker } = DatePicker;
import moment from 'moment';

const dateFormat = 'YYYY-MM-DD HH:mm:ss';
const BidForm = ({ dispatch, bidForm, loading }) => {
  const { list, total, suppFlag, editBtnFlag, viewBtnFlag } = bidForm;

  const deadlineTimeFrom = stringUtils.queryURL('deadlineTimeFrom');
  const deadlineTimeTo = stringUtils.queryURL('deadlineTimeTo');
  const bidOpenTimeFrom = stringUtils.queryURL('bidOpenTimeFrom');
  const bidOpenTimeTo = stringUtils.queryURL('bidOpenTimeTo');

  const domain = 'bidForm';
  let readOnly = false;
  const tableProps = {
    rowKey: 'id',
    limit: stringUtils.queryURL('limit'),
    offset: stringUtils.queryURL('offset'),
    sortBy: stringUtils.queryURL('sortBy') || 'id',
    sortDirection: stringUtils.queryURL('sortDirection') || 'DESC',
    scroll: {
      x: '150%'
    },
    domain,
    dispatch,
    loading,
    showAdd: false,
    showDelete: false,
    showEdit: false,
    showView: viewBtnFlag,
    total,
    dataSource: list,
    filter: [
      {
        title: '招标单',
        dataIndex: 'docNoName',
        options: {
          initialValue: stringUtils.queryURL('docNoName')
        },
        tag: <Input placeholder="请输入招标编号/名称" />
      },
      /*{
     title: '招标名称',
     dataIndex: 'docNoName',
     options: {
     initialValue: stringUtils.queryURL('docNoName'),
     },
     }, */
      {
        title: '招标方式',
        dataIndex: 'biType',
        options: {
          initialValue: stringUtils.queryURL('biType')
        },
        tag: <UdcSelect domain="SRM" code="BID_MODE" />
      },
      {
        title: '供应商',
        dataIndex: 'suppCodeName',
        options: {
          initialValue: stringUtils.queryURL('suppCodeName')
        },
        tag: (
          <Input
            placeholder="请输入供应商编号/名称"
            disabled={suppFlag ? true : false}
          />
        )
      },
      {
        title: '投标截止时间',
        dataIndex: 'deadlineTime',
        options: {
          initialValue: deadlineTimeFrom
            ? [
                moment(deadlineTimeFrom, dateFormat),
                moment(deadlineTimeTo, dateFormat)
              ]
            : []
        },
        tag: (
          <RangePicker
            showTime={{ format: 'HH:mm:ss' }}
            format="YYYY-MM-DD HH:mm:ss"
            placeholder={['开始', '结束']}
            style={{ width: '100%' }}
          />
        )
      },
      {
        title: '开标时间',
        dataIndex: 'bidOpenTime',
        options: {
          initialValue: bidOpenTimeFrom
            ? [
                moment(bidOpenTimeFrom, dateFormat),
                moment(bidOpenTimeTo, dateFormat)
              ]
            : []
        },
        tag: (
          <RangePicker
            showTime={{ format: 'HH:mm:ss' }}
            format="YYYY-MM-DD HH:mm:ss"
            placeholder={['开始', '结束']}
            style={{ width: '100%' }}
            disabled={readOnly}
          />
        )
      },
      {
        title: '投标状态',
        dataIndex: 'docStatus',
        options: {
          initialValue: stringUtils.queryURL('docStatus')
        },
        tag: <UdcSelect domain="SRM" code="BID_INFO_STATUS" />
      }
    ],
    columns: [
      {
        title: '招标编号',
        dataIndex: 'tendersDocNo',
        width: '9%'
      },
      {
        title: '招标名称',
        dataIndex: 'biName',
        width: '10%'
      },
      {
        title: '招标方式',
        dataIndex: 'biTypeName',
        width: '8%'
      },
      {
        title: '投标单编号',
        dataIndex: 'bidDocNo',
        width: '10%'
      },
      {
        title: '投标次数',
        dataIndex: 'bidNo',
        width: '5%'
      },
      {
        title: '招标负责人',
        dataIndex: 'personInCharge',
        width: '7%'
      },
      {
        title: '负责人电话',
        dataIndex: 'tel',
        width: '8%'
      },
      {
        title: '投标截止日期',
        dataIndex: 'deadline',
        className: 'noWrap',
        width: '10%',
        render: (value, row, index) => {
          return <span>{dataUtils.dtm(value)}</span>;
        },
        sorter: true
      },
      {
        title: '供应商名称',
        dataIndex: 'suppName',
        width: '10%'
      },
      {
        title: '开标时间',
        dataIndex: 'bidOpenTime',
        className: 'noWrap',
        width: '10%',
        render: (value, row, index) => {
          return <span>{dataUtils.dtm(value)}</span>;
        },
        sorter: true
      },
      {
        title: '投标状态',
        dataIndex: 'docStatusName',
        width: '5%'
      },
      {
        title: '投标结果',
        dataIndex: 'bidResultName',
        width: '5%'
      }
    ],
    buttons: [
      {
        key: 'update',
        type: 'primary',
        name: '修改',
        requiredSelected: 1,
        hidden: editBtnFlag ? (suppFlag ? false : true) : true,
        cb: selectedRowKeys => {
          dispatch({
            type: `${domain}/getSingle`,
            payload: {
              mode: 'update',
              id: selectedRowKeys[0]
            }
          });
        }
      }
    ]
  };

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

export default connect(({ bidForm, loading }) => ({ bidForm, loading }))(
  BidForm
);
