import { config, dataUtils, stringUtils } from '@utils';
import { DatePicker, Input, Tooltip,message, Checkbox, Modal } 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';
import * as quoteService from '../../../services/source/quote';
const { api } = config;
const { excel } = api;
const { exportExcelURL } = excel;
const QuoteItems = ({ dispatch, quoteItems, loading }) => {
  const { list, total, filterParams, detailBtnFlag, confirmBtnFlag, exportBtnFlag } = quoteItems;
  const { RangePicker } = DatePicker;
  const domain = 'quoteItems';
  const dateFormat = 'YYYY-MM-DD';
  const priceEffectDateFrom = stringUtils.queryURL('priceEffectDateFrom');
  const priceEffectDateTo = stringUtils.queryURL('priceEffectDateTo');
  const priceExpireDateFrom = stringUtils.queryURL('priceExpireDateFrom');
  const priceExpireDateTo = stringUtils.queryURL('priceExpireDateTo');
  const quoteDeadlineFrom = stringUtils.queryURL('quoteDeadlineFrom');
  const quoteDeadlineTo = stringUtils.queryURL('quoteDeadlineTo');
  const bargainDeadlineFrom = stringUtils.queryURL('bargainDeadlineFrom');
  const bargainDeadlineTo = stringUtils.queryURL('bargainDeadlineTo');
  let peDocIds: string[];

  const tableProps = {
    rowKey: 'peDocId',
    limit: stringUtils.queryURL('limit'),
    offset: stringUtils.queryURL('offset'),
    // sortBy: stringUtils.queryURL('sortBy') || 'itemId',
    // sortDirection: stringUtils.queryURL('sortDirection') || 'DESC',
    enableDoubleClick: false,
    scroll: {
      x: '150%'
    },
    domain,
    dispatch,
    loading,
    showAdd: false,
    showDelete: false,
    showEdit: false,
    showView: false,
    total,
    dataSource: list,
    rowSelection: {
      type: 'checkbox'
    },
    filter: [{
      title: '询价单',
      dataIndex: 'peDocCode',
      tag: <Input placeholder="请输入询价单编号/名称"/>,
      options: {
        initialValue: stringUtils.queryURL('peDocCode'),
      },
    }, {
      title: '询价人',
      dataIndex: 'createUser',
      tag: <Input placeholder="请输入询价人编号/名称"/>,
      options: {
        initialValue: stringUtils.queryURL('createUser'),
      },
    }, {
      title: '价格生效日',
      dataIndex: 'priceEffectDate',
      options: {
        initialValue: priceEffectDateFrom ? [moment(priceEffectDateFrom, dateFormat), moment(priceEffectDateTo, dateFormat)] : []
      },
      tag: <RangePicker format="YYYY-MM-DD" style={{ width: '100%' }} />,
    },{
      title: '价格失效日',
      dataIndex: 'priceExpireDate',
      options: {
        initialValue: priceExpireDateFrom ? [moment(priceExpireDateFrom, dateFormat), moment(priceExpireDateTo, dateFormat)] : []
      },
      tag: <RangePicker format="YYYY-MM-DD" style={{ width: '100%' }}/>,
    },{
      title: '报价截止日',
      dataIndex: 'quoteDeadlineDate',
      options: {
        initialValue: quoteDeadlineFrom ? [moment(quoteDeadlineFrom, dateFormat), moment(quoteDeadlineTo, dateFormat)] : []
      },
      tag: <RangePicker format="YYYY-MM-DD" style={{ width: '100%' }}/>,
    },{
      title: '议价截止日',
      dataIndex: 'bargainDeadlineDate',
      options: {
        initialValue: bargainDeadlineFrom ? [moment(bargainDeadlineFrom, dateFormat), moment(bargainDeadlineTo, dateFormat)] : []
      },
      tag: <RangePicker format="YYYY-MM-DD" style={{ width: '100%' }}/>,
    },{
      title: '供货区域',
      dataIndex: 'suppArea',
      tag: <SuggestSelect apiName="regions" mode="multiple"/>,
      options: {
        initialValue: stringUtils.queryURL('suppArea') ? stringUtils.queryURL('suppArea').split('%2C') : []
      },
    }
    //   {
    //   title: '询价月份',
    //   dataIndex: 'monthDate',
    //   tag: <MonthPicker style={{ width: '100%' }}/>,
    //   options: {
    //     initialValue: stringUtils.queryURL('monthDate') ? moment(stringUtils.queryURL('monthDate')) : moment(),
    //   },
    // },{
    //   title: '物料品类',
    //   dataIndex: 'c3Name',
    //   tag: <Input placeholder="请输入品类编号/品类名称"/>,
    //   options: {
    //     initialValue: stringUtils.queryURL('c3Name'),
    //   },
    // },{
    //   title: '报价确认状态',
    //   dataIndex: 'docStatus',
    //   tag: <UdcSelect domain="SRM" code="PQ_D_STATUS"/>,
    //   options: {
    //     initialValue: stringUtils.queryURL('docStatus'),
    //   },
    // }
    ],
    columns: [
      {
        title: '询价单编号',
        dataIndex: 'peDocNo',
        width: '10%',
        className: 'noWrap',
        sorter: true,
      }, {
        title: '供货区域',
        dataIndex: 'suppArea',
        width: '10%',
        align: 'alignCenter',
        sorter: true,
      }, {
        title: '询价单名称',
        dataIndex: 'peDocName',
        width: '10%',
        align: 'alignLeft',
        sorter: true,
      }, {
        title: '供应商编号',
        dataIndex: 'suppCode',
        width: '10%',
        align: 'alignLeft',
        sorter: true,
      }, {
        title: '供应商名称',
        dataIndex: 'suppName',
        width: '10%',
        align: 'alignLeft',
        sorter: true,
      }, {
        title: '报价截止时间',
        dataIndex: 'quoteDeadline',
        width: '10%',
        align: 'alignCenter',
        sorter: true,
      }, {
        title: '议价截止时间',
        dataIndex: 'bargainDeadline',
        width: '10%',
        className: 'alignCenter',
      }, {
        title: '价格生效期间',
        width: '10%',
        className: 'noWrap',
        render: (value, row, index) => {
          return (<span>{dataUtils.ymd(row.priceEffectDate)}--{dataUtils.ymd(row.priceExpireDate)}</span>);
        }
      }, {
        title: '询价周期(月)',
        dataIndex: 'quotePeriod',
        width: '10%',
        className: 'noWrap'
      }, {
        title: '创建人',
        dataIndex: 'createUserName',
        width: '10%',
        className: 'noWrap',
        sorter: true,
      }
    ],
    buttons: [
    //   {
    //   key: 'detail',
    //   type: 'primary',
    //   name: '报价明细',
    //   requiredSelected: 1,
    //   hidden: !detailBtnFlag,
    //   cb: (selectedRowKeys, searchBarForm, selectedRows) => {
    //     // console.log("selectedRows==>",selectedRows);
    //     dispatch({
    //       type: `${domain}/getSingle`,
    //       payload: {
    //         mode: 'view',
    //         itemName: selectedRows[0].itemCode,
    //         monthDate: selectedRows[0].quiryMonth,
    //       },
    //     });
    //   },
    // },
      {
      key: 'confirm',
      type: 'primary',
      name: '比价确认',
      requiredSelected: 2,
      hidden: confirmBtnFlag ? false : true,
      cb: (selectedRowKeys, searchBarForm, selectedRows) => {
        peDocIds = [];
        selectedRows.map(row => {
          peDocIds.push(row.peDocId);
        });
        if (peDocIds.length > 0) {
          quoteService.checkCompList({ docId: peDocIds }).then(data => {
            if (data.success) {
              if (data.data > 0) {
                Modal.confirm({
                  title: '比价提醒',
                  content: '有其它相同供货区域、价格期间、报价截止时间、议价截止时间的询价单没有一起比价，是否继续？',
                  okText: '继续',
                  onOk() {
                    dispatch({
                      type: `${domain}/getSingle`,
                      payload: {
                        mode: 'create',
                        docId: peDocIds
                      },
                    });
                  },
                  cancelText: '取消',
                  onCancel() {
                  },
                });
              } else {
                dispatch({
                  type: `${domain}/getSingle`,
                  payload: {
                    mode: 'create',
                    docId: peDocIds
                  },
                });
              }
            } else {
              message.error(data.data.info);
            }
          });
        } else {
          message.warn("请选择要比价的询价单");
        }
      },
    }],
  };

  const handleChange = (key, index, newValue) => {
    // if(newValue===''){
    //   message.error('请填写关闭原因');
    //   return;
    // }
    dispatch({ type: `${domain}/updateCell`, payload: { key, index, value: newValue } });
  };

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

};

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