import React from 'react';
import { connect } from 'dva';
import { Input, DatePicker, Modal } from 'antd';
import { config, stringUtils } from '@utils';
import { DataTable, UdcSelect, SuggestSelect } from '@components';
import { ExcelImport } from '@bizComponents';
import moment from 'moment';

const { api } = config;
const { excel } = api;
const { exportExcelURL } = excel;
const { MonthPicker } = DatePicker;
const { confirm } = Modal;
const Performances = ({ dispatch, performances, loading }) => {
  const { list, total, supplier, showUploadModal, showMenu } = performances;

  const domain = 'performances';
  const domainCN = '供应商绩效管理';
  const monthFormat = 'YYYY-MM';

  const tableProps = {
    rowKey: 'id',
    limit: stringUtils.queryURL('limit'),
    offset: stringUtils.queryURL('offset'),
    sortBy: stringUtils.queryURL('sortBy') || 'yearMonth',
    sortDirection: stringUtils.queryURL('sortDirection') || 'DESC',
    scroll: { x: '100%', y: 400 },
    domain,
    dispatch,
    loading,
    total,
    showAdd: false,
    showDelete: false,
    showEdit: false,
    showUpload: !supplier.supplier,
    handleDownload: {
      requestUrl: `${exportExcelURL}/performance`,
      requestParam: window.location.search,
    },
    buttons: [showMenu[0] && {
      key: 'create',
      type: 'primary',
      name: '修改',
      requiredSelected: 1,
      cb: (selectedKeys) => {
        dispatch({
          type: `${domain}/getSingle`,
          payload: {
            id: selectedKeys,
            mode: 'create',
          },
        });
      },
    }, showMenu[1] && {
      key: 'approve',
      type: 'primary',
      name: '审批',
      requiredSelected: 1,
      cb: (selectedKeys) => {
        dispatch({
          type: `${domain}/getSingle`,
          payload: {
            id: selectedKeys,
            mode: 'approve',
          },
        });
      },
    }, showMenu[2] && {
      key: 'cancel',
      type: 'primary',
      name: '取消绩效评分',
      requiredSelected: 1,
      cb: (selectedKeys) => {
        confirm({
          title: '确定要取消绩效评分吗？',
          okText: '确定',
          okType: 'danger',
          cancelText: '取消',
          onOk() {
            dispatch({
              type: `${domain}/cancel`,
              payload: {
                id: selectedKeys
              },
            });
          }
        });
      },
    }],
    dataSource: list,
    filter: [{
      title: '供应商',
      dataIndex: 'suppCode',
      tag: <Input placeholder="请输入名称/编号" disabled={supplier.supplier} />,
      options: {
        initialValue: supplier.supplier ? supplier.suppName.trim() : supplier.suppCode,
      },
    }, {
      title: '评分状态',
      dataIndex: 'status',
      options: {
        initialValue: supplier.status,
      },
      tag: <UdcSelect domain="SUPP" code="SCORE_STATUS" />,
    }, {
      title: '绩效等级',
      dataIndex: 'scoreGrade',
      options: {
        initialValue: supplier.scoreGrade,
      },
      tag: <UdcSelect domain="SUPP" code="SCORE_LEVEL" />,
    }, {
      title: '绩效月份起',
      dataIndex: 'performanceFromDate',
      tag: (<MonthPicker placeholder="请选择月份" style={{ width: '100%' }} />),
      options: {
        initialValue: supplier.performanceFromDate ? moment(supplier.performanceFromDate) : null,
      },
    }, {
      title: '绩效月份止',
      dataIndex: 'performanceToDate',
      tag: (<MonthPicker placeholder="请选择月份" style={{ width: '100%' }} />),
      options: {
        initialValue: supplier.performanceToDate ? moment(supplier.performanceToDate) : null,
      },
    }, /*{
      title: '当前绩差',
      dataIndex: 'lowQualityStatus',
      tag: <UdcSelect domain="SUPP" code="LOW_QUALITY_STATUS" />,
      options: {
        initialValue: supplier.lowQualityStatus,
      },
    },*/ {
      title: '任务节点',
      dataIndex: 'activeTaskKey',
      options: {
        initialValue: supplier.activeTaskKey,
      },
      tag: <SuggestSelect apiName="taskDefs" params={{ defKeys: ['SuppliperPerformance'] }} optionKey="code" />,
    },{
      title: '评分规则',
      dataIndex: 'perforType',
      options: {
        initialValue: supplier.perforType,
      },
      tag: <UdcSelect domain="SUPP" code="PERFOR_TYPE" />,
    },{
      title: '考核周期',
      dataIndex: 'assessPeriod',
      options: {
        initialValue: supplier.assessPeriod,
      },
      tag: <UdcSelect domain="SUPP" code="ASSESS_PERIOD" />,
    },{
      title: '流程状态',
      dataIndex: 'bpmStatus',
      options: {
        initialValue: supplier.bpmStatus || 'NONE',
      },
      tag: <UdcSelect domain="SRM" code="TRANSACT_STATUS" />,
    },{
      title: '观察期',
      dataIndex: 'observePeriod',
      options: {
        initialValue: supplier.observePeriod ,
      },
      tag: <UdcSelect domain="SUPP" code="OBSERVE_PERIOD" />,
    }],
    columns: [{
      title: '编号',
      dataIndex: 'suppCode',
      sorter: true,
      key: 'suppCode',
      width: '8%',
    }, {
      title: '名称',
      dataIndex: 'suppName',
      className: 'alignLeft nowrap',
      key: 'suppName',
      sorter: true,
      width: '18%',
    }, {
      title: '评分规则',
      dataIndex: 'perforType',
      className: 'alignLeft nowrap',
      key: 'perforType',
      sorter: true,
      width: '%',
    }, {
      title: '月份',
      dataIndex: 'yearMonth',
      sorter: true,
      key: 'yearMonth',
      width: '7%',
    }, {
      title: '评分状态',
      dataIndex: 'statusName',
      key: 'statusName',
      width: '8%',
    }, {
      title: '当前任务',
      dataIndex: 'activeTaskName',
      className: 'alignLeft',
      width: '14%',
    }, {
      title: '评分',
      dataIndex: 'showScore',
      className: 'alignRight',
      sorter: true,
      key: 'showScore',
      width: '8%',
    }, {
      title: '等级',
      dataIndex: 'sgName',
      key: 'sgName',
      width: '6%',
    }, {
      title: '上期评级',
      dataIndex: 'pre1Name',
      key: 'pre1Name',
      width: '7%',
    }, {
      title: '上上期评级',
      dataIndex: 'pre2Name',
      key: 'pre2Name',
      width: '8%',
    }, {
      title: '观察期',
      dataIndex: 'lqsName',
      key: 'lqsName',
      width: '6%',
    }/* {
      title: '上月评级',
      dataIndex: 'pre1Name',
      key: 'pre1Name',
      width: '8%',
    }, {
      title: '上上月评级',
      dataIndex: 'pre2Name',
      key: 'pre2Name',
      width: '10%',
    }, {
      title: '当前绩差',
      dataIndex: 'lqsName',
      key: 'lqsName',
      width: '8%',
    }*/],
  };

  const uploadModal = {
    domain,
    dispatch,
    showModal: showUploadModal,
    dataSource: [{
      aim: 'performance',
      name: '绩效导入',
      temp: 'Performance',
      tempName: '绩效评分',
    }, {
      aim: 'performanceHistory',
      name: '历史绩效导入',
      temp: 'Performance_History',
      tempName: '历史绩效评分',
    }],
  };

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

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