import { config, dataUtils, stringUtils } from '@utils';
import { DatePicker, Input, Tooltip, message } from 'antd';
import { connect } from 'dva';
import moment from 'moment';
import React from 'react';
import { DataTable, UdcSelect, SuggestSelect } from '../../../components';
import { consolidateStreamedStyles } from 'styled-components';
const { api } = config;
const { excel } = api;
const { exportExcelURL } = excel;
const compareAudit = ({ dispatch, compareAudit, loading }) => {
  const { list, total, filterParams, detailBtnFlag, confirmBtnFlag, exportBtnFlag, deleteBtnFlag, batchApproveBtnFlag } = compareAudit;
  const { RangePicker } = DatePicker;
  const { MonthPicker } = DatePicker;
  const domain = 'compareAudit';
  const dateFormat = 'YYYY-MM-DD';

  const tableProps = {
    rowKey: 'pcDocId',
    limit: stringUtils.queryURL('limit'),
    offset: stringUtils.queryURL('offset'),
    sortBy: stringUtils.queryURL('sortBy') || 'pcDocId',
    sortDirection: stringUtils.queryURL('sortDirection') || 'DESC',
    enableDoubleClick: false,
    scroll: {
      x: '140%'
    },
    domain,
    dispatch,
    loading,
    showAdd: false,
    showDelete: false,
    showEdit: false,
    showView: false,
    total,
    dataSource: list,
    rowSelection: {
      type: 'checkbox'
    },
    filter: [{
      title: '比价单编号',
      dataIndex: 'docNo',
      tag: <Input placeholder="请输入比价单编号" />,
      options: {
        initialValue: stringUtils.queryURL('docNo'),
      },
    }, {
      title: '询价单',
      dataIndex: 'peDocCode',
      tag: <Input placeholder="请输入询价单编号/名称"/>,
      options: {
        initialValue: stringUtils.queryURL('peDocCode'),
      },
    }, {
      title: '供货区域',
      dataIndex: 'suppCenterId',
      options: {
        rules: [
          {
            required: false,
            message: '必填',
          },
        ],
      },
      tag: <SuggestSelect apiName="regions"/>,
    }, {
      title: '比价人',
      dataIndex: 'user',
      tag: <Input placeholder="请输入比价人员工编号/姓名" />,
      options: {
        initialValue: stringUtils.queryURL('user'),
      },
    }, {
      title: '比价时间',
      dataIndex: 'priceEffectDate',
      tag: <MonthPicker style={{ width: '100%' }} />,
      options: {
        initialValue: stringUtils.queryURL('monthDate') ? moment(stringUtils.queryURL('monthDate')) : null,
      },
    }, {
      title: '比价单状态',
      dataIndex: 'docStatus',
      tag: <UdcSelect domain="SRM" code="PC_STATUS"/>,
      options: {
        initialValue: stringUtils.queryURL('docStatus'),
      },
    }, {
      title: '任务节点',
      dataIndex: 'activeTaskKey',
      options: {
        initialValue: stringUtils.queryURL('activeTaskKey'),
      },
      tag: <SuggestSelect apiName="taskDefs" params={{ defKeys: ['PriceCompare'] }} optionKey="code" />,
    }, {
      title: '流程状态',
      dataIndex: 'bpmStatus',
      options: {
        initialValue: stringUtils.queryURL('bpmStatus') || 'NONE',
      },
      tag: <UdcSelect domain="SRM" code="TRANSACT_STATUS" />,
    }
    ],
    columns: [
      {
        title: '比价单编号',
        dataIndex: 'docNo',
        width: '8%',
        className: 'noWrap',
        sorter: true,
      }, {
        title: '供货区域',
        dataIndex: 'suppArea',
        width: '7%',
        align: 'alignCenter',
        sorter: true,
      }, {
        title: '价格生效期间',
        width: '10%',
        className: 'noWrap',
        render: (value, row, index) => {
          return (<span>{dataUtils.ymd(row.priceEffectDate)}--{dataUtils.ymd(row.priceExpireDate)}</span>);
        }
      }, {
        title: '报价截止时间',
        dataIndex: 'quoteDeadline',
        width: '10%',
        align: 'alignCenter',
        sorter: true,
      }, {
        title: '议价截止时间',
        dataIndex: 'bargainDeadline',
        width: '10%',
        className: 'alignCenter',
      }, {
        title: '比价人',
        dataIndex: 'createUserName',
        width: '5%',
        className: 'noWrap',
        sorter: true,
      }, {
        title: '比价时间',
        dataIndex: 'createTime',
        width: '10%',
        align: 'alignCenter',
        sorter: true,
      }, {
        title: '状态',
        dataIndex: 'docStatusName',
        width: '5%',
        align: 'alignCenter',
      }, {
        title: '当前任务',
        dataIndex: 'activeTaskName',
        width: '10%',
        align: 'alignCenter',
      }
    ],
    buttons: [
      {
        key: 'view',
        type: 'primary',
        name: '查看',
        requiredSelected: 1,
        cb: (selectedRowKeys, searchBarForm, selectedRows) => {
          dispatch({
            type: `${domain}/getSingle`,
            payload: {
              docId: selectedRowKeys[0],
              mode: 'view'
            },
          });
        },
      }, {
        key: 'edit',
        type: 'primary',
        name: '修改',
        requiredSelected: 1,
        cb: (selectedRowKeys, searchBarForm, selectedRows) => {
          dispatch({
            type: `${domain}/getSingle`,
            payload: {
              row: selectedRows[0],
              mode: 'edit'
            },
          });
        },
      }, {
        key: 'confirm',
        type: 'primary',
        name: '审批',
        requiredSelected: 1,
        cb: (selectedRowKeys, searchBarForm, selectedRows) => {
          dispatch({
            type: `${domain}/getSingle`,
            payload: {
              docId: selectedRowKeys[0],
              prcId: selectedRows[0].prcId,
              docStatus: selectedRows[0].docStatus,
              mode: 'approval'
            },
          });
        },
      }, {
        key: 'batchApprove',
        type: 'primary',
        name: '批量通过',
        hidden: !batchApproveBtnFlag,
        requiredSelected: 2,
        cb: (selectedRowKeys, searchBarForm, selectedRows) => {
          dispatch({
            type: `${domain}/batchApprove`,
            payload: {
              pcIds: selectedRowKeys
            },
          });
        },
      }, {
        key: 'delete',
        type: 'primary',
        name: '删除',
        hidden: !deleteBtnFlag,
        requiredSelected: 1,
        cb: (selectedRowKeys, searchBarForm, selectedRows) => {
          dispatch({
            type: `${domain}/delete`,
            payload: {
              pcId: selectedRowKeys[0],
              prcId: selectedRows[0].prcId
            },
          });
        },
      }
      ],
  };

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

};

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