/**
 * Created by mar105 on 2019-02-13.
 */
/* eslint-disable no-undef,import/first,prefer-destructuring,jsx-a11y/alt-text */
import React, { Component } from 'react';
import { Form } from '@ant-design/compatible';
// import '@ant-design/compatible/assets/index.css';
import { Row, Col, Radio } from 'antd-v4';
import * as commonUtils from '../../utils/utils';
import ShowType from './CommonComponent';/* 通用方法 */
import AntdDraggableModal from '../Common/AntdDraggableModal';
import * as commonFunc from './commonFunc';


const FormItem = Form.Item;
export default class ContextMenuModal extends Component {
  /**   构造函数   */
  constructor(props) {
    super(props);
    this.state = {
      contextMenuValue: null,
      contextType: 1,
    };
    this.form = {};
    /*   表单对象   */
  }
  handleViewClick = (name, sName, sId) => {
    this.props.onViewClick(name, sName, sId);
  };
  /* 右击弹出窗确定 取表单值 */
 handleContextMenuOk= () => {
   const { contextMenuTbName } = this.props;
   const {
     masterData,
     contextMenuConfig,
     [`${contextMenuTbName}Data`]: tableData,
     [`${contextMenuTbName}SelectedRowKeys`]: selectedRowKeys,
   } = this.props;
   if (commonUtils.isNotEmptyObject(masterData) && commonUtils.isNotEmptyArr(contextMenuConfig)) {
     const showConfig = contextMenuConfig[0];
     if (commonUtils.isNotEmptyObject(showConfig.sName)) {
       //  const contextMenuValue = masterData[showConfig.sName]; /* 取弹窗框的值 */
       let { contextMenuData } = this.state;
       const { contextMenuValue, contextType } = this.state;
       console.log('dddd', contextType);
       const contextMenuType = commonUtils.isNotEmptyNumber(contextType) ? contextType : 1;
       if (!commonUtils.isEmpty(contextMenuValue)) {
         if (!contextMenuData) {
           let tableDataRow = {};
           if (commonUtils.isNotEmptyArr(tableData)) {
             const tableFilterData = tableData.filter(item => selectedRowKeys.includes(item.sId));
             if (commonUtils.isNotEmptyArr(tableFilterData)) {
               tableDataRow = tableFilterData[0];
               tableDataRow.contextType = commonUtils.isNotEmptyNumber(contextType) ? contextType : 1;
             }
           }
           contextMenuData = commonFunc.getAssignFieldValue(showConfig.sAssignField, tableDataRow);
         }
         console.log('ddda1a', contextMenuData, contextMenuType);
         this.props.onContextMenuOk(contextMenuValue, contextMenuData, contextMenuType);
       }
     }
   }
 }

  handleModalCancel = (modelVisible) => {
    this.props.onSaveState({
      [modelVisible]: false,
    });
  };

  handleRdoChange = (e) => {
    this.setState({ contextType: e.target.value });
  };


  /**   渲染   */
  render() {
    const {
      contextMenuModalVisible,
      contextMenuTbName,
      contextMenuConfig,
      contextMenuName,
      app,
      form,
      sTabId,
      [`${contextMenuTbName}Data`]: tableData, [`${contextMenuTbName}SelectedRowKeys`]: selectedRowKeys,
    } = this.props;
    const { contextMenuValue, contextType } = this.state;
    let tableDataRow = {};
    if (commonUtils.isNotEmptyArr(tableData)) {
      const tableFilterData = tableData.filter(item => selectedRowKeys.includes(item.sId));
      if (commonUtils.isNotEmptyArr(tableFilterData)) {
        tableDataRow = tableFilterData[0];
      }
    }

    if (contextMenuValue === null && commonUtils.isNotEmptyObject(tableDataRow) && commonUtils.isNotEmptyArr(contextMenuConfig)) {
      this.setState({
        contextMenuValue: tableDataRow[contextMenuConfig[0].sName],
      });
    }

    const radioValue = commonUtils.isNotEmptyNumber(contextType) ? contextType : 1;
    const pane = app.panes.filter(paneTmp => paneTmp.key === sTabId)[0];
    return (
      <div>
        {
          (pane.notCurrentPane ? false : contextMenuModalVisible) ?
            <AntdDraggableModal
              title={`更新:${contextMenuName}`}
              visible={contextMenuModalVisible}
              onOk={this.handleContextMenuOk}
              onCancel={this.handleModalCancel.bind(this, 'contextMenuModalVisible')}
              width={380}
              okButtonProps={{ style: { marginRight: '5px' } }}
              // cancelButtonProps={{ style: { marginRight: '10px' } }}
              bodyStyle={{ paddingLeft: '3px', paddingRight: '3px' }}
            >
              <div className="contextMenuStyle" >
                <div className="xly_radio" style={{ padding: '5px 20px' }} >
                  <Radio.Group onChange={this.handleRdoChange} value={radioValue}>
                    <Radio value={1}>全部更新</Radio>
                    <Radio value={2}>当前数据往下更新</Radio>
                  </Radio.Group>
                </div>
                <FormItem className="searchMainForm">
                  <Row type="flex" style={{ height: 'auto', overflow: 'hidden' }}>
                    { commonUtils.isNotEmptyArr(contextMenuConfig) ?
                      contextMenuConfig.map((child) => {
                        const sMemo = child.sName.toLowerCase().endsWith('memo');
                        let enabledNew = !child.bReadonly;
                        if (child.iTag === 1) {
                          enabledNew = false;
                        } else if (child.iTag === 3) {
                          enabledNew = true;
                        }
                        const showTypeProps = {
                          name: 'slave',
                          app,
                          form,
                          record: tableDataRow,
                          sId: commonUtils.createSid(),
                          getSqlDropDownData: this.props.getSqlDropDownData,
                          getSqlCondition: this.props.getSqlCondition,
                          handleSqlDropDownNewRecord: this.props.handleSqlDropDownNewRecord,
                          getFloatNum: this.props.getFloatNum,
                          onChange: (name, sFieldName, changeValue) => {
                            this.setState({
                              contextMenuValue: changeValue[sFieldName],
                              contextMenuData: changeValue,
                            });
                          },
                          onViewClick: this.handleViewClick,
                          getDateFormat: this.props.getDateFormat,
                          showConfig: child,
                          textArea: sMemo,
                          enabled: enabledNew,
                          dataValue: !commonUtils.isEmpty(contextMenuValue) ? contextMenuValue : commonUtils.isNotEmptyObject(tableDataRow) ? tableDataRow[child.sName] : '',
                          bTable: true,
                          formRoute: this.props.formRoute,
                          formItemLayout: {},
                          onExecInstructSet: this.props.onExecInstructSet,
                        };
                        return (
                          <Col key={child.sId} span={24} order={child.iOrder} className="ContextMenuCol" style={{ border: '1px solid #d3d3d3' }}>
                            <ShowType {...showTypeProps} />
                          </Col>
                        );
                      }) : ''
                    }
                  </Row>
                </FormItem>
              </div>
            </AntdDraggableModal>
            : ''
        }

      </div>
    );
  }
}
