/*
* 高级查询弹框
 * Creator:     FJP初始 ML改版
 * CreatDate:   2020-04-
 * descripts：  高级查询 
* */


import React, { Component } from 'react';
import {
    Input, Form, Select, Modal, Button, Progress, Upload, message, Tooltip, Icon, Table,
    Card, Row, Col, DatePicker, Popconfirm, Checkbox
} from 'antd';
import { Util, dayFormat } from 'tools/index';
import { $http } from 'containers/config/https';
import ShowInformationPage from './ShowInformationPage';
import ShowExaminationFormPage from './ShowExaminationFormPage';
import '../style/index.less';
import moment from 'moment';

const Option = Select.Option;
const Search = Input.Search;
const FormItem = Form.Item;
const { TextArea } = Input;

class ShowQueryAdvancedModal extends Component {
    constructor(props) {
        super(props);
        var date = new Date();
        var userData = JSON.parse(sessionStorage.getItem('userData'));
        this.state = {
            leftData: [],
            rightData: [],
            userData: userData,
            showCompent: 0,
            adressData: [],
            checkboxData: [],
            checkboxDataTwo: [],
            searching:false
        }
    }
    componentWillReceiveProps(nextProp) {
        if (nextProp.isShow === true) {
            this.loadSelectData()
            // console.log("searchBase=true",this.props.searchBase)


        } else if (this.props.isShow === false) {
            // console.log("searchBase=false",this.props.searchBase)
        }
    }

    componentDidMount() {
        this.props.onQuery(this)
    }

