/**
 * 接待人
 * 
 */

import React, { Component } from 'react';
import { Input, Form, Select, Modal, message} from 'antd';
import { Util } from 'tools/index';
import { $http } from 'containers/config/https';
import '../style/index.less';
import moment from 'moment';

const Option = Select.Option;

class ShowReceptionModal extends Component {

    constructor(props) {
        super(props);
        this.state = {
        }
    }

    componentWillReceiveProps(nextProp) {
        if (nextProp.isShow === true) {
            this.setState({
                deskClerk: nextProp.ReceptionistID
            })
            this.loadUnawares()//获取接待人数据
        } else if (this.props.isShow === false) {
            this.cleanInput();
        }
    }

    //获取接待人
    loadUnawares() {
        var params = {
            params: [{}]
        }
        $http.post('urlDeault', this, {
            code: "03040036",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    this.setState({
                        unawaresList: res.result,
                    })
                }
            }
        });
    }

    closeModal() {
        let { onHide } = this.props;
        onHide && onHide();
        this.cleanInput();
    }

    //清空输入框
    cleanInput() {
        this.setState({
            deskClerk:undefined
        })
    }

    //提交
    onSubmit() {
        if(Util.isEmpty(this.state.deskClerk)){
            message.error('接待人不能为空，请选择接待人！')
            return
        }
        let { onSubmit } = this.props;
        onSubmit && onSubmit(this.state.deskClerk);
    }

    render() {

        //接待人
        let unawaresListOption = []
        this.state.unawaresList && this.state.unawaresList.map((item, index) => {
            unawaresListOption.push(<Option key={index} value={item.id} code={item.descripts} name={item.descriptsSPCode}>{item.descripts}</Option>)
        })

        return <Modal className='unawares-modal'
            title={'接待人修改'}
            onCancel={this.closeModal.bind(this)}
            onOk={this.onSubmit.bind(this)}
            maskClosable={false}
            visible={this.props.isShow}
        >
            接待人：<Select style={{ width: 200 }} getPopupContainer={triggerNode => triggerNode.parentNode}
                placeholder='请选择接待人员' allowClear showSearch
                filterOption={(input, option) =>
                    option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
                    || option.props.code.toLowerCase().indexOf(input.toLowerCase()) >= 0
                    || option.props.name.toLowerCase().indexOf(input.toLowerCase()) >= 0
                }
                value={this.state.deskClerk}
                onChange={Util.handleSelectChange.bind(this, 'deskClerk')}>
                {unawaresListOption}
            </Select>

        </Modal>
    }
}

export default Form.create()(ShowReceptionModal);