/*
* 收费项弹框
* */

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';

const Option = Select.Option;

class ShowPosionStatusModal extends Component {

    constructor(props) {
        super(props);
        var date = new Date();
        var userData = JSON.parse(sessionStorage.getItem('userData'));
        var patientData = JSON.parse(sessionStorage.getItem('patientData'))
        this.state = {
            leftData: [],
            rightData: [],
            userData: userData,
            patientData: patientData,
            cardType: [],
            agentCredTypeID: "1",
        }
    }
    componentWillReceiveProps(nextProp) {
        if (nextProp.isShow === true) {

        } else if (this.props.isShow === false) {
            this.cleanInput();
        }
    }

    componentDidMount() {
        this.GetCardType();
    }


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

    //清空输入框
    cleanInput() {
        this.setState({
            IDCardNo: undefined,
            agentName: undefined,
            agentIDCardNo: undefined,
            agentCredTypeID: "1",
        })
    }

    handleChangeInput(name, e) {
        this.setState({
            [name]: e.target.value
        })
    }

    onSubmit() {
        const { IDCardNo, agentName, agentIDCardNo, agentCredTypeID, cardType } = this.state
        let messageTime = this.state.userData && this.state.userData.messageTime || ''
        var idCard = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
        if (Util.isEmpty(agentName) && Util.isEmpty(agentIDCardNo) && Util.isEmpty(IDCardNo)) {
            message.error('患者身份证信息和代办人信息不能同时为空！', messageTime)
            return
        }
        //不填代办人----> 患者身份证必填, agentName agentIDCardNo
        // 不填患者身份证IDCardNo ---->代办人姓名还有身份证必填; 不填代办人----> 患者身份证必填, agentName agentIDCardNo
        var idarr = cardType.filter(item => {
            return item.id == agentCredTypeID
        })
        if (!IDCardNo) {
            if (Util.isEmpty(agentName)) {
                message.error('代办人姓名不能为空，请输入',)
            } else if (Util.isEmpty(agentCredTypeID)) {
                message.error('代办人证件类型不能为空', messageTime)
            } else if (Util.isEmpty(agentIDCardNo)) {
                message.error('代办人证件号码不能为空，请输入',)
            } else if (idarr && idarr[0].code == "01" && !idCard.test(agentIDCardNo)) {
                message.error('代办人身份证号码输入格式不正确，请重新输入', messageTime)
            }
        }
        if (IDCardNo && !idCard.test(IDCardNo)) {
            message.error('患者身份证号码输入格式不正确，请重新输入', messageTime)
        }
        var { onSubmit } = this.props;
        onSubmit && onSubmit(IDCardNo, agentName, agentIDCardNo, agentCredTypeID)
    }

    GetCardType() {
        var data = {
            params: [{
                code: ""
            }]
        }
        $http.post('urlS', this, {
            code: "01040036",
            data: data,
            success: function (res) {
                let errorCode = res.errorCode;
                if (errorCode === '0') {
                    this.setState({ cardType: res.result });
                }
            }
        })
    }

    changeCardType = (e) => {
        this.setState({ agentCredTypeID: e })
    }

    render() {
        return (
            <div>
                <Modal
                    className="posion-modal"
                    title="更新患者身份信息"
                    maskClosable={false}
                    visible={this.props.isShow}
                    onOk={this.onSubmit.bind(this)}
                    onCancel={this.closeModal.bind(this)}
                >
                    <div>
                        <div>
                            患者身份证：<Input placeholder='请输入患者身份证号码' style={{ width: 220 }} maxLength={18}
                                value={this.state.IDCardNo}
                                disabled={(!Util.isEmpty(this.state.agentName) || !Util.isEmpty(this.state.agentIDCardNo)) ? true : false}
                                onChange={this.handleChangeInput.bind(this, 'IDCardNo')} />
                        </div>
                        <div style={{ marginTop: 10 }}>
                            代办人姓名：<Input placeholder='请输入代办人姓名' style={{ width: 220 }} maxLength={30}
                                value={this.state.agentName}
                                disabled={!Util.isEmpty(this.state.IDCardNo) ? true : false}
                                onChange={this.handleChangeInput.bind(this, 'agentName')} />
                        </div>
                        <div style={{ marginTop: 10 }}>
                            代办人证件类型：
                            <Select
                                placeholder='请选择代办人证件类型'
                                showSearch
                                allowClear
                                value={this.state.agentCredTypeID}
                                onChange={this.changeCardType.bind(this)}
                                style={{ width: 200 }}>
                                {
                                    this.state.cardType.map((item) => {
                                        return (<Option key={item.id} value={item.id} title={item.descripts}>{item.descripts}</Option>)
                                    })
                                }
                            </Select>
                        </div>
                        <div style={{ marginTop: 10 }}>
                            代办人证件号：<Input placeholder='请输入代办人证件号码' style={{ width: 205 }} maxLength={18}
                                value={this.state.agentIDCardNo}
                                disabled={!Util.isEmpty(this.state.IDCardNo) ? true : false}
                                onChange={this.handleChangeInput.bind(this, 'agentIDCardNo')} />
                        </div>
                    </div>
                </Modal>
            </div>
        )
    }
}

export default Form.create()(ShowPosionStatusModal);