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

const dateFormat = 'YYYY-MM-DD HH:mm:ss';
const { RangePicker } = DatePicker;
const { bidStatus } = enums;
const Option = Select.Option;

const TenderForm = ({ dispatch, tenderForm, loading }) => {
  const {
    list,
    total,
    suppFlag,
    addBtnFlag,
    editBtnFlag,
    viewBtnFlag,
    deleteBtnFlag,
    approvalBtnFlag,
    bidViewBtnFlag,
    bidTeamBtnFlag,
    bidResultBtnFlag,
    groupModalVisible,
    groupData,
    groupList
  } = tenderForm;

  const domain = 'tenderForm';
  const domainCN = '招标单列表';

  const deadlineTimeFrom = stringUtils.queryURL('deadlineTimeFrom');
  const deadlineTimeTo = stringUtils.queryURL('deadlineTimeTo');
  const bidOpenTimeFrom = stringUtils.queryURL('bidOpenTimeFrom');
  const bidOpenTimeTo = stringUtils.queryURL('bidOpenTimeTo');
  const bidFromDateFrom = stringUtils.queryURL('bidFromDateFrom');
  const bidFromDateTo = stringUtils.queryURL('bidFromDateTo');
  const bidToDateFrom = stringUtils.queryURL('bidToDateFrom');
  const bidToDateTo = stringUtils.queryURL('bidToDateTo');

  const tableProps = {
    rowKey: 'id',
    limit: stringUtils.queryURL('limit'),
    offset: stringUtils.queryURL('offset'),
    sortBy: stringUtils.queryURL('sortBy') || 'id',
    sortDirection: stringUtils.queryURL('sortDirection') || 'DESC',
    scroll: {
      x: 2000,
      y: 500
    },
    showAdd: addBtnFlag ? !suppFlag : false,
    showEdit: editBtnFlag ? !suppFlag : false,
    showDelete: false,
    showView: viewBtnFlag,
    domain,
    dispatch,
    loading,
    total,
    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: 'personInCharge',
        options: {
          initialValue: stringUtils.queryURL('personInCharge')
        }
      },
      {
        title: '招标单状态',
        dataIndex: 'docStatus',
        options: {
          initialValue: stringUtils.queryURL('docStatus')
        },
        tag: <UdcSelect domain="SRM" code="BID_INVITING_STATUS" />
      },
      {
        title: '供货区域',
        dataIndex: 'suppCenterId',
        options: {
          initialValue: stringUtils.queryURL('suppCenterId')
        },
        tag: <SuggestSelect apiName="regions"/>,
      },
      {
        title: '价格生效日',
        dataIndex: 'bidFromDate',
        options: {
          initialValue: bidFromDateFrom ? [moment(bidFromDateFrom, dateFormat), moment(bidFromDateTo, dateFormat)] : []
        },
        tag: <RangePicker format="YYYY-MM-DD" style={{ width: '100%' }} />,
      },
      {
        title: '价格失效日',
          dataIndex: 'bidToDate',
          options: {
          initialValue: bidToDateFrom ? [moment(bidToDateFrom, dateFormat), moment(bidToDateTo, dateFormat)] : []
        },
        tag: <RangePicker format="YYYY-MM-DD" style={{ width: '100%' }} />,
      },
      {
        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%' }}
          />
        )
      },
      {
        title: '是否已投标',
        dataIndex: 'tender',
        hidden: !suppFlag,
        options: {
          initialValue: stringUtils.queryURL('tender'),
        },
        tag: <Select
          defaultActiveFirstOption={false}
          style={{ width: '100%' }}
          filterOption={false}
          allowClear
        >
          <Option key='1' title='是'>是</Option>
          <Option key='0' title='否'>否</Option>
        </Select>,
      },
      {
        title: '任务节点',
        dataIndex: 'activeTaskKey',
        hidden: suppFlag,
        options: {
          initialValue: stringUtils.queryURL('activeTaskKey')
        },
        tag: (
          <SuggestSelect
            apiName="taskDefs"
            params={{ defKeys: ['Bidding'] }}
            optionKey="code"
          />
        )
      },
      {
        title: '流程状态',
        dataIndex: 'bpmStatus',
        hidden: suppFlag,
        options: {
          initialValue: stringUtils.queryURL('bpmStatus') || 'NONE'
        },
        tag: <UdcSelect domain="SRM" code="TRANSACT_STATUS" />
      }
    ],
    columns: [
      {
        title: '招标编号',
        dataIndex: 'docNo',
        className: 'noWrap',
        width: 150,
        sorter: true
      },
      {
        title: '招标名称',
        dataIndex: 'biName',
        className: 'alignLeft',
        width: 150,
        // sorter: true
      },
      {
        title: '招标方式',
        dataIndex: 'biTypeName',
        width: 150,
        // sorter: true
      },
      {
        title: '招标负责人',
        dataIndex: 'personInCharge',
        className: 'alignLeft',
        width: 100,
        // sorter: true
      },
      {
        title: '负责人电话',
        dataIndex: 'tel',
        width: 120
      },
      {
        title: '状态',
        dataIndex: 'docStatusName',
        width: 100
      },
      {
        title: '供货区域',
        dataIndex: 'suppCenterName',
        width: 200
      },
      {
        title: '投标截止时间',
        dataIndex: 'deadline',
        className: 'noWrap',
        width: 200,
        render: (value, row, index) => {
          return <span>{dataUtils.dtm(value)}</span>;
        },
        sorter: true
      },
      {
        title: '开标时间',
        dataIndex: 'bidOpenTime',
        className: 'noWrap',
        width: 200,
        render: (value, row, index) => {
          return <span>{dataUtils.dtm(value)}</span>;
        },
        sorter: true
      },
      {
        title: '价格生效区间',
        dataIndex: 'intervalDate',
        className: 'noWrap',
        width: 200,
        // render: (value, row, index) => {
        //   return <span>{dataUtils.dtm(value)}</span>;
        // },
        // sorter: true
      },
      {
        title: '有效投标数',
        dataIndex: 'bidTimes',
        width: 100,
        // sorter: true
      },
      {
        title: '当前任务',
        dataIndex: 'activeTaskName',
        className: 'alignLeft',
        width: 150
      }
    ],
    buttons: [
      {
        key: 'delete',
        type: 'primary',
        name: '作废',
        requiredSelected: 1,
        hidden: deleteBtnFlag ? suppFlag : true,
        cb: (selectedRowKeys, searchBarForm, selectedRows) => {
          dispatch({
            type: `${domain}/delete`,
            payload: { ids: selectedRowKeys }
          });
        }
      },
      {
        key: 'approval',
        type: 'primary',
        name: '审批',
        hidden: approvalBtnFlag ? suppFlag : true,
        requiredSelected: 1,
        cb: (selectedRowKeys, searchBarForm, selectedRows) => {
          dispatch({
            type: `${domain}/getSingle`,
            payload: { mode: 'approval', id: selectedRowKeys[0] }
          });
        }
      },
      {
        key: 'bidView',
        type: 'primary',
        name: '投标单查看',
        hidden: !bidViewBtnFlag,
        requiredSelected: 1,
        cb: (selectedRowKeys, searchBarForm, selectedRows) => {
          dispatch({
            type: `${domain}/bidView`,
            payload: selectedRows[0]
          });
        }
      },
      {
        key: 'bidTeam',
        type: 'primary',
        name: '评标小组',
        hidden: bidTeamBtnFlag ? suppFlag : true,
        requiredSelected: 1,
        cb: (selectedRowKeys, searchBarForm, selectedRows) => {
          dispatch({
            type: `${domain}/bidGroupView`,
            payload: selectedRows[0]
          });
        }
      },
      {
        key: 'bidResult',
        type: 'primary',
        name: '查看评标结果',
        hidden: bidResultBtnFlag ? suppFlag : true,
        requiredSelected: 1,
        cb: (selectedRowKeys, searchBarForm, selectedRows) => {
          dispatch({
            type: `${domain}/bidResult`,
            payload: selectedRows[0]
          });
        }
      }
    ]
  };

  const modalProps = {
    dispatch,
    loading,
    mode: 'view',
    rowKey: 'itemCode',
    formData: groupData,
    list: groupList,
    visible: groupModalVisible,
    maskClosable: false,
    width: '70%',
    title: '评标小组详情',
    wrapClassName: 'vertical-center-modal'
  };

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

export default connect(({ tenderForm, loading }) => ({ tenderForm, loading }))(
  TenderForm
);
