import React from 'react';
import {Input} from 'semantic-ui-react';
import {getRequest} from '../../../../../actions';

class UnitDigitalInput extends React.Component {
    state = {
        list: [],
        data: {
            id: '',
            op: '',
            value: ''
        }
    }

    componentDidMount(){
        let {showDialog, toggleLoading} = this.props;

        toggleLoading(1);
        fetch('/api/system/getselectlist', getRequest({type: 'di'}))
            .then(response=>response.json())
            .then(json=>{
                toggleLoading(0);
                if(json.status != 1) return showDialog(json.message);
                this.setState({
                    list: json.data.record || []
                })
            })
    }

    componentWillReceiveProps(np) {
        let data = {...this.state.data};
        if(np.data.id != data.id) data.id = np.data.id;
        if(np.data.op != data.op) data.op = np.data.op;
        if(np.data.value != data.value) data.value = np.data.op;

        this.setState({
            data: {...data}
        })
    }

    changeState = (key, value) => {
        let data = {
            ...this.state.data
        }
        if(key == 'dev') {
            data.id = value == '' ? '' : `di${value},di_status`;
        }
        if(key == 'op'){
            data.op = value;
        }
        if(key == 'value') {
            data.value = value;
        }

        this.setState({
            data: {...data}
        }, ()=>{ this.sendUpdate() });
    }

    sendUpdate = () => {
        this.props.updateData({...this.props.data, ...this.state.data});
    }

    render() {
        let {ops} = this.props;
        let id = '';
        if(this.state.data.id != '') {
            let m = this.state.data.id.match(/(\d+)/);
            if(m != null && m.length > 1){
                id = m[1];
            }
        }
        return (
            <div>
                <select value={id} onChange={e => {this.changeState('dev', e.target.value)}}>
                    <option value="">選擇裝置</option>
                    {
                        this.state.list.map((t,idx) => (
                            <option key={idx} value={t.id}>{t.name}</option>
                        ))
                    }
                </select>
                <select value={this.state.data.op} onChange={e => {this.changeState('op', e.target.value)}}>
                    <option value="">選擇條件</option>
                    {
                        ops.map((t,idx) => {
                            if(t.code != 8 && t.code != 9) {
                                return (
                                    <option value={t.code} key={idx}>{t.name}</option>
                                )
                            }
                        })
                    }
                </select>
                <select value={this.state.data.value} onChange={e => {this.changeState('value', e.target.value)}}>
                    <option value="">選擇狀態</option>
                    <option value="0">0</option>
                    <option value="1">1</option>
                </select>
            </div>
        )
    }
}

export default UnitDigitalInput;