import { message } from "antd/lib/index";
import { Button, Popconfirm, Row, Col, Form, Input,InputNumber, DatePicker,Select, Table } from 'antd';
import React from 'react';
import { initFilter, extendRule, extendInclude, setDataFieldValue, guid, initQueryFilter } from "../../../utils/common";
import styles from  '../../CustomPages/HI/StepForm.css';
import { connect } from 'dva';
import moment from 'moment';

class SEConfigPage extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            isLoading:false,
            data: {
                ID: null,
                SAFE_SERVEY_TRIGGER_TIME: null,
                S_TEST_COUNT: 0,
                S_TEST_SCORE: 0,
                S_TEST_TOTAL: 0,
                M_TEST_COUNT: 0,
                M_TEST_SCORE: 0,
                M_TEST_TOTAL: 0,
                C_TEST_COUNT: 0,
                C_TEST_SCORE: 0,
                C_TEST_TOTAL: 0,
                DEP_TRAIN_PLAN_START_TIME: null,
                DEP_TRAIN_PLAN_END_TIME: null,
                TRAIN_SURVEY_TRIGGER_TIME: null,
            },
        };
    };

    componentDidMount() {
        this.loadData();
    }
    BtnClose = () => {
        if (typeof this.props.closeFn != "undefined")
            this.props.closeFn();
        else if (typeof this.props.data.closeTab == 'function') {
            this.props.data.closeTab(this.props.data.id)
        }  else if (typeof this.props.onCancel === "function"){
            this.props.onCancel();
        }
    }
    loadData = () => {
        let json = initFilter(this.props.login.OrgId);
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'SE/SEConfig/Entities',
            onComplete: (ret) => {
                if (ret && ret.length > 0) {
                   this.setState({ data: ret[0] });
                }
            }
        });
    }
    fmtEnum(name,value){
        const enums = this.props.app.enums;
        if (!enums || !enums[name]) return '';
        return enums[name].enums[value] || ''; 
    }
    onSave = () =>{
        let data = JSON.parse(JSON.stringify(this.state.data));
        if (data.DEP_TRAIN_PLAN_END_TIME && data.DEP_TRAIN_PLAN_START_TIME){
            let s = new Date(data.DEP_TRAIN_PLAN_START_TIME);
            let e = new Date(data.DEP_TRAIN_PLAN_END_TIME);
            if (e.getTime() - s.getTime() < 1 * 24 * 60 * 60 * 1000){
                message.error('截止时间至少比开始时间大一天');
                return;
            }
        }
        if (!data.ID){
            data.ID = guid();
        }
        this.setState({isLoading:true});
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: data,
            url: 'SE/SEConfig/FullUpdate',
            onComplete: (ret) => {
                if (ret) {
                    message.success('保存成功！');
                    this.setState({isLoading:false});
                }
            }
        });
    }
    render() {
        const { data } = this.state;
        const dateFormat = 'YYYY/MM/DD';
        return <>
            <div style={{ padding: '10px' }}>
                <Button style={{ marginLeft: '8px' }} onClick={() => this.onSave()} icon="save" type="primary" loading={this.state.isLoading}>保存</Button>         
            </div>  
            <div ref={el => (this.componentRef = el)} style={{ padding: '20px',backgroundColor:'FFF' }} id={'tableId' + this.props.data.id}>
                <h1 style={{ textAlign: 'center', margin: '15px' }}>安全教育培训基础设置</h1>
                <table style={{ width: '100%', textAlign: 'center', borderTop: '1px solid #333', borderLeft: '1px solid #333' }} className={styles.PrintForm}>
                    <tr>
                        <td colSpan={24} rowSpan={1} >题库设置</td>
                    </tr>
                    <tr>
                        <td colSpan={4} rowSpan={1} >是非题数目</td>
                        <td colSpan={4} rowSpan={1} >
                            <InputNumber value={data.C_TEST_COUNT} min={0} max={9999} 
                                onChange={ 
                                    value =>{  
                                        let data = this.state.data;
                                        data.C_TEST_COUNT = value;
                                        this.setState(data)
                                    } 
                                } 
                            >
                            </InputNumber>
                        </td>
                        <td colSpan={4} rowSpan={1} >是非题分值</td>
                        <td colSpan={4} rowSpan={1} >
                            <InputNumber value={data.C_TEST_SCORE} min={0} max={9999} 
                                onChange={ 
                                    value =>{  
                                        let data = this.state.data;
                                        data.C_TEST_SCORE = value;
                                        this.setState(data)
                                    } 
                                } 
                            >
                            </InputNumber>
                        </td>
                        <td colSpan={4} rowSpan={1} >是非题总分</td>
                        <td colSpan={4} rowSpan={1} >{data.C_TEST_COUNT * data.C_TEST_SCORE}</td>
                    </tr>
                    <tr>
                        <td colSpan={4} rowSpan={1} >单选题数目</td>
                        <td colSpan={4} rowSpan={1} >
                            <InputNumber value={data.S_TEST_COUNT} min={0} max={9999} 
                                onChange={ 
                                    value =>{  
                                        let data = this.state.data;
                                        data.S_TEST_COUNT = value;
                                        this.setState(data)
                                    } 
                                } 
                            >
                            </InputNumber>
                        </td>
                        <td colSpan={4} rowSpan={1} >单选题分值</td>
                        <td colSpan={4} rowSpan={1} >
                            <InputNumber value={data.S_TEST_SCORE} min={0} max={9999} 
                                onChange={ 
                                    value =>{  
                                        let data = this.state.data;
                                        data.S_TEST_SCORE = value;
                                        this.setState(data)
                                    } 
                                } 
                            >
                            </InputNumber>
                        </td>
                        <td colSpan={4} rowSpan={1} >单选题总分</td>
                        <td colSpan={4} rowSpan={1} >{data.S_TEST_COUNT * data.S_TEST_SCORE}</td>
                    </tr>
                    <tr>
                        <td colSpan={4} rowSpan={1} >多选题数目</td>
                        <td colSpan={4} rowSpan={1} >
                            <InputNumber value={data.M_TEST_COUNT} min={0} max={9999} 
                                onChange={ 
                                    value =>{  
                                        let data = this.state.data;
                                        data.M_TEST_COUNT = value;
                                        this.setState(data)
                                    } 
                                } 
                            >
                            </InputNumber>
                        </td>
                        <td colSpan={4} rowSpan={1} >多选题分值</td>
                        <td colSpan={4} rowSpan={1} >
                            <InputNumber value={data.M_TEST_SCORE} min={0} max={9999} 
                                onChange={ 
                                    value =>{  
                                        let data = this.state.data;
                                        data.M_TEST_SCORE = value;
                                        this.setState(data)
                                    } 
                                } 
                            >
                            </InputNumber>
                        </td>
                        <td colSpan={4} rowSpan={1} >多选题总分</td>
                        <td colSpan={4} rowSpan={1} >{data.M_TEST_COUNT * data.M_TEST_SCORE}</td>
                    </tr>
                    <tr>
                        <td colSpan={4} rowSpan={1} >试题总分</td>
                        <td colSpan={20} rowSpan={1} > { data.C_TEST_COUNT * data.C_TEST_SCORE + data.S_TEST_COUNT * data.S_TEST_SCORE + data.M_TEST_COUNT * data.M_TEST_SCORE}
                        </td>
                    </tr>
                    <tr>
                        <td colSpan={24} rowSpan={1} >其它设置</td>
                    </tr>
                    <tr>
                        <td colSpan={4} rowSpan={1} >安全意识调查触发时间</td>
                        <td colSpan={20} rowSpan={1} > 
                            <DatePicker
                                value={data.SAFE_SERVEY_TRIGGER_TIME ? moment(data.SAFE_SERVEY_TRIGGER_TIME) : null}
                                onChange={ value => {
                                    let data = this.state.data;
                                    data.SAFE_SERVEY_TRIGGER_TIME = value;
                                    this.setState(data)
                                }}
                            />
                        </td>
                    </tr>
                    <tr>
                        <td colSpan={4} rowSpan={1} >培训需求调查触发时间</td>
                        <td colSpan={20} rowSpan={1} >
                            <DatePicker
                                value={data.TRAIN_SURVEY_TRIGGER_TIME ? moment(data.TRAIN_SURVEY_TRIGGER_TIME) : null }
                                onChange={ value => {
                                    let data = this.state.data;
                                    data.TRAIN_SURVEY_TRIGGER_TIME = value;
                                    this.setState(data)
                                }}
                            />
                        </td>
                    </tr>
                    <tr>
                        <td colSpan={4} rowSpan={1} >部门培训计划发起时间</td>
                        <td colSpan={20} rowSpan={1} > 
                            <DatePicker
                                value={data.DEP_TRAIN_PLAN_START_TIME ? moment(data.DEP_TRAIN_PLAN_START_TIME) : null}
                                onChange={ value => {
                                    let data = this.state.data;
                                    data.DEP_TRAIN_PLAN_START_TIME = value;
                                    this.setState(data)
                                }}
                            />
                        </td>
                    </tr>
                    <tr>
                        <td colSpan={4} rowSpan={1} >部门培训计划截至时间</td>
                        <td colSpan={20} rowSpan={1} >
                            <DatePicker
                                value={data.DEP_TRAIN_PLAN_END_TIME ? moment(data.DEP_TRAIN_PLAN_END_TIME) : null }
                                onChange={ value => {
                                    let data = this.state.data;
                                    data.DEP_TRAIN_PLAN_END_TIME = value;
                                    this.setState(data)
                                }}
                            />
                        </td>
                    </tr>
                    
                </table>
            </div>
        </>
    }

}

export default connect(({ login, app }) => ({ login, app }))(SEConfigPage) 