    loadSelectData() {
        var params = {
            params: [{
                logonHospID: this.state.userData.hospID,
                logonGroupID: this.state.userData.groupID,
                sexFlag: 'Y'
            }]
        }
        $http.post('urlDeault', this, {
            code: "03010037",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    this.setState({
                        sex: res.result.sex,

                    })
                    //点击首页查询按钮 清空高级查询条件
                    if (this.props.searchBase == true) {
                        this.setState({
                            searchValue: "",
                            searchValueTwo: "",
                            showCompent: 0,//进来默认显示个人基本信息
                            searching:false
                        }, () => {
                            this.handleSearch(2)
                            this.childRef.handleRest()
                            this.childRefHealth.handleRest()
                        })

                    }

                }
            }
        });
    }

    closeModal=()=>{
        // let { onHide } = this.props;
        // onHide && oniHide();
        this.setState({
            searching:false
        })
        this.props.closeIsShowQueryAdvancedModal()
    }


    //获取选中数据
    // getData = (data, otherData, healthData, healthObj, addressData) => {
    //     this.setState({
    //         patData: data,
    //         otherData,
    //         healthData,
    //         healthObj,
    //         addressData
    //     }, () => {
    //         const { patData, healthData, otherData, healthObj, addressData } = this.state
    //         let obj = {}
    //         if (healthObj != undefined) {
    //             obj = {
    //                 field: healthData,
    //             }
    //             //可能携带体检日期和责任医生
    //             if (healthObj.classShortName) {
    //                 obj.classShortName = healthObj.classShortName
    //             }
    //             if (healthObj.startDate) {
    //                 obj.startDate = healthObj.startDate
    //             }
    //             if (healthObj.stopDate) {
    //                 obj.stopDate = healthObj.stopDate
    //             } if (healthObj.doctorID) {
    //                 obj.doctorID = healthObj.doctorID
    //             }
    //         } else {
    //             obj = {
    //                 field: [],
    //                 classShortName: "health"
    //             }

    //         }

    //         var params = {
    //             params: [{
    //                 patient: {
    //                     field: patData || [],
    //                     classShortName: "pat"
    //                 },
    //                 address: {
    //                     field: addressData || [],
    //                     classShortName: "address"
    //                 },
    //                 other: {
    //                     field: otherData || [],
    //                     classShortName: "other"
    //                 },
    //                 health: obj

    //             }],
    //             pagination: [{
    //                 pageSize: this.props.size || "10",
    //                 currentPage: this.props.page || "1"
    //             }]

    //         }
    //         $http.post('urlDeault', this, {
    //             code: "03070100",
    //             data: params,
    //             success: function (res) {
    //                 if (res.errorCode == '0') {
    //                     this.setState({

    //                     })
    //                     this.closeModal()
    //                     this.props.handleMaxSearch(res.result.data, res.result.totalCount)
    //                     this.props.changeSearch()
    //                 }
    //             }
    //         });
    //     })
    // }
    //提交
    onSubmit = () => {
        this.setState({
            searching:true
        },()=>{
            this.childRef.toSearchData()
            this.childRefHealth.toSearchData()
        })
    }
    //个人信息 搜索数据
    getInfoData = (data, otherData, addressData, checkboxData) => {
        this.setState({
            patData: data,
            otherData,
            addressData,
            checkboxData,
        })
    }
    //体检表 搜索数据
    getHealthData = (healthData, healthObj, checkboxDataTwo) => {
        this.setState({
            healthData,
            healthObj,
            checkboxDataTwo,
        }, () => {
            const { patData, healthData, otherData, healthObj, addressData } = this.state
            if (this.state.checkboxData.length == 0 && this.state.checkboxDataTwo.length == 0) {
                this.setState({
                    searching:false
                },()=>{
                    message.warning("请勾选筛选条件")
                })
                return
            }
            let obj = {}
            if (healthObj != undefined) {
                obj = {
                    field: healthData,
                }
                //可能携带体检日期和责任医生
                if (healthObj.classShortName) {
                    obj.classShortName = healthObj.classShortName
                }
                if (healthObj.startDate) {
                    obj.startDate = healthObj.startDate
                }
                if (healthObj.stopDate) {
                    obj.stopDate = healthObj.stopDate
                } if (healthObj.doctorID) {
                    obj.doctorID = healthObj.doctorID
                }
            } else {
                obj = {
                    field: [],
                    classShortName: "health"
                }

            }

            var params = {
                params: [{
                    patient: {
                        field: patData || [],
                        classShortName: "pat"
                    },
                    address: {
                        field: addressData || [],
                        classShortName: "address"
                    },
                    other: {
                        field: otherData || [],
                        classShortName: "other"
                    },
                    health: obj

                }],
                pagination: [{
                    pageSize: this.props.size || "10",
                    currentPage: this.props.page || "1"
                }]

            }
            $http.post('urlDeault', this, {
                code: "03070100",
                data: params,
                success: function (res) {
                    if (res.errorCode == '0') {
                        this.setState({
                            searching:false
                        },()=>{
                            this.closeModal()
                            this.props.handleMaxSearch(res.result.data, res.result.totalCount,res.result.data[0]&&res.result.data[0].titleData?res.result.data[0].titleData:[])
                            this.props.changeSearch()
                        })
                    }else{
                        this.setState({
                            searching:false
                        })
                    }
                }
            });
        })
    }
    //选择来源
    handleSouce = (item) => {
        this.setState({
            showCompent: item.id
        },()=>{
           if (item.id == 0) {
            this.setState({
                healthData: [],
                healthObj: {}
            })
        } else {
            this.setState({
                patData: [],
                otherData: []
            })
        } 
        if(this.props.searchBase == true&&item.id == 1){
            this.childRefHealth.scrollTo("tj")
        }
        })
       
    }
    //输入改变值
    handleChange = (e) => {
        if(this.state.showCompent==0){
            this.setState({
                searchValue: e.target.value
            })
        }else{
            this.setState({
                searchValueTwo: e.target.value
            })
        }
      
    }
    //个人信息 
    onRef = (ref) => {
        this.childRef = ref
    }
    //体检表 
    onRefHealth = (ref) => {
        this.childRefHealth = ref
    }

    handleSearch = (showCompent) => {
        if (showCompent == 0) {
            this.childRef.getCompent(this.state.searchValue)
        } else if(showCompent == 1){
            this.childRefHealth.getCompent(this.state.searchValueTwo)
        }else{
            this.childRef.getCompent(this.state.searchValue)
            this.childRefHealth.getCompent(this.state.searchValueTwo)
        }

    }
    render() {
        var { sex, previewImage, fileList, previewVisible, showCompent } = this.state;
        let { getFieldDecorator } = this.props.form;
        let formItemLayout = {
            labelCol: { span: 8 },
            wrapperCol: { span: 15 }
        };

        let formItemSexLayout = {
            labelCol: { span: 8 },
            wrapperCol: { span: 22 }
        };

        let searchColConfig = {
            xs: { span: 24 },
            sm: { span: 12 },
            md: { span: 8 },
            lg: { span: 10 }
        }
        let souce = [
            {
                id: 0,
                descripts: "个人基本信息",
                key: 0,
            },
            {
                id: 1,
                descripts: "健康体检表",
                key: 1,
            },
        ]
        //性别
        var sexOption = []
        sex && sex.map((item, index) => {
            sexOption.push(<Option key={index} value={item.id}>{item.descripts}</Option>)
        })
        //表来源
        let souceOption = []
        souce && souce.map((item, index) => {
            souceOption.push(<Option key={index} onClick={() => this.handleSouce(item)} value={item.id}>{item.descripts}</Option>)
        })

        return <div>
            <Modal
                className='show-information-modal show-search-modal'
                title={'高级查询'}
                maskClosable={false}
                visible={this.props.isShow}
                onCancel={this.closeModal}
                footer={null}
                style={{
                    position: "relative",
                    paddingBottom: "20px"
                }}
            >
                <Card>
                    <Row style={{ marginBottom: "5px" }}>
                        <Col span={5}>
                            选择表来源：<Select placeholder='请选择表来源' value={this.state.showCompent} style={{ width: '58%' }}>
                                {souceOption}
                            </Select>
                        </Col>
                        <Col span={6}>
                            列描述：<Input value={showCompent == 0?this.state.searchValue:this.state.searchValueTwo} onChange={this.handleChange}
                             onPressEnter={()=>this.handleSearch(this.state.showCompent)} placeholder='请输入名称回车查找字段' style={{ width: '70%' }} />
                        </Col>
                    </Row>
                </Card>
                <div>
                    <div style={{ display: (showCompent == 0 ? "block" : "none") }}>
                        <ShowInformationPage onRef={this.onRef}  getInfoData={this.getInfoData} />
                    </div>
                    <div style={{ display: (showCompent != 0 ? "block" : "none") }}>
                        <ShowExaminationFormPage onRefHealth={this.onRefHealth} getHealthData={this.getHealthData} />
                    </div>
                    {/* {
                        showCompent == 0 ? 
                        <ShowInformationPage onRef={this.onRef} getData={this.getData} /> 
                        : <ShowExaminationFormPage onRef={this.onRef} getData={this.getData} />
                    } */}
                </div>
                <Row style={{
                    position: "absolute",
                    bottom: "13px",
                    left: "0",
                    textAlign: "center",
                    width: "100%"
                }}>
                    <Button onClick={this.closeModal}>取消</Button>
                    <Button loading={this.state.searching} onClick={this.onSubmit} type="primary" style={{
                        margin: "0 16px"
                    }}>确定</Button>
                </Row>
            </Modal>
        </div>
    }
}

export default Form.create()(ShowQueryAdvancedModal);