import { message } from 'antd';
import { connect } from 'dva';
import { stringUtils } from '@utils';
import React from 'react';
import { DataTable } from '@components';

// const RangePicker = DatePicker.RangePicker;

const ContractTmpls = ({ dispatch, contractTmpls, loading }) => {
  const { list, total, currentItem, modalVisible, mode , addBtnFlag, editBtnFlag, viewBtnFlag,  deleteBtnFlag} = contractTmpls;

  const domain = 'contractTmpls';
  const domainCN = '合同模板列表';

  const tableProps = {
    withFetch: true,
    rowKey: 'id',
    limit: stringUtils.queryURL('limit'),
    offset: stringUtils.queryURL('offset'),
    sortBy: stringUtils.queryURL('sortBy') || 'contractModelCode',
    sortDirection: stringUtils.queryURL('sortDirection') || 'DESC',
    scroll: { x: '100%', y: 400 },
    domain,
    dispatch,
    loading,
    total,
    showAdd: addBtnFlag,
    showView: viewBtnFlag,
    showDelete: false,
    showEdit: false,
    dataSource: list,
    filter: [],
    columns: [
      {
        title: '合同模板编号',
        dataIndex: 'contractModelCode',
        sorter: true,
        className: 'noWrap',
        width: '20%',
      },
      {
        title: '合同模板标题',
        dataIndex: 'contractModelName',
        sorter: true,
        width: '60%',
        className: 'alignLeft',
      },
      {
        title: '状态',
        dataIndex: 'deleteFlag',
        sorter: false,
        width: '10%',
        className: 'noWrap',
        render: (value, row, index) => {
          return (<span>{'1' === value ? '作废' : '正常'}</span>);
        },
      },
    ],
    buttons: [
      {
        key: 'edit',
        type: 'primary',
        name: '修改',
        requiredSelected: 1,
        hidden: !editBtnFlag,
        cb: (selectedRowKeys, searchBarForm, selectedRows) => {
          if ('1' === selectedRows[0].deleteFlag) {
            message.warn('被作废的模板不能修改');
          } else {
            dispatch({
              type: `${domain}/getSingle`,
              payload: {
                mode: 'update',
                id: selectedRowKeys[0],
              },
            });
          }
        },
      },
      {
        key: 'delete',
        type: 'primary',
        name: '作废',
        hidden: !deleteBtnFlag,
        requiredSelected: 1,
        cb: (selectedRowKeys, searchBarForm, selectedRows) => {
          if ('1' === selectedRows[0].deleteFlag) {
            message.warn('模板已被作废');
          } else {
            dispatch({
              type: `${domain}/delete`,
              payload: { ids: selectedRowKeys, },
            });
          }
        },
      },
      {
        key: 'copy',
        type: 'primary',
        name: '复制',
        hidden: !addBtnFlag,
        requiredSelected: 1,
        cb: (selectedRowKeys, searchBarForm, selectedRows) => {
          dispatch({
            type: `${domain}/copy`,
            payload: { id: selectedRowKeys[0] },
          })
        },
      },
    ]
  };

  return (

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

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