import React from 'react';
import { connect } from 'dva';
import { dataUtils, enums, stringUtils } from '@utils';
import { DatePicker, Input } from 'antd';
import { DataTable, UdcSelect, SuggestSelect } from '../../../components';
import moment from 'moment';

const { RangePicker } = DatePicker;

const dateFormat = 'YYYY-MM-DD HH:mm:ss';
const ExamForm = ({ dispatch, examForm, loading }) => {
  const {
    list,
    total,
    suppFlag,
    bidOpenBtnFlag,
    approvalBtnFlag,
    suppScoreBtnFlag
  } = examForm;

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

  const domain = 'examForm';
  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: '100%'
      // y: '50%',
    },
    domain,
    dispatch,
    loading,
    total,
    showAdd: false,
    showEdit: false,
    showView: false,
    showDelete: false,
    dataSource: list,
    filter: [
      {
        title: '招标单',
        dataIndex: 'docNoName',
        options: {
          initialValue: stringUtils.queryURL('docNoName')
        },
        tag: <Input placeholder="请输入招标编号/名称" />
      },
      {
        title: '招标方式',
        dataIndex: 'biType',
        options: {
          initialValue: stringUtils.queryURL('biType')
        },
        tag: <UdcSelect domain="SRM" code="BID_MODE" />
      },
      {
        title: '评标人',
        dataIndex: 'bidAppraiser',
        options: {
          initialValue: ''
        }
      },
      {
        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_INVITING_STATUS_2" />
      },
      {
        title: '任务节点',
        dataIndex: 'activeTaskKey',
        options: {
          initialValue: stringUtils.queryURL('activeTaskKey')
        },
        tag: (
          <SuggestSelect
            apiName="taskDefs"
            params={{ defKeys: ['BidResultEvaluation'] }}
            optionKey="code"
          />
        )
      },
      {
        title: '流程状态',
        dataIndex: 'bpmStatus',
        options: {
          initialValue: stringUtils.queryURL('bpmStatus') || 'NONE'
        },
        tag: <UdcSelect domain="SRM" code="TRANSACT_STATUS" />
      }
    ],
    columns: [
      {
        title: '招标编号',
        dataIndex: 'docNo',
        className: 'noWrap',
        width: '10%',
        sorter: true
      },
      {
        title: '招标名称',
        dataIndex: 'biName',
        className: 'alignLeft',
        width: '20%',
        sorter: true
      },
      {
        title: '招标方式',
        dataIndex: 'biTypeName',
        width: '8%'
      },
      {
        title: '开标时间',
        dataIndex: 'bidOpenTime',
        className: 'noWrap',
        width: '15%',
        render: (value, row, index) => {
          return <span>{dataUtils.dtm(value)}</span>;
        },
        sorter: true
      },
      {
        title: '招标单状态',
        dataIndex: 'docStatusName',
        width: '8%'
      },
      {
        title: '投标截止时间',
        dataIndex: 'deadline',
        width: '15%',
        render: (value, row, index) => {
          return <span>{dataUtils.dtm(value)}</span>;
        },
        sorter: true
      },
      {
        title: '有效投标数',
        dataIndex: 'bidTimes',
        width: '6%',
        sorter: true
      },
      {
        title: '当前任务',
        dataIndex: 'activeTaskName',
        className: 'alignLeft',
        width: '10%'
      }
    ],
    buttons: [
      {
        key: 'bidOpen',
        type: 'primary',
        name: '评标',
        requiredSelected: 1,
        hidden: !bidOpenBtnFlag,
        cb: selectedRowKeys => {
          dispatch({
            type: `${domain}/getSingle`,
            payload: {
              mode: 'update',
              id: selectedRowKeys[0]
              // item: dataSource.filter(d => d.buzId === this.state.selectedRowKeys[0])[0]
            }
          });
        }
      },
      {
        key: 'suppScore',
        type: 'primary',
        name: '评标详情',
        requiredSelected: 1,
        hidden: !suppScoreBtnFlag,
        cb: selectedRowKeys => {
          dispatch({
            type: `${domain}/getSingle`,
            payload: {
              mode: 'view',
              id: selectedRowKeys[0]
              // item: dataSource.filter(d => d.buzId === this.state.selectedRowKeys[0])[0]
            }
          });
        }
      },
      {
        key: 'approval',
        type: 'primary',
        name: '审批',
        requiredSelected: 1,
        hidden: !approvalBtnFlag,
        cb: (selectedRowKeys, searchBarForm, selectedRows) => {
          dispatch({
            type: `${domain}/getSingle`,
            payload: {
              mode: 'approval',
              id: selectedRowKeys[0],
              item: selectedRows[0]
            }
          });
        }
      },
      {
        key: 'approveView',
        type: 'primary',
        name: '审批详情',
        requiredSelected: 1,
        hidden: !suppScoreBtnFlag,
        cb: selectedRowKeys => {
          dispatch({
            type: `${domain}/getSingle`,
            payload: {
              mode: 'apprview',
              id: selectedRowKeys[0]
            }
          });
        }
      }
    ]
  };

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

export default connect(({ examForm, loading }) => ({ examForm, loading }))(
  ExamForm
);